Skip to content

Commit 1b7d9bc

Browse files
committed
fix: objects flagged by CurrentUIDFilter should be removed from the inventory
During pruning the code is filtering objects with `CurrentUIDFilter`: > // CurrentUIDFilter implements ValidationFilter interface to determine // if an object should not be pruned (deleted) because it has recently // been applied. This must mean that the object has ended up being tracked in the inventory by more than one reference and the one that was flagged by the filter should be removed from the inventory. Otherwise, it gets stuck in the inventory indefinitely and during every invocation of prune it'll be logged as skipped. A good example of this is `Ingress` kind existing in both groups `extensions` and `networking.k8s.io` in k8s 1.19 & 1.20. This means when you rename the apiGroup in your local yaml to the new recommended group, then the reference with the old group name gets permastuck in the inventory.
1 parent 982fab3 commit 1b7d9bc

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

pkg/apply/prune/prune.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ func (p *Pruner) Prune(
153153
}
154154
}
155155

156+
// Remove the object from inventory if it was determined that the object should not be pruned,
157+
// because it had recently been applied. This probably means that the object is in the inventory
158+
// more than one time with a different group (e.g. kind Ingress and apiGroups networking.k8s.io & extensions)
159+
var deleteAfterApplyErr *filter.ApplyPreventedDeletionError
160+
if errors.As(filterErr, &deleteAfterApplyErr) {
161+
if !opts.DryRunStrategy.ClientOrServerDryRun() {
162+
// Register for removal from the inventory.
163+
taskContext.AddAbandonedObject(id)
164+
}
165+
}
166+
156167
taskContext.SendEvent(eventFactory.CreateSkippedEvent(obj, filterErr))
157168
taskContext.InventoryManager().AddSkippedDelete(id)
158169
break

pkg/apply/prune/prune_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func TestPrune(t *testing.T) {
325325
},
326326
},
327327
},
328-
"UID match means prune skipped": {
328+
"UID match means prune skipped and object abandoned": {
329329
clusterObjs: []*unstructured.Unstructured{pod},
330330
pruneObjs: []*unstructured.Unstructured{pod},
331331
pruneFilters: []filter.ValidationFilter{
@@ -351,8 +351,11 @@ func TestPrune(t *testing.T) {
351351
expectedSkipped: object.ObjMetadataSet{
352352
object.UnstructuredToObjMetadata(pod),
353353
},
354+
expectedAbandoned: object.ObjMetadataSet{
355+
object.UnstructuredToObjMetadata(pod),
356+
},
354357
},
355-
"UID match for only one object one pruned, one skipped": {
358+
"UID match for only one object one pruned, one skipped and abandoned": {
356359
clusterObjs: []*unstructured.Unstructured{pod, pdb},
357360
pruneObjs: []*unstructured.Unstructured{pod, pdb},
358361
pruneFilters: []filter.ValidationFilter{
@@ -386,6 +389,9 @@ func TestPrune(t *testing.T) {
386389
expectedSkipped: object.ObjMetadataSet{
387390
object.UnstructuredToObjMetadata(pod),
388391
},
392+
expectedAbandoned: object.ObjMetadataSet{
393+
object.UnstructuredToObjMetadata(pod),
394+
},
389395
},
390396
"Prevent delete annotation equals prune skipped": {
391397
clusterObjs: []*unstructured.Unstructured{

0 commit comments

Comments
 (0)