diff --git a/pkg/controller/controllerutil/controllerutil.go b/pkg/controller/controllerutil/controllerutil.go index 1207ee6cd8..9fc2350272 100644 --- a/pkg/controller/controllerutil/controllerutil.go +++ b/pkg/controller/controllerutil/controllerutil.go @@ -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 { diff --git a/pkg/controller/controllerutil/controllerutil_test.go b/pkg/controller/controllerutil/controllerutil_test.go index 74b736c7bb..0de2221650 100644 --- a/pkg/controller/controllerutil/controllerutil_test.go +++ b/pkg/controller/controllerutil/controllerutil_test.go @@ -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)) + }) + }) }) })