From 4b71653099c4b54ea0a09a8ae690a8ee52b25e25 Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Mon, 22 Mar 2021 09:44:17 -0700 Subject: [PATCH] Don't emit Prune event for NoMatchError --- pkg/apply/prune/prune.go | 2 +- pkg/apply/prune/prune_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/apply/prune/prune.go b/pkg/apply/prune/prune.go index 1d7832b0..0e9bb3df 100644 --- a/pkg/apply/prune/prune.go +++ b/pkg/apply/prune/prune.go @@ -130,7 +130,7 @@ func (po *PruneOptions) Prune(localInv inventory.InventoryInfo, obj, err := po.getObject(pruneObj) if err != nil { // Object not found in cluster, so no need to delete it; skip to next object. - if apierrors.IsNotFound(err) { + if apierrors.IsNotFound(err) || meta.IsNoMatchError(err) { klog.V(5).Infof("%s/%s not found in cluster--skipping", pruneObj.Namespace, pruneObj.Name) continue diff --git a/pkg/apply/prune/prune_test.go b/pkg/apply/prune/prune_test.go index a0389505..fe85b367 100644 --- a/pkg/apply/prune/prune_test.go +++ b/pkg/apply/prune/prune_test.go @@ -138,6 +138,20 @@ var role = &unstructured.Unstructured{ }, } +var unknownCR = &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "cli-utils.test/v1", + "kind": "Unknown", + "metadata": map[string]interface{}{ + "name": "test", + "namespace": "default", + "annotations": map[string]interface{}{ + "config.k8s.io/owning-inventory": testInventoryLabel, + }, + }, + }, +} + // Returns a inventory object with the inventory set from // the passed "children". func createInventoryInfo(children ...*unstructured.Unstructured) inventory.InventoryInfo { @@ -231,6 +245,13 @@ func TestPrune(t *testing.T) { finalClusterObjs: []*unstructured.Unstructured{namespace, pod}, pruneEventObjs: []*unstructured.Unstructured{pdb, namespace}, }, + "unknown type doesn't emit prune failed event": { + pastObjs: []*unstructured.Unstructured{unknownCR}, + currentObjs: []*unstructured.Unstructured{}, + prunedObjs: []*unstructured.Unstructured{unknownCR}, + finalClusterObjs: []*unstructured.Unstructured{}, + pruneEventObjs: []*unstructured.Unstructured{}, + }, } for name, tc := range tests { for i := range common.Strategies {