I'm using helm (sprig, go templates). I'm trying to build guards to selectively include stuff in my helm chart, but only if one of the components needs them.
So, I have a list:
- name: foo
flag1: true
flag2: false
flag3: false
- name: bar
flag1: false
flag2: true
flag3: false
I want to do something akin to a (pseudocode) list.any(flag), where over a variable length list, if I passed in flag1 or flag2 I'd get back true, but flag3 would get me false. If possible, I'd like to be able to ask about a different flag without repeating myself each time.
Is there a concise way to accomplish this? Can it be done?
The set of things that are and aren't possible in Go templates can be a little mysterious. A named template always returns a string, but an empty string is logically "false", so it should be possible to write a template call like
A template only takes a single parameter, so in the call we've packed the multiple inputs we need into a
list. We've also used the Helm-specificincludefunction to invoke a template and get its output as a string.How can the template work? Template
rangeloops don't havebreakorreturnactions or any other way to stop early. If we only want to output the "success" value once, this means we need to manually iterate through the list. For reasonably short lists, a recursive template call works here.(For this specific thing, outputting
yesoryesyesyeswould both be non-empty and therefore logically "true", so you could use arangeloop here successfully. This would not work for an equivalentlist.all, though.)In the template definition
we need to start by unpacking the parameter list
We only do something if the list is non-empty.
If it is non-empty, we can split out its first element. We expect that to be a map, so we can look up the requested key in that with the standard
indexfunction. This will returnnilif the key is absent andfalseif it's false, both of which are logically false; if it'struethen theiftest will pass.If we do find the item, we can write out a success value and not do anything else
If we don't, then we can recursively call ourselves with the remainder of the list.
Combining that all together gives this template (indented for clarity, the
-markers will consume all of the whitespace):