mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
fix namespace transformer for cluster-scoped resources
This commit is contained in:
@@ -48,14 +48,65 @@ func NewNamespaceTransformer(ns string, cf []config.FieldSpec) Transformer {
|
|||||||
|
|
||||||
// Transform adds the namespace.
|
// Transform adds the namespace.
|
||||||
func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
|
func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
|
||||||
mf := resmap.ResMap{}
|
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
|
||||||
|
}
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
for id := range m {
|
||||||
found := false
|
found := false
|
||||||
for _, path := range o.fieldSpecsToSkip {
|
for _, path := range o.fieldSpecsToSkip {
|
||||||
if id.Gvk().IsSelected(&path.Gvk) {
|
if id.Gvk().IsSelected(&path.Gvk) {
|
||||||
found = true
|
found = true
|
||||||
break
|
mf[id] = m[id]
|
||||||
|
delete(m, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
@@ -63,31 +114,7 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
|
|||||||
delete(m, id)
|
delete(m, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return mf
|
||||||
for id := range mf {
|
|
||||||
objMap := mf[id].Map()
|
|
||||||
for _, path := range o.fieldSpecsToUse {
|
|
||||||
if !id.Gvk().IsSelected(&path.Gvk) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// 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]
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
o.updateClusterRoleBinding(m)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *namespaceTransformer) updateClusterRoleBinding(m resmap.ResMap) {
|
func (o *namespaceTransformer) updateClusterRoleBinding(m resmap.ResMap) {
|
||||||
|
|||||||
Reference in New Issue
Block a user