diff --git a/plugin/builtin/PrefixSuffixTransformer.go b/plugin/builtin/PrefixSuffixTransformer.go index 4f7482d0d3..85c960a216 100644 --- a/plugin/builtin/PrefixSuffixTransformer.go +++ b/plugin/builtin/PrefixSuffixTransformer.go @@ -56,21 +56,23 @@ func (p *PrefixSuffixTransformerPlugin) Transform(m resmap.ResMap) error { if p.shouldSkip(r.OrgId()) { continue } - fs, ok := p.shouldInclude(r.OrgId()) - if !ok { - continue - } - if smellsLikeANameChange(fs) { - r.AddNamePrefix(p.Prefix) - r.AddNameSuffix(p.Suffix) - } - err := transformers.MutateField( - r.Map(), - fs.PathSlice(), - fs.CreateIfNotPresent, - p.addPrefixSuffix) - if err != nil { - return err + id := r.OrgId() + for _, path := range p.FieldSpecs { + if !id.IsSelected(&path.Gvk) { + continue + } + if smellsLikeANameChange(&path) { + r.AddNamePrefix(p.Prefix) + r.AddNameSuffix(p.Suffix) + } + err := transformers.MutateField( + r.Map(), + path.PathSlice(), + path.CreateIfNotPresent, + p.addPrefixSuffix) + if err != nil { + return err + } } } return nil @@ -80,16 +82,6 @@ func smellsLikeANameChange(fs *config.FieldSpec) bool { return fs.Path == "metadata/name" } -func (p *PrefixSuffixTransformerPlugin) shouldInclude( - id resid.ResId) (*config.FieldSpec, bool) { - for _, path := range p.FieldSpecs { - if id.IsSelected(&path.Gvk) { - return &path, true - } - } - return nil, false -} - func (p *PrefixSuffixTransformerPlugin) shouldSkip( id resid.ResId) bool { for _, path := range prefixSuffixFieldSpecsToSkip { diff --git a/plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer.go b/plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer.go index e800afcde3..4e490ef457 100644 --- a/plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer.go +++ b/plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer.go @@ -57,21 +57,23 @@ func (p *plugin) Transform(m resmap.ResMap) error { if p.shouldSkip(r.OrgId()) { continue } - fs, ok := p.shouldInclude(r.OrgId()) - if !ok { - continue - } - if smellsLikeANameChange(fs) { - r.AddNamePrefix(p.Prefix) - r.AddNameSuffix(p.Suffix) - } - err := transformers.MutateField( - r.Map(), - fs.PathSlice(), - fs.CreateIfNotPresent, - p.addPrefixSuffix) - if err != nil { - return err + id := r.OrgId() + for _, path := range p.FieldSpecs { + if !id.IsSelected(&path.Gvk) { + continue + } + if smellsLikeANameChange(&path) { + r.AddNamePrefix(p.Prefix) + r.AddNameSuffix(p.Suffix) + } + err := transformers.MutateField( + r.Map(), + path.PathSlice(), + path.CreateIfNotPresent, + p.addPrefixSuffix) + if err != nil { + return err + } } } return nil @@ -81,16 +83,6 @@ func smellsLikeANameChange(fs *config.FieldSpec) bool { return fs.Path == "metadata/name" } -func (p *plugin) shouldInclude( - id resid.ResId) (*config.FieldSpec, bool) { - for _, path := range p.FieldSpecs { - if id.IsSelected(&path.Gvk) { - return &path, true - } - } - return nil, false -} - func (p *plugin) shouldSkip( id resid.ResId) bool { for _, path := range prefixSuffixFieldSpecsToSkip { diff --git a/plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer_test.go b/plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer_test.go index d7406c5b2c..a52f55b1f2 100644 --- a/plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer_test.go +++ b/plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer_test.go @@ -66,4 +66,61 @@ kind: ConfigMap metadata: name: baked-cm-pie `) + + rm = th.LoadAndRunTransformer(` +apiVersion: builtin +kind: PrefixSuffixTransformer +metadata: + name: notImportantHere +prefix: test- +fieldSpecs: + - kind: Deployment + path: metadata/name + - kind: Deployment + path: spec/template/spec/containers/name +`, ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: deployment +spec: + template: + spec: + containers: + - image: myapp + name: main +--- +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: crd +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: cm +`) + + th.AssertActualEqualsExpected(rm, ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test-deployment +spec: + template: + spec: + containers: + - image: myapp + name: test-main +--- +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: crd +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: cm +`) }