From d481dbad627a5bea9d160b8a2f67bf0f7187ae44 Mon Sep 17 00:00:00 2001 From: zoncoen Date: Thu, 15 Nov 2018 18:32:07 +0900 Subject: [PATCH] combine transformers --- pkg/resid/resid.go | 19 ++-- pkg/target/kusttarget.go | 7 +- .../config/defaultconfig/defaultconfig.go | 2 - .../config/defaultconfig/namesuffix.go | 24 ---- pkg/transformers/prefixname_test.go | 98 ---------------- .../{prefixname.go => prefixsuffixname.go} | 39 +++---- ...xname_test.go => prefixsuffixname_test.go} | 16 +-- pkg/transformers/suffixname.go | 107 ------------------ 8 files changed, 44 insertions(+), 268 deletions(-) delete mode 100644 pkg/transformers/config/defaultconfig/namesuffix.go delete mode 100644 pkg/transformers/prefixname_test.go rename pkg/transformers/{prefixname.go => prefixsuffixname.go} (58%) rename pkg/transformers/{suffixname_test.go => prefixsuffixname_test.go} (83%) delete mode 100644 pkg/transformers/suffixname.go diff --git a/pkg/resid/resid.go b/pkg/resid/resid.go index 94584dea4..84d738d1b 100644 --- a/pkg/resid/resid.go +++ b/pkg/resid/resid.go @@ -149,14 +149,17 @@ func (n ResId) Namespace() string { return n.namespace } -// CopyWithNewPrefix make a new copy from current ResId and append a new prefix -func (n ResId) CopyWithNewPrefix(p string) ResId { - return ResId{gvKind: n.gvKind, name: n.name, prefix: n.concatPrefix(p), suffix: n.suffix, namespace: n.namespace} -} - -// CopyWithNewSuffix make a new copy from current ResId and append a new suffix -func (n ResId) CopyWithNewSuffix(p string) ResId { - return ResId{gvKind: n.gvKind, name: n.name, prefix: n.prefix, suffix: n.concatSuffix(p), namespace: n.namespace} +// CopyWithNewPrefixSuffix make a new copy from current ResId and append a new prefix and suffix +func (n ResId) CopyWithNewPrefixSuffix(p, s string) ResId { + prefix := n.prefix + if p != "" { + prefix = n.concatPrefix(p) + } + suffix := n.suffix + if s != "" { + suffix = n.concatSuffix(s) + } + return ResId{gvKind: n.gvKind, name: n.name, prefix: prefix, suffix: suffix, namespace: n.namespace} } // CopyWithNewNamespace make a new copy from current ResId and set a new namespace diff --git a/pkg/target/kusttarget.go b/pkg/target/kusttarget.go index 1db5c9a31..5e43fc0aa 100644 --- a/pkg/target/kusttarget.go +++ b/pkg/target/kusttarget.go @@ -282,8 +282,11 @@ func (kt *KustTarget) newTransformer(patches []*resource.Resource) (transformers r = append(r, t) r = append(r, transformers.NewNamespaceTransformer( string(kt.kustomization.Namespace), kt.tConfig.NameSpace)) - t, err = transformers.NewNamePrefixTransformer( - string(kt.kustomization.NamePrefix), kt.tConfig.NamePrefix) + t, err = transformers.NewNamePrefixSuffixTransformer( + string(kt.kustomization.NamePrefix), + "", // TODO(zoncoen): pass the name suffix + kt.tConfig.NamePrefix, + ) if err != nil { return nil, err } diff --git a/pkg/transformers/config/defaultconfig/defaultconfig.go b/pkg/transformers/config/defaultconfig/defaultconfig.go index 0a62e359d..d96639a8a 100644 --- a/pkg/transformers/config/defaultconfig/defaultconfig.go +++ b/pkg/transformers/config/defaultconfig/defaultconfig.go @@ -26,7 +26,6 @@ import ( func GetDefaultFieldSpecs() []byte { configData := [][]byte{ []byte(namePrefixFieldSpecs), - []byte(nameSuffixFieldSpecs), []byte(commonLabelFieldSpecs), []byte(commonAnnotationFieldSpecs), []byte(namespaceFieldSpecs), @@ -41,7 +40,6 @@ func GetDefaultFieldSpecs() []byte { func GetDefaultFieldSpecsAsMap() map[string]string { result := make(map[string]string) result["nameprefix"] = namePrefixFieldSpecs - result["namesuffix"] = nameSuffixFieldSpecs result["commonlabels"] = commonLabelFieldSpecs result["commonannotations"] = commonAnnotationFieldSpecs result["namespace"] = namespaceFieldSpecs diff --git a/pkg/transformers/config/defaultconfig/namesuffix.go b/pkg/transformers/config/defaultconfig/namesuffix.go deleted file mode 100644 index eec7ee54a..000000000 --- a/pkg/transformers/config/defaultconfig/namesuffix.go +++ /dev/null @@ -1,24 +0,0 @@ -/* -Copyright 2018 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package defaultconfig - -const ( - nameSuffixFieldSpecs = ` -nameSuffix: -- path: metadata/name -` -) diff --git a/pkg/transformers/prefixname_test.go b/pkg/transformers/prefixname_test.go deleted file mode 100644 index e384f604b..000000000 --- a/pkg/transformers/prefixname_test.go +++ /dev/null @@ -1,98 +0,0 @@ -/* -Copyright 2018 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package transformers - -import ( - "reflect" - "testing" - - "sigs.k8s.io/kustomize/k8sdeps/kunstruct" - "sigs.k8s.io/kustomize/pkg/resid" - "sigs.k8s.io/kustomize/pkg/resmap" - "sigs.k8s.io/kustomize/pkg/resource" -) - -func TestPrefixNameRun(t *testing.T) { - rf := resource.NewFactory( - kunstruct.NewKunstructuredFactoryImpl()) - m := resmap.ResMap{ - resid.NewResId(cmap, "cm1"): rf.FromMap( - map[string]interface{}{ - "apiVersion": "v1", - "kind": "ConfigMap", - "metadata": map[string]interface{}{ - "name": "cm1", - }, - }), - resid.NewResId(cmap, "cm2"): rf.FromMap( - map[string]interface{}{ - "apiVersion": "v1", - "kind": "ConfigMap", - "metadata": map[string]interface{}{ - "name": "cm2", - }, - }), - resid.NewResId(crd, "crd"): rf.FromMap( - map[string]interface{}{ - "apiVersion": "apiextensions.k8s.io/v1beta1", - "kind": "CustomResourceDefinition", - "metadata": map[string]interface{}{ - "name": "crd", - }, - }), - } - expected := resmap.ResMap{ - resid.NewResIdWithPrefix(cmap, "cm1", "someprefix-"): rf.FromMap( - map[string]interface{}{ - "apiVersion": "v1", - "kind": "ConfigMap", - "metadata": map[string]interface{}{ - "name": "someprefix-cm1", - }, - }), - resid.NewResIdWithPrefix(cmap, "cm2", "someprefix-"): rf.FromMap( - map[string]interface{}{ - "apiVersion": "v1", - "kind": "ConfigMap", - "metadata": map[string]interface{}{ - "name": "someprefix-cm2", - }, - }), - resid.NewResId(crd, "crd"): rf.FromMap( - map[string]interface{}{ - "apiVersion": "apiextensions.k8s.io/v1beta1", - "kind": "CustomResourceDefinition", - "metadata": map[string]interface{}{ - "name": "crd", - }, - }), - } - - npt, err := NewNamePrefixTransformer( - "someprefix-", defaultTransformerConfig.NamePrefix) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - err = npt.Transform(m) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - if !reflect.DeepEqual(m, expected) { - err = expected.ErrorIfNotEqual(m) - t.Fatalf("actual doesn't match expected: %v", err) - } -} diff --git a/pkg/transformers/prefixname.go b/pkg/transformers/prefixsuffixname.go similarity index 58% rename from pkg/transformers/prefixname.go rename to pkg/transformers/prefixsuffixname.go index 2aa161c12..4dc97aba6 100644 --- a/pkg/transformers/prefixname.go +++ b/pkg/transformers/prefixsuffixname.go @@ -26,40 +26,41 @@ import ( "sigs.k8s.io/kustomize/pkg/transformers/config" ) -// namePrefixTransformer contains the prefix and the FieldSpecs -// for each field needing a name prefix. -type namePrefixTransformer struct { +// namePrefixSuffixTransformer contains the prefix, suffix, and the FieldSpecs +// for each field needing a name prefix and suffix. +type namePrefixSuffixTransformer struct { prefix string + suffix string fieldSpecsToUse []config.FieldSpec fieldSpecsToSkip []config.FieldSpec } -var _ Transformer = &namePrefixTransformer{} +var _ Transformer = &namePrefixSuffixTransformer{} -var prefixFieldSpecsToSkip = []config.FieldSpec{ +var prefixSuffixFieldSpecsToSkip = []config.FieldSpec{ { Gvk: gvk.Gvk{Kind: "CustomResourceDefinition"}, }, } -// deprecateNamePrefixFieldSpec will be moved into prefixFieldSpecsToSkip in next release -var deprecateNamePrefixFieldSpec = config.FieldSpec{ +// deprecateNamePrefixSuffixFieldSpec will be moved into prefixSuffixFieldSpecsToSkip in next release +var deprecateNamePrefixSuffixFieldSpec = config.FieldSpec{ Gvk: gvk.Gvk{Kind: "Namespace"}, } -// NewNamePrefixTransformer construct a namePrefixTransformer. -func NewNamePrefixTransformer(np string, pc []config.FieldSpec) (Transformer, error) { - if len(np) == 0 { +// NewNamePrefixSuffixTransformer construct a namePrefixSuffixTransformer. +func NewNamePrefixSuffixTransformer(np, ns string, pc []config.FieldSpec) (Transformer, error) { + if len(np) == 0 && len(ns) == 0 { return NewNoOpTransformer(), nil } if pc == nil { return nil, errors.New("fieldSpecs is not expected to be nil") } - return &namePrefixTransformer{fieldSpecsToUse: pc, prefix: np, fieldSpecsToSkip: prefixFieldSpecsToSkip}, nil + return &namePrefixSuffixTransformer{fieldSpecsToUse: pc, prefix: np, suffix: ns, fieldSpecsToSkip: prefixSuffixFieldSpecsToSkip}, nil } -// Transform prepends the name prefix. -func (o *namePrefixTransformer) Transform(m resmap.ResMap) error { +// Transform prepends the name prefix and appends the name suffix. +func (o *namePrefixSuffixTransformer) Transform(m resmap.ResMap) error { // Fill map "mf" with entries subject to name modification, and // delete these entries from "m", so that for now m retains only // the entries whose names will not be modified. @@ -79,29 +80,29 @@ func (o *namePrefixTransformer) Transform(m resmap.ResMap) error { } for id := range mf { - if id.Gvk().IsSelected(&deprecateNamePrefixFieldSpec.Gvk) { - log.Println("Adding nameprefix to Namespace resource will be deprecated in next release.") + if id.Gvk().IsSelected(&deprecateNamePrefixSuffixFieldSpec.Gvk) { + log.Println("Adding nameprefix and namesuffix to Namespace resource will be deprecated in next release.") } objMap := mf[id].Map() for _, path := range o.fieldSpecsToUse { if !id.Gvk().IsSelected(&path.Gvk) { continue } - err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addPrefix) + err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addPrefixSuffix) if err != nil { return err } - newId := id.CopyWithNewPrefix(o.prefix) + newId := id.CopyWithNewPrefixSuffix(o.prefix, o.suffix) m[newId] = mf[id] } } return nil } -func (o *namePrefixTransformer) addPrefix(in interface{}) (interface{}, error) { +func (o *namePrefixSuffixTransformer) addPrefixSuffix(in interface{}) (interface{}, error) { s, ok := in.(string) if !ok { return nil, fmt.Errorf("%#v is expected to be %T", in, s) } - return o.prefix + s, nil + return fmt.Sprintf("%s%s%s", o.prefix, s, o.suffix), nil } diff --git a/pkg/transformers/suffixname_test.go b/pkg/transformers/prefixsuffixname_test.go similarity index 83% rename from pkg/transformers/suffixname_test.go rename to pkg/transformers/prefixsuffixname_test.go index 6a23930a4..d9fb9366c 100644 --- a/pkg/transformers/suffixname_test.go +++ b/pkg/transformers/prefixsuffixname_test.go @@ -26,7 +26,7 @@ import ( "sigs.k8s.io/kustomize/pkg/resource" ) -func TestSuffixNameRun(t *testing.T) { +func TestPrefixSuffixNameRun(t *testing.T) { rf := resource.NewFactory( kunstruct.NewKunstructuredFactoryImpl()) m := resmap.ResMap{ @@ -56,20 +56,20 @@ func TestSuffixNameRun(t *testing.T) { }), } expected := resmap.ResMap{ - resid.NewResIdWithSuffix(cmap, "cm1", "-somesuffix"): rf.FromMap( + resid.NewResIdWithPrefixSuffix(cmap, "cm1", "someprefix-", "-somesuffix"): rf.FromMap( map[string]interface{}{ "apiVersion": "v1", "kind": "ConfigMap", "metadata": map[string]interface{}{ - "name": "cm1-somesuffix", + "name": "someprefix-cm1-somesuffix", }, }), - resid.NewResIdWithSuffix(cmap, "cm2", "-somesuffix"): rf.FromMap( + resid.NewResIdWithPrefixSuffix(cmap, "cm2", "someprefix-", "-somesuffix"): rf.FromMap( map[string]interface{}{ "apiVersion": "v1", "kind": "ConfigMap", "metadata": map[string]interface{}{ - "name": "cm2-somesuffix", + "name": "someprefix-cm2-somesuffix", }, }), resid.NewResId(crd, "crd"): rf.FromMap( @@ -82,12 +82,12 @@ func TestSuffixNameRun(t *testing.T) { }), } - npt, err := NewNameSuffixTransformer( - "-somesuffix", defaultTransformerConfig.NameSuffix) + npst, err := NewNamePrefixSuffixTransformer( + "someprefix-", "-somesuffix", defaultTransformerConfig.NamePrefix) if err != nil { t.Fatalf("unexpected error: %v", err) } - err = npt.Transform(m) + err = npst.Transform(m) if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/pkg/transformers/suffixname.go b/pkg/transformers/suffixname.go deleted file mode 100644 index ccc3d3790..000000000 --- a/pkg/transformers/suffixname.go +++ /dev/null @@ -1,107 +0,0 @@ -/* -Copyright 2018 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package transformers - -import ( - "errors" - "fmt" - "log" - - "sigs.k8s.io/kustomize/pkg/gvk" - "sigs.k8s.io/kustomize/pkg/resmap" - "sigs.k8s.io/kustomize/pkg/transformers/config" -) - -// nameSuffixTransformer contains the suffix and the FieldSpecs -// for each field needing a name suffix. -type nameSuffixTransformer struct { - suffix string - fieldSpecsToUse []config.FieldSpec - fieldSpecsToSkip []config.FieldSpec -} - -var _ Transformer = &nameSuffixTransformer{} - -var suffixFieldSpecsToSkip = []config.FieldSpec{ - { - Gvk: gvk.Gvk{Kind: "CustomResourceDefinition"}, - }, -} - -// deprecateNameSuffixFieldSpec will be moved into suffixFieldSpecsToSkip in next release -var deprecateNameSuffixFieldSpec = config.FieldSpec{ - Gvk: gvk.Gvk{Kind: "Namespace"}, -} - -// NewNameSuffixTransformer construct a nameSuffixTransformer. -func NewNameSuffixTransformer(ns string, pc []config.FieldSpec) (Transformer, error) { - if len(ns) == 0 { - return NewNoOpTransformer(), nil - } - if pc == nil { - return nil, errors.New("fieldSpecs is not expected to be nil") - } - return &nameSuffixTransformer{fieldSpecsToUse: pc, suffix: ns, fieldSpecsToSkip: suffixFieldSpecsToSkip}, nil -} - -// Transform appends the name suffix. -func (o *nameSuffixTransformer) Transform(m resmap.ResMap) error { - // Fill map "mf" with entries subject to name modification, and - // delete these entries from "m", so that for now m retains only - // the entries whose names will not be modified. - 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) - } - } - - for id := range mf { - if id.Gvk().IsSelected(&deprecateNameSuffixFieldSpec.Gvk) { - log.Println("Adding name suffix to Namespace resource will be deprecated in next release.") - } - objMap := mf[id].Map() - for _, path := range o.fieldSpecsToUse { - if !id.Gvk().IsSelected(&path.Gvk) { - continue - } - err := mutateField(objMap, path.PathSlice(), path.CreateIfNotPresent, o.addSuffix) - if err != nil { - return err - } - newId := id.CopyWithNewSuffix(o.suffix) - m[newId] = mf[id] - } - } - return nil -} - -func (o *nameSuffixTransformer) addSuffix(in interface{}) (interface{}, error) { - s, ok := in.(string) - if !ok { - return nil, fmt.Errorf("%#v is expectd to be %T", in, s) - } - return s + o.suffix, nil -}