Ownership and failure recovery
Verify child ownership, let reconciliation repair a missing child, and check what happens on parent deletion.
What you will understand
- Use owner reference UIDs to verify that a child belongs to the current WebApp.
- Distinguish a missing child that can be recreated from a name collision.
- Test garbage collection and understand where external cleanup differs.
Builds on: Leases and leader election, Build an idempotent operator
Verify which WebApp owns a child
The WebApp operator expects a ConfigMap called m16. It finds an object with that name. Before updating it, the operator needs to know whether it created the object for the current WebApp. It could belong to another workload, or to an older WebApp that used the same name. An owner reference includes the parent’s UID, so the operator can check ownership of this exact object lifetime.
If ownership matches, the operator can repair fields it manages. If there is no child, it can create one using the parent’s current UID. If the name belongs to somebody else, report the conflict and let an operator decide how to handle it. Step through these cases in the owner-check scene and see what changes when a parent is replaced under the same name.
Is the existing child safe for this operator to manage?
Compare the current parent UID with the child’s owner reference before applying changes. Same-name parents can have different UIDs.
Read the current WebApp UID
- Current WebApp UID AOperatordesired parent identity
The operator uses the current WebApp UID when deriving child owner references.
Read the child owner reference
- OperatorExisting childinspect before apply
A child with the expected name must already have the matching controller owner before it is managed.
Apply only with a matching owner
- OperatorExisting childinspect before apply
A matching owner permits normal reconciliation; names and labels alone do not.
The teaching controller intentionally refuses adoption; production adoption policies require additional explicit design.
Explore another case
Compare child repair and parent deletion
Recreating a missing child serves a parent that still exists and wants that child. When the parent is being deleted, the intended outcome changes. Kubernetes garbage collection can remove API dependents whose owner reference points to the deleted parent. An object in an external system needs its own cleanup policy, often implemented with a finalizer when cleanup should complete before parent deletion. In the scene, compare deleting the generated ConfigMap with deleting the WebApp.
Open Inspect the child repair to follow one pass of the lab reconciler. Look at which UID changes when it recreates the ConfigMap and which owner UID stays the same.
Deleting a child and deleting its parent lead to different outcomes
The reconciler can recreate a child while the parent still requests it. Deleting the parent lets Kubernetes collect its owned dependents.
Delete the generated ConfigMap
- WebAppConfigMap / DeploymentownerReference UID
With the parent still active, a missing child is drift from desired state.
Recreate the missing child
- WebAppOperatorexists and active
- OperatorConfigMap / Deploymentrepair if missing
The next operator pass creates the same desired child name with a new child UID and the same parent UID.
Delete the parent and verify cleanup
- Garbage collectorConfigMap / Deploymentdelete after owner removal
When the parent is removed, garbage collection removes owned children; the operator does not recreate a deleted parent.
The actual lab creates only Kubernetes children and needs no external-resource finalizer.
Inspect the child repair
Delete the generated ConfigMap m16 while its WebApp stays present. Run one pass of the lab’s polling reconciler and inspect the repair.
From kubectlTo API server
Delete only the generated ConfigMap
The ConfigMap disappears. The Deployment and WebApp still exist, and the WebApp still declares the page the reconciler should generate.
- Before
- Present
- After
- Absent
The ConfigMap disappears. The Deployment and WebApp still exist, and the WebApp still declares the page the reconciler should generate.
This replay follows the teaching script with an unchanged WebApp spec. The repair is a reconcile pass against current API state. Kubernetes garbage collection is a separate path when an owner is deleted.
Reveal prediction answer
The script checks the controlling owner’s UID. A mismatched UID stops the apply for that child.
After the reconcile pass, compare the generated ConfigMap owner UID with the WebApp UID and verify the Deployment’s ready replica count.
Delete and repair the generated child in the operator labExplore another case
Recreate a child and inspect its owner
Start with the example WebApp and generated children from the disposable lab. Record the WebApp UID and the ConfigMap’s owner reference. Delete only the lab’s generated ConfigMap and run one reconcile pass. The new ConfigMap gets a fresh UID and should point to the same parent UID. Read its data and owner reference to verify recovery. To complete the deletion exercise, follow the operator lab cleanup sequence and confirm the expected children are collected.
apiVersion: labs.kv.example/v1alpha1
kind: WebApp
metadata:
name: m16
namespace: kv-lab
labels:
lab: m16
spec:
replicas: 1
message: repaired-by-reconciliationkubectl --context kind-kv-lab -n kv-lab get webapp m16 -o jsonpath='{.metadata.uid}{"\n"}'
kubectl --context kind-kv-lab -n kv-lab get configmap m16 -o jsonpath='{.metadata.ownerReferences}{"\n"}'
# Delete only this generated, disposable ConfigMap.
kubectl --context kind-kv-lab -n kv-lab delete configmap m16
python3 webapp-reconciler.py --once
kubectl --context kind-kv-lab -n kv-lab get configmap m16 -o yamlChoose which resources should survive deletion
Some objects should survive parent deletion. A database backup or persistent data volume may need a retention policy independent of the controller’s app objects. Document which resources the API owns and whether deletion retains or removes them. Allow time for asynchronous garbage collection before investigating a child that still exists, and verify the exact owning UID before considering manual removal.
Check your understanding
The ConfigMap name matches, but its owner reference contains the UID of an older WebApp. How should a cautious reconciler react?
Report a conflict and preserve the child until its lifecycle is understood. An identical name cannot establish ownership across parent lifetimes. After the old object is safely resolved, the reconciler can create or manage a child owned by the current WebApp.
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise