Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #315 from bitnami/duplicates
Browse files Browse the repository at this point in the history
Implement duplicate resource detection
  • Loading branch information
mkmik authored Sep 21, 2021
2 parents 4968d7d + 25ebe01 commit ee64d79
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ func readObjs(cmd *cobra.Command, paths []string) ([]*unstructured.Unstructured,
}
res = append(res, utils.FlattenToV1(objs)...)
}
if err := utils.CheckDuplicates(res); err != nil {
return nil, err
}
return res, nil
}

Expand Down
18 changes: 18 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"path/filepath"
"testing"
)

func TestReadObjsDuplicates(t *testing.T) {
cmd := RootCmd
if err := cmd.ParseFlags(nil); err != nil {
t.Fatal(err)
}

_, err := readObjs(cmd, []string{filepath.FromSlash("../testdata/duplicates.jsonnet")})
if got, want := err.Error(), `duplicate resource /v1, Kind=ConfigMap, "myns", "foo"`; got != want {
t.Fatalf("got: %s, want: %s", got, want)
}
}
22 changes: 22 additions & 0 deletions testdata/duplicates.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
t1: {
o1: {
apiVersion: 'v1',
kind: 'ConfigMap',
metadata: {
name: 'foo',
namespace: 'myns',
},
},
},
t2: {
o2: {
apiVersion: 'v1',
kind: 'ConfigMap',
metadata: {
name: 'foo',
namespace: 'myns',
},
},
},
}
36 changes: 36 additions & 0 deletions utils/duplicates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2021 The kubecfg authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package utils

import (
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// CheckDuplicates returns error if the provided object slice contains multiple
// objects sharing the same version/kind/namespace/name combination.
func CheckDuplicates(objs []*unstructured.Unstructured) error {
seen := map[string]struct{}{}
for _, o := range objs {
k := fmt.Sprintf("%s, %q, %q", o.GroupVersionKind(), o.GetNamespace(), o.GetName())
if _, found := seen[k]; found {
return fmt.Errorf("duplicate resource %s", k)
}
seen[k] = struct{}{}
}
return nil
}

0 comments on commit ee64d79

Please sign in to comment.