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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions pkg/controller/controllerutil/controllerutil.go
Expand Up @@ -349,26 +349,31 @@ func mutate(f MutateFn, key client.ObjectKey, obj client.Object) error {
type MutateFn func() error

// AddFinalizer accepts an Object and adds the provided finalizer if not present.
func AddFinalizer(o client.Object, finalizer string) {
// It returns an indication of whether it updated the object's list of finalizers.
func AddFinalizer(o client.Object, finalizer string) (finalizersUpdated bool) {
f := o.GetFinalizers()
for _, e := range f {
if e == finalizer {
return
return false
Copy link
Member

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

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?

}
}
o.SetFinalizers(append(f, finalizer))
return true
}

// RemoveFinalizer accepts an Object and removes the provided finalizer if present.
func RemoveFinalizer(o client.Object, finalizer string) {
// It returns an indication of whether it updated the object's list of finalizers.
func RemoveFinalizer(o client.Object, finalizer string) (finalizersUpdated bool) {
f := o.GetFinalizers()
for i := 0; i < len(f); i++ {
if f[i] == finalizer {
f = append(f[:i], f[i+1:]...)
i--
finalizersUpdated = true
}
}
o.SetFinalizers(f)
return
}

// ContainsFinalizer checks an Object that the provided finalizer is present.
Expand Down
57 changes: 57 additions & 0 deletions pkg/controller/controllerutil/controllerutil_test.go
Expand Up @@ -700,6 +700,62 @@ var _ = Describe("Controllerutil", func() {
})
})

Describe("AddFinalizer, which returns an indication of whether it modified the object's list of finalizers,", func() {
deploy = &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Finalizers: []string{},
},
}

When("the object's list of finalizers has no instances of the input finalizer", func() {
It("should return true", func() {
Expect(controllerutil.AddFinalizer(deploy, testFinalizer)).To(BeTrue())
})
It("should add the input finalizer to the object's list of finalizers", func() {
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
})
})

When("the object's list of finalizers has an instance of the input finalizer", func() {
It("should return false", func() {
Expect(controllerutil.AddFinalizer(deploy, testFinalizer)).To(BeFalse())
})
It("should not modify the object's list of finalizers", func() {
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
})
})
})

Describe("RemoveFinalizer, which returns an indication of whether it modified the object's list of finalizers,", func() {
When("the object's list of finalizers has no instances of the input finalizer", func() {
It("should return false", func() {
Expect(controllerutil.RemoveFinalizer(deploy, testFinalizer1)).To(BeFalse())
})
It("should not modify the object's list of finalizers", func() {
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
})
})

When("the object's list of finalizers has one instance of the input finalizer", func() {
It("should return true", func() {
Expect(controllerutil.RemoveFinalizer(deploy, testFinalizer)).To(BeTrue())
})
It("should remove the instance of the input finalizer from the object's list of finalizers", func() {
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
})
})

When("the object's list of finalizers has multiple instances of the input finalizer", func() {
It("should return true", func() {
deploy.SetFinalizers(append(deploy.Finalizers, testFinalizer, testFinalizer))
Expect(controllerutil.RemoveFinalizer(deploy, testFinalizer)).To(BeTrue())
})
It("should remove each instance of the input finalizer from the object's list of finalizers", func() {
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
})
})
})

Describe("ContainsFinalizer", func() {
It("should check that finalizer is present", func() {
controllerutil.AddFinalizer(deploy, testFinalizer)
Expand All @@ -715,6 +771,7 @@ var _ = Describe("Controllerutil", func() {
})

const testFinalizer = "foo.bar.baz"
const testFinalizer1 = testFinalizer + "1"

var _ runtime.Object = &errRuntimeObj{}
var _ metav1.Object = &errMetaObj{}
Expand Down