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

🌱 remove redundant watch event handlers via Owns() #10048

Merged

Conversation

adityabhatia
Copy link
Contributor

@adityabhatia adityabhatia commented Jan 24, 2024

What this PR does / why we need it:

Owns() watches for child resources and enqueues requests for owners (owner reference) with controller reference. This PR aims at removing Owns() where such enqueuing is already in place via Watches(), thereby removing both redundant watch and enqueuing of resources.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-area PR is missing an area label labels Jan 24, 2024
@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jan 24, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @adityabhatia. 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.

@adityabhatia adityabhatia changed the title WIP: remove Watches()for resources implemented with Owns() 🌱 WIP: remove Watches() for resources implemented with Owns() Jan 24, 2024
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 24, 2024
@k8s-ci-robot
Copy link
Contributor

@adityabhatia: The label(s) area/help cannot be applied, because the repository doesn't have them.

In response to this:

/area help

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.

@adityabhatia
Copy link
Contributor Author

/area machine
/area machineset

@k8s-ci-robot k8s-ci-robot added area/machine Issues or PRs related to machine lifecycle management area/machineset Issues or PRs related to machinesets and removed do-not-merge/needs-area PR is missing an area label labels Jan 24, 2024
@sbueringer
Copy link
Member

@adityabhatia let me know when/if I should take a look (I'm ignoring WIP PRs per default if nobody asks for a review)

@adityabhatia
Copy link
Contributor Author

adityabhatia commented Jan 25, 2024

@adityabhatia let me know when/if I should take a look (I'm ignoring WIP PRs per default if nobody asks for a review)

Sure please have a look. I wanted to create an issue for it, explaining the goal (to do a feasibility check).

Eg.

		Owns(&clusterv1.MachineSet{}).
		Watches(
			&clusterv1.MachineSet{},
			handler.EnqueueRequestsFromMapFunc(r.MachineSetToDeployments),
		)

here Owns() (internally translates to a watching and enqueuing owner resources of controller type) and Watches() do the same thing. If the idea sounds good, I can go ahead and create an issue with a more detailed description. 🙂

Comment on lines 84 to 89
Owns(&clusterv1.MachineSet{}).
Watches(
&clusterv1.MachineSet{},
handler.EnqueueRequestsFromMapFunc(r.MachineSetToDeployments),
).
Copy link
Member

Choose a reason for hiding this comment

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

This is only equivalent to Owns if we basically implement the same thing. But MachineSetToDeployments does more than that. I assume because we can't always rely on ownerRefs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, the labels are there as backup. Going by that logic, now thinking from the opposite perspective wouldn't Owns() be redundant?

Copy link
Member

Choose a reason for hiding this comment

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

Assuming our implementation is bug free (which should be easy to verify). Yes!

@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jan 25, 2024
@adityabhatia adityabhatia changed the title 🌱 WIP: remove Watches() for resources implemented with Owns() 🌱 remove redundant watch event handlers via Owns() Jan 25, 2024
@@ -82,7 +82,6 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt

err = ctrl.NewControllerManagedBy(mgr).
For(&clusterv1.MachineDeployment{}).
Copy link
Member

Choose a reason for hiding this comment

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

I took a closer look into the mapping functions. Looks like they don't enqueue at all based on ownerRefs

	// Check if the controller reference is already set and
	// return an empty result when one is found.
	for _, ref := range ms.ObjectMeta.GetOwnerReferences() {
		if ref.Controller != nil && *ref.Controller {
			return result
		}
	}

I think the goal of these function is to only enqueue if owner is not set. And Owns then takes care of it once the owner is set.

// MachineSetToDeployments is a handler.ToRequestsFunc to be used to enqueue requests for reconciliation
// for MachineDeployments that might adopt an orphaned MachineSet.
func (r *Reconciler) MachineSetToDeployments(ctx context.Context, o client.Object) []ctrl.Request {

Maybe we should add the comment here in the builder as well? (and of course keep Owns)

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sry somehow lost track of this PR, I added comments now.

@sbueringer
Copy link
Member

Thank you!

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 10, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: bb67874ce29a28459d0f7339124d389f3d6c690d

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sbueringer

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 Apr 10, 2024
@sbueringer
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. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 10, 2024
@k8s-ci-robot k8s-ci-robot merged commit f7c0959 into kubernetes-sigs:main Apr 10, 2024
21 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.8 milestone Apr 10, 2024
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. area/machine Issues or PRs related to machine lifecycle management area/machineset Issues or PRs related to machinesets 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/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants