return error for duplicate keys rather than panicking

This commit is contained in:
Natasha Sarkar
2021-03-03 12:03:36 -08:00
parent 93dd571df9
commit 722b0131f0
15 changed files with 86 additions and 31 deletions

View File

@@ -399,7 +399,8 @@ func find(name string, resMap resmap.ResMap) *resource.Resource {
func getCommand(r *resource.Resource) string {
var m map[string]interface{}
var c []interface{}
m, _ = r.Map()["spec"].(map[string]interface{})
resourceMap, _ := r.Map()
m, _ = resourceMap["spec"].(map[string]interface{})
m, _ = m["template"].(map[string]interface{})
m, _ = m["spec"].(map[string]interface{})
c, _ = m["containers"].([]interface{})

View File

@@ -43,7 +43,11 @@ func (o *multiTransformer) transform(m resmap.ResMap) error {
}
}
for _, r := range m.Resources() {
if r.IsEmpty() {
empty, err := r.IsEmpty()
if err != nil {
return err
}
if empty {
err := m.Remove(r.CurId())
if err != nil {
return err

View File

@@ -306,13 +306,16 @@ items:
assert.False(t, tc.exp.isErr)
assert.Equal(t, len(tc.exp.out), len(rs))
for i := range rs {
rsMap, err := rs[i].Map()
assert.NoError(t, err)
assert.Equal(
t, fmt.Sprintf("%v", tc.exp.out[i]), fmt.Sprintf("%v", rs[i].Map()))
t, fmt.Sprintf("%v", tc.exp.out[i]), fmt.Sprintf("%v", rsMap))
if n != "listWithAnchors" {
// https://github.com/kubernetes-sigs/kustomize/issues/3271
if !reflect.DeepEqual(tc.exp.out[i], rs[i].Map()) {
m, _ := rs[i].Map()
if !reflect.DeepEqual(tc.exp.out[i], m) {
t.Fatalf("%s:\nexpected: %v\n actual: %v",
n, tc.exp.out[i], rs[i].Map())
n, tc.exp.out[i], m)
}
}
}

View File

@@ -220,7 +220,7 @@ func (wn *WNode) GetString(path string) (string, error) {
}
// Map implements ifc.Kunstructured.
func (wn *WNode) Map() map[string]interface{} {
func (wn *WNode) Map() (map[string]interface{}, error) {
return wn.node.Map()
}

View File

@@ -559,7 +559,9 @@ func TestGetSlice(t *testing.T) {
}
func TestMapEmpty(t *testing.T) {
assert.Equal(t, 0, len(NewWNode().Map()))
newNodeMap, err := NewWNode().Map()
assert.NoError(t, err)
assert.Equal(t, 0, len(newNodeMap))
}
func TestMap(t *testing.T) {
@@ -577,7 +579,8 @@ func TestMap(t *testing.T) {
},
}
actual := wn.Map()
actual, err := wn.Map()
assert.NoError(t, err)
if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatalf("actual map does not deep equal expected map:\n%v", diff)
}