Skip to content

Commit

Permalink
refactor: finalizer example to re-fetch after updating resource
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzofelletti committed Nov 25, 2024
1 parent 99da1bb commit 5bdafe0
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/book/src/cronjob-tutorial/testdata/finalizer_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
if err := r.Update(ctx, cronJob); err != nil {
return ctrl.Result{}, err
}

// we re-fetch after having updated the CronJob, so that we have the latest
// state of the resource, and avoid the error "the object has been modified,
// please apply your changes to the latest version and try again".
if err := r.Get(ctx, req.NamespacedName, cronJob); err != nil {
log.Error(err, "unable to fetch CronJob")
return ctrl.Result{}, client.IgnoreNotFound(err)
}
}
} else {
// The object is being deleted
Expand Down

0 comments on commit 5bdafe0

Please sign in to comment.