Sub topic 15
Reporting progress with status and conditions, and finalizers
This chapter assumes you know the spec and status split, the Reconcile function, and owner references from earlier chapters. You should also be able to read short Go snippets.
Status and conditions
A controller writes status to tell users and other controllers what it has observed. Users never write it. Two habits keep it trustworthy:
- Report observations, not intentions. Say "2 replicas ready", not "will create 3".
- Copy
metadata.generationintostatus.observedGenerationonce the controller has processed that spec. If the two numbers differ, the status describes an older spec.
A single phase string such as Running hides detail. Conditions are a list of independent facts, each with a fixed shape:
status:
observedGeneration: 2
conditions:
- type: Available
status: "True"
reason: MinimumReplicasAvailable
message: Deployment has minimum availability.
lastTransitionTime: "2026-12-05T07:00:00Z"
- type: Progressing
status: "True"
reason: ReplicaSetUpdated
message: ReplicaSet "web-6d4cf56db6" is progressing.type names the question, status is "True", "False" or "Unknown", and reason is a short CamelCase code for programs. message is text for people. lastTransitionTime changes only when status flips. In Go, meta.SetStatusCondition from k8s.io/apimachinery/pkg/api/meta handles that rule for you.
Write status through the status subresource, with r.Status().Update(...). A normal update cannot change status. That keeps a user edit of the spec from clobbering controller output.
Finalizers
Deleting an object normally removes it at once. Sometimes a controller must first clean up something outside the cluster, such as a cloud load balancer. A finalizer is a string in metadata.finalizers that blocks removal.
When you delete an object that has finalizers, the API server only sets metadata.deletionTimestamp. The object stays visible. The controller sees the timestamp, does its cleanup, and removes its own finalizer. When the list is empty, the API server deletes the object.
This helper adds the finalizer on normal passes and runs cleanup during deletion:
package controllers
import (
"context"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
const finalizerName = "example.com/cleanup"
// handleFinalizer returns true when the caller should stop reconciling.
func handleFinalizer(ctx context.Context, c client.Client, obj client.Object, cleanup func(context.Context) error) (bool, error) {
if obj.GetDeletionTimestamp().IsZero() {
if !controllerutil.ContainsFinalizer(obj, finalizerName) {
controllerutil.AddFinalizer(obj, finalizerName)
return false, c.Update(ctx, obj)
}
return false, nil
}
if controllerutil.ContainsFinalizer(obj, finalizerName) {
if err := cleanup(ctx); err != nil {
return true, err
}
controllerutil.RemoveFinalizer(obj, finalizerName)
if err := c.Update(ctx, obj); err != nil {
return true, err
}
}
return true, nil
}Returning an error from cleanup requeues the request with backoff, so cleanup must be idempotent. Treat "already gone" as success.
Exercise
A custom resource has been Terminating for an hour. kubectl get shows deletionTimestamp set and one finalizer, example.com/cleanup. What does this tell you, and what do you check next?
Show answer
The API server accepted the delete and is waiting for the controller that owns example.com/cleanup to finish cleanup and remove the finalizer. Check that this controller is running, then read its logs and the object's events for repeated cleanup errors.