Sub topic 10
Owned resources and garbage collection
This chapter assumes you can read the WebApp reconciler from earlier chapters, know what a Deployment and a ReplicaSet are, and have used kubectl get -o yaml. It covers how an operator marks the objects it creates as its own, and how Kubernetes deletes them automatically.
Why ownership matters
A WebApp creates a Deployment. When someone deletes the WebApp, the Deployment should go away too. The operator could watch for deletions and clean up by hand. Kubernetes already has a mechanism for this: owner references.
An owner reference is an entry in a child object's metadata.ownerReferences list. It names the parent by apiVersion, kind, name and uid. The garbage collector, a controller inside kube-controller-manager, watches these links. When every owner of an object is gone, the collector deletes the object.
metadata:
name: hello
namespace: default
ownerReferences:
- apiVersion: example.com/v1
kind: WebApp
name: hello
uid: 6f1c2a9e-0000-0000-0000-000000000000
controller: true
blockOwnerDeletion: trueThe uid identifies one specific object. If you delete a WebApp and create a new one with the same name, the new one has a different uid, so the old children are not mistaken for its own.
Setting the reference in the reconciler
You rarely write this YAML. The reconciler from the spine calls controllerutil.SetControllerReference(&app, deploy, r.Scheme) inside the CreateOrUpdate mutate function. That call fills in the owner reference with controller: true and blockOwnerDeletion: true.
Only one owner on an object can have controller: true. This marks which controller manages it, and it prevents two operators from fighting over the same child.
Ownership chains. Deleting the WebApp removes the Deployment, which removes the ReplicaSet, which removes the Pods.
Owns() and event handling
Ownership also drives event routing. In SetupWithManager, Owns(&appsv1.Deployment{}) tells controller-runtime to watch Deployments. When a Deployment changes, the framework reads its controller owner reference and queues a reconcile for that WebApp. This is how the operator notices a changed readyReplicas count or a manually deleted Deployment.
Deletion propagation
When you delete a parent, the propagation policy decides what happens to its children.
| Policy | Behavior |
|---|---|
| Background (default) | The parent is removed at once. The collector deletes children afterward. |
| Foreground | The parent stays, marked with a `foregroundDeletion` finalizer, until children with `blockOwnerDeletion: true` are gone. |
| Orphan | The collector removes the owner reference and leaves the children running. |
You can choose a policy with kubectl:
kubectl delete webapp hello --cascade=foreground
kubectl delete webapp hello --cascade=orphanCommon errors
- Cross-namespace owners. A namespaced owner can only own objects in its own namespace. A cluster-scoped owner can own anything. A namespaced child with an owner in another namespace is treated as having a missing owner, and the collector may delete it.
- Missing owner reference. Without it, deleting the
WebAppleaves the Deployment behind. - Wrong scheme.
SetControllerReferencefails if the owner's type is not registered in the scheme. - Owning external resources. Owner references only work for Kubernetes objects. Cloud resources such as buckets need a finalizer, covered in a later chapter.
Exercise
You delete a WebApp with kubectl delete webapp hello --cascade=orphan. What happens to the Deployment the operator created, and why might the operator recreate or adopt it later?
Show answer
The garbage collector removes the owner reference from the Deployment but does not delete it, so it keeps running. If a new WebApp named hello is created, the reconciler finds the existing Deployment by name and namespace, and CreateOrUpdate calls SetControllerReference, which adds the new owner reference. The Deployment is adopted by the new object.