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

return a bool from AddFinalizer and RemoveFinalizer #1636

Merged
merged 1 commit into from Apr 16, 2022

Conversation

hatfieldbrian
Copy link
Contributor

@hatfieldbrian hatfieldbrian commented Aug 19, 2021

When a finalizer is added or removed to an object, typically a client requests the object be updated on the server. Instead of making an update request unconditionally, it may be desirable to do so only when the object is modified, as an optimization. Since the add and remove finalizer routines do not return an indication whether a finalizer was add or removed respectively, a caller needs to determine this information some other way.

One way is to call ContainsFinalizer before calling AddFinalizer or RemoveFinalizer. But the time complexity of each of those routines is linear, resulting in two linear time functions when a finalizer is added or removed.

Another solution is to save the length of a the finalizer slice before calling add or remove finalizer and checking if the length is different upon return. This improves over the previous solution presuming len() of a slice is a constant time complexity operation.

This solution provided in this pull request simplifies further by not requiring the add or remove finalizer caller save slice length prior to call. Instead, the caller can simply call add or remove finalizer and if it returns true, then make the object update request.

@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Aug 19, 2021

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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. I understand the commands that are listed here.

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

k8s-ci-robot commented Aug 19, 2021

Welcome @hatfieldbrian!

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 Aug 19, 2021
@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Aug 19, 2021

