Skip to content

Commit

Permalink
Merge pull request #560 from vasrem/bugfix/ignore_null_objects_when_r…
Browse files Browse the repository at this point in the history
…endering

Ignore nil objects occured when rendering a file
  • Loading branch information
adrianchiris authored Dec 17, 2023
2 parents 1939bc8 + 1822145 commit c0a2f9c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ func RenderTemplate(path string, d *RenderData) ([]*unstructured.Unstructured, e
}
return nil, errors.Wrapf(err, "failed to unmarshal manifest %s", path)
}

if u.Object == nil {
continue
}

out = append(out, &u)
}

Expand Down
23 changes: 22 additions & 1 deletion pkg/render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,35 @@ func TestTemplate(t *testing.T) {
g.Expect(o[0].Object["bar"]).To(Equal("myns"))
}

// TestTemplateWithEmptyObject tests the case where a file generates additional nil objects when rendered. An empty
// object can also occur in the particular case shown in the testfile below when minus is missing at the end of the
// first expression (i.e. {{- if .Enable }}).
func TestTemplateWithEmptyObject(t *testing.T) {
g := NewGomegaWithT(t)

p := "testdata/manifests/template_with_empty_object.yaml"

d := MakeRenderData()
d.Data["Enable"] = true
o, err := RenderTemplate(p, &d)
g.Expect(err).NotTo(HaveOccurred())

g.Expect(len(o)).To(Equal(2))
g.Expect(o[0].GetName()).To(Equal("pod1"))
g.Expect(o[0].GetNamespace()).To(Equal("namespace1"))
g.Expect(o[1].GetName()).To(Equal("pod2"))
g.Expect(o[1].GetNamespace()).To(Equal("namespace2"))
}

func TestRenderDir(t *testing.T) {
g := NewGomegaWithT(t)

d := MakeRenderData()
d.Funcs["fname"] = func(s string) string { return s }
d.Data["Namespace"] = "myns"
d.Data["Enable"] = true

o, err := RenderDir("testdata/manifests", &d)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(o).To(HaveLen(6))
g.Expect(o).To(HaveLen(8))
}
20 changes: 20 additions & 0 deletions pkg/render/testdata/manifests/template_with_empty_object.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{- if .Enable }}
---
apiVersion: v1
kind: Pod
metadata:
namespace: namespace1
name: pod1
spec:
containers:
- image: "busybox"
---
apiVersion: v1
kind: Pod
metadata:
namespace: namespace2
name: pod2
spec:
containers:
- image: "busybox"
{{- end }}

0 comments on commit c0a2f9c

Please sign in to comment.