Owner References
Example1: Pet -> Person
-
Prepare CRD
PersonandPetPerson
apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: name: persons.example.com spec: group: example.com names: kind: Person listKind: PersonList plural: persons singular: person scope: Namespaced versions: - name: v1 served: true storage: true schema: openAPIV3Schema: type: object properties: spec: type: object properties: name: type: string age: type: integer address: type: string email: type: string phone: type: string hobbies: type: array items: type: string required: - namePet
apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: name: pets.example.com spec: group: example.com versions: - name: v1 served: true storage: true schema: openAPIV3Schema: type: object properties: spec: type: object properties: name: type: string age: type: integer breed: type: string owner: type: string status: type: object properties: message: type: string scope: Namespaced names: plural: pets singular: pet kind: Pet listKind: PetList -
Install the CRD
kubectl apply -f crd/person.yaml,crd/pet.yaml -
Create
PersonnamedAlicekubectl apply -f example/person-alice.yaml - Get Alice's uid and update
ownerReferencesinexample/pet-dog.yamluid=$(kubectl get -f example/person-alice.yaml -o yaml | yq .metadata.uid); echo $uid yq e -i ".metadata.ownerReferences[0].uid = \"$uid\"" example/pet-dog.yaml - Create a
Petdogkubectl apply -f example/pet-dog.yaml - Get Person and Dog
kubectl get person,pet NAME AGE person.example.com/alice 5m25s NAME AGE pet.example.com/dog 8s - Delete alice
kubectl delete person alice person.example.com "alice" deleted - The owned resource
dogis also deleted. (cascading deletion)kubectl get person,pet No resources found in default namespace.
Example2: OwnerReferences & Finalizer
- Create
PersonnamedAlice(same as above)kubectl apply -f example/person-alice.yaml - Get Alice's uid and update
ownerReferencesinexample/pet-dog.yamluid=$(kubectl get -f example/person-alice.yaml -o yaml | yq .metadata.uid); echo $uid yq e -i ".metadata.ownerReferences[0].uid = \"$uid\"" example/pet-dog-with-finalizer.yaml - Create a
Petdogkubectl apply -f example/pet-dog-with-finalizer.yaml - Delete alice
kubectl delete person alice person.example.com "alice" deleted -
You can check alice is deleted, while
Petstill exists as it has finalizer.kubectl get person alice Error from server (NotFound): persons.example.com "alice" not found1. To clean up the service,kubectl get pet NAME AGE dog 43sfinalizersmust be removed.kubectl patch pet dog -p '{"metadata":{"finalizers": []}}' --type=merge
Garbage Collection does not wait to delete the owner object until the dependent object is actually deleted.
Example3: OwnerReferences + BlockOwnerDeletion=true & Finalizer
- Create
PersonnamedAlice(same as above)kubectl apply -f example/person-alice.yaml - Get Alice's uid and update
ownerReferencesinexample/pet-dog.yamluid=$(kubectl get -f example/person-alice.yaml -o yaml | yq .metadata.uid); echo $uid yq e -i ".metadata.ownerReferences[0].uid = \"$uid\"" example/pet-dog-with-finalizer-and-blockownerdeletion.yaml - Create a
Petdogkubectl apply -f example/pet-dog-with-finalizer-and-blockownerdeletion.yaml - Delete alice
kubectl delete person alice person.example.com "alice" deleted -
You can check alice is deleted, while
Petstill exists as it has finalizer.kubectl get person alice Error from server (NotFound): persons.example.com "alice" not found1. To clean up the service,kubectl get pet NAME AGE dog 43sfinalizersmust be removed.kubectl patch pet dog -p '{"metadata":{"finalizers": []}}' --type=merge
Garbage Collection does not wait to delete the owner object until the dependent object is actually deleted.
Example4: OwnerReferences + BlockOwnerDeletion=true & Finalizer + delete --cascade=foreground
- Create
PersonnamedAlice(same as above)kubectl apply -f example/person-alice.yaml -
Get Alice's uid and update
ownerReferencesinexample/pet-dog.yamluid=$(kubectl get -f example/person-alice.yaml -o yaml | yq .metadata.uid); echo $uid1. Create ayq e -i ".metadata.ownerReferences[0].uid = \"$uid\"" example/pet-dog-with-finalizer-and-blockownerdeletion.yamlPetdog1. Delete alice withkubectl apply -f example/pet-dog-with-finalizer-and-blockownerdeletion.yaml--cascade=foregroundThe command gets stuck as it waits until all the dependent objects are removed. 1. Removekubectl delete person alice --cascade=foregroundfinalizersmanually in another terminal.kubectl patch pet dog -p '{"metadata":{"finalizers": []}}' --type=mergeOnce the finalizer is removed, the command above completes the deletion of the owner object. The pet is also deleted when the finalizer is removed.
the owner object is deleted after the dependent object deletion completed.
Example5: OwnerReferences + BlockOwnerDeletion=true & Finalizer + CRD with propagationPolicy: Background
-
Install the CRD
kubectl apply -f crd/person-deletionpolicy-foreground.yaml,crd/pet.yaml