Skip to content

Commit

Permalink
Skip garbage collection of objects with owner references
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Aug 18, 2021
1 parent 34582cb commit 94f68a8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
23 changes: 23 additions & 0 deletions controllers/kustomization_gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,17 @@ func (kgc *KustomizeGarbageCollector) Prune(timeout time.Duration, name string,
if err == nil {
for _, item := range ulist.Items {
id := fmt.Sprintf("%s/%s/%s", item.GetKind(), item.GetNamespace(), item.GetName())

if kgc.shouldSkip(item) {
kgc.log.V(1).Info(fmt.Sprintf("gc is disabled for '%s'", id))
continue
}

if kgc.hasBlockOwnerDeletion(item) {
kgc.log.V(1).Info(fmt.Sprintf("gc is disabled for '%s' due to 'ownerReference.blockOwnerDeletion=true'", id))
continue
}

if kgc.isStale(item) && item.GetDeletionTimestamp().IsZero() {
err = kgc.Delete(ctx, &item)
if err != nil {
Expand Down Expand Up @@ -113,6 +119,11 @@ func (kgc *KustomizeGarbageCollector) Prune(timeout time.Duration, name string,
continue
}

if kgc.hasBlockOwnerDeletion(item) {
kgc.log.V(1).Info(fmt.Sprintf("gc is disabled for '%s' due to 'ownerReference.blockOwnerDeletion=true'", id))
continue
}

if kgc.isStale(item) && item.GetDeletionTimestamp().IsZero() {
err = kgc.Delete(ctx, &item)
if err != nil {
Expand Down Expand Up @@ -142,15 +153,27 @@ func (kgc *KustomizeGarbageCollector) isStale(obj unstructured.Unstructured) boo
itemAnnotationChecksum := obj.GetAnnotations()[fmt.Sprintf("%s/checksum", kustomizev1.GroupVersion.Group)]

switch kgc.newChecksum {
// when the Kustomization is deleted the new checksum is set to string empty making all objects stale
case "":
return true
// if the new checksum matches the object checksum skip GC
case itemAnnotationChecksum:
return false
// if the new checksum doesn't match the object checksum then it should be deleted
default:
return true
}
}

func (kgc *KustomizeGarbageCollector) hasBlockOwnerDeletion(obj unstructured.Unstructured) bool {
for _, ownerReference := range obj.GetOwnerReferences() {
if bod := ownerReference.BlockOwnerDeletion; bod != nil && *bod == true {
return true
}
}
return false
}

func (kgc *KustomizeGarbageCollector) shouldSkip(obj unstructured.Unstructured) bool {
key := fmt.Sprintf("%s/prune", kustomizev1.GroupVersion.Group)

Expand Down
3 changes: 3 additions & 0 deletions docs/spec/v1beta1/kustomization.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ labeling or annotating them with:
kustomize.toolkit.fluxcd.io/prune: disabled
```

Note that Kubernetes objects generated by other controllers that have `ownerReference.blockOwnerDeletion=true`
are skipped from garbage collection.

## Health assessment

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

0 comments on commit 94f68a8

Please sign in to comment.