update checker code in patchStrategicMerge

This commit is contained in:
yugo kobayashi
2022-08-28 16:53:36 +00:00
parent b4d25b1b26
commit 032bf3338e
3 changed files with 68 additions and 6 deletions

View File

@@ -7,8 +7,8 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"strings"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
) )
@@ -218,7 +218,7 @@ func (k *Kustomization) FixKustomizationPostUnmarshalling() {
// FixKustomizationPreMarshalling fixes things // FixKustomizationPreMarshalling fixes things
// that should occur after the kustomization file // that should occur after the kustomization file
// has been processed. // has been processed.
func (k *Kustomization) FixKustomizationPreMarshalling() error { func (k *Kustomization) FixKustomizationPreMarshalling(fSys filesys.FileSystem) error {
// PatchesJson6902 should be under the Patches field. // PatchesJson6902 should be under the Patches field.
k.Patches = append(k.Patches, k.PatchesJson6902...) k.Patches = append(k.Patches, k.PatchesJson6902...)
k.PatchesJson6902 = nil k.PatchesJson6902 = nil
@@ -226,8 +226,7 @@ func (k *Kustomization) FixKustomizationPreMarshalling() error {
if k.PatchesStrategicMerge != nil { if k.PatchesStrategicMerge != nil {
for _, patchStrategicMerge := range k.PatchesStrategicMerge { for _, patchStrategicMerge := range k.PatchesStrategicMerge {
// check this patch is file path select. // check this patch is file path select.
if strings.Count(string(patchStrategicMerge), "\n") < 1 && if _, err := fSys.ReadFile(string(patchStrategicMerge)); err == nil {
(patchStrategicMerge[len(patchStrategicMerge)-5:] == ".yaml" || patchStrategicMerge[len(patchStrategicMerge)-4:] == ".yml") {
// path patch // path patch
k.Patches = append(k.Patches, Patch{Path: string(patchStrategicMerge)}) k.Patches = append(k.Patches, Patch{Path: string(patchStrategicMerge)})
} else { } else {

View File

@@ -55,8 +55,7 @@ func RunFix(fSys filesys.FileSystem, w io.Writer) error {
return err return err
} }
err = m.FixKustomizationPreMarshalling() if err := m.FixKustomizationPreMarshalling(fSys); err != nil {
if err != nil {
return err return err
} }

View File

@@ -32,6 +32,7 @@ func TestFixCommand(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
input string input string
files map[string]string
expected string expected string
}{ }{
{ {
@@ -170,6 +171,21 @@ kind: Kustomization
patchesStrategicMerge: patchesStrategicMerge:
- deploy.yaml - 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: ` expected: `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
@@ -183,6 +199,21 @@ patches:
patchesStrategicMerge: patchesStrategicMerge:
- deploy.yml - 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: ` expected: `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
@@ -209,6 +240,21 @@ patchesStrategicMerge:
- name: CONFIG_FILE_PATH - name: CONFIG_FILE_PATH
value: home.yaml 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: ` expected: `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
@@ -239,6 +285,21 @@ patchesJson6902:
target: target:
kind: Deployment 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: ` expected: `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
@@ -310,6 +371,9 @@ kind: Kustomization
for _, test := range tests { for _, test := range tests {
fSys := filesys.MakeFsInMemory() fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, []byte(test.input)) 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) cmd := NewCmdFix(fSys, os.Stdout)
require.NoError(t, cmd.RunE(cmd, nil)) require.NoError(t, cmd.RunE(cmd, nil))