Skip to content

Commit 6c27970

Browse files
committed
Add static value source for replacement
Introduces `sourceValue` on replacement object for replacing from a static value instead of sourcing from another object. Solves #5516
1 parent cc9dd34 commit 6c27970

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

api/filters/replacement/replacement.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Filter struct {
2323
// Filter replaces values of targets with values from sources
2424
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
2525
for i, r := range f.Replacements {
26-
if r.Source == nil || r.Targets == nil {
26+
if (r.SourceValue == nil && r.Source == nil) || r.Targets == nil {
2727
return nil, fmt.Errorf("replacements must specify a source and at least one target")
2828
}
2929
value, err := getReplacement(nodes, &f.Replacements[i])
@@ -39,6 +39,13 @@ func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
3939
}
4040

4141
func getReplacement(nodes []*yaml.RNode, r *types.Replacement) (*yaml.RNode, error) {
42+
if r.SourceValue != nil && r.Source != nil {
43+
return nil, fmt.Errorf("value and resource selectors are mutually exclusive")
44+
}
45+
if r.SourceValue != nil {
46+
return yaml.NewScalarRNode(*r.SourceValue), nil
47+
}
48+
4249
source, err := selectSourceNode(nodes, r.Source)
4350
if err != nil {
4451
return nil, err

api/filters/replacement/replacement_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,6 +2829,70 @@ spec:
28292829
create: true
28302830
`,
28312831
expectedErr: "unable to find or create field \"spec.tls.5.hosts.5\" in replacement target: index 5 specified but only 0 elements found",
2832+
}, "replace with static value": {
2833+
input: `apiVersion: v1
2834+
kind: Deployment
2835+
metadata:
2836+
name: deploy
2837+
spec:
2838+
template:
2839+
spec:
2840+
containers:
2841+
- image: nginx:1.7.9
2842+
name: nginx-tagged
2843+
- image: postgres:1.8.0
2844+
name: postgresdb
2845+
`,
2846+
replacements: `replacements:
2847+
- sourceValue: custom/postgres:1.9.0
2848+
targets:
2849+
- select:
2850+
kind: Deployment
2851+
name: deploy
2852+
fieldPaths:
2853+
- spec.template.spec.containers.[name=postgresdb].image
2854+
`,
2855+
expected: `apiVersion: v1
2856+
kind: Deployment
2857+
metadata:
2858+
name: deploy
2859+
spec:
2860+
template:
2861+
spec:
2862+
containers:
2863+
- image: nginx:1.7.9
2864+
name: nginx-tagged
2865+
- image: custom/postgres:1.9.0
2866+
name: postgresdb
2867+
`,
2868+
}, "source value and selector error": {
2869+
input: `apiVersion: v1
2870+
kind: Deployment
2871+
metadata:
2872+
name: deploy
2873+
spec:
2874+
replicas: 1
2875+
template:
2876+
spec:
2877+
containers:
2878+
- image: nginx:1.7.9
2879+
name: nginx-tagged
2880+
- image: postgres:1.8.0
2881+
name: postgresdb
2882+
`,
2883+
replacements: `replacements:
2884+
- source:
2885+
kind: Deployment
2886+
name: deploy
2887+
sourceValue: 2
2888+
targets:
2889+
- select:
2890+
kind: Deployment
2891+
name: deploy
2892+
fieldPaths:
2893+
- spec.replicas
2894+
`,
2895+
expectedErr: "value and resource selectors are mutually exclusive",
28322896
},
28332897
}
28342898

api/types/replacement.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ type Replacement struct {
2020

2121
// The N fields to write the value to.
2222
Targets []*TargetSelector `json:"targets,omitempty" yaml:"targets,omitempty"`
23+
24+
// Used to define an static value
25+
SourceValue *string `json:"sourceValue,omitempty" yaml:"sourceValue,omitempty"`
2326
}
2427

2528
// SourceSelector is the source of the replacement transformer.

0 commit comments

Comments
 (0)