Skip to content

Commit

Permalink
use finalizer function
Browse files Browse the repository at this point in the history
  • Loading branch information
kanolato committed Aug 11, 2020
1 parent f7a3b65 commit f26572c
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/book/src/cronjob-tutorial/testdata/finalizer_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
// The object is not being deleted, so if it does not have our finalizer,
// then lets add the finalizer and update the object. This is equivalent
// registering our finalizer.
if !containsString(cronJob.ObjectMeta.Finalizers, myFinalizerName) {
cronJob.ObjectMeta.Finalizers = append(cronJob.ObjectMeta.Finalizers, myFinalizerName)
if !containsString(cronJob.GetFinalizers(), myFinalizerName) {
cronJob.SetFinalizers(append(cronJob.GetFinalizers(), myFinalizerName))
if err := r.Update(context.Background(), cronJob); err != nil {
return ctrl.Result{}, err
}
}
} else {
// The object is being deleted
if containsString(cronJob.ObjectMeta.Finalizers, myFinalizerName) {
if containsString(cronJob.GetFinalizers(), myFinalizerName) {
// our finalizer is present, so lets handle any external dependency
if err := r.deleteExternalResources(cronJob); err != nil {
// if fail to delete the external dependency here, return with error
Expand All @@ -75,7 +75,7 @@ func (r *CronJobReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}

// remove our finalizer from the list and update it.
cronJob.ObjectMeta.Finalizers = removeString(cronJob.ObjectMeta.Finalizers, myFinalizerName)
cronJob.SetFinalizers(removeString(cronJob.GetFinalizers(), myFinalizerName))
if err := r.Update(context.Background(), cronJob); err != nil {
return ctrl.Result{}, err
}
Expand Down

0 comments on commit f26572c

Please sign in to comment.