From 64f42ea45f079ac011fcc32d9ce93c2a295d109a Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Fri, 29 Jul 2022 17:34:38 +0000 Subject: [PATCH 01/15] 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 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: From bb7ebe029cf27fad48f0e6fbc68e0ff8804db490 Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Sat, 6 Aug 2022 20:02:42 +0000 Subject: [PATCH 02/15] 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: From 3f0c21304ccb0190a99cbed9851dfb00a2ae12cd Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Sat, 6 Aug 2022 20:25:00 +0000 Subject: [PATCH 03/15] refactor edit fix test cases --- kustomize/commands/edit/fix/fix_test.go | 238 ++++++++---------------- 1 file changed, 75 insertions(+), 163 deletions(-) diff --git a/kustomize/commands/edit/fix/fix_test.go b/kustomize/commands/edit/fix/fix_test.go index 22ae64c5c..6c4b79b5b 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)) +`, + }, + } + 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") + 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) + 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 04/15] 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 6c4b79b5b..8987f22a5 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 05/15] 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 6c67d30fa..4e7ac9e87 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 69afb953b..ba0565577 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 8987f22a5..2a38b6b0b 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 06/15] 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 2a38b6b0b..85df3c3df 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 + name: nginx spec: -template: -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 + name: nginx spec: -template: -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 + name: nginx spec: -template: -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 07/15] 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 85df3c3df..fc6cced28 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 08/15] 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 ba0565577..bae67811d 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()) } From 0fbaa2d0e8d400f5f3a297e6e62667053b0220ca Mon Sep 17 00:00:00 2001 From: Florian Lehner Date: Thu, 6 Oct 2022 05:37:41 +0200 Subject: [PATCH 09/15] kyaml/sets: preallocate memory Signed-off-by: Florian Lehner --- kyaml/sets/string.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kyaml/sets/string.go b/kyaml/sets/string.go index 07f02afb0..d590e5328 100644 --- a/kyaml/sets/string.go +++ b/kyaml/sets/string.go @@ -10,7 +10,7 @@ func (s String) Len() int { } func (s String) List() []string { - var val []string + val := make([]string, 0, len(s)) for k := range s { val = append(val, k) } From c2312c4018fe03bb6331846de5d1336938993f44 Mon Sep 17 00:00:00 2001 From: Florian Lehner Date: Thu, 6 Oct 2022 05:38:18 +0200 Subject: [PATCH 10/15] kyaml/yaml: preallocate memory Signed-off-by: Florian Lehner --- kyaml/yaml/filters.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kyaml/yaml/filters.go b/kyaml/yaml/filters.go index e364035e6..e7d4b5f7b 100644 --- a/kyaml/yaml/filters.go +++ b/kyaml/yaml/filters.go @@ -68,7 +68,7 @@ func (y *YFilter) UnmarshalYAML(unmarshal func(interface{}) error) error { type YFilters []YFilter func (y YFilters) Filters() []Filter { - var f []Filter + f := make([]Filter, 0, len(y)) for i := range y { f = append(f, y[i].Filter) } From 1b1e6ccab0a740a34ef7ec4c2524e9658f60961d Mon Sep 17 00:00:00 2001 From: Ahmed AbouZaid Date: Sun, 9 Oct 2022 15:16:47 +0200 Subject: [PATCH 11/15] Test transformers krm exec function --- api/krusty/fnplugin_test.go | 126 +++++++++++++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/api/krusty/fnplugin_test.go b/api/krusty/fnplugin_test.go index a4be95d55..81f6f3c82 100644 --- a/api/krusty/fnplugin_test.go +++ b/api/krusty/fnplugin_test.go @@ -41,6 +41,12 @@ spec: EOF ` +const krmEchoDotSh = `#!/bin/bash + +resourceList=$(cat) +echo "$resourceList" +` + func TestFnExecGenerator(t *testing.T) { fSys := filesys.MakeFsOnDisk() @@ -126,7 +132,7 @@ spec: assert.NoError(t, fSys.RemoveAll(tmpDir.String())) } -func TestFnExecGeneratorWithOverlay(t *testing.T) { +func TestFnExecGeneratorInOverlay(t *testing.T) { fSys := filesys.MakeFsOnDisk() th := kusttest_test.MakeHarnessWithFs(t, fSys) @@ -217,6 +223,124 @@ spec: assert.NoError(t, fSys.RemoveAll(tmpDir.String())) } +func TestFnExecTransformer(t *testing.T) { + fSys := filesys.MakeFsOnDisk() + + th := kusttest_test.MakeHarnessWithFs(t, fSys) + o := th.MakeOptionsPluginsEnabled() + o.PluginConfig.FnpLoadingOptions.EnableExec = true + + tmpDir, err := filesys.NewTmpConfirmedDir() + assert.NoError(t, err) + base := filepath.Join(tmpDir.String(), "base") + assert.NoError(t, fSys.Mkdir(base)) + th.WriteK(base, ` +resources: +- secret.yaml +transformers: +- krm-echo.yaml +`) + th.WriteF(filepath.Join(base, "secret.yaml"), + ` +apiVersion: v1 +kind: Secret +metadata: + name: dummy +type: Opaque +stringData: + foo: bar +`) + th.WriteF(filepath.Join(base, "krmEcho.sh"), krmEchoDotSh) + + assert.NoError(t, os.Chmod(filepath.Join(base, "krmEcho.sh"), 0777)) + th.WriteF(filepath.Join(base, "krm-echo.yaml"), ` +apiVersion: examples.config.kubernetes.io/v1beta1 +kind: MyPlugin +metadata: + name: notImportantHere + annotations: + config.kubernetes.io/function: | + exec: + path: ./krmEcho.sh +`) + + m := th.Run(base, o) + assert.NoError(t, err) + yml, err := m.AsYaml() + assert.NoError(t, err) + assert.Equal(t, `apiVersion: v1 +kind: Secret +metadata: + name: dummy +stringData: + foo: bar +type: Opaque +`, string(yml)) + assert.NoError(t, fSys.RemoveAll(tmpDir.String())) +} + +func TestFnExecTransformerInOverlay(t *testing.T) { + fSys := filesys.MakeFsOnDisk() + + th := kusttest_test.MakeHarnessWithFs(t, fSys) + o := th.MakeOptionsPluginsEnabled() + o.PluginConfig.FnpLoadingOptions.EnableExec = true + + tmpDir, err := filesys.NewTmpConfirmedDir() + assert.NoError(t, err) + base := filepath.Join(tmpDir.String(), "base") + prod := filepath.Join(tmpDir.String(), "prod") + assert.NoError(t, fSys.Mkdir(base)) + assert.NoError(t, fSys.Mkdir(prod)) + th.WriteK(base, ` +resources: +- secret.yaml +`) + th.WriteK(prod, ` +resources: +- ../base +transformers: +- krm-echo.yaml +`) + th.WriteF(filepath.Join(base, "secret.yaml"), + ` +apiVersion: v1 +kind: Secret +metadata: + name: dummy +type: Opaque +stringData: + foo: bar +`) + th.WriteF(filepath.Join(prod, "krmEcho.sh"), krmEchoDotSh) + + assert.NoError(t, os.Chmod(filepath.Join(prod, "krmEcho.sh"), 0777)) + th.WriteF(filepath.Join(prod, "krm-echo.yaml"), ` +apiVersion: examples.config.kubernetes.io/v1beta1 +kind: MyPlugin +metadata: + name: notImportantHere + annotations: + config.kubernetes.io/function: | + exec: + path: ./krmEcho.sh +`) + + m := th.Run(prod, o) + assert.NoError(t, err) + yml, err := m.AsYaml() + assert.NoError(t, err) + assert.Equal(t, `apiVersion: v1 +kind: Secret +metadata: + name: dummy +stringData: + foo: bar +type: Opaque +`, string(yml)) + assert.NoError(t, fSys.RemoveAll(tmpDir.String())) +} + func skipIfNoDocker(t *testing.T) { t.Helper() if _, err := exec.LookPath("docker"); err != nil { From d29febecb71aebc7b6db89e4e6071ed0ccdfdc7f Mon Sep 17 00:00:00 2001 From: Ahmed AbouZaid Date: Sun, 9 Oct 2022 15:22:42 +0200 Subject: [PATCH 12/15] Fix krm exec function working dir --- api/internal/plugins/loader/loader.go | 14 ++++++++++++-- api/internal/plugins/loader/loader_test.go | 14 ++++++++++++++ api/internal/target/kusttarget.go | 4 +++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/api/internal/plugins/loader/loader.go b/api/internal/plugins/loader/loader.go index 26441ed51..36d929d7d 100644 --- a/api/internal/plugins/loader/loader.go +++ b/api/internal/plugins/loader/loader.go @@ -47,8 +47,18 @@ func (l *Loader) Config() *types.PluginConfig { return l.pc } -// SetWorkDir sets the working directory for this loader's plugins -func (l *Loader) SetWorkDir(wd string) { +// DeepCopyPluginConfig makes a full copy the actual values of PluginConfig. +func (l *Loader) DeepCopyPluginConfig() { + l.pc = &types.PluginConfig{ + PluginRestrictions: l.pc.PluginRestrictions, + BpLoadingOptions: l.pc.BpLoadingOptions, + FnpLoadingOptions: l.pc.FnpLoadingOptions, + HelmConfig: l.pc.HelmConfig, + } +} + +// SetPluginConfigWorkingDir sets the working directory for the loader's plugins. +func (l *Loader) SetPluginConfigWorkingDir(wd string) { l.pc.FnpLoadingOptions.WorkingDir = wd } diff --git a/api/internal/plugins/loader/loader_test.go b/api/internal/plugins/loader/loader_test.go index c5e30b808..ba40762b8 100644 --- a/api/internal/plugins/loader/loader_test.go +++ b/api/internal/plugins/loader/loader_test.go @@ -78,3 +78,17 @@ func TestLoader(t *testing.T) { } } } + +func TestLoaderSetPluginConfigWorkingDir(t *testing.T) { + p := provider.NewDefaultDepProvider() + rmF := resmap.NewFactory(p.GetResourceFactory()) + fsys := filesys.MakeFsInMemory() + c := types.EnabledPluginConfig(types.BploLoadFromFileSys) + pLdr := NewLoader(c, rmF, fsys) + pLdrCopy := *pLdr + pLdrCopy.DeepCopyPluginConfig() + pLdrCopy.SetPluginConfigWorkingDir("/tmp/dummy") + if pLdrCopy.Config().FnpLoadingOptions.WorkingDir != "/tmp/dummy" { + t.Fatal("plugin working dir is not set correctly") + } +} diff --git a/api/internal/target/kusttarget.go b/api/internal/target/kusttarget.go index 4bbf7e98a..17318c396 100644 --- a/api/internal/target/kusttarget.go +++ b/api/internal/target/kusttarget.go @@ -45,7 +45,9 @@ func NewKustTarget( rFactory *resmap.Factory, pLdr *loader.Loader) *KustTarget { pLdrCopy := *pLdr - pLdrCopy.SetWorkDir(ldr.Root()) + pLdrCopy.DeepCopyPluginConfig() + pLdrCopy.SetPluginConfigWorkingDir(ldr.Root()) + return &KustTarget{ ldr: ldr, validator: validator, From ea21b37d67f27c22f2d8fce76794bbe4b8a21d4d Mon Sep 17 00:00:00 2001 From: Ahmed AbouZaid Date: Sat, 15 Oct 2022 16:45:50 +0200 Subject: [PATCH 13/15] proposal v2 --- api/internal/plugins/loader/loader.go | 20 +++++++++----------- api/internal/plugins/loader/loader_test.go | 14 +++++++------- api/internal/target/kusttarget.go | 6 +----- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/api/internal/plugins/loader/loader.go b/api/internal/plugins/loader/loader.go index 36d929d7d..43b11d6ff 100644 --- a/api/internal/plugins/loader/loader.go +++ b/api/internal/plugins/loader/loader.go @@ -42,24 +42,22 @@ func NewLoader( return &Loader{pc: pc, rf: rf, fs: fs} } -// Config provides the global (not plugin specific) PluginConfig data. -func (l *Loader) Config() *types.PluginConfig { - return l.pc -} - -// DeepCopyPluginConfig makes a full copy the actual values of PluginConfig. -func (l *Loader) DeepCopyPluginConfig() { - l.pc = &types.PluginConfig{ +// LoaderWithWorkingDir returns loader after setting its working directory. +// NOTE: This is not really a new loader since some of the Loader struct fields are pointers. +func (l *Loader) LoaderWithWorkingDir(wd string) *Loader { + lpc := &types.PluginConfig{ PluginRestrictions: l.pc.PluginRestrictions, BpLoadingOptions: l.pc.BpLoadingOptions, FnpLoadingOptions: l.pc.FnpLoadingOptions, HelmConfig: l.pc.HelmConfig, } + lpc.FnpLoadingOptions.WorkingDir = wd + return &Loader{pc: lpc, rf: l.rf, fs: l.fs} } -// SetPluginConfigWorkingDir sets the working directory for the loader's plugins. -func (l *Loader) SetPluginConfigWorkingDir(wd string) { - l.pc.FnpLoadingOptions.WorkingDir = wd +// Config provides the global (not plugin specific) PluginConfig data. +func (l *Loader) Config() *types.PluginConfig { + return l.pc } func (l *Loader) LoadGenerators( diff --git a/api/internal/plugins/loader/loader_test.go b/api/internal/plugins/loader/loader_test.go index ba40762b8..53992db52 100644 --- a/api/internal/plugins/loader/loader_test.go +++ b/api/internal/plugins/loader/loader_test.go @@ -6,6 +6,7 @@ package loader_test import ( "testing" + "github.com/stretchr/testify/require" . "sigs.k8s.io/kustomize/api/internal/plugins/loader" "sigs.k8s.io/kustomize/api/loader" "sigs.k8s.io/kustomize/api/provider" @@ -79,16 +80,15 @@ func TestLoader(t *testing.T) { } } -func TestLoaderSetPluginConfigWorkingDir(t *testing.T) { +func TestLoaderWithWorkingDir(t *testing.T) { p := provider.NewDefaultDepProvider() rmF := resmap.NewFactory(p.GetResourceFactory()) fsys := filesys.MakeFsInMemory() c := types.EnabledPluginConfig(types.BploLoadFromFileSys) pLdr := NewLoader(c, rmF, fsys) - pLdrCopy := *pLdr - pLdrCopy.DeepCopyPluginConfig() - pLdrCopy.SetPluginConfigWorkingDir("/tmp/dummy") - if pLdrCopy.Config().FnpLoadingOptions.WorkingDir != "/tmp/dummy" { - t.Fatal("plugin working dir is not set correctly") - } + npLdr := pLdr.LoaderWithWorkingDir("/tmp/dummy") + require.Equal(t, + "/tmp/dummy", + npLdr.Config().FnpLoadingOptions.WorkingDir, + "plugin working dir is not set correctly") } diff --git a/api/internal/target/kusttarget.go b/api/internal/target/kusttarget.go index 17318c396..d5d7c1474 100644 --- a/api/internal/target/kusttarget.go +++ b/api/internal/target/kusttarget.go @@ -44,15 +44,11 @@ func NewKustTarget( validator ifc.Validator, rFactory *resmap.Factory, pLdr *loader.Loader) *KustTarget { - pLdrCopy := *pLdr - pLdrCopy.DeepCopyPluginConfig() - pLdrCopy.SetPluginConfigWorkingDir(ldr.Root()) - return &KustTarget{ ldr: ldr, validator: validator, rFactory: rFactory, - pLdr: &pLdrCopy, + pLdr: pLdr.LoaderWithWorkingDir(ldr.Root()), } } From 3e447da6ef302a24d24b83794f7ccff0c75081af Mon Sep 17 00:00:00 2001 From: Ahmed AbouZaid Date: Sat, 22 Oct 2022 02:50:28 +0200 Subject: [PATCH 14/15] proposal v2.1 --- api/internal/plugins/loader/loader_test.go | 6 +- api/krusty/fnplugin_test.go | 197 ++++++++++++++++++--- 2 files changed, 181 insertions(+), 22 deletions(-) diff --git a/api/internal/plugins/loader/loader_test.go b/api/internal/plugins/loader/loader_test.go index 53992db52..23cf55cb9 100644 --- a/api/internal/plugins/loader/loader_test.go +++ b/api/internal/plugins/loader/loader_test.go @@ -87,8 +87,12 @@ func TestLoaderWithWorkingDir(t *testing.T) { c := types.EnabledPluginConfig(types.BploLoadFromFileSys) pLdr := NewLoader(c, rmF, fsys) npLdr := pLdr.LoaderWithWorkingDir("/tmp/dummy") + require.Equal(t, + "", + pLdr.Config().FnpLoadingOptions.WorkingDir, + "the plugin working dir should not change") require.Equal(t, "/tmp/dummy", npLdr.Config().FnpLoadingOptions.WorkingDir, - "plugin working dir is not set correctly") + "the plugin working dir is not updated") } diff --git a/api/krusty/fnplugin_test.go b/api/krusty/fnplugin_test.go index 81f6f3c82..40f63b642 100644 --- a/api/krusty/fnplugin_test.go +++ b/api/krusty/fnplugin_test.go @@ -41,13 +41,19 @@ spec: EOF ` -const krmEchoDotSh = `#!/bin/bash - -resourceList=$(cat) -echo "$resourceList" +const krmTransformerDotSh = `#!/bin/bash +cat << EOF +apiVersion: v1 +kind: Secret +metadata: + name: dummyTransformed +stringData: + foo: bar +type: Opaque +EOF ` -func TestFnExecGenerator(t *testing.T) { +func TestFnExecGeneratorInBase(t *testing.T) { fSys := filesys.MakeFsOnDisk() th := kusttest_test.MakeHarnessWithFs(t, fSys) @@ -93,7 +99,6 @@ spec: `) m := th.Run(tmpDir.String(), o) - assert.NoError(t, err) yml, err := m.AsYaml() assert.NoError(t, err) assert.Equal(t, `apiVersion: v1 @@ -132,7 +137,7 @@ spec: assert.NoError(t, fSys.RemoveAll(tmpDir.String())) } -func TestFnExecGeneratorInOverlay(t *testing.T) { +func TestFnExecGeneratorInBaseWithOverlay(t *testing.T) { fSys := filesys.MakeFsOnDisk() th := kusttest_test.MakeHarnessWithFs(t, fSys) @@ -223,7 +228,98 @@ spec: assert.NoError(t, fSys.RemoveAll(tmpDir.String())) } -func TestFnExecTransformer(t *testing.T) { +func TestFnExecGeneratorInOverlay(t *testing.T) { + fSys := filesys.MakeFsOnDisk() + + th := kusttest_test.MakeHarnessWithFs(t, fSys) + o := th.MakeOptionsPluginsEnabled() + o.PluginConfig.FnpLoadingOptions.EnableExec = true + + tmpDir, err := filesys.NewTmpConfirmedDir() + assert.NoError(t, err) + base := filepath.Join(tmpDir.String(), "base") + prod := filepath.Join(tmpDir.String(), "prod") + assert.NoError(t, fSys.Mkdir(base)) + assert.NoError(t, fSys.Mkdir(prod)) + th.WriteK(base, ` +resources: +- short_secret.yaml +`) + th.WriteK(prod, ` +resources: +- ../base +generators: +- gener.yaml +`) + th.WriteF(filepath.Join(base, "short_secret.yaml"), + ` +apiVersion: v1 +kind: Secret +metadata: + labels: + airshipit.org/ephemeral-user-data: "true" + name: node1-bmc-secret +type: Opaque +stringData: + userData: | + bootcmd: + - mkdir /mnt/vda +`) + th.WriteF(filepath.Join(prod, "generateDeployment.sh"), generateDeploymentDotSh) + + assert.NoError(t, os.Chmod(filepath.Join(prod, "generateDeployment.sh"), 0777)) + th.WriteF(filepath.Join(prod, "gener.yaml"), ` +kind: executable +metadata: + name: demo + annotations: + config.kubernetes.io/function: | + exec: + path: ./generateDeployment.sh +spec: +`) + + m := th.Run(prod, o) + assert.NoError(t, err) + yml, err := m.AsYaml() + assert.NoError(t, err) + assert.Equal(t, `apiVersion: v1 +kind: Secret +metadata: + labels: + airshipit.org/ephemeral-user-data: "true" + name: node1-bmc-secret +stringData: + userData: | + bootcmd: + - mkdir /mnt/vda +type: Opaque +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + tshirt-size: small + labels: + app: nginx + name: nginx +spec: + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - image: nginx + name: nginx +`, string(yml)) + assert.NoError(t, fSys.RemoveAll(tmpDir.String())) +} + +func TestFnExecTransformerInBase(t *testing.T) { fSys := filesys.MakeFsOnDisk() th := kusttest_test.MakeHarnessWithFs(t, fSys) @@ -238,7 +334,7 @@ func TestFnExecTransformer(t *testing.T) { resources: - secret.yaml transformers: -- krm-echo.yaml +- krm-transformer.yaml `) th.WriteF(filepath.Join(base, "secret.yaml"), ` @@ -250,10 +346,10 @@ type: Opaque stringData: foo: bar `) - th.WriteF(filepath.Join(base, "krmEcho.sh"), krmEchoDotSh) + th.WriteF(filepath.Join(base, "krmTransformer.sh"), krmTransformerDotSh) - assert.NoError(t, os.Chmod(filepath.Join(base, "krmEcho.sh"), 0777)) - th.WriteF(filepath.Join(base, "krm-echo.yaml"), ` + assert.NoError(t, os.Chmod(filepath.Join(base, "krmTransformer.sh"), 0777)) + th.WriteF(filepath.Join(base, "krm-transformer.yaml"), ` apiVersion: examples.config.kubernetes.io/v1beta1 kind: MyPlugin metadata: @@ -261,17 +357,77 @@ metadata: annotations: config.kubernetes.io/function: | exec: - path: ./krmEcho.sh + path: ./krmTransformer.sh `) m := th.Run(base, o) - assert.NoError(t, err) yml, err := m.AsYaml() assert.NoError(t, err) assert.Equal(t, `apiVersion: v1 kind: Secret +metadata: + name: dummyTransformed +stringData: + foo: bar +type: Opaque +`, string(yml)) + assert.NoError(t, fSys.RemoveAll(tmpDir.String())) +} + +func TestFnExecTransformerInBaseWithOverlay(t *testing.T) { + fSys := filesys.MakeFsOnDisk() + + th := kusttest_test.MakeHarnessWithFs(t, fSys) + o := th.MakeOptionsPluginsEnabled() + o.PluginConfig.FnpLoadingOptions.EnableExec = true + + tmpDir, err := filesys.NewTmpConfirmedDir() + assert.NoError(t, err) + base := filepath.Join(tmpDir.String(), "base") + prod := filepath.Join(tmpDir.String(), "prod") + assert.NoError(t, fSys.Mkdir(base)) + assert.NoError(t, fSys.Mkdir(prod)) + th.WriteK(base, ` +resources: +- secret.yaml +transformers: +- krm-transformer.yaml +`) + th.WriteK(prod, ` +resources: +- ../base +`) + th.WriteF(filepath.Join(base, "secret.yaml"), + ` +apiVersion: v1 +kind: Secret metadata: name: dummy +type: Opaque +stringData: + foo: bar +`) + th.WriteF(filepath.Join(base, "krmTransformer.sh"), krmTransformerDotSh) + + assert.NoError(t, os.Chmod(filepath.Join(base, "krmTransformer.sh"), 0777)) + th.WriteF(filepath.Join(base, "krm-transformer.yaml"), ` +apiVersion: examples.config.kubernetes.io/v1beta1 +kind: MyPlugin +metadata: + name: notImportantHere + annotations: + config.kubernetes.io/function: | + exec: + path: ./krmTransformer.sh +`) + + m := th.Run(prod, o) + yml, err := m.AsYaml() + assert.NoError(t, err) + assert.Equal(t, `apiVersion: v1 +kind: Secret +metadata: + name: dummyTransformed stringData: foo: bar type: Opaque @@ -300,7 +456,7 @@ resources: resources: - ../base transformers: -- krm-echo.yaml +- krm-transformer.yaml `) th.WriteF(filepath.Join(base, "secret.yaml"), ` @@ -312,10 +468,10 @@ type: Opaque stringData: foo: bar `) - th.WriteF(filepath.Join(prod, "krmEcho.sh"), krmEchoDotSh) + th.WriteF(filepath.Join(prod, "krmTransformer.sh"), krmTransformerDotSh) - assert.NoError(t, os.Chmod(filepath.Join(prod, "krmEcho.sh"), 0777)) - th.WriteF(filepath.Join(prod, "krm-echo.yaml"), ` + assert.NoError(t, os.Chmod(filepath.Join(prod, "krmTransformer.sh"), 0777)) + th.WriteF(filepath.Join(prod, "krm-transformer.yaml"), ` apiVersion: examples.config.kubernetes.io/v1beta1 kind: MyPlugin metadata: @@ -323,17 +479,16 @@ metadata: annotations: config.kubernetes.io/function: | exec: - path: ./krmEcho.sh + path: ./krmTransformer.sh `) m := th.Run(prod, o) - assert.NoError(t, err) yml, err := m.AsYaml() assert.NoError(t, err) assert.Equal(t, `apiVersion: v1 kind: Secret metadata: - name: dummy + name: dummyTransformed stringData: foo: bar type: Opaque From 997e6fcc63c7fb3c2ada62e2981c9adf19c4eac7 Mon Sep 17 00:00:00 2001 From: Natasha Sarkar Date: Mon, 31 Oct 2022 12:58:44 -0500 Subject: [PATCH 15/15] fix `TestRemoteLoad_LocalProtocol` (#4844) * remote load test fix * fix spacing --- api/krusty/remoteloader_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/api/krusty/remoteloader_test.go b/api/krusty/remoteloader_test.go index e5ee3ded6..5a7950dab 100644 --- a/api/krusty/remoteloader_test.go +++ b/api/krusty/remoteloader_test.go @@ -62,6 +62,7 @@ export GIT_COMMITTER_NAME=Nobody cp -r testdata/remoteload/simple $ROOT/simple.git ( cd $ROOT/simple.git + git config --global protocol.file.allow always git init --initial-branch=main git add . git commit -m "import"