Skip to content

Commit

Permalink
Merge pull request #552 from mortent/cherry-pick-fixInvNamespace
Browse files Browse the repository at this point in the history
fix: Don't error if inventory namespace already exists
  • Loading branch information
k8s-ci-robot authored Feb 19, 2022
2 parents cb75cc8 + 0dcdbc1 commit 81b92bd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/inventory/inventory-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ func (cic *ClusterClient) ApplyInventoryNamespace(obj *unstructured.Unstructured
}

_, err = cic.dc.Resource(mapping.Resource).Create(context.TODO(), invNamespace, metav1.CreateOptions{})
if apierrors.IsAlreadyExists(err) {
return nil
}
return err
}

Expand Down
41 changes: 41 additions & 0 deletions pkg/inventory/inventory-client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/resource"
clienttesting "k8s.io/client-go/testing"
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
Expand Down Expand Up @@ -496,6 +498,45 @@ func TestDeleteInventoryObj(t *testing.T) {
}
}

func TestApplyInventoryNamespace(t *testing.T) {
testCases := map[string]struct {
namespace *unstructured.Unstructured
dryRunStrategy common.DryRunStrategy
reactorError error
}{
"inventory namespace doesn't exist": {
namespace: inventoryNamespace,
dryRunStrategy: common.DryRunNone,
reactorError: nil,
},
"inventory namespace already exist": {
namespace: inventoryNamespace,
dryRunStrategy: common.DryRunNone,
reactorError: errors.NewAlreadyExists(schema.GroupResource{
Group: "",
Resource: "namespaces",
}, testNamespace),
},
}

for tn, tc := range testCases {
t.Run(tn, func(t *testing.T) {
tf := cmdtesting.NewTestFactory().WithNamespace(testNamespace)
defer tf.Cleanup()

tf.FakeDynamicClient.PrependReactor("create", "namespaces", func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, tc.reactorError
})

invClient, err := NewClient(tf,
WrapInventoryObj, InvInfoToConfigMap)
require.NoError(t, err)
err = invClient.ApplyInventoryNamespace(tc.namespace, tc.dryRunStrategy)
assert.NoError(t, err)
})
}
}

func ignoreErrInfoToObjMeta(info *resource.Info) object.ObjMetadata {
objMeta, _ := object.InfoToObjMeta(info)
return objMeta
Expand Down
10 changes: 10 additions & 0 deletions pkg/inventory/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ var pod3Info = &resource.Info{
Object: pod3,
}

var inventoryNamespace = &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": testNamespace,
},
},
}

func TestFindInventoryObj(t *testing.T) {
tests := map[string]struct {
infos []*unstructured.Unstructured
Expand Down

0 comments on commit 81b92bd

Please sign in to comment.