-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
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. |
Welcome @hatfieldbrian! |
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 Once the patch is verified, the new status will be reflected by the 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. |
f := o.GetFinalizers() | ||
for _, e := range f { | ||
if e == finalizer { | ||
return | ||
return false |
There was a problem hiding this comment.
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:
- 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). - 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?
There was a problem hiding this comment.
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:
controller-runtime/pkg/finalizer/finalizer.go
Lines 57 to 58 in 4e7f0c9
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?
@@ -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()) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
/ok-to-test |
@@ -700,6 +700,42 @@ var _ = Describe("Controllerutil", func() { | |||
}) | |||
}) | |||
|
|||
Describe("AddFinalizer", func() { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
724c752
to
4761631
Compare
The 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 |
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 |
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>
@hatfieldbrian: The following test failed, say
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
[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 |
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 callingAddFinalizer
orRemoveFinalizer
. 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.