Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't error if inventory namespace already exists #551

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just to prevent a timing issue with an sync create call?
We don't create it if it already exists, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently we don't check if it exists, we just try to create it and handle the error if it already exists. We could check first, but either way should work.

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