This feels like such a simple problem.. but I can't seem to work out how to fix it..
I'm evaluating whether to create an ingress manifest, but need to evaluate whether ingress is enabled, but ignore if there is no ingress block.
{{- if index .Values (.Values.env) "ingress" "enabled" -}}
So this works when values has an ingress block. But if I create another environment in the values file it fails with nil pointer. I've tried various iterations of hasKey
and some and
& or
combinations.
No ingress should be templated if either .Values (.Values.env) "ingress" "enabled"
is equal to false OR if .Values (.Values.env) "ingress"
does not exist.
Any ideas?
TIA
You can use the Helm default
and dict
functions to work around this. At each level, instead of getting a Go nil
value if the item doesn't exist, you can use default
to make its value an empty list, which you can then step into.
{{- $env := index .Values .Values.env | default dict }}
{{- $ingress := $env.ingress | default dict }}
{{- if $ingress.enabled }}
...
{{- end }}
Syntactically, the bare word dict
is equivalent to (dict)
and parallel to (dict "key" "value")
but with no arguments; it calls the dict
function and returns an empty dictionary. Then that's the argument to the default
function.