mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +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.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
|
||||
|
||||
@@ -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`"+`
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user