Deletion policy#
Every resource has a spec.lifecycle.deletionPolicy field:
Delete(default): When the Kubernetes resource is deleted, genop runs your delete reconcilers to clean up external resources, then removes the finalizer.Retain: When the Kubernetes resource is deleted, genop removes the finalizer immediately without running delete reconcilers. External resources are left intact.
A third option, fromAncestor, delegates the decision to the top parent
being deleted (via ownerReferences) instead of a fixed policy on the
resource itself — see
Operator configuration
for how to set it and the RBAC it requires.
How it’s wired#
Setting deletionPolicy in .genop.yaml generates a call to one of two
genop-runtime providers in the write-once
Configure<Kind>Operator escape hatch:
operators.DefaultDeletionPolicyFuncreadsspec.lifecycle.deletionPolicydirectly off the resource. It runs whendeletionPolicyis left unset (or set todefault).operators.NewDeletionFromAncestorPolicyis generated forfromAncestor— see the next section.
Ancestor policy#
With fromAncestor, a child doesn’t apply its own policy: it applies the
policy of the topmost ancestor that is being deleted. Deleting a Project
with Retain therefore retains every external resource beneath it, even if the
VPC and Subnet children are individually set to Delete.
The provider walks ownerReferences[0] upward from the resource being
deleted:
- If the resource has no owner, its own policy applies.
- If the owner has a
deletionTimestamp, the owner is being deleted too: move up and repeat from the owner. - If the owner has no
deletionTimestamp, the walk stops and the policy of the current resource applies — the last one visited, i.e. the top of the deleting subtree.
An empty policy is treated as Delete.
flowchart TD
A([Resource is being deleted]) --> B{Has an owner?}
B -- no --> R["Apply this resource's policy"]
B -- yes --> C["Get ownerReferences[0]"]
C --> D{Owner has a deletionTimestamp?}
D -- yes --> E["Move up: owner becomes the current resource"]
E --> B
D -- no --> R
R --> F{Policy}
F -- Delete --> G["Run delete reconcilers, then remove finalizer"]
F -- Retain --> H["Remove finalizer, leave external resources"]Example#
Take a Project → VPC → Subnet hierarchy, where each child is owned by
its parent:
flowchart TD
P["Project<br/>deletionPolicy: Retain"] --> V["VPC<br/>deletionPolicy: Delete"]
V --> S["Subnet<br/>deletionPolicy: Delete"]| What is deleted | Ancestors being deleted | Policy the Subnet applies |
|---|---|---|
The Project | VPC, Project | Retain (the Project’s) |
Only the VPC | VPC (its owner, the Project, is not deleting) | Delete (the VPC’s) |
Only the Subnet | none | Delete (its own) |
Every evaluation records a DeletionPolicyEvaluated event with the resolved
policy and the full chain walked, for example:
apply Retain
<project resource>, Namespace=demo, Name=acme, DeletionPolicy=Retain, DeletionTimestamp=...
<vpc resource>, Namespace=demo, Name=main, DeletionPolicy=Delete, DeletionTimestamp=...
<subnet resource>, Namespace=demo, Name=a, DeletionPolicy=Delete, DeletionTimestamp=...Requirements#
- Foreground deletion. Delete the top parent with
--cascade=foreground(propagationPolicy: Foreground). Kubernetes then keeps the parent, with itsdeletionTimestamp, until its children are gone, which is what lets a child observe that its ancestors are being deleted. With background deletion the parent may already be gone when the child reconciles. - RBAC. The controller needs
geton every parent kind, since ancestors are read through the dynamic client. The generatedConfigure<Kind>Operatorcomment lists them. - Only the first owner is followed. If a resource has several
ownerReferences, only the first one is walked.
func ConfigureVPCOperator(
b *operators.OperatorBuilder[*infrav1alpha1.VPCStatus, *infrav1alpha1.VPC],
) *operators.OperatorBuilder[*infrav1alpha1.VPCStatus, *infrav1alpha1.VPC] {
return b.WithDeletionPolicyProvider(
operators.NewDeletionFromAncestorPolicy[*infrav1alpha1.VPCStatus, *infrav1alpha1.VPC](
b.GetClient().Scheme(), b.GetClient().RESTMapper(), b.GetDynamicClient(),
),
)
}Custom logic#
Any decision that isn’t “my own spec field” or “my ancestor’s policy” is a plain function — swap it in the same way, in the same generated file:
func myDeletionPolicy(ctx context.Context, logger logr.Logger, resource *v1alpha1.VPC) (model.DeletionPolicy, string, error) {
if resource.Spec.Protected {
return model.DeletionPolicyRetain, "resource is protected", nil
}
return model.DeletionPolicyDelete, "resource is not protected", nil
}func ConfigureVPCOperator(
b *operators.OperatorBuilder[*infrav1alpha1.VPCStatus, *infrav1alpha1.VPC],
) *operators.OperatorBuilder[*infrav1alpha1.VPCStatus, *infrav1alpha1.VPC] {
return b.WithDeletionPolicyProvider(myDeletionPolicy)
}CleanupHooks still run before the finalizer is removed regardless of
which provider resolves the policy, or what it resolves to.