Lesson 6 of 9 · 26m
Building Operators with Kubebuilder and controller-runtime
The practical controller lifecycle: watches, owner references, finalizers, conditions, and the failure modes only production traffic reveals.
Kubebuilder generates scaffolding, not operational judgment. The generated code creates a manager, registers a reconciler, and wires watches. The hard part is deciding what the controller owns, how it reports progress, and what happens when deletion or partial failure occurs.
Watches determine your blast radius
return ctrl.NewControllerManagedBy(mgr).
For(&examplev1.Widget{}).
Owns(&corev1.Service{}).
Owns(&appsv1.Deployment{}).
Complete(r)For watches the custom resource. Owns watches children created with an owner reference. When a child changes, controller-runtime maps it back to the owning object and reconciles the owner. This is how drift repair works: delete the Service and the next child event causes it to be recreated.
Only watch what you can meaningfully reconcile. Watching every Secret in the cluster because one might matter creates noise, memory pressure, and security exposure.
Owner references and finalizers solve different problems
An owner reference is garbage collection metadata: when the parent is deleted, dependent children can be deleted automatically.
A finalizer is a deletion gate: Kubernetes marks the object for deletion but keeps it present until the controller removes the finalizer. Use finalizers when external cleanup is required.
if !widget.DeletionTimestamp.IsZero() {
if controllerutil.ContainsFinalizer(&widget, widgetFinalizer) {
if err := r.deleteExternalBucket(ctx, &widget); err != nil {
return ctrl.Result{RequeueAfter: time.Minute}, err
}
controllerutil.RemoveFinalizer(&widget, widgetFinalizer)
return ctrl.Result{}, r.Update(ctx, &widget)
}
return ctrl.Result{}, nil
}Deleting the custom resource is now an operation with a protocol. If external deletion hangs forever, the object hangs forever too. That is correct behavior, but it must be visible in status and alerts.
Status is part of the controller, not decoration
Set conditions that answer operator questions:
conditions:
- {type: Ready, status: "False", reason: StorageBackendUnavailable, message: "bucket provision request timed out after 45s"}Ready means the whole resource is usable. More specific conditions (Reconciling, Degraded, ExternalResourcesReady) let users distinguish normal convergence from stuck work. Include error detail in events as well; events are transient but excellent for immediate debugging.
Production failure modes
- Cache sync delays mean the controller can observe stale state immediately after writes.
- Conflict errors are normal under concurrency; requeue rather than treating them as fatal.
- A bad object can monopolize reconciliation if errors requeue too aggressively.
- Leader election matters. Run one active reconciler unless you have deliberately designed sharding.
Field notes
- Use
controllerutil.CreateOrUpdateonly when the desired object is fully specified; it can silently drop unmanaged fields. - Emit Kubernetes Events for significant transitions, but do not use events as an audit log.
- Test reconciliation with envtest; fake clients are useful but do not emulate every API-server behavior.
- Keep the operator's RBAC narrow. Cluster-wide secret access is usually a design failure, not a convenience.
Was this lesson helpful?