diff --git a/kustomize/commands/edit/fix/convert_test.go b/kustomize/commands/edit/fix/convert_test.go index 2472c5046fe..532aaebdec4 100644 --- a/kustomize/commands/edit/fix/convert_test.go +++ b/kustomize/commands/edit/fix/convert_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils" "sigs.k8s.io/kustomize/kyaml/filesys" ) @@ -1143,3 +1144,76 @@ metadata: a.b.c: SOME_SECRET_NAME_PLACEHOLDER `, string(content)) } + +func TestFixVarsWithPatch(t *testing.T) { + kustomization := []byte(` +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization + +patchesStrategicMerge: + - patch.yaml + +vars: + - name: CERTIFICATE_NAMESPACE + objref: + name: system + fieldref: + fieldpath: metadata.namespace +`) + patch := []byte(` +apiVersion: apps/v1 +kapiVersion: apps/v1 +kind: Deployment +metadata: + name: controller-manager + namespace: system +spec: + template: + spec: + containers: + - name: $(CERTIFICATE_NAMESPACE) +`) + + fSys := filesys.MakeFsInMemory() + testutils_test.WriteTestKustomizationWith(fSys, kustomization) + fSys.WriteFile("patch.yaml", patch) + 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 + + +patches: +- path: patch.yaml +replacements: +- source: + fieldPath: metadata.namespace + name: system + targets: + - fieldPaths: + - spec.template.spec.containers.0.name + select: + namespace: system +`, string(content)) + + content, err = fSys.ReadFile("patch.yaml") + assert.NoError(t, err) + assert.Equal(t, ` +apiVersion: apps/v1 +kapiVersion: apps/v1 +kind: Deployment +metadata: + name: controller-manager + namespace: system +spec: + template: + spec: + containers: + - name: CERTIFICATE_NAMESPACE_PLACEHOLDER +`, string(content)) +}