Skip to content

Commit

Permalink
Skip pruning an object whose Group/Kind is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
haiyanmeng committed Oct 6, 2021
1 parent d50496a commit f0e71ba
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pkg/apply/prune/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,17 @@ func (po *PruneOptions) GetPruneObjs(inv inventory.InventoryInfo,
for _, pruneID := range pruneIds {
pruneObj, err := po.GetObject(pruneID)
if err != nil {
// If prune object is not in cluster, no need to prune it--skip.
if apierrors.IsNotFound(err) {
klog.V(4).Infof("prune obj not in cluster--skip (%s/%s)",
if meta.IsNoMatchError(err) {
klog.V(4).Infof("skip pruning obj %s/%s: the resource type is unrecognized by the cluster (kind: %s, group %s)",
pruneID.Namespace, pruneID.Name, pruneID.GroupKind.Kind, pruneID.GroupKind.Group)
continue
} else if apierrors.IsNotFound(err) {
// If prune object is not in cluster, no need to prune it--skip.
klog.V(4).Infof("skip pruning obj %s/%s: not found in the cluster",
pruneID.Namespace, pruneID.Name)
continue
} else {
return nil, err
}
return nil, err
}
pruneObjs = append(pruneObjs, pruneObj)
}
Expand Down
49 changes: 49 additions & 0 deletions pkg/apply/prune/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/meta/testrestmapper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -110,6 +112,14 @@ var pdbDeleteFailure = &unstructured.Unstructured{
},
}

var crontabCRManifest = `
apiVersion: "stable.example.com/v1"
kind: CronTab
metadata:
name: cron-tab-01
namespace: test-namespace
`

// Returns a inventory object with the inventory set from
// the passed "children".
func createInventoryInfo(children ...*unstructured.Unstructured) inventory.InventoryInfo {
Expand Down Expand Up @@ -549,6 +559,11 @@ func TestGetPruneObjs(t *testing.T) {
prevInventory: []*unstructured.Unstructured{pod, pdb, namespace},
expectedObjs: []*unstructured.Unstructured{pod, namespace},
},
"skip pruning objects whose resource types are unrecognized by the cluster": {
localObjs: []*unstructured.Unstructured{pdb},
prevInventory: []*unstructured.Unstructured{testutil.Unstructured(t, crontabCRManifest), pdb, namespace},
expectedObjs: []*unstructured.Unstructured{namespace},
},
"local objs, inventory disjoint means inventory is pruned": {
localObjs: []*unstructured.Unstructured{pdb},
prevInventory: []*unstructured.Unstructured{pod, namespace},
Expand Down Expand Up @@ -588,6 +603,40 @@ func TestGetPruneObjs(t *testing.T) {
}
}

func TestGetObject_NoMatchError(t *testing.T) {
po := PruneOptions{
Client: fake.NewSimpleDynamicClient(scheme.Scheme, pod, namespace),
Mapper: testrestmapper.TestOnlyStaticRESTMapper(scheme.Scheme,
scheme.Scheme.PrioritizedVersionsAllGroups()...),
}
_, err := po.GetObject(testutil.ToIdentifier(t, crontabCRManifest))
if err == nil {
t.Fatalf("expected GetObject() to return a NoKindMatchError, got nil")
}
if !meta.IsNoMatchError(err) {
t.Fatalf("expected GetObject() to return a NoKindMatchError, got %v", err)
}
}

func TestGetObject_NotFoundError(t *testing.T) {
po := PruneOptions{
Client: fake.NewSimpleDynamicClient(scheme.Scheme, pod, namespace),
Mapper: testrestmapper.TestOnlyStaticRESTMapper(scheme.Scheme,
scheme.Scheme.PrioritizedVersionsAllGroups()...),
}
objMeta, err := object.UnstructuredToObjMeta(pdb)
if err != nil {
t.Fatalf("unexpected error %s returned", err)
}
_, err = po.GetObject(objMeta)
if err == nil {
t.Fatalf("expected GetObject() to return a NotFound error, got nil")
}
if !apierrors.IsNotFound(err) {
t.Fatalf("expected GetObject() to return a NotFound error, got %v", err)
}
}

type optionsCaptureNamespaceClient struct {
dynamic.ResourceInterface
options metav1.DeleteOptions
Expand Down

0 comments on commit f0e71ba

Please sign in to comment.