Skip to content

Commit

Permalink
convert vars to replacements: mapping value with dot
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Jun 11, 2021
1 parent 8fdb3f1 commit 3b37fed
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions kustomize/commands/edit/fix/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ func findVarName(node *kyaml.RNode, varName string, path []string) ([]string, []
case kyaml.MappingNode:
err := node.VisitFields(func(n *kyaml.MapNode) error {
nextPathItem := strings.TrimSpace(n.Key.MustString())
if strings.Contains(nextPathItem, ".") {
nextPathItem = fmt.Sprintf("[%s]", nextPathItem)
}
fieldPathsToAdd, optionsToAdd, err := findVarName(n.Value.Copy(),
varName, append(path, nextPathItem))
if err != nil {
Expand Down
66 changes: 66 additions & 0 deletions kustomize/commands/edit/fix/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1077,3 +1077,69 @@ spec:
value: SOME_SECRET_NAME_PLACEHOLDER
`, string(content))
}

func TestFixVarsWithDot(t *testing.T) {
kustomization := []byte(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- pod.yaml
vars:
- name: SOME_SECRET_NAME
objref:
kind: Secret
name: my-secret
apiVersion: v1
`)
pod := []byte(`
apiVersion: v1
kind: Pod
metadata:
name: my-pod
annotations:
a.b.c: $(SOME_SECRET_NAME)
`)

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("pod.yaml", pod)
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
content, err := testutils_test.ReadTestKustomization(fSys)
assert.NoError(t, err)

assert.Equal(t, `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- pod.yaml
replacements:
- source:
kind: Secret
name: my-secret
version: v1
targets:
- fieldPaths:
- metadata.annotations.[a.b.c]
select:
kind: Pod
name: my-pod
version: v1
`, string(content))

content, err = fSys.ReadFile("pod.yaml")
assert.NoError(t, err)
assert.Equal(t, `
apiVersion: v1
kind: Pod
metadata:
name: my-pod
annotations:
a.b.c: SOME_SECRET_NAME_PLACEHOLDER
`, string(content))
}

0 comments on commit 3b37fed

Please sign in to comment.