Hi @hatfieldbrian. 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/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Aug 19, 2021
f := o.GetFinalizers()
for _, e := range f {
if e == finalizer {
return
return false
Copy link
Contributor

@varshaprasad96 varshaprasad96 Aug 19, 2021

Choose a reason for hiding this comment

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

This addition does seem useful. But the other way which I could think of it is this:

  1. Case 1: Finalizer is present and user wants it to be or finalizer is not present and user wants it removed - In this case, we can just use ConatinsFinalizer(), which is a linear operation of complexity O(n).
  2. Case 2: When the finalizer is present and user wants it removed or vice versa: Here there needs to be an update to the object which needs to happen. As you have explained, we would have to use ContainsFinalizer + Add/RemoveFinalizer where there would be linear iteration twice. The complexity still remains linear. I am not sure if there would be much performance improvement by saving on one iteration, since updating the object is anyway a necessity.

We could still return a signal in Add or Remove Finalizer saying it has been updated or not, but I think boolean may not be apt. Returning false in AddFinalizer can also be interpreted by users that it has not been added to the object at all (though with proper docs we could explain that its just adding element to a list). We can instead maybe return an indication that it is already present.

type FinalizerStatus string

const (
	FinalizerUpdated    FinalizerStatus = "updated"
	FinalizerNotUpdated FinalizerStatus = "notupdated"
)

// AddFinalizer accepts an Object and adds the provided finalizer if not present.
func AddFinalizer(o client.Object, finalizer string) FinalizerStatus {
	f := o.GetFinalizers()
	for _, e := range f {
		if e == finalizer {
			return FinalizerUpdated
		}
	}
	o.SetFinalizers(append(f, finalizer))
	return FinalizerNotUpdated
}

However, this would be a breaking change. Also, I think the finalizer library already implements this in a better way. Would the library helpers satisfy your use case?

Copy link
Contributor Author

@hatfieldbrian hatfieldbrian Aug 20, 2021

Choose a reason for hiding this comment

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

Thanks for the fast response!

The finalizer library contains the primary issue I intend to solve: two linear iterations. For example, on lines 57 and 58 when a finalizer is added:

if dt := obj.GetDeletionTimestamp(); dt.IsZero() && !controllerutil.ContainsFinalizer(obj, key) {
controllerutil.AddFinalizer(obj, key)

and lines 60 and 72 when one is removed. It does provide a bool res.Updated indicating whether the object was updated, but the reason I propose it be added is to avoid the two linear iterations issue it contains.

The functions are called AddFinalizer and RemoveFinalizer so I think it is intuitive and logical to interpret a boolean return code as an indication of whether each did, in this invocation, what its name indicates it should.

I would be ok with wrapping the boolean in a struct just to give it a name that the programmer must type. The library's struct has more than one field, making a structure more reasonable.

A string takes more work to match.

I propose naming the bool return value finalizersUpdated in the function signature to avoid misinterpretation. I updated the pull request with that solution. Thoughts?

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Aug 20, 2021
@@ -677,27 +677,33 @@ var _ = Describe("Controllerutil", func() {
}

It("should add the finalizer when not present", func() {
controllerutil.AddFinalizer(deploy, testFinalizer)
Expect(controllerutil.AddFinalizer(deploy, testFinalizer)).To(BeTrue())
Copy link
Contributor

@dmvolod dmvolod Jan 6, 2022

Choose a reason for hiding this comment

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

It would be nice to keep old test "as is" without return validation to check for backward compatibility and create an additional tests for the new functionality validation

Copy link
Contributor Author

@hatfieldbrian hatfieldbrian Jan 12, 2022

Choose a reason for hiding this comment

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

A change was force-pushed that leaves the old tests unchanged and adds the new ones

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 11, 2022
@alvaroaleman
Copy link
Member

alvaroaleman commented Apr 7, 2022

/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 7, 2022
@@ -700,6 +700,42 @@ var _ = Describe("Controllerutil", func() {
})
})

Describe("AddFinalizer", func() {
Copy link
Member

@alvaroaleman alvaroaleman Apr 7, 2022

Choose a reason for hiding this comment

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

I think you need to change the name, otherwise Ginko de-duplicates, the junit shows two instances of AddFinalizer and not four: https://prow.k8s.io/view/gs/kubernetes-jenkins/pr-logs/pull/kubernetes-sigs_controller-runtime/1636/pull-controller-runtime-test-master/1512120552670629888

Copy link
Contributor Author

@hatfieldbrian hatfieldbrian Apr 15, 2022

Choose a reason for hiding this comment

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

The new tests' descriptions have been changed to be different than the originals

@hatfieldbrian hatfieldbrian force-pushed the master branch 2 times, most recently from 724c752 to 4761631 Compare Apr 15, 2022
@hatfieldbrian
Copy link
Contributor Author

hatfieldbrian commented Apr 15, 2022

The apidiff job failed:

verifying api diff
invoking: 'hack/tools/bin/go-apidiff 954449d15797527d32eb9071c4b95b3e44c[26](https://prow.k8s.io/view/gs/kubernetes-jenkins/pr-logs/pull/kubernetes-sigs_controller-runtime/1636/pull-controller-runtime-apidiff-master/1514882772970246144#1:build-log.txt%3A26)115 --print-compatible'
sigs.k8s.io/controller-runtime/pkg/controller/controllerutil
  Incompatible changes:
  - AddFinalizer: changed from func(sigs.k8s.io/controller-runtime/pkg/client.Object, string) to func(sigs.k8s.io/controller-runtime/pkg/client.Object, string) bool
  - RemoveFinalizer: changed from func(sigs.k8s.io/controller-runtime/pkg/client.Object, string) to func(sigs.k8s.io/controller-runtime/pkg/client.Object, string) bool

It seems an API function may not be changed to return something, so new versions of the routines were defined named AddFinalizerV2 and RemoveFinalizerV2.

@alvaroaleman
Copy link
Member

alvaroaleman commented Apr 15, 2022

The apidiff job failed:

Yes, but it is not mandatory, it is only intending to inform us about compatibility breaks.

I don't think an added non-error return to a func that didn't return anything before can actually break anyone, because nothing can not be assigned to a variable nor returned and linters won't care either.

@alvaroaleman
Copy link
Member

alvaroaleman commented Apr 15, 2022

I meant to say: Please revert that and keep only one version of the funcs with the old name.

Signed-off-by: hatfieldbrian <bhatfiel@redhat.com>
@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Apr 16, 2022

@hatfieldbrian: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-controller-runtime-apidiff-master b8bc54a link false /test pull-controller-runtime-apidiff-master

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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. I understand the commands that are listed here.

Copy link
Member

@alvaroaleman alvaroaleman left a comment

thanks!

@k8s-ci-robot k8s-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Apr 16, 2022
@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Apr 16, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, hatfieldbrian

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 16, 2022
@k8s-ci-robot k8s-ci-robot merged commit eb39b8e into kubernetes-sigs:master Apr 16, 2022
4 of 5 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v0.10.x milestone Apr 16, 2022
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 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/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants