Skip to content

Commit

Permalink
return a bool from AddFinalizer and RemoveFinalizer
Browse files Browse the repository at this point in the history
Signed-off-by: hatfieldbrian <bhatfiel@us.ibm.com>
  • Loading branch information
hatfieldbrian committed Aug 19, 2021
1 parent 4e7f0c9 commit b3803f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 9 additions & 5 deletions pkg/controller/controllerutil/controllerutil.go
Expand Up @@ -348,27 +348,31 @@ func mutate(f MutateFn, key client.ObjectKey, obj client.Object) error {
// MutateFn is a function which mutates the existing object into it's desired state.
type MutateFn func() error

// AddFinalizer accepts an Object and adds the provided finalizer if not present.
func AddFinalizer(o client.Object, finalizer string) {
// AddFinalizer accepts an Object and adds the provided finalizer and returns true if not present.
func AddFinalizer(o client.Object, finalizer string) bool {
f := o.GetFinalizers()
for _, e := range f {
if e == finalizer {
return
return false
}
}
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) {
// RemoveFinalizer accepts an Object and removes the provided finalizer and returns true if present.
func RemoveFinalizer(o client.Object, finalizer string) bool {
finalizerRemoved := false
f := o.GetFinalizers()
for i := 0; i < len(f); i++ {
if f[i] == finalizer {
f = append(f[:i], f[i+1:]...)
i--
finalizerRemoved = true
}
}
o.SetFinalizers(f)
return finalizerRemoved
}

// ContainsFinalizer checks an Object that the provided finalizer is present.
Expand Down
15 changes: 11 additions & 4 deletions pkg/controller/controllerutil/controllerutil_test.go
Expand Up @@ -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())
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
})

It("should not add the finalizer when already present", func() {
controllerutil.AddFinalizer(deploy, testFinalizer)
Expect(controllerutil.AddFinalizer(deploy, testFinalizer)).To(BeFalse())
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
})
})

Describe("RemoveFinalizer", func() {
It("should not remove a finalizer not present", func() {
Expect(controllerutil.RemoveFinalizer(deploy, testFinalizer1)).To(BeFalse())
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
})

It("should remove finalizer if present", func() {
controllerutil.RemoveFinalizer(deploy, testFinalizer)
Expect(controllerutil.RemoveFinalizer(deploy, testFinalizer)).To(BeTrue())
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
})

It("should remove all equal finalizers if present", func() {
deploy.SetFinalizers(append(deploy.Finalizers, testFinalizer, testFinalizer))
controllerutil.RemoveFinalizer(deploy, testFinalizer)
Expect(controllerutil.RemoveFinalizer(deploy, testFinalizer)).To(BeTrue())
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
})

})

Describe("ContainsFinalizer", func() {
Expand All @@ -715,6 +721,7 @@ var _ = Describe("Controllerutil", func() {
})

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

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

0 comments on commit b3803f2

Please sign in to comment.