Sub topic 12
Cleaning up with finalizers
This chapter assumes you know how a reconcile loop works and what owner references do. It also assumes you can read basic Go and kubectl output.
Why owner references are not enough
Garbage collection removes child objects that live inside the cluster. It cannot delete a DNS record, a cloud load balancer, or a database that your operator created outside Kubernetes. If the user runs kubectl delete webapp hello, the API server removes the object at once, and your controller never learns what was deleted.
A finalizer solves this. A finalizer is a string in metadata.finalizers. While that list is not empty, the API server will not remove the object.
How deletion works with a finalizer
- A client deletes the object.
- The API server sees finalizers on it. It sets
metadata.deletionTimestampand keeps the object. - Your controller gets a reconcile event and sees the timestamp. It runs its cleanup.
- The controller removes its finalizer from the list.
- When the list is empty, the API server deletes the object.
Adding and removing the finalizer
Add the finalizer on the first reconcile of a live object. When deletionTimestamp is set, clean up and then remove it.
const webAppFinalizer = "example.com/cleanup"
func (r *WebAppReconciler) cleanupExternal(ctx context.Context, app *examplev1.WebApp) error {
// Delete anything outside the cluster here. It must be safe to call repeatedly.
return nil
}
func (r *WebAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var app examplev1.WebApp
if err := r.Get(ctx, req.NamespacedName, &app); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if app.DeletionTimestamp.IsZero() {
if controllerutil.AddFinalizer(&app, webAppFinalizer) {
if err := r.Update(ctx, &app); err != nil {
return ctrl.Result{}, err
}
}
} else {
if controllerutil.ContainsFinalizer(&app, webAppFinalizer) {
if err := r.cleanupExternal(ctx, &app); err != nil {
return ctrl.Result{}, err
}
controllerutil.RemoveFinalizer(&app, webAppFinalizer)
if err := r.Update(ctx, &app); err != nil {
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil
}
// ...normal reconcile of the Deployment and status goes here...
return ctrl.Result{}, nil
}If cleanupExternal returns an error, the finalizer stays, and controller-runtime retries with backoff. The object remains in the Terminating state until cleanup succeeds.
Common errors
- Stuck in Terminating. The controller is down, crashing, or failing cleanup, so nobody removes the finalizer. Check the controller logs first.
- Cleanup that is not idempotent. Cleanup can run more than once. Treat "already gone" as success.
- Skipping the early return. Once deletion has begun, do not create or update children again.
- Removing finalizers by hand. Running
kubectl patchto clear the list unblocks deletion, but it leaks the external resources.
Exercise
A WebApp shows a deletionTimestamp but never disappears. Its finalizers list contains example.com/cleanup, and the operator pod is crash-looping. What is the cause, and what is the right fix?
Show answer
The API server waits for the controller to remove its finalizer, and a crashing controller cannot do that. Fix the operator so it starts again, runs cleanup, and removes the finalizer. Patching the finalizer away by hand would delete the object but leak the external resources.