Sub topic 13
Owner references and garbage collection
This chapter assumes you know the Deployment, ReplicaSet and Pod relationship, can read kubectl get -o yaml output, and understand the Reconcile function from earlier chapters.
Why dependents need an owner
A controller that creates objects must also clean them up. If you delete a Deployment, nobody should have to hunt down its ReplicaSets and Pods. Kubernetes records the relationship on each created object in metadata.ownerReferences. A separate controller, the garbage collector, reads those references and deletes objects whose owners no longer exist.
The ownerReferences field
Here is the metadata of a Pod created by a ReplicaSet:
metadata:
name: web-7d9c5b6f4-x2k8q
namespace: default
ownerReferences:
- apiVersion: apps/v1
kind: ReplicaSet
name: web-7d9c5b6f4
uid: 6f1c2a9e-1b7d-4c1e-9a53-0d2f4e8b7c11
controller: true
blockOwnerDeletion: trueuididentifies the exact owner instance. If you delete a ReplicaSet and recreate one with the same name, the new object has a different UID, so the old Pods still count as orphaned.controller: truemarks the managing owner. An object can have at most one controller owner.blockOwnerDeletion: truemakes foreground deletion wait for this dependent.
In controller-runtime you set this with controllerutil.SetControllerReference(owner, child, scheme) before creating the child.
How the garbage collector works
The garbage collector watches all resource types and builds a graph of owners and dependents. When an owner is deleted, it deletes the dependents. It is another level-triggered controller: it compares the references with the objects that exist and acts on the difference.
Deletion propagation
You choose what happens to dependents when you delete the owner:
| Policy | Behavior |
|---|---|
| Background | The owner is removed at once. The garbage collector deletes dependents afterward. This is the default for `kubectl delete`. |
| Foreground | The owner stays visible with a `foregroundDeletion` finalizer until all dependents with `blockOwnerDeletion: true` are gone. |
| Orphan | Dependents are kept and their owner reference is removed. |
kubectl delete deployment web --cascade=background
kubectl delete deployment web --cascade=foreground
kubectl delete deployment web --cascade=orphanAdoption and scope rules
A ReplicaSet adopts existing Pods that match its selector and have no controller owner. It adds its own reference to them. This is why changing the labels on a Pod can detach it from its ReplicaSet, and the ReplicaSet then creates a replacement.
Scope rules limit what an owner can be:
- A namespaced owner can own objects in its own namespace only.
- A cluster-scoped owner can own objects in any namespace.
- A cluster-scoped dependent cannot have a namespaced owner.
Exercise
You run kubectl delete deployment web --cascade=orphan. What happens to the ReplicaSet and Pods, and what happens if you later recreate a Deployment named web?
Show answer
The Deployment is deleted, and the garbage collector removes the ownerReferences from the ReplicaSet and Pods, which keep running. A new Deployment named web has a different UID, so it does not own them automatically. However, its controller can adopt a ReplicaSet whose labels match its selector and which has no controller owner.