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
This commit is contained in:
Graham Steffaniak 2021-08-18 21:07:29 +00:00
parent b77f9e2910
commit 5f9ddcc62a
4 changed files with 65 additions and 4 deletions

View File

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

View File

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

View File

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

View File

@ -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
`,
},
}