Skip to content

✨ Add ContainsFinalizer helper to the controllerutil #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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