From 5f9ddcc62a9e3f2bae73d50ca3a721c54efb8f80 Mon Sep 17 00:00:00 2001 From: Graham Steffaniak Date: Wed, 18 Aug 2021 21:07:29 +0000 Subject: [PATCH] Update Transformer to accept blank source string ADD: return condition for blank source string. ADD: test case 26 for example. CHG: Updated source value variable type Closes: #620 Change-Id: Id2bac427cd0570eac3e87b98ded665d70613d2d6 --- pkg/api/v1alpha1/replacement_plugin_types.go | 2 +- pkg/api/v1alpha1/zz_generated.deepcopy.go | 5 ++ .../plugin/replacement/transformer.go | 10 ++-- .../plugin/replacement/transformer_test.go | 52 +++++++++++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/pkg/api/v1alpha1/replacement_plugin_types.go b/pkg/api/v1alpha1/replacement_plugin_types.go index 8bfdc2dc6..58a6e4391 100644 --- a/pkg/api/v1alpha1/replacement_plugin_types.go +++ b/pkg/api/v1alpha1/replacement_plugin_types.go @@ -85,7 +85,7 @@ type Replacement struct { type ReplSource struct { ObjRef *Target `json:"objref,omitempty" yaml:"objref,omitempty"` FieldRef string `json:"fieldref,omitempty" yaml:"fiedldref,omitempty"` - Value string `json:"value,omitempty" yaml:"value,omitempty"` + Value *string `json:"value,omitempty" yaml:"value,omitempty"` } // ReplTarget defines where a substitution is to. diff --git a/pkg/api/v1alpha1/zz_generated.deepcopy.go b/pkg/api/v1alpha1/zz_generated.deepcopy.go index 88b389d6c..dcb5b5ae7 100644 --- a/pkg/api/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/api/v1alpha1/zz_generated.deepcopy.go @@ -1481,6 +1481,11 @@ func (in *ReplSource) DeepCopyInto(out *ReplSource) { *out = new(Target) **out = **in } + if in.Value != nil { + in, out := &in.Value, &out.Value + *out = new(string) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ReplSource. diff --git a/pkg/document/plugin/replacement/transformer.go b/pkg/document/plugin/replacement/transformer.go index e87430337..233c0e791 100644 --- a/pkg/document/plugin/replacement/transformer.go +++ b/pkg/document/plugin/replacement/transformer.go @@ -62,9 +62,12 @@ func New(obj map[string]interface{}) (kio.Filter, error) { if r.Target != nil && r.Targets != nil { return nil, ErrBadConfiguration{Msg: "only target OR targets is allowed in one replacement, not both"} } - if r.Source.ObjRef != nil && r.Source.Value != "" { + if r.Source.ObjRef != nil && r.Source.Value != nil { return nil, ErrBadConfiguration{Msg: "only one of fieldref and value is allowed in one replacement"} } + if r.Source.ObjRef == nil && r.Source.Value == nil { + return nil, ErrBadConfiguration{Msg: "no source value or source objectref value was given"} + } } return p, nil } @@ -91,9 +94,10 @@ func (p *plugin) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) { } func getValue(items []*yaml.RNode, source *airshipv1.ReplSource) (*yaml.RNode, error) { - if source.Value != "" { - return yaml.NewScalarRNode(source.Value), nil + if source.Value != nil { + return yaml.NewScalarRNode(*source.Value), nil } + sources, err := kyamlutils.DocumentSelector{}. ByAPIVersion(source.ObjRef.APIVersion). ByGVK(source.ObjRef.Group, source.ObjRef.Version, source.ObjRef.Kind). diff --git a/pkg/document/plugin/replacement/transformer_test.go b/pkg/document/plugin/replacement/transformer_test.go index ff86d0283..dd1963aef 100644 --- a/pkg/document/plugin/replacement/transformer_test.go +++ b/pkg/document/plugin/replacement/transformer_test.go @@ -1360,6 +1360,58 @@ spec: name: nginx-sha256 - image: alpine:1.8.0 name: init-alpine +`, + }, + { + cfg: ` +apiVersion: airshipit.org/v1alpha1 +kind: ReplacementTransformer +metadata: + name: Test_Case_26_Empty_Source_String +replacements: +- source: + value: "" + target: + objref: + kind: Deployment + name: source-controller + fieldrefs: ["spec.template.spec.containers[name=manager].env[name=http_proxy].value%REPLACEMENT_HTTP_PROXY%"] +`, + + in: ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: source-controller +spec: + template: + spec: + containers: + - name: manager + env: + - name: http_proxy + value: REPLACEMENT_HTTP_PROXY + - name: https_proxy + value: REPLACEMENT_HTTPS_PROXY + - name: no_proxy + value: REPLACEMENT_NO_PROXY +`, + expectedOut: `apiVersion: apps/v1 +kind: Deployment +metadata: + name: source-controller +spec: + template: + spec: + containers: + - name: manager + env: + - name: http_proxy + value: "" + - name: https_proxy + value: REPLACEMENT_HTTPS_PROXY + - name: no_proxy + value: REPLACEMENT_NO_PROXY `, }, }