Skip to content

Commit

Permalink
update checker code in patchStrategicMerge
Browse files Browse the repository at this point in the history
  • Loading branch information
koba1t committed Aug 28, 2022
1 parent b4d25b1 commit 7081b22
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
7 changes: 3 additions & 4 deletions api/types/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"

"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -218,16 +218,15 @@ 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

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 {
Expand Down
5 changes: 2 additions & 3 deletions kustomize/commands/edit/fix/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ func RunFix(fSys filesys.FileSystem, w io.Writer) error {
return err
}

err = m.FixKustomizationPreMarshalling()
if err != nil {
return err
if err := m.FixKustomizationPreMarshalling(fSys); err != nil {
return fmt.Errorf("failed to preMarshalling the kustomization file: %w", err)
}

if flags.vars {
Expand Down
64 changes: 64 additions & 0 deletions kustomize/commands/edit/fix/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestFixCommand(t *testing.T) {
tests := []struct {
name string
input string
files map[string]string
expected string
}{
{
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit 7081b22

Please sign in to comment.