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

✨ Add reconcile.ObjectReconciler #2592

Merged
merged 3 commits into from Jan 9, 2024
Merged

✨ Add reconcile.ObjectReconciler #2592

merged 3 commits into from Jan 9, 2024

Conversation

JamesOwenHall
Copy link
Contributor

Part of #2582.
Fixes #2221.

This PR adds a new reconcile.ObjectReconciler interface to eliminate the following common boilerplate.

func (r *MyReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
	example := &examplev1.Example{}
	if err := r.Get(ctx, req.NamespacedName, example); err != nil {
		return ctrl.Result{}, client.IgnoreNotFound(err)
	}

	// Do business logic…
}

ObjectReconciler leverages generics to write reconcilers in terms of deserialized instances of client.Object. It does not replace the existing reconcile.Reconciler interface.

Developers can create a controller with an ObjectReconciler by calling the AsReconciler function.

builder.ControllerManagedBy(manager).
	For(&corev1.ConfigMap{}).
	Complete(reconcile.AsReconciler(client, &MyObjectReconciler{}))

Copy link

linux-foundation-easycla bot commented Nov 23, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Nov 23, 2023
@k8s-ci-robot
Copy link
Contributor

Welcome @JamesOwenHall!

It looks like this is your first PR to kubernetes-sigs/controller-runtime 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-runtime has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Nov 23, 2023
@k8s-ci-robot
Copy link
Contributor

Hi @JamesOwenHall. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 23, 2023
}

// AsReconciler creates a Reconciler based on the given ObjectReconciler.
func AsReconciler[T client.Object](client client.Client, rec ObjectReconciler[T]) Reconciler {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In #2582 (comment), @alvaroaleman proposed adding a new method to builder.Builder so that an adapter function like AsReconciler here wouldn't be necessary. However, Go does not allow a method to add new type parameters, only a method receiver can define type parameters. Therefore, I believe that adding a Builder.CompleteObjectReconciler method would only be possible if Builder defined a type parameter, i.e. Builder[T client.Object]. I don't think this type parameter makes much sense for instances of reconcile.Reconciler so I opted to use an adapter function instead.

Copy link
Member

Choose a reason for hiding this comment

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

I actually realized the same thing later on while being on a walk, so agree 👍

@alvaroaleman
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Nov 23, 2023
@alvaroaleman alvaroaleman added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Nov 27, 2023
@troy0820
Copy link
Member

troy0820 commented Jan 5, 2024

I'm not sure what else this needs but after reviewing it, it looks good to me.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 5, 2024
@alvaroaleman
Copy link
Member

So @vincepri @sbueringer and myself had a bit of discussion around this and weren't really sure if this is what we want. Given that this is a rather trivial change that can easily be managed downstream, we'd like to defer merging this for now.

We can probably use one of the issues to discuss this and see if we can up with something everyone is comfortable with.

@alvaroaleman
Copy link
Member

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jan 8, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: JamesOwenHall, vincepri

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 9, 2024
@alvaroaleman
Copy link
Member

/hold cancel
nvm, looks like ppl are actually fine with it. Thanks for your work :)

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jan 9, 2024
@k8s-ci-robot k8s-ci-robot merged commit 984a121 into kubernetes-sigs:main Jan 9, 2024
9 checks passed
@JamesOwenHall JamesOwenHall deleted the object-reconciler branch January 9, 2024 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider using generics for the reconcile.Reconciler interface
6 participants