From bb7ebe029cf27fad48f0e6fbc68e0ff8804db490 Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Sat, 6 Aug 2022 20:02:42 +0000 Subject: [PATCH] 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 0aef52e2c..6c67d30fa 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 e6e1e5b0d..22ae64c5c 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: