From 9997e7427e5591ef58254f4c4d54534bcfb4d2df Mon Sep 17 00:00:00 2001 From: Matt McEuen Date: Thu, 10 Sep 2020 19:11:55 -0500 Subject: [PATCH] Allow substring replacement of numerics This change adds the ability to use substring substitution to inject numeric source values into string targets. This is required for our networking catalogue, since a numeric k8s API port needs to be inserted into numeric and (sub)string targets in different phases. Co-Authored-By: Ian Howell Change-Id: I24beb46a2bda4e118406129a0a922b0c56142c76 --- .../replacement/v1alpha1/transformer.go | 16 +++-- .../replacement/v1alpha1/transformer_test.go | 59 +++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) 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: