diff --git a/pkg/document/plugin/replacement/v1alpha1/transformer.go b/pkg/document/plugin/replacement/v1alpha1/transformer.go index 591e26d25..712e40a10 100644 --- a/pkg/document/plugin/replacement/v1alpha1/transformer.go +++ b/pkg/document/plugin/replacement/v1alpha1/transformer.go @@ -231,9 +231,17 @@ func applySubstringPattern(target interface{}, replacement interface{}, return replacement, nil } - repl, ok := replacement.(string) - if !ok { - return nil, ErrPatternSubstring{Msg: "pattern-based substitution can only be applied with string replacement values"} + // if replacement is numeric, convert it to a string for the sake of + // substring substitution. + var replacementString string + switch t := replacement.(type) { + case uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64: + replacementString = fmt.Sprint(t) + case string: + replacementString = t + default: + return nil, ErrPatternSubstring{Msg: "pattern-based substitution can only be applied " + + "with string or numeric replacement values"} } tgt, ok := target.(string) @@ -248,7 +256,7 @@ func applySubstringPattern(target interface{}, replacement interface{}, substringPattern, tgt), } } - return p.ReplaceAllString(tgt, repl), nil + return p.ReplaceAllString(tgt, replacementString), nil } func updateMapField(m map[string]interface{}, pathToField []string, replacement interface{}) error { diff --git a/pkg/document/plugin/replacement/v1alpha1/transformer_test.go b/pkg/document/plugin/replacement/v1alpha1/transformer_test.go index 894bdd6a3..3fd98d40d 100644 --- a/pkg/document/plugin/replacement/v1alpha1/transformer_test.go +++ b/pkg/document/plugin/replacement/v1alpha1/transformer_test.go @@ -554,6 +554,65 @@ spec: cfg: ` apiVersion: airshipit.org/v1alpha1 kind: ReplacementTransformer +metadata: + name: test-for-numeric-conversion +replacements: +- source: + objref: + kind: Pod + name: pod + fieldref: spec.containers[0].image + target: + objref: + kind: Deployment + fieldrefs: + - spec.template.spec.containers[name=myapp-container].image%TAG%`, + in: ` +apiVersion: v1 +kind: Pod +metadata: + name: pod +spec: + containers: + - name: repl + image: 12345 +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: deploy2 +spec: + template: + spec: + containers: + - image: busybox:TAG + name: myapp-container +`, + expectedOut: `apiVersion: v1 +kind: Pod +metadata: + name: pod +spec: + containers: + - image: 12345 + name: repl +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: deploy2 +spec: + template: + spec: + containers: + - image: busybox:12345 + name: myapp-container +`, + }, + { + cfg: ` +apiVersion: airshipit.org/v1alpha1 +kind: ReplacementTransformer metadata: name: notImportantHere replacements: