Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nil pointer dereferencing when converting vars to replacements #5211

Merged
merged 7 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions kustomize/commands/edit/fix/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func getNodesFromFile(fileName string, fSys filesys.FileSystem) ([]*kyaml.RNode,
}
out := &bytes.Buffer{}
r := kio.ByteReadWriter{
Reader: bytes.NewBufferString(string(b)),
Reader: bytes.NewBuffer(b),
Writer: out,
KeepReaderAnnotations: true,
OmitReaderAnnotations: true,
Expand Down Expand Up @@ -284,7 +284,11 @@ func constructTargets(file string, node *kyaml.RNode, fieldPaths []string,
func writePatchTargets(patch types.Patch, node *kyaml.RNode, fieldPaths []string,
options []*types.FieldOptions) ([]*types.TargetSelector, error) {
var result []*types.TargetSelector
selector := patch.Target.Copy()

selector := types.Selector{}
if patch.Target != nil {
selector = patch.Target.Copy()
}

for i := range fieldPaths {
target := &types.TargetSelector{
Expand Down
107 changes: 89 additions & 18 deletions kustomize/commands/edit/fix/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ spec:

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("pod.yaml", pod)
assert.NoError(t, fSys.WriteFile("pod.yaml", pod))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
Expand Down Expand Up @@ -119,7 +119,7 @@ spec:

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("pod.yaml", pod)
assert.NoError(t, fSys.WriteFile("pod.yaml", pod))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
Expand Down Expand Up @@ -197,7 +197,7 @@ spec:

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("pod.yaml", pod)
assert.NoError(t, fSys.WriteFile("pod.yaml", pod))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
Expand Down Expand Up @@ -276,7 +276,7 @@ spec:

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("pod.yaml", pod)
assert.NoError(t, fSys.WriteFile("pod.yaml", pod))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
Expand Down Expand Up @@ -355,7 +355,7 @@ spec:

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("pod.yaml", pod)
assert.NoError(t, fSys.WriteFile("pod.yaml", pod))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
err := cmd.RunE(cmd, nil)
Expand Down Expand Up @@ -412,8 +412,8 @@ spec:

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("pod.yaml", pod)
fSys.WriteFile("patch.yaml", patch)
assert.NoError(t, fSys.WriteFile("pod.yaml", pod))
assert.NoError(t, fSys.WriteFile("patch.yaml", patch))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
Expand Down Expand Up @@ -533,8 +533,8 @@ spec:

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("pod.yaml", pod)
fSys.WriteFile("patch.yaml", patch)
assert.NoError(t, fSys.WriteFile("pod.yaml", pod))
assert.NoError(t, fSys.WriteFile("patch.yaml", patch))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
Expand Down Expand Up @@ -657,8 +657,8 @@ spec:

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("pod.yaml", pod)
fSys.WriteFile("patch.yaml", patch)
assert.NoError(t, fSys.WriteFile("pod.yaml", pod))
assert.NoError(t, fSys.WriteFile("patch.yaml", patch))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
Expand Down Expand Up @@ -780,8 +780,8 @@ spec:

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("pod.yaml", pod)
fSys.WriteFile("patch.yaml", patch)
assert.NoError(t, fSys.WriteFile("pod.yaml", pod))
assert.NoError(t, fSys.WriteFile("patch.yaml", patch))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
Expand Down Expand Up @@ -910,8 +910,8 @@ spec:

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("pod.yaml", pod)
fSys.WriteFile("patch.yaml", patch)
assert.NoError(t, fSys.WriteFile("pod.yaml", pod))
assert.NoError(t, fSys.WriteFile("patch.yaml", patch))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
Expand Down Expand Up @@ -1023,8 +1023,8 @@ spec:

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomizationOverlay)
fSys.WriteFile("base/pod.yaml", pod)
fSys.WriteFile("base/kustomization.yaml", kustomizationBase)
assert.NoError(t, fSys.WriteFile("base/pod.yaml", pod))
assert.NoError(t, fSys.WriteFile("base/kustomization.yaml", kustomizationBase))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
Expand Down Expand Up @@ -1104,7 +1104,7 @@ metadata:

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("pod.yaml", pod)
assert.NoError(t, fSys.WriteFile("pod.yaml", pod))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
Expand Down Expand Up @@ -1143,3 +1143,74 @@ metadata:
a.b.c: SOME_SECRET_NAME_PLACEHOLDER
`, string(content))
}

func TestFixVarsWithPatch(t *testing.T) {
kustomization := []byte(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

patchesStrategicMerge:
- patch.yaml

vars:
- name: CERTIFICATE_NAMESPACE
objref:
name: system
fieldref:
fieldpath: metadata.namespace
`)
patch := []byte(`
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: $(CERTIFICATE_NAMESPACE)
`)

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
assert.NoError(t, fSys.WriteFile("patch.yaml", patch))
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
content, err := testutils_test.ReadTestKustomization(fSys)
assert.NoError(t, err)

assert.Equal(t, `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization


Aceralon marked this conversation as resolved.
Show resolved Hide resolved
patches:
- path: patch.yaml
replacements:
- source:
fieldPath: metadata.namespace
name: system
targets:
- fieldPaths:
- spec.template.spec.containers.0.name
select:
namespace: system
`, string(content))

content, err = fSys.ReadFile("patch.yaml")
assert.NoError(t, err)
assert.Equal(t, `
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: CERTIFICATE_NAMESPACE_PLACEHOLDER
`, string(content))
}
Loading