diff --git a/api/types/kustomization.go b/api/types/kustomization.go index c194dcc3c..0aef52e2c 100644 --- a/api/types/kustomization.go +++ b/api/types/kustomization.go @@ -222,6 +222,13 @@ func (k *Kustomization) FixKustomizationPreMarshalling() error { k.Patches = append(k.Patches, k.PatchesJson6902...) k.PatchesJson6902 = nil + if k.PatchesStrategicMerge != nil { + for _, patchStrategicMerge := range k.PatchesStrategicMerge { + k.Patches = append(k.Patches, Patch{Patch: string(patchStrategicMerge)}) + } + k.PatchesStrategicMerge = nil + } + // this fix is not in FixKustomizationPostUnmarshalling because // it will break some commands like `create` and `add`. those // commands depend on 'commonLabels' field diff --git a/kustomize/commands/edit/fix/fix.go b/kustomize/commands/edit/fix/fix.go index eb1e260fb..69afb953b 100644 --- a/kustomize/commands/edit/fix/fix.go +++ b/kustomize/commands/edit/fix/fix.go @@ -68,12 +68,14 @@ func RunFix(fSys filesys.FileSystem, w io.Writer) error { fmt.Fprintln(w, ` Fixed fields: patchesJson6902 -> patches + patchesStrategicMerge -> patches commonLabels -> labels vars -> replacements`) } else { fmt.Fprintln(w, ` Fixed fields: patchesJson6902 -> patches + patchesStrategicMerge -> patches commonLabels -> labels To convert vars -> replacements, run the command `+"`kustomize edit fix --vars`"+` diff --git a/kustomize/commands/edit/fix/fix_test.go b/kustomize/commands/edit/fix/fix_test.go index 163d03baa..e6e1e5b0d 100644 --- a/kustomize/commands/edit/fix/fix_test.go +++ b/kustomize/commands/edit/fix/fix_test.go @@ -112,6 +112,107 @@ kind: Kustomization } } +func TestFixOutdatedPatchesStrategicMergeFieldTitle(t *testing.T) { + kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +patchesStrategicMerge: +- |- + apiVersion: apps/v1 + kind: Deployment + metadata: + name: nginx + spec: + template: + spec: + containers: + - name: nginx + image: nignx:latest +`) + + expected := []byte(` +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +patches: +- patch: |- + apiVersion: apps/v1 + kind: Deployment + metadata: + name: nginx + spec: + template: + spec: + containers: + - name: nginx + image: nignx:latest +`) + fSys := filesys.MakeFsInMemory() + testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) + cmd := NewCmdFix(fSys, os.Stdout) + assert.NoError(t, cmd.RunE(cmd, nil)) + + content, err := testutils_test.ReadTestKustomization(fSys) + assert.NoError(t, err) + assert.Contains(t, string(content), "apiVersion: ") + assert.Contains(t, string(content), "kind: Kustomization") + + if diff := cmp.Diff(expected, content); diff != "" { + t.Errorf("Mismatch (-expected, +actual):\n%s", diff) + } +} + +func TestFixAndMergeOutdatedPatchesStrategicMergeFieldTitle(t *testing.T) { + kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +patchesStrategicMerge: +- |- + apiVersion: apps/v1 + kind: Deployment + metadata: + name: nginx + spec: + template: + spec: + containers: + - name: nginx + image: nignx:latest +patches: +- path: patch2.yaml + target: + kind: Deployment +`) + + expected := []byte(` +patches: +- path: patch2.yaml + target: + kind: Deployment +- patch: |- + apiVersion: apps/v1 + kind: Deployment + metadata: + name: nginx + spec: + template: + spec: + containers: + - name: nginx + image: nignx:latest +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +`) + fSys := filesys.MakeFsInMemory() + testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedPatchesFieldTitle) + cmd := NewCmdFix(fSys, os.Stdout) + assert.NoError(t, cmd.RunE(cmd, nil)) + + content, err := testutils_test.ReadTestKustomization(fSys) + assert.NoError(t, err) + assert.Contains(t, string(content), "apiVersion: ") + assert.Contains(t, string(content), "kind: Kustomization") + + if diff := cmp.Diff(expected, content); diff != "" { + t.Errorf("Mismatch (-expected, +actual):\n%s", diff) + } +} + func TestFixOutdatedCommonLabels(t *testing.T) { kustomizationContentWithOutdatedCommonLabels := []byte(` commonLabels: