Skip to content

Commit

Permalink
Merge pull request #1000 from detiber/backport-922
Browse files Browse the repository at this point in the history
✨ Add ContainsFinalizer helper to the controllerutil
  • Loading branch information
k8s-ci-robot authored Jun 16, 2020
2 parents 5291db5 + 3252e64 commit 3e5a1d7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/controller/controllerutil/controllerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,17 @@ func RemoveFinalizerWithError(o runtime.Object, finalizer string) error {
return nil
}

// ContainsFinalizer checks a metav1 object that the provided finalizer is present.
func ContainsFinalizer(o Object, finalizer string) bool {
f := o.GetFinalizers()
for _, e := range f {
if e == finalizer {
return true
}
}
return false
}

// Object allows functions to work indistinctly with any resource that
// implements both Object interfaces.
type Object interface {
Expand Down
12 changes: 12 additions & 0 deletions pkg/controller/controllerutil/controllerutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,18 @@ var _ = Describe("Controllerutil", func() {
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
})
})

Describe("ContainsFinalizer", func() {
It("should check that finalizer is present", func() {
controllerutil.AddFinalizer(deploy, testFinalizer)
Expect(controllerutil.ContainsFinalizer(deploy, testFinalizer)).To(Equal(true))
})

It("should check that finalizer is not present after RemoveFinalizer call", func() {
controllerutil.RemoveFinalizer(deploy, testFinalizer)
Expect(controllerutil.ContainsFinalizer(deploy, testFinalizer)).To(Equal(false))
})
})
})
})

Expand Down

0 comments on commit 3e5a1d7

Please sign in to comment.