From 64f42ea45f079ac011fcc32d9ce93c2a295d109a Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Fri, 29 Jul 2022 17:34:38 +0000 Subject: [PATCH 1/8] add edit-fix for patchesStrategicMerge to patches --- api/types/kustomization.go | 7 ++ kustomize/commands/edit/fix/fix.go | 2 + kustomize/commands/edit/fix/fix_test.go | 101 ++++++++++++++++++++++++ 3 files changed, 110 insertions(+) diff --git a/api/types/kustomization.go b/api/types/kustomization.go index c194dcc3c1..0aef52e2cb 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 eb1e260fb4..69afb953b6 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 163d03baad..e6e1e5b0da 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: From bb7ebe029cf27fad48f0e6fbc68e0ff8804db490 Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Sat, 6 Aug 2022 20:02:42 +0000 Subject: [PATCH 2/8] support file path patch to patchesStrategicMerge --- api/types/kustomization.go | 11 ++- kustomize/commands/edit/fix/fix_test.go | 105 ++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) diff --git a/api/types/kustomization.go b/api/types/kustomization.go index 0aef52e2cb..6c67d30fa9 100644 --- a/api/types/kustomization.go +++ b/api/types/kustomization.go @@ -7,6 +7,7 @@ import ( "bytes" "encoding/json" "fmt" + "strings" "sigs.k8s.io/yaml" ) @@ -224,7 +225,15 @@ func (k *Kustomization) FixKustomizationPreMarshalling() error { if k.PatchesStrategicMerge != nil { for _, patchStrategicMerge := range k.PatchesStrategicMerge { - k.Patches = append(k.Patches, Patch{Patch: string(patchStrategicMerge)}) + // check this patch is file path select. + if strings.Count(string(patchStrategicMerge), "\n") < 1 && + (patchStrategicMerge[len(patchStrategicMerge)-5:] == ".yaml" || patchStrategicMerge[len(patchStrategicMerge)-4:] == ".yml") { + // path patch + k.Patches = append(k.Patches, Patch{Path: string(patchStrategicMerge)}) + } else { + // inline string patch + k.Patches = append(k.Patches, Patch{Patch: string(patchStrategicMerge)}) + } } k.PatchesStrategicMerge = nil } diff --git a/kustomize/commands/edit/fix/fix_test.go b/kustomize/commands/edit/fix/fix_test.go index e6e1e5b0da..22ae64c5ca 100644 --- a/kustomize/commands/edit/fix/fix_test.go +++ b/kustomize/commands/edit/fix/fix_test.go @@ -213,6 +213,111 @@ kind: Kustomization } } +func TestFixOutdatedPatchesStrategicMergeToPathFieldTitle(t *testing.T) { + kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +patchesStrategicMerge: +- deploy.yaml +`) + + expected := []byte(` +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +patches: +- path: deploy.yaml +`) + 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 TestFixOutdatedPatchesStrategicMergeToPathFieldYMLTitle(t *testing.T) { + kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +patchesStrategicMerge: +- deploy.yml +`) + + expected := []byte(` +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +patches: +- path: deploy.yml +`) + 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 TestFixOutdatedPatchesStrategicMergeFieldPatchEndOfYamlTitle(t *testing.T) { + kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +patchesStrategicMerge: +- |- + apiVersion: apps/v1 + kind: Deployment + metadata: + name: nginx + spec: + template: + spec: + containers: + - name: nginx + env: + - name: CONFIG_FILE_PATH + value: home.yaml +`) + + 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 + env: + - name: CONFIG_FILE_PATH + value: home.yaml +`) + 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: From 3f0c21304ccb0190a99cbed9851dfb00a2ae12cd Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Sat, 6 Aug 2022 20:25:00 +0000 Subject: [PATCH 3/8] refactor edit fix test cases --- kustomize/commands/edit/fix/fix_test.go | 242 ++++++++---------------- 1 file changed, 77 insertions(+), 165 deletions(-) diff --git a/kustomize/commands/edit/fix/fix_test.go b/kustomize/commands/edit/fix/fix_test.go index 22ae64c5ca..6c4b79b5b1 100644 --- a/kustomize/commands/edit/fix/fix_test.go +++ b/kustomize/commands/edit/fix/fix_test.go @@ -27,8 +27,15 @@ func TestFix(t *testing.T) { assert.Contains(t, string(content), "kind: Kustomization") } -func TestFixOutdatedPatchesFieldTitle(t *testing.T) { - kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +func TestFixCommand(t *testing.T) { + tests := []struct { + name string + input string + expected string + }{ + { + name: "FixOutdatedPatchesFieldTitle", + input: ` patchesJson6902: - path: patch1.yaml target: @@ -38,9 +45,8 @@ patchesJson6902: group: apps kind: Deployment version: v1 -`) - - expected := []byte(` +`, + expected: ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization patches: @@ -52,24 +58,11 @@ patches: group: apps kind: Deployment version: v1 -`) - 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 TestRenameAndKeepOutdatedPatchesField(t *testing.T) { - kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +`, + }, + { + name: "TestRenameAndKeepOutdatedPatchesField", + input: ` patchesJson6902: - path: patch1.yaml target: @@ -81,9 +74,8 @@ patches: - path: patch3.yaml target: kind: Service -`) - - expected := []byte(` +`, + expected: ` patches: - path: patch2.yaml target: @@ -96,24 +88,11 @@ patches: kind: Deployment 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 TestFixOutdatedPatchesStrategicMergeFieldTitle(t *testing.T) { - kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +`, + }, + { + name: "TestFixOutdatedPatchesStrategicMergeFieldTitle", + input: ` patchesStrategicMerge: - |- apiVersion: apps/v1 @@ -126,9 +105,8 @@ patchesStrategicMerge: containers: - name: nginx image: nignx:latest -`) - - expected := []byte(` +`, + expected: ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization patches: @@ -143,24 +121,11 @@ patches: 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(` +`, + }, + { + name: "TestFixAndMergeOutdatedPatchesStrategicMergeFieldTitle", + input: ` patchesStrategicMerge: - |- apiVersion: apps/v1 @@ -177,9 +142,8 @@ patches: - path: patch2.yaml target: kind: Deployment -`) - - expected := []byte(` +`, + expected: ` patches: - path: patch2.yaml target: @@ -197,78 +161,37 @@ patches: 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 TestFixOutdatedPatchesStrategicMergeToPathFieldTitle(t *testing.T) { - kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +`, + }, + { + name: "TestFixOutdatedPatchesStrategicMergeToPathFieldTitle", + input: ` patchesStrategicMerge: - deploy.yaml -`) - - expected := []byte(` +`, + expected: ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization patches: - path: deploy.yaml -`) - 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 TestFixOutdatedPatchesStrategicMergeToPathFieldYMLTitle(t *testing.T) { - kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +`, + }, + { + name: "TestFixOutdatedPatchesStrategicMergeToPathFieldYMLTitle", + input: ` patchesStrategicMerge: - deploy.yml -`) - - expected := []byte(` +`, + expected: ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization patches: - path: deploy.yml -`) - 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 TestFixOutdatedPatchesStrategicMergeFieldPatchEndOfYamlTitle(t *testing.T) { - kustomizationContentWithOutdatedPatchesFieldTitle := []byte(` +`, + }, + { + name: "TestFixOutdatedPatchesStrategicMergeFieldPatchEndOfYamlTitle", + input: ` patchesStrategicMerge: - |- apiVersion: apps/v1 @@ -283,9 +206,8 @@ patchesStrategicMerge: env: - name: CONFIG_FILE_PATH value: home.yaml -`) - - expected := []byte(` +`, + expected: ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization patches: @@ -302,32 +224,18 @@ patches: env: - name: CONFIG_FILE_PATH value: home.yaml -`) - 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(` +`, + }, + { + name: "TestFixOutdatedCommonLabels", + input: ` commonLabels: foo: bar labels: - pairs: a: b -`) - - expected := []byte(` +`, + expected: ` labels: - pairs: a: b @@ -336,19 +244,23 @@ labels: foo: bar apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization -`) - fSys := filesys.MakeFsInMemory() - testutils_test.WriteTestKustomizationWith(fSys, kustomizationContentWithOutdatedCommonLabels) - 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) +`, + }, + } + for _, test := range tests { + fSys := filesys.MakeFsInMemory() + testutils_test.WriteTestKustomizationWith(fSys, []byte(test.input)) + 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([]byte(test.expected), content); diff != "" { + t.Errorf("%s: Mismatch (-expected, +actual):\n%s", test.name, diff) + } } } From b4d25b1b2604f43eb46266f2b9c4c6b0f923b0de Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Sun, 28 Aug 2022 15:45:47 +0000 Subject: [PATCH 4/8] add testcases --- kustomize/commands/edit/fix/fix_test.go | 68 +++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/kustomize/commands/edit/fix/fix_test.go b/kustomize/commands/edit/fix/fix_test.go index 6c4b79b5b1..8987f22a55 100644 --- a/kustomize/commands/edit/fix/fix_test.go +++ b/kustomize/commands/edit/fix/fix_test.go @@ -9,6 +9,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" testutils_test "sigs.k8s.io/kustomize/kustomize/v4/commands/internal/testutils" "sigs.k8s.io/kustomize/kyaml/filesys" ) @@ -187,6 +188,65 @@ apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization patches: - path: deploy.yml +`, + }, + { + name: "Test fix outdated patchesStrategicMerge from a file and one string literal", + input: ` +patchesStrategicMerge: +- deploy.yaml +- |- + apiVersion: apps/v1 + kind: Deployment + metadata: + name: nginx + spec: + template: + spec: + containers: + - name: nginx + env: + - name: CONFIG_FILE_PATH + value: home.yaml +`, + expected: ` +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +patches: +- path: deploy.yaml +- patch: |- + apiVersion: apps/v1 + kind: Deployment + metadata: + name: nginx + spec: + template: + spec: + containers: + - name: nginx + env: + - name: CONFIG_FILE_PATH + value: home.yaml +`, + }, + { + name: "Test fix outdated patchesStrategicMerge and patchesJson6902", + input: ` +patchesStrategicMerge: +- deploy.yaml +patchesJson6902: +- path: patch1.yaml + target: + kind: Deployment +`, + expected: ` +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +patches: +- path: patch1.yaml + target: + kind: Deployment +- path: deploy.yaml `, }, { @@ -251,12 +311,12 @@ kind: Kustomization fSys := filesys.MakeFsInMemory() testutils_test.WriteTestKustomizationWith(fSys, []byte(test.input)) cmd := NewCmdFix(fSys, os.Stdout) - assert.NoError(t, cmd.RunE(cmd, nil)) + require.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") + require.NoError(t, err) + require.Contains(t, string(content), "apiVersion: ") + require.Contains(t, string(content), "kind: Kustomization") if diff := cmp.Diff([]byte(test.expected), content); diff != "" { t.Errorf("%s: Mismatch (-expected, +actual):\n%s", test.name, diff) From 032bf3338e92b4a81e3b00e73689150f663c6f4c Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Sun, 28 Aug 2022 16:53:36 +0000 Subject: [PATCH 5/8] update checker code in patchStrategicMerge --- api/types/kustomization.go | 7 ++- kustomize/commands/edit/fix/fix.go | 3 +- kustomize/commands/edit/fix/fix_test.go | 64 +++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/api/types/kustomization.go b/api/types/kustomization.go index 6c67d30fa9..4e7ac9e878 100644 --- a/api/types/kustomization.go +++ b/api/types/kustomization.go @@ -7,8 +7,8 @@ import ( "bytes" "encoding/json" "fmt" - "strings" + "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/yaml" ) @@ -218,7 +218,7 @@ func (k *Kustomization) FixKustomizationPostUnmarshalling() { // FixKustomizationPreMarshalling fixes things // that should occur after the kustomization file // has been processed. -func (k *Kustomization) FixKustomizationPreMarshalling() error { +func (k *Kustomization) FixKustomizationPreMarshalling(fSys filesys.FileSystem) error { // PatchesJson6902 should be under the Patches field. k.Patches = append(k.Patches, k.PatchesJson6902...) k.PatchesJson6902 = nil @@ -226,8 +226,7 @@ func (k *Kustomization) FixKustomizationPreMarshalling() error { if k.PatchesStrategicMerge != nil { for _, patchStrategicMerge := range k.PatchesStrategicMerge { // check this patch is file path select. - if strings.Count(string(patchStrategicMerge), "\n") < 1 && - (patchStrategicMerge[len(patchStrategicMerge)-5:] == ".yaml" || patchStrategicMerge[len(patchStrategicMerge)-4:] == ".yml") { + if _, err := fSys.ReadFile(string(patchStrategicMerge)); err == nil { // path patch k.Patches = append(k.Patches, Patch{Path: string(patchStrategicMerge)}) } else { diff --git a/kustomize/commands/edit/fix/fix.go b/kustomize/commands/edit/fix/fix.go index 69afb953b6..ba05655773 100644 --- a/kustomize/commands/edit/fix/fix.go +++ b/kustomize/commands/edit/fix/fix.go @@ -55,8 +55,7 @@ func RunFix(fSys filesys.FileSystem, w io.Writer) error { return err } - err = m.FixKustomizationPreMarshalling() - if err != nil { + if err := m.FixKustomizationPreMarshalling(fSys); err != nil { return err } diff --git a/kustomize/commands/edit/fix/fix_test.go b/kustomize/commands/edit/fix/fix_test.go index 8987f22a55..2a38b6b0ba 100644 --- a/kustomize/commands/edit/fix/fix_test.go +++ b/kustomize/commands/edit/fix/fix_test.go @@ -32,6 +32,7 @@ func TestFixCommand(t *testing.T) { tests := []struct { name string input string + files map[string]string expected string }{ { @@ -170,6 +171,21 @@ kind: Kustomization patchesStrategicMerge: - deploy.yaml `, + files: map[string]string{ + "deploy.yaml": ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx +spec: + template: + spec: + containers: + - name: nginx + env: + - name: CONFIG_FILE_PATH + value: home.yaml`, + }, expected: ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization @@ -183,6 +199,21 @@ patches: patchesStrategicMerge: - deploy.yml `, + files: map[string]string{ + "deploy.yml": ` +apiVersion: apps/v1 +kind: Deployment +metadata: +name: nginx +spec: +template: +spec: +containers: + - name: nginx + env: + - name: CONFIG_FILE_PATH + value: home.yaml`, + }, expected: ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization @@ -209,6 +240,21 @@ patchesStrategicMerge: - name: CONFIG_FILE_PATH value: home.yaml `, + files: map[string]string{ + "deploy.yaml": ` +apiVersion: apps/v1 +kind: Deployment +metadata: +name: nginx +spec: +template: +spec: +containers: +- name: nginx +env: +- name: CONFIG_FILE_PATH + value: home.yaml`, + }, expected: ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization @@ -239,6 +285,21 @@ patchesJson6902: target: kind: Deployment `, + files: map[string]string{ + "deploy.yaml": ` +apiVersion: apps/v1 +kind: Deployment +metadata: +name: nginx +spec: +template: +spec: +containers: +- name: nginx +env: +- name: CONFIG_FILE_PATH + value: home.yaml`, + }, expected: ` apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization @@ -310,6 +371,9 @@ kind: Kustomization for _, test := range tests { fSys := filesys.MakeFsInMemory() testutils_test.WriteTestKustomizationWith(fSys, []byte(test.input)) + for filename, content := range test.files { + require.NoError(t, fSys.WriteFile(filename, []byte(content))) + } cmd := NewCmdFix(fSys, os.Stdout) require.NoError(t, cmd.RunE(cmd, nil)) From cbb61fc6681c80d3b380894073c415f0b4a184f1 Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Thu, 1 Sep 2022 22:41:24 +0000 Subject: [PATCH 6/8] fix yaml in test --- kustomize/commands/edit/fix/fix_test.go | 48 ++++++++++++------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/kustomize/commands/edit/fix/fix_test.go b/kustomize/commands/edit/fix/fix_test.go index 2a38b6b0ba..85df3c3df9 100644 --- a/kustomize/commands/edit/fix/fix_test.go +++ b/kustomize/commands/edit/fix/fix_test.go @@ -204,15 +204,15 @@ patchesStrategicMerge: apiVersion: apps/v1 kind: Deployment metadata: -name: nginx -spec: -template: + name: nginx spec: -containers: - - name: nginx - env: - - name: CONFIG_FILE_PATH - value: home.yaml`, + template: + spec: + containers: + - name: nginx + env: + - name: CONFIG_FILE_PATH + value: home.yaml`, }, expected: ` apiVersion: kustomize.config.k8s.io/v1beta1 @@ -245,15 +245,15 @@ patchesStrategicMerge: apiVersion: apps/v1 kind: Deployment metadata: -name: nginx -spec: -template: + name: nginx spec: -containers: -- name: nginx -env: -- name: CONFIG_FILE_PATH - value: home.yaml`, + template: + spec: + containers: + - name: nginx + env: + - name: CONFIG_FILE_PATH + value: home.yaml`, }, expected: ` apiVersion: kustomize.config.k8s.io/v1beta1 @@ -290,15 +290,15 @@ patchesJson6902: apiVersion: apps/v1 kind: Deployment metadata: -name: nginx -spec: -template: + name: nginx spec: -containers: -- name: nginx -env: -- name: CONFIG_FILE_PATH - value: home.yaml`, + template: + spec: + containers: + - name: nginx + env: + - name: CONFIG_FILE_PATH + value: home.yaml`, }, expected: ` apiVersion: kustomize.config.k8s.io/v1beta1 From 7c2e8845ad1127c5dc52cc7c8450ac3bb2ddaa46 Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Mon, 12 Sep 2022 17:47:33 +0000 Subject: [PATCH 7/8] fix test with t.Run() --- kustomize/commands/edit/fix/fix_test.go | 28 ++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/kustomize/commands/edit/fix/fix_test.go b/kustomize/commands/edit/fix/fix_test.go index 85df3c3df9..fc6cced289 100644 --- a/kustomize/commands/edit/fix/fix_test.go +++ b/kustomize/commands/edit/fix/fix_test.go @@ -369,22 +369,22 @@ kind: Kustomization }, } for _, test := range tests { - fSys := filesys.MakeFsInMemory() - testutils_test.WriteTestKustomizationWith(fSys, []byte(test.input)) - for filename, content := range test.files { - require.NoError(t, fSys.WriteFile(filename, []byte(content))) - } - cmd := NewCmdFix(fSys, os.Stdout) - require.NoError(t, cmd.RunE(cmd, nil)) + t.Run(test.name, func(t *testing.T) { + fSys := filesys.MakeFsInMemory() + testutils_test.WriteTestKustomizationWith(fSys, []byte(test.input)) + for filename, content := range test.files { + require.NoError(t, fSys.WriteFile(filename, []byte(content))) + } + cmd := NewCmdFix(fSys, os.Stdout) + require.NoError(t, cmd.RunE(cmd, nil)) - content, err := testutils_test.ReadTestKustomization(fSys) - require.NoError(t, err) - require.Contains(t, string(content), "apiVersion: ") - require.Contains(t, string(content), "kind: Kustomization") + content, err := testutils_test.ReadTestKustomization(fSys) + require.NoError(t, err) + require.Contains(t, string(content), "apiVersion: ") + require.Contains(t, string(content), "kind: Kustomization") - if diff := cmp.Diff([]byte(test.expected), content); diff != "" { - t.Errorf("%s: Mismatch (-expected, +actual):\n%s", test.name, diff) - } + require.Empty(t, cmp.Diff([]byte(test.expected), content)) + }) } } From 0d68e0c7be168df1193d14287faf24ccbbcdc62d Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Mon, 12 Sep 2022 17:56:11 +0000 Subject: [PATCH 8/8] add newline for fix.go --- kustomize/commands/edit/fix/fix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kustomize/commands/edit/fix/fix.go b/kustomize/commands/edit/fix/fix.go index ba05655773..bae67811d4 100644 --- a/kustomize/commands/edit/fix/fix.go +++ b/kustomize/commands/edit/fix/fix.go @@ -90,7 +90,7 @@ We recommend doing this in a clean git repository where the change is easy to un fixedBuildCmd := build.NewCmdBuild(fSys, build.MakeHelp(konfig.ProgramName, "build"), &fixedOutput) err = fixedBuildCmd.RunE(fixedBuildCmd, nil) if err != nil { - fmt.Fprintf(w, "Warning: 'Fixed' kustomization now produces the error when running `kustomize build`: %s", err.Error()) + fmt.Fprintf(w, "Warning: 'Fixed' kustomization now produces the error when running `kustomize build`: %s\n", err.Error()) } else if fixedOutput.String() != oldOutput.String() { fmt.Fprintf(w, "Warning: 'Fixed' kustomization now produces different output when running `kustomize build`:\n...%s...\n", fixedOutput.String()) }