Skip to content

Commit a49bbf9

Browse files
committed
Skip garbage collection of objects with owner references
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
1 parent 34582cb commit a49bbf9

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

controllers/kustomization_gc.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@ func (kgc *KustomizeGarbageCollector) Prune(timeout time.Duration, name string,
7171
if err == nil {
7272
for _, item := range ulist.Items {
7373
id := fmt.Sprintf("%s/%s/%s", item.GetKind(), item.GetNamespace(), item.GetName())
74+
7475
if kgc.shouldSkip(item) {
7576
kgc.log.V(1).Info(fmt.Sprintf("gc is disabled for '%s'", id))
7677
continue
7778
}
7879

80+
if kgc.hasBlockOwnerDeletion(item) {
81+
kgc.log.V(1).Info(fmt.Sprintf("gc is disabled for '%s' due to 'ownerReference.blockOwnerDeletion=true'", id))
82+
continue
83+
}
84+
7985
if kgc.isStale(item) && item.GetDeletionTimestamp().IsZero() {
8086
err = kgc.Delete(ctx, &item)
8187
if err != nil {
@@ -113,6 +119,11 @@ func (kgc *KustomizeGarbageCollector) Prune(timeout time.Duration, name string,
113119
continue
114120
}
115121

122+
if kgc.hasBlockOwnerDeletion(item) {
123+
kgc.log.V(1).Info(fmt.Sprintf("gc is disabled for '%s' due to 'ownerReference.blockOwnerDeletion=true'", id))
124+
continue
125+
}
126+
116127
if kgc.isStale(item) && item.GetDeletionTimestamp().IsZero() {
117128
err = kgc.Delete(ctx, &item)
118129
if err != nil {
@@ -142,13 +153,25 @@ func (kgc *KustomizeGarbageCollector) isStale(obj unstructured.Unstructured) boo
142153
itemAnnotationChecksum := obj.GetAnnotations()[fmt.Sprintf("%s/checksum", kustomizev1.GroupVersion.Group)]
143154

144155
switch kgc.newChecksum {
156+
// when the Kustomization is deleted the new checksum is set to string empty making all objects stale
145157
case "":
146158
return true
159+
// skip GC if the new checksum matches the object checksum
147160
case itemAnnotationChecksum:
148161
return false
149-
default:
150-
return true
151162
}
163+
164+
// skip GC if the checksum annotation is missing from the object
165+
return itemAnnotationChecksum != ""
166+
}
167+
168+
func (kgc *KustomizeGarbageCollector) hasBlockOwnerDeletion(obj unstructured.Unstructured) bool {
169+
for _, ownerReference := range obj.GetOwnerReferences() {
170+
if bod := ownerReference.BlockOwnerDeletion; bod != nil && *bod == true {
171+
return true
172+
}
173+
}
174+
return false
152175
}
153176

154177
func (kgc *KustomizeGarbageCollector) shouldSkip(obj unstructured.Unstructured) bool {

docs/spec/v1beta1/kustomization.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@ labeling or annotating them with:
390390
kustomize.toolkit.fluxcd.io/prune: disabled
391391
```
392392

393+
Note that Kubernetes objects generated by other controllers that have `ownerReference.blockOwnerDeletion=true`
394+
are skipped from garbage collection.
395+
393396
## Health assessment
394397

395398
A Kustomization can contain a series of health checks used to determine the

0 commit comments

Comments
 (0)