Skip to content

Commit 3e5e25e

Browse files
committed
implements nested kyaml(KEP-5295) structure replacements
1 parent c44253d commit 3e5e25e

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

api/filters/replacement/replacement_test.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4944,12 +4944,68 @@ data:
49444944
- name: sidecar
49454945
image: busybox:latest`,
49464946
},
4947+
"replacement yaml flow style (kyaml) field": {
4948+
input: `apiVersion: v1
4949+
kind: ConfigMap
4950+
metadata:
4951+
name: source-values
4952+
data:
4953+
app_name: "my-awesome-app"
4954+
---
4955+
apiVersion: v1
4956+
kind: ConfigMap
4957+
metadata:
4958+
name: target-config
4959+
data:
4960+
config.yaml: |-
4961+
labels: {
4962+
app: "REPLACE_APP_NAME",
4963+
version: "1.0.0",
4964+
env: "production",
4965+
}
4966+
spec: {
4967+
replicas: 3,
4968+
selector: {
4969+
matchLabels: {
4970+
app: "REPLACE_APP_NAME"
4971+
}
4972+
}
4973+
}`,
4974+
replacements: `replacements:
4975+
- source:
4976+
kind: ConfigMap
4977+
name: source-values
4978+
fieldPath: data.app_name
4979+
targets:
4980+
- select:
4981+
kind: ConfigMap
4982+
name: target-config
4983+
fieldPaths:
4984+
- data.config\.yaml.labels.app
4985+
- data.config\.yaml.spec.selector.matchLabels.app
4986+
`,
4987+
expected: `apiVersion: v1
4988+
kind: ConfigMap
4989+
metadata:
4990+
name: source-values
4991+
data:
4992+
app_name: "my-awesome-app"
4993+
---
4994+
apiVersion: v1
4995+
kind: ConfigMap
4996+
metadata:
4997+
name: target-config
4998+
data:
4999+
config.yaml: |-
5000+
labels: {app: "my-awesome-app", version: "1.0.0", env: "production"}
5001+
spec: {replicas: 3, selector: {matchLabels: {app: "my-awesome-app"}}}`,
5002+
},
49475003
}
49485004

49495005
for tn := range testCases {
49505006
t.Run(tn, func(t *testing.T) {
49515007
// Test one case to see if structured data replacement works
4952-
if tn == "replacement contain jsonfield" || tn == "replacement yaml field in configmap" {
5008+
if tn == "replacement contain jsonfield" || tn == "replacement yaml field in configmap" || tn == "replacement yaml flow style (kyaml) field" {
49535009
f := Filter{}
49545010
err := yaml.Unmarshal([]byte(testCases[tn].replacements), &f)
49555011
if !assert.NoError(t, err) {

kyaml/yaml/match_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,52 @@ data:
423423
path: []string{"data", "config.json", "database", "connections", "primary", "host"},
424424
expected: "- \"primary-db.example.com\"\n",
425425
},
426+
"yaml flow style (kyaml) field access": {
427+
input: `apiVersion: v1
428+
kind: ConfigMap
429+
metadata:
430+
name: test-config
431+
data:
432+
config.yaml: |-
433+
labels: {
434+
app: "foobar",
435+
foo: "bar",
436+
something: "12345",
437+
}
438+
spec: {
439+
replicas: 3,
440+
selector: {
441+
matchLabels: {
442+
app: "foobar"
443+
}
444+
}
445+
}`,
446+
path: []string{"data", "config.yaml", "labels", "app"},
447+
expected: "- \"foobar\"\n",
448+
},
449+
"yaml flow style nested field access": {
450+
input: `apiVersion: v1
451+
kind: ConfigMap
452+
metadata:
453+
name: test-config
454+
data:
455+
config.yaml: |-
456+
labels: {
457+
app: "foobar",
458+
foo: "bar",
459+
something: "12345",
460+
}
461+
spec: {
462+
replicas: 3,
463+
selector: {
464+
matchLabels: {
465+
app: "foobar"
466+
}
467+
}
468+
}`,
469+
path: []string{"data", "config.yaml", "spec", "selector", "matchLabels", "app"},
470+
expected: "- \"foobar\"\n",
471+
},
426472
"invalid json returns field value as-is": {
427473
input: `apiVersion: v1
428474
kind: ConfigMap

0 commit comments

Comments
 (0)