From 3b37fed24b292e4221343e94e4e73508c7e5faab Mon Sep 17 00:00:00 2001 From: Natasha Sarkar Date: Fri, 11 Jun 2021 13:45:04 -0700 Subject: [PATCH] convert vars to replacements: mapping value with dot --- kustomize/commands/edit/fix/convert.go | 3 + kustomize/commands/edit/fix/convert_test.go | 66 +++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/kustomize/commands/edit/fix/convert.go b/kustomize/commands/edit/fix/convert.go index bb8a898e72..cbe3b6754b 100644 --- a/kustomize/commands/edit/fix/convert.go +++ b/kustomize/commands/edit/fix/convert.go @@ -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 { diff --git a/kustomize/commands/edit/fix/convert_test.go b/kustomize/commands/edit/fix/convert_test.go index c803a3c5f8..b818ab718c 100644 --- a/kustomize/commands/edit/fix/convert_test.go +++ b/kustomize/commands/edit/fix/convert_test.go @@ -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)) +}