fix namespace transformer for cluster-scoped resources

This commit is contained in:
Mengqi Yu
2019-04-18 10:31:17 -07:00
parent 6f4b104c9e
commit bcb697eb0b

View File

@@ -48,25 +48,28 @@ func NewNamespaceTransformer(ns string, cf []config.FieldSpec) Transformer {
// Transform adds the namespace.
func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
mf := resmap.ResMap{}
for id := range m {
found := false
for _, path := range o.fieldSpecsToSkip {
if id.Gvk().IsSelected(&path.Gvk) {
found = true
break
}
}
if !found {
mf[id] = m[id]
delete(m, id)
}
}
mf := o.filterResmap(m)
for id := range mf {
objMap := mf[id].Map()
for _, path := range o.fieldSpecsToUse {
switch path.Path {
// Special casing .metadata.namespace since it is a common metadata field across all runtime.Object
// We should add namespace if it's namespaced resource; otherwise, we should not.
case "metadata/namespace":
if id.Gvk().IsSelected(&path.Gvk) && !id.Gvk().IsClusterKind() {
if len(objMap) > 0 {
err := mutateField(
objMap, path.PathSlice(), path.CreateIfNotPresent,
func(_ interface{}) (interface{}, error) {
return o.namespace, nil
})
if err != nil {
return err
}
}
}
default:
if !id.Gvk().IsSelected(&path.Gvk) {
continue
}
@@ -81,15 +84,39 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
return err
}
}
newid := id.CopyWithNewNamespace(o.namespace)
m[newid] = mf[id]
}
if !id.Gvk().IsClusterKind() {
newid := id.CopyWithNewNamespace(o.namespace)
m[newid] = mf[id]
} else {
m[id] = mf[id]
}
}
}
o.updateClusterRoleBinding(m)
return nil
}
func (o *namespaceTransformer) filterResmap(m resmap.ResMap) resmap.ResMap {
mf := resmap.ResMap{}
for id := range m {
found := false
for _, path := range o.fieldSpecsToSkip {
if id.Gvk().IsSelected(&path.Gvk) {
found = true
mf[id] = m[id]
delete(m, id)
}
}
if !found {
mf[id] = m[id]
delete(m, id)
}
}
return mf
}
func (o *namespaceTransformer) updateClusterRoleBinding(m resmap.ResMap) {
saMap := map[string]bool{}
for id := range m {