Skip to content

Commit

Permalink
Merge pull request #1636 from hatfieldbrian/master
Browse files Browse the repository at this point in the history
✨return a bool from AddFinalizer and RemoveFinalizer
  • Loading branch information
k8s-ci-robot committed Apr 16, 2022
2 parents 954449d + b8bc54a commit eb39b8e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
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
}
}
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

0 comments on commit eb39b8e

Please sign in to comment.