Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ForEach reconciler #547

Merged
merged 2 commits into from
Sep 24, 2024
Merged

ForEach reconciler #547

merged 2 commits into from
Sep 24, 2024

Conversation

scothis
Copy link
Member

@scothis scothis commented Sep 23, 2024

The ForEach reconciler can iterator over a slice calling a nested reconciler for each item. The current item is stashed on the context to be retrieved with a CursorStasher.

Nested iterators are allowed so long as the type of the items being iterated over have unique names. We may enhance the generated stash key in the future to relax this requirement, so users should not assume the specific key to be stable between releases.

The ForEach reconciler can iterator over a slice calling a nested
reconciler for each item. The current item is stashed on the context to
be retrieved with a CursorStasher.

Nested iterators are allowed so long as the type of the items being
iterated over have unique names. We may enhance the generated stash key
in the future to relax this requirement, so users should not assume the
specific key to be stable between releases.

Signed-off-by: Scott Andrews <scott@andrews.me>
@scothis scothis requested a review from mamachanko September 23, 2024 16:34
Copy link

codecov bot commented Sep 23, 2024

Codecov Report

Attention: Patch coverage is 65.90909% with 15 lines in your changes missing coverage. Please review.

Project coverage is 60.60%. Comparing base (4a934e2) to head (1871a44).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
reconcilers/flow.go 65.90% 14 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #547      +/-   ##
==========================================
+ Coverage   60.52%   60.60%   +0.08%     
==========================================
  Files          32       32              
  Lines        2880     2924      +44     
==========================================
+ Hits         1743     1772      +29     
- Misses       1043     1057      +14     
- Partials       94       95       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@mamachanko mamachanko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgmt! left a few thoughts.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated

A [`ForEach`](https://pkg.go.dev/reconciler.io/runtime/reconcilers#ForEach) calls the reconciler for each item returned from Items. The current cursor can be retrieved with [`CursorStasher`](https://pkg.go.dev/reconciler.io/runtime/reconcilers#CursorStasher).

Nested iteration is allowed so long as the types being iterated over contain unique names. Otherwise the stash keys will collide. For testing the nested reconciler outside the scope of the loop, use the stasher's Key method to lookup the StashKey, do not expect the StashKey to be stable between releases.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[suggestion, non-blocking]: When reading "so long as the types being iterated over contain unique names" I was assuming that the items need to have unique names but it's speaking about types. I struggle to parse what it means for a "type" to "contain a name". The doc string of ForEach is a little clearer:

// Multiple ForEach reconcilers are nestable so long as the types being iterated over are unique.

So the types must be unique then, I think. That makes sense, because we identify each nested reconciler by type.

Maybe this is better:

Nested iteration is allowed so long as each reconciler's type is unique.

Copy link
Member Author

@scothis scothis Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Types have names in go. For example, the type corev1.Pod has the name "Pod". Because the stash key is based on a string, we need to coerce the type to a string. In general this will be transparent to users, but if they have nested iteration of the same type or different types that have the same name, then a collision will occur.

I'll take a point to try to clean this up sooner rather than later.

reconcilers/flow.go Outdated Show resolved Hide resolved
reconcilers/flow.go Outdated Show resolved Hide resolved
Comment on lines +338 to +339
d.ContainerDie("die", func(d *diecorev1.ContainerDie) {
d.Image("die")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[issue, absolutely non-blocking]: overloading the term "die" with "exit early" is fine, but makes the reader squint

Copy link
Member Author

@scothis scothis Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the other meaning of "die" 😄

SpecDie(func(d *dies.TestResourceSpecDie) {
d.Fields(map[string]string{})
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[thought, off-topic]: In terms of reading order this is the moment where I expect to see the reconciler-under-test. I keep scrolling back and forth between the test cases and the reconciler below. It's absolutely fine. Declaring the controller here would work, but it's not something we do conventionally in our test suite. All good 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are places were we do define a unique reconciler for each test case. Since there are only a couple test cases I think this is cleaner, but if those cases needed to diverge more then it would be worth localizing the reconciler definition.

@@ -174,7 +174,6 @@ func typeName(i interface{}) string {
}

t := reflect.TypeOf(i)
// TODO do we need this?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems we don't?! 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do now

Co-authored-by: Max Brauer <mamachanko@users.noreply.github.com>
Signed-off-by: Scott Andrews <scott@andrews.me>
@scothis scothis merged commit 7c8780b into reconcilerio:main Sep 24, 2024
2 checks passed
@scothis scothis deleted the for-each branch September 24, 2024 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants