From 7e391bfc1bb0200ab2aa01beaf088f5be705831d Mon Sep 17 00:00:00 2001 From: Sirajudeen Date: Mon, 31 Aug 2020 14:10:24 +0000 Subject: [PATCH] Substring replacement fix in ReplacementTranformer * Substring replacement failing in ReplacementTranformer when target substring contains periods (.) * This is temporary fix till the kpt-function way of replacement transformer is implemented * In this fix it is assumed that the substring condition can only occur at the end of the path. example: expected: replace the nginx version from 1.7.9 to 1.17.0 for container named `nginx-tagged` target condition: spec.template.spec.containers[name=nginx-tagged].image%1.7.9% Change-Id: I76fb65f69a6eedf3cbdd692d3bc1835a214dc8fa Relates-To: #336 Closes: #336 --- .../replacement/v1alpha1/transformer.go | 9 ++++ .../replacement/v1alpha1/transformer_test.go | 43 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/pkg/document/plugin/replacement/v1alpha1/transformer.go b/pkg/document/plugin/replacement/v1alpha1/transformer.go index 6d12a3852..27e2b1a8a 100644 --- a/pkg/document/plugin/replacement/v1alpha1/transformer.go +++ b/pkg/document/plugin/replacement/v1alpha1/transformer.go @@ -170,8 +170,17 @@ func substitute(m resmap.ResMap, to *types.ReplTarget, replacement interface{}) tmp = append(tmp, part) } p = strings.Join(tmp, "[") + // Exclude substring portion from dot replacer + // substring can contain IP or any dot separated string + substringPattern := "" + p, substringPattern = extractSubstringPattern(p) pathSlice := strings.Split(p, ".") + // append back the extracted substring + if len(substringPattern) > 0 { + pathSlice[len(pathSlice)-1] = pathSlice[len(pathSlice)-1] + "%" + + substringPattern + "%" + } for i, part := range pathSlice { pathSlice[i] = strings.ReplaceAll(part, dotReplacer, ".") } diff --git a/pkg/document/plugin/replacement/v1alpha1/transformer_test.go b/pkg/document/plugin/replacement/v1alpha1/transformer_test.go index f4fd2c577..32378d7df 100644 --- a/pkg/document/plugin/replacement/v1alpha1/transformer_test.go +++ b/pkg/document/plugin/replacement/v1alpha1/transformer_test.go @@ -158,6 +158,49 @@ kind: ReplacementTransformer metadata: name: notImportantHere replacements: +- source: + value: 1.17.0 + target: + objref: + kind: Deployment + fieldrefs: + - spec.template.spec.containers[name=nginx-tagged].image%1.7.9% +`, + + in: ` +group: apps +apiVersion: v1 +kind: Deployment +metadata: + name: deploy1 +spec: + template: + spec: + containers: + - image: nginx:1.7.9 + name: nginx-tagged +`, + expectedOut: `apiVersion: v1 +group: apps +kind: Deployment +metadata: + name: deploy1 +spec: + template: + spec: + containers: + - image: nginx:1.17.0 + name: nginx-tagged +`, + }, + + { + cfg: ` +apiVersion: airshipit.org/v1alpha1 +kind: ReplacementTransformer +metadata: + name: notImportantHere +replacements: - source: objref: kind: Pod