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

Ignore nil objects occured when rendering a file #560

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
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 }}
Loading