Skip to content

Commit 2f1457b

Browse files
authored
Merge pull request #922 from dmvolod/issue-920
✨ Add ContainsFinalizer helper to the controllerutil
2 parents a2d55b5 + b999e72 commit 2f1457b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

pkg/controller/controllerutil/controllerutil.go

+11
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,17 @@ func RemoveFinalizerWithError(o runtime.Object, finalizer string) error {
282282
return nil
283283
}
284284

285+
// ContainsFinalizer checks a metav1 object that the provided finalizer is present.
286+
func ContainsFinalizer(o Object, finalizer string) bool {
287+
f := o.GetFinalizers()
288+
for _, e := range f {
289+
if e == finalizer {
290+
return true
291+
}
292+
}
293+
return false
294+
}
295+
285296
// Object allows functions to work indistinctly with any resource that
286297
// implements both Object interfaces.
287298
type Object interface {

pkg/controller/controllerutil/controllerutil_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,18 @@ var _ = Describe("Controllerutil", func() {
452452
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
453453
})
454454
})
455+
456+
Describe("ContainsFinalizer", func() {
457+
It("should check that finalizer is present", func() {
458+
controllerutil.AddFinalizer(deploy, testFinalizer)
459+
Expect(controllerutil.ContainsFinalizer(deploy, testFinalizer)).To(Equal(true))
460+
})
461+
462+
It("should check that finalizer is not present after RemoveFinalizer call", func() {
463+
controllerutil.RemoveFinalizer(deploy, testFinalizer)
464+
Expect(controllerutil.ContainsFinalizer(deploy, testFinalizer)).To(Equal(false))
465+
})
466+
})
455467
})
456468
})
457469

0 commit comments

Comments
 (0)