@@ -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
154177func (kgc * KustomizeGarbageCollector ) shouldSkip (obj unstructured.Unstructured ) bool {
0 commit comments