Skip to content

Commit e51c785

Browse files
authored
Merge pull request #2426 from dmvolod/issue-2425
📖 Utilize controllerutil.ContainsFinalizer inside kubebuilder book CronJob finalizer example
2 parents 2526ae5 + d64c1bd commit e51c785

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

docs/book/src/cronjob-tutorial/testdata/finalizer_example.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
6666
// The object is not being deleted, so if it does not have our finalizer,
6767
// then lets add the finalizer and update the object. This is equivalent
6868
// registering our finalizer.
69-
if !containsString(cronJob.GetFinalizers(), myFinalizerName) {
69+
if !controllerutil.ContainsFinalizer(cronJob, myFinalizerName) {
7070
controllerutil.AddFinalizer(cronJob, myFinalizerName)
7171
if err := r.Update(ctx, cronJob); err != nil {
7272
return ctrl.Result{}, err
7373
}
7474
}
7575
} else {
7676
// The object is being deleted
77-
if containsString(cronJob.GetFinalizers(), myFinalizerName) {
77+
if controllerutil.ContainsFinalizer(cronJob, myFinalizerName) {
7878
// our finalizer is present, so lets handle any external dependency
7979
if err := r.deleteExternalResources(cronJob); err != nil {
8080
// if fail to delete the external dependency here, return with error
@@ -106,12 +106,3 @@ func (r *Reconciler) deleteExternalResources(cronJob *batch.CronJob) error {
106106
// multiple times for same object.
107107
}
108108

109-
// Helper functions to check and remove string from a slice of strings.
110-
func containsString(slice []string, s string) bool {
111-
for _, item := range slice {
112-
if item == s {
113-
return true
114-
}
115-
}
116-
return false
117-
}

0 commit comments

Comments
 (0)