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
This commit is contained in:
Sirajudeen 2020-08-31 14:10:24 +00:00
parent 61633d30fc
commit 7e391bfc1b
2 changed files with 52 additions and 0 deletions
pkg/document/plugin/replacement/v1alpha1

@ -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, ".")
}

@ -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