Skip to content

Commit 4761631

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

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

pkg/config/v1alpha1/zz_generated.deepcopy.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/controller/controllerutil/controllerutil.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,25 +350,38 @@ type MutateFn func() error
350350

351351
// AddFinalizer accepts an Object and adds the provided finalizer if not present.
352352
func AddFinalizer(o client.Object, finalizer string) {
353+
AddFinalizerV2(o, finalizer)
354+
}
355+
356+
// AddFinalizerV2 accepts an Object and adds the provided finalizer and returns true if not present.
357+
func AddFinalizerV2(o client.Object, finalizer string) (finalizersUpdated bool) {
353358
f := o.GetFinalizers()
354359
for _, e := range f {
355360
if e == finalizer {
356-
return
361+
return false
357362
}
358363
}
359364
o.SetFinalizers(append(f, finalizer))
365+
return true
360366
}
361367

362368
// RemoveFinalizer accepts an Object and removes the provided finalizer if present.
363369
func RemoveFinalizer(o client.Object, finalizer string) {
370+
RemoveFinalizerV2(o, finalizer)
371+
}
372+
373+
// RemoveFinalizerV2 accepts an Object and removes the provided finalizer and returns true if present.
374+
func RemoveFinalizerV2(o client.Object, finalizer string) (finalizersUpdated bool) {
364375
f := o.GetFinalizers()
365376
for i := 0; i < len(f); i++ {
366377
if f[i] == finalizer {
367378
f = append(f[:i], f[i+1:]...)
368379
i--
380+
finalizersUpdated = true
369381
}
370382
}
371383
o.SetFinalizers(f)
384+
return
372385
}
373386

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

pkg/controller/controllerutil/controllerutil_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,62 @@ var _ = Describe("Controllerutil", func() {
700700
})
701701
})
702702

703+
Describe("AddFinalizerV2, which returns an indication of whether it modified the input object's finalizers list,", func() {
704+
deploy = &appsv1.Deployment{
705+
ObjectMeta: metav1.ObjectMeta{
706+
Finalizers: []string{},
707+
},
708+
}
709+
710+
When("the input object's finalizers list has no instances of the input finalizer", func() {
711+
It("should return true", func() {
712+
Expect(controllerutil.AddFinalizerV2(deploy, testFinalizer)).To(BeTrue())
713+
})
714+
It("should add the input finalizer to the input object's finalizers list", func() {
715+
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
716+
})
717+
})
718+
719+
When("the input object's finalizers list has an instance of the input finalizer", func() {
720+
It("should return false", func() {
721+
Expect(controllerutil.AddFinalizerV2(deploy, testFinalizer)).To(BeFalse())
722+
})
723+
It("should not modify the input object's finalizers list", func() {
724+
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
725+
})
726+
})
727+
})
728+
729+
Describe("RemoveFinalizerV2, which returns an indication of whether it modified the input object's finalizers list,", func() {
730+
When("the input object's finalizers list has no instances of the input finalizer", func() {
731+
It("should return false", func() {
732+
Expect(controllerutil.RemoveFinalizerV2(deploy, testFinalizer1)).To(BeFalse())
733+
})
734+
It("should not modify the input object's finalizers list", func() {
735+
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{testFinalizer}))
736+
})
737+
})
738+
739+
When("the input object's finalizers list has one instance of the input finalizer", func() {
740+
It("should return true", func() {
741+
Expect(controllerutil.RemoveFinalizerV2(deploy, testFinalizer)).To(BeTrue())
742+
})
743+
It("should remove the instance of the input finalizer from the input object's finalizers list", func() {
744+
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
745+
})
746+
})
747+
748+
When("the input object's finalizers list has multiple instances of the input finalizer", func() {
749+
It("should return true", func() {
750+
deploy.SetFinalizers(append(deploy.Finalizers, testFinalizer, testFinalizer))
751+
Expect(controllerutil.RemoveFinalizerV2(deploy, testFinalizer)).To(BeTrue())
752+
})
753+
It("should remove each instance of the input finalizer from the input object's finalizers list", func() {
754+
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
755+
})
756+
})
757+
})
758+
703759
Describe("ContainsFinalizer", func() {
704760
It("should check that finalizer is present", func() {
705761
controllerutil.AddFinalizer(deploy, testFinalizer)
@@ -715,6 +771,7 @@ var _ = Describe("Controllerutil", func() {
715771
})
716772

717773
const testFinalizer = "foo.bar.baz"
774+
const testFinalizer1 = testFinalizer + "1"
718775

719776
var _ runtime.Object = &errRuntimeObj{}
720777
var _ metav1.Object = &errMetaObj{}

0 commit comments

Comments
 (0)