add edit-fix for patchesStrategicMerge to patches

This commit is contained in:
yugo kobayashi
2022-07-29 17:34:38 +00:00
parent 1b0fe2a078
commit 64f42ea45f
3 changed files with 110 additions and 0 deletions

View File

@@ -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

View File

@@ -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`"+`

View File

@@ -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: