Skip to content

Commit 89c8930

Browse files
committed
return a bool from AddFinalizer and RemoveFinalizer
Signed-off-by: hatfieldbrian <[email protected]>
1 parent 4e7f0c9 commit 89c8930

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

pkg/controller/controllerutil/controllerutil.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,27 +348,30 @@ func mutate(f MutateFn, key client.ObjectKey, obj client.Object) error {
348348
// MutateFn is a function which mutates the existing object into it's desired state.
349349
type MutateFn func() error
350350

351-
// AddFinalizer accepts an Object and adds the provided finalizer if not present.
352-
func AddFinalizer(o client.Object, finalizer string) {
351+
// AddFinalizer accepts an Object and adds the provided finalizer and returns true if not present.
352+
func AddFinalizer(o client.Object, finalizer string) (finalizersUpdated bool) {
353353
f := o.GetFinalizers()
354354
for _, e := range f {
355355
if e == finalizer {
356-
return
356+
return false
357357
}
358358
}
359359
o.SetFinalizers(append(f, finalizer))
360+
return true
360361
}
361362

362-
// RemoveFinalizer accepts an Object and removes the provided finalizer if present.
363-
func RemoveFinalizer(o client.Object, finalizer string) {
363+
// RemoveFinalizer accepts an Object and removes the provided finalizer and returns true if present.
364+
func RemoveFinalizer(o client.Object, finalizer string) (finalizersUpdated bool) {
364365
f := o.GetFinalizers()
365366
for i := 0; i < len(f); i++ {
366367
if f[i] == finalizer {
367368
f = append(f[:i], f[i+1:]...)
368369
i--
370+
finalizersUpdated = true
369371
}
370372
}
371373
o.SetFinalizers(f)
374+
return
372375
}
373376

374377
// ContainsFinalizer checks an Object that the provided finalizer is present.

pkg/controller/controllerutil/controllerutil_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,27 +677,33 @@ var _ = Describe("Controllerutil", func() {
677677
}
678678

679679
It("should add the finalizer when not present", func() {
680-
controllerutil.AddFinalizer(deploy, testFinalizer)
680+
Expect(controllerutil.AddFinalizer(deploy, testFinalizer)).To(BeTrue())
681681
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
682682
})
683683

684684
It("should not add the finalizer when already present", func() {
685-
controllerutil.AddFinalizer(deploy, testFinalizer)
685+
Expect(controllerutil.AddFinalizer(deploy, testFinalizer)).To(BeFalse())
686686
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
687687
})
688688
})
689689

690690
Describe("RemoveFinalizer", func() {
691+
It("should not remove a finalizer not present", func() {
692+
Expect(controllerutil.RemoveFinalizer(deploy, testFinalizer1)).To(BeFalse())
693+
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
694+
})
695+
691696
It("should remove finalizer if present", func() {
692-
controllerutil.RemoveFinalizer(deploy, testFinalizer)
697+
Expect(controllerutil.RemoveFinalizer(deploy, testFinalizer)).To(BeTrue())
693698
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
694699
})
695700

696701
It("should remove all equal finalizers if present", func() {
697702
deploy.SetFinalizers(append(deploy.Finalizers, testFinalizer, testFinalizer))
698-
controllerutil.RemoveFinalizer(deploy, testFinalizer)
703+
Expect(controllerutil.RemoveFinalizer(deploy, testFinalizer)).To(BeTrue())
699704
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
700705
})
706+
701707
})
702708

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

717723
const testFinalizer = "foo.bar.baz"
724+
const testFinalizer1 = testFinalizer + "1"
718725

719726
var _ runtime.Object = &errRuntimeObj{}
720727
var _ metav1.Object = &errMetaObj{}

0 commit comments

Comments
 (0)