skip adding namespace when the object is empty

This commit is contained in:
Jingfang Liu
2019-03-19 14:53:29 -07:00
parent 2c0c0c9497
commit bfc3655bad
2 changed files with 16 additions and 6 deletions

View File

@@ -70,12 +70,16 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
if !id.Gvk().IsSelected(&path.Gvk) {
continue
}
err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, func(_ interface{}) (interface{}, error) {
return o.namespace, nil
})
if err != nil {
return err
// make sure the object is non empty
if len(objMap) > 0 {
err := mutateField(
objMap, path.PathSlice(), path.CreateIfNotPresent,
func(_ interface{}) (interface{}, error) {
return o.namespace, nil
})
if err != nil {
return err
}
}
newid := id.CopyWithNewNamespace(o.namespace)
m[newid] = mf[id]

View File

@@ -47,6 +47,9 @@ func TestNamespaceRun(t *testing.T) {
"namespace": "foo",
},
}),
resid.NewResId(cmap, "cm3"): rf.FromMap(
map[string]interface{}{},
),
resid.NewResId(ns, "ns1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
@@ -134,6 +137,9 @@ func TestNamespaceRun(t *testing.T) {
"namespace": "test",
},
}),
resid.NewResIdWithPrefixNamespace(cmap, "cm3", "", "test"): rf.FromMap(
map[string]interface{}{},
),
resid.NewResIdWithPrefixNamespace(sa, "default", "", "test"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",