Skip to content

Commit 02dd58c

Browse files
authored
Merge pull request #125 from weaveworks/ignore-empty-repeat
Ignore empty repeat values.
2 parents 4221307 + bf13d0e commit 02dd58c

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

controllers/templates/renderer.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ func repeat(index int, tmpl templatesv1.GitOpsSetTemplate, params map[string]any
8989
slice, ok := v.Interface().([]any)
9090
if ok {
9191
repeated = append(repeated, slice...)
92-
} else {
92+
continue
93+
}
94+
95+
if !v.IsNil() {
9396
repeated = append(repeated, v.Interface())
9497
}
9598
}

controllers/templates/renderer_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,34 @@ func TestRender(t *testing.T) {
206206
addLabels[*corev1.Namespace](map[string]string{"templates.weave.works/name": "test-gitops-set", "templates.weave.works/namespace": testNS}))),
207207
},
208208
},
209+
{
210+
name: "repeat elements with no elements does not error",
211+
elements: []apiextensionsv1.JSON{
212+
{Raw: []byte(`{"env": "engineering-dev","externalIP": "192.168.50.50","namespaces":null}`)},
213+
},
214+
setOptions: []func(*templatesv1.GitOpsSet){
215+
func(s *templatesv1.GitOpsSet) {
216+
s.Spec.Templates = []templatesv1.GitOpsSetTemplate{
217+
{
218+
Content: runtime.RawExtension{
219+
Raw: mustMarshalJSON(t, makeTestService(types.NamespacedName{Name: "{{ .Element.env}}-demo1", Namespace: testNS})),
220+
},
221+
},
222+
{
223+
Repeat: "{ $.namespaces }",
224+
Content: runtime.RawExtension{
225+
Raw: mustMarshalJSON(t, makeTestNamespace("{{ .Repeat.name }}-{{ .Element.env }}")),
226+
},
227+
},
228+
}
229+
},
230+
},
231+
want: []*unstructured.Unstructured{
232+
test.ToUnstructured(t, makeTestService(nsn(testNS, "engineering-dev-demo1"), setClusterIP("192.168.50.50"),
233+
addAnnotations(map[string]string{"app.kubernetes.io/instance": "engineering-dev"}),
234+
addLabels[*corev1.Service](map[string]string{"templates.weave.works/name": "test-gitops-set", "templates.weave.works/namespace": testNS}))),
235+
},
236+
},
209237
{
210238
name: "repeat elements with maps",
211239
elements: []apiextensionsv1.JSON{

0 commit comments

Comments
 (0)