mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 10:30:59 +00:00
add edit-fix for patchesStrategicMerge to patches
This commit is contained in:
@@ -222,6 +222,13 @@ func (k *Kustomization) FixKustomizationPreMarshalling() error {
|
|||||||
k.Patches = append(k.Patches, k.PatchesJson6902...)
|
k.Patches = append(k.Patches, k.PatchesJson6902...)
|
||||||
k.PatchesJson6902 = nil
|
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
|
// this fix is not in FixKustomizationPostUnmarshalling because
|
||||||
// it will break some commands like `create` and `add`. those
|
// it will break some commands like `create` and `add`. those
|
||||||
// commands depend on 'commonLabels' field
|
// commands depend on 'commonLabels' field
|
||||||
|
|||||||
@@ -68,12 +68,14 @@ func RunFix(fSys filesys.FileSystem, w io.Writer) error {
|
|||||||
fmt.Fprintln(w, `
|
fmt.Fprintln(w, `
|
||||||
Fixed fields:
|
Fixed fields:
|
||||||
patchesJson6902 -> patches
|
patchesJson6902 -> patches
|
||||||
|
patchesStrategicMerge -> patches
|
||||||
commonLabels -> labels
|
commonLabels -> labels
|
||||||
vars -> replacements`)
|
vars -> replacements`)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintln(w, `
|
fmt.Fprintln(w, `
|
||||||
Fixed fields:
|
Fixed fields:
|
||||||
patchesJson6902 -> patches
|
patchesJson6902 -> patches
|
||||||
|
patchesStrategicMerge -> patches
|
||||||
commonLabels -> labels
|
commonLabels -> labels
|
||||||
|
|
||||||
To convert vars -> replacements, run the command `+"`kustomize edit fix --vars`"+`
|
To convert vars -> replacements, run the command `+"`kustomize edit fix --vars`"+`
|
||||||
|
|||||||
@@ -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) {
|
func TestFixOutdatedCommonLabels(t *testing.T) {
|
||||||
kustomizationContentWithOutdatedCommonLabels := []byte(`
|
kustomizationContentWithOutdatedCommonLabels := []byte(`
|
||||||
commonLabels:
|
commonLabels:
|
||||||
|
|||||||
Reference in New Issue
Block a user