From 70dcc79bf4a73ba18d72b3189a334038606e0bf5 Mon Sep 17 00:00:00 2001 From: Donny Xia Date: Thu, 22 Oct 2020 13:18:20 -0700 Subject: [PATCH] cleanup namespace transformer --- api/builtins/NamespaceTransformer.go | 70 ------------------- .../NamespaceTransformer.go | 70 ------------------- 2 files changed, 140 deletions(-) diff --git a/api/builtins/NamespaceTransformer.go b/api/builtins/NamespaceTransformer.go index c459cd44c..8d7b226ee 100644 --- a/api/builtins/NamespaceTransformer.go +++ b/api/builtins/NamespaceTransformer.go @@ -7,9 +7,7 @@ import ( "fmt" "sigs.k8s.io/kustomize/api/filters/namespace" - "sigs.k8s.io/kustomize/api/resid" "sigs.k8s.io/kustomize/api/resmap" - "sigs.k8s.io/kustomize/api/resource" "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/kyaml/filtersutil" "sigs.k8s.io/yaml" @@ -53,74 +51,6 @@ func (p *NamespaceTransformerPlugin) Transform(m resmap.ResMap) error { return nil } -// Special casing metadata.namespace since -// all objects have it, even "ClusterKind" objects -// that don't exist in a namespace (the Namespace -// object itself doesn't live in a namespace). -func (p *NamespaceTransformerPlugin) applicableFieldSpecs(id resid.ResId) []types.FieldSpec { - var res []types.FieldSpec - for _, fs := range p.FieldSpecs { - if id.IsSelected(&fs.Gvk) && - (fs.Path != types.MetadataNamespacePath || - (fs.Path == types.MetadataNamespacePath && id.IsNamespaceableKind())) { - res = append(res, fs) - } - } - return res -} - -func (p *NamespaceTransformerPlugin) changeNamespace( - _ *resource.Resource) func(in interface{}) (interface{}, error) { - return func(in interface{}) (interface{}, error) { - switch in.(type) { - case string: - // will happen when the metadata/namespace - // value is replaced - return p.Namespace, nil - case []interface{}: - l, _ := in.([]interface{}) - for idx, item := range l { - switch item.(type) { - case map[string]interface{}: - // Will happen when mutating the subjects - // field of ClusterRoleBinding and RoleBinding - inMap, _ := item.(map[string]interface{}) - if _, ok := inMap["name"]; !ok { - continue - } - name, ok := inMap["name"].(string) - if !ok { - continue - } - // The only case we need to force the namespace - // if for the "service account". "default" is - // kind of hardcoded here for right now. - if name != "default" { - continue - } - inMap["namespace"] = p.Namespace - l[idx] = inMap - default: - // nothing to do for right now - } - } - return in, nil - case map[string]interface{}: - // Will happen if the createField=true - // when the namespace is added to the - // object - inMap := in.(map[string]interface{}) - if len(inMap) == 0 { - return p.Namespace, nil - } else { - return in, nil - } - default: - return in, nil - } - } -} - func NewNamespaceTransformerPlugin() resmap.TransformerPlugin { return &NamespaceTransformerPlugin{} } diff --git a/plugin/builtin/namespacetransformer/NamespaceTransformer.go b/plugin/builtin/namespacetransformer/NamespaceTransformer.go index 7341669a2..549a17a6d 100644 --- a/plugin/builtin/namespacetransformer/NamespaceTransformer.go +++ b/plugin/builtin/namespacetransformer/NamespaceTransformer.go @@ -8,9 +8,7 @@ import ( "fmt" "sigs.k8s.io/kustomize/api/filters/namespace" - "sigs.k8s.io/kustomize/api/resid" "sigs.k8s.io/kustomize/api/resmap" - "sigs.k8s.io/kustomize/api/resource" "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/kyaml/filtersutil" "sigs.k8s.io/yaml" @@ -56,71 +54,3 @@ func (p *plugin) Transform(m resmap.ResMap) error { } return nil } - -// Special casing metadata.namespace since -// all objects have it, even "ClusterKind" objects -// that don't exist in a namespace (the Namespace -// object itself doesn't live in a namespace). -func (p *plugin) applicableFieldSpecs(id resid.ResId) []types.FieldSpec { - var res []types.FieldSpec - for _, fs := range p.FieldSpecs { - if id.IsSelected(&fs.Gvk) && - (fs.Path != types.MetadataNamespacePath || - (fs.Path == types.MetadataNamespacePath && id.IsNamespaceableKind())) { - res = append(res, fs) - } - } - return res -} - -func (p *plugin) changeNamespace( - _ *resource.Resource) func(in interface{}) (interface{}, error) { - return func(in interface{}) (interface{}, error) { - switch in.(type) { - case string: - // will happen when the metadata/namespace - // value is replaced - return p.Namespace, nil - case []interface{}: - l, _ := in.([]interface{}) - for idx, item := range l { - switch item.(type) { - case map[string]interface{}: - // Will happen when mutating the subjects - // field of ClusterRoleBinding and RoleBinding - inMap, _ := item.(map[string]interface{}) - if _, ok := inMap["name"]; !ok { - continue - } - name, ok := inMap["name"].(string) - if !ok { - continue - } - // The only case we need to force the namespace - // if for the "service account". "default" is - // kind of hardcoded here for right now. - if name != "default" { - continue - } - inMap["namespace"] = p.Namespace - l[idx] = inMap - default: - // nothing to do for right now - } - } - return in, nil - case map[string]interface{}: - // Will happen if the createField=true - // when the namespace is added to the - // object - inMap := in.(map[string]interface{}) - if len(inMap) == 0 { - return p.Namespace, nil - } else { - return in, nil - } - default: - return in, nil - } - } -}