Generate reconcileFuncs
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 276
- Forks
- 344
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 1
Description
The generated reconcilers/controllers had me so hyped up, that I now want to generate all the things 😂.
In all seriousness: What if we could generate the Reconcile${Type} functions as well? They usually follow a common schema:
func ReconcileT(ctx context.Context, desired *type.T) (*type.T, error) {
lister := type.GetListerForT(ctx)
client := type.GetClientForT(ctx)
actual, err := lister.Ts(desired.namespace).Get(desired.name)
if errors.IsNotFound(err) {
created, err = client.Ts(desired.namespace).Create(desired)
if err != nil {
return nil, fmt.Errorf("error creating T: %w", err)
}
return created, nil
} else if err != nil {
return nil, fmt.Errorf("error fetching T: %w", err)
} else if *metav1.GetControllerOf(actual) != *metav1.GetControllerOf(desired) {
return nil, NewNotControlledError(...)
} else {
if !equality.Semantic.DeepEqual(desired.Spec, actual.Spec) {
want := actual.DeepCopy()
want.Spec = desired.Spec
updated, err := client.Ts(want.namespace).Update(want)
if err != nil {
return nil, fmt.Errorf("error updating T: %w", err)
}
return updated, nil
}
}
}
They do have diverged quite a bit in Serving though. I looked a bit through all of our instances of such functions and noticed a few (maybe key) differences between some of them:
- Some of the emit events when creating/updating succeeds/fails.
- Some of them determine
DeepEqualincluding the annotations/labels. - Some of them alter the parent object's Status based on the action that happened.
I wonder if there is room for us to converge on a common set of actions we want to do for each of these reconcile functions and whether or not we can generate them. Writing them is usually mindless boilerplate.
Some ideas on the divergences above: We could add a return value "Event", which can be used to determine what happened in the function to send events (if the reconciler wants to do that) or set status based on that event too.
Note: I don't think the clients/informers are currently available on the context being passed through the reconcile loop, so the interface I've shown is not quite right.
I prototyped this very roughly in Serving here https://github.com/knative/serving/compare/master...markusthoemmes:codegen-reconcilefunc?expand=1.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the generated reconciler/controller approach described in the issue and the linked Serving prototype comparison. Catalog the differing event, equality, and status behaviors, then determine whether a common reconcile schema can be agreed on and generated; done means an accepted design and implementation plan for those divergences.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100