Skip to content

Commit ac71afe

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

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,42 @@ var _ = Describe("Controllerutil", func() {
700700
})
701701
})
702702

703+
Describe("AddFinalizer", func() {
704+
deploy = &appsv1.Deployment{
705+
ObjectMeta: metav1.ObjectMeta{
706+
Finalizers: []string{},
707+
},
708+
}
709+
710+
It("should add the finalizer when not present", func() {
711+
Expect(controllerutil.AddFinalizer(deploy, testFinalizer)).To(BeTrue())
712+
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
713+
})
714+
715+
It("should not add the finalizer when already present", func() {
716+
Expect(controllerutil.AddFinalizer(deploy, testFinalizer)).To(BeFalse())
717+
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
718+
})
719+
})
720+
721+
Describe("RemoveFinalizer", func() {
722+
It("should not remove a finalizer not present", func() {
723+
Expect(controllerutil.RemoveFinalizer(deploy, testFinalizer1)).To(BeFalse())
724+
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
725+
})
726+
727+
It("should remove finalizer if present", func() {
728+
Expect(controllerutil.RemoveFinalizer(deploy, testFinalizer)).To(BeTrue())
729+
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
730+
})
731+
732+
It("should remove all equal finalizers if present", func() {
733+
deploy.SetFinalizers(append(deploy.Finalizers, testFinalizer, testFinalizer))
734+
Expect(controllerutil.RemoveFinalizer(deploy, testFinalizer)).To(BeTrue())
735+
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
736+
})
737+
})
738+
703739
Describe("ContainsFinalizer", func() {
704740
It("should check that finalizer is present", func() {
705741
controllerutil.AddFinalizer(deploy, testFinalizer)
@@ -715,6 +751,7 @@ var _ = Describe("Controllerutil", func() {
715751
})
716752

717753
const testFinalizer = "foo.bar.baz"
754+
const testFinalizer1 = testFinalizer + "1"
718755

719756
var _ runtime.Object = &errRuntimeObj{}
720757
var _ metav1.Object = &errMetaObj{}

0 commit comments

Comments
 (0)