Skip to content

Commit

Permalink
resolver: Don't crash on nil objs (adobe#142)
Browse files Browse the repository at this point in the history
If a YAML specifies an empty `initContainers:`, this will crash this
code if we also specify yamls. regardless of how pointless this is, this
is, it is valid k8s yaml that applies cleanly.
  • Loading branch information
DolceTriade authored and arturo-skydio committed Oct 5, 2023
1 parent c6f1ad2 commit b17c82d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions resolver/pkg/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ func (pt *imageTagTransformer) findAndReplaceTag(obj map[string]interface{}) err
}

func (pt *imageTagTransformer) updateContainers(obj map[string]interface{}, path string) error {
if obj[path] == nil {
return nil
}
containers := obj[path].([]interface{})
for i := range containers {
container := containers[i].(map[string]interface{})
Expand All @@ -134,6 +137,9 @@ func (pt *imageTagTransformer) updateContainers(obj map[string]interface{}, path
}

func (pt *imageTagTransformer) updateContainer(obj map[string]interface{}, path string) error {
if obj[path] == nil {
return nil
}
container := obj[path].(map[string]interface{})
image, found := container["image"]
if found {
Expand Down
3 changes: 3 additions & 0 deletions resolver/pkg/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func TestNoError(t *testing.T) {
{"zk", map[string]string{
"zk-image": "dummy",
}},
{"emptyinit", map[string]string{
"helloworld-image": "docker.io/kube/hello/image:tag",
}},
}
for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions resolver/pkg/testdata/emptyinit.expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: apps/v1
kind: CronWorkFlow
metadata:
name: aaa
namespace: stats-dev
spec:
workflowSpec:
metadata:
labels:
app: app
templates:
container:
image: docker.io/kube/hello/image:tag
initContainers: null
14 changes: 14 additions & 0 deletions resolver/pkg/testdata/emptyinit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: apps/v1
kind: CronWorkFlow
metadata:
name: aaa
namespace: stats-dev
spec:
workflowSpec:
metadata:
labels:
app: app
templates:
initContainers:
container:
image: helloworld-image

0 comments on commit b17c82d

Please sign in to comment.