From 69c90e3427f02127dc72a60309e9efeabb1d7e0d Mon Sep 17 00:00:00 2001 From: Jerome Brette Date: Sat, 20 Jul 2019 12:11:46 -0500 Subject: [PATCH] Fix namereference and stacked kustomization contexts (1/3) - Update PrefixSuffixTransfomer to add empty prefix and suffix --- plugin/builtin/PrefixSuffixTransformer.go | 24 ++++++++++++++++--- .../PrefixSuffixTransformer.go | 24 ++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/plugin/builtin/PrefixSuffixTransformer.go b/plugin/builtin/PrefixSuffixTransformer.go index 3ea0f159a..6e4e2e6d6 100644 --- a/plugin/builtin/PrefixSuffixTransformer.go +++ b/plugin/builtin/PrefixSuffixTransformer.go @@ -49,22 +49,40 @@ func (p *PrefixSuffixTransformerPlugin) Config( } func (p *PrefixSuffixTransformerPlugin) Transform(m resmap.ResMap) error { - if len(p.Prefix) == 0 && len(p.Suffix) == 0 { - return nil - } + + // Even if both the Prefix and Suffix are empty we want + // to proceed with the transformation. This allows to add contextual + // information to the resources (AddNamePrefix and AddNameSuffix). + for _, r := range m.Resources() { if p.shouldSkip(r.OrgId()) { + // Don't change the actual definition + // of a CRD. continue } id := r.OrgId() + // current default configuration contains + // only one entry: "metadata/name" with no GVK for _, path := range p.FieldSpecs { if !id.IsSelected(&path.Gvk) { + // With the currrent default configuration, + // because no Gvk is specified, so a wild + // card continue } + if smellsLikeANameChange(&path) { + // "metadata/name" is the only field. + // this will add a prefix and a suffix + // to the resource even if those are + // empty r.AddNamePrefix(p.Prefix) r.AddNameSuffix(p.Suffix) } + + // the addPrefixSuffix method will not + // change the name if both the prefix and suffix + // are empty. err := transformers.MutateField( r.Map(), path.PathSlice(), diff --git a/plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer.go b/plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer.go index 20a9ac9ce..80c57462e 100644 --- a/plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer.go +++ b/plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer.go @@ -50,22 +50,40 @@ func (p *plugin) Config( } func (p *plugin) Transform(m resmap.ResMap) error { - if len(p.Prefix) == 0 && len(p.Suffix) == 0 { - return nil - } + + // Even if both the Prefix and Suffix are empty we want + // to proceed with the transformation. This allows to add contextual + // information to the resources (AddNamePrefix and AddNameSuffix). + for _, r := range m.Resources() { if p.shouldSkip(r.OrgId()) { + // Don't change the actual definition + // of a CRD. continue } id := r.OrgId() + // current default configuration contains + // only one entry: "metadata/name" with no GVK for _, path := range p.FieldSpecs { if !id.IsSelected(&path.Gvk) { + // With the currrent default configuration, + // because no Gvk is specified, so a wild + // card continue } + if smellsLikeANameChange(&path) { + // "metadata/name" is the only field. + // this will add a prefix and a suffix + // to the resource even if those are + // empty r.AddNamePrefix(p.Prefix) r.AddNameSuffix(p.Suffix) } + + // the addPrefixSuffix method will not + // change the name if both the prefix and suffix + // are empty. err := transformers.MutateField( r.Map(), path.PathSlice(),