Skip to content

Instantly share code, notes, and snippets.

@gerritjvv
Created September 21, 2023 10:01
Show Gist options
  • Save gerritjvv/e23d6098b91f86a1aca04ff0e73a869f to your computer and use it in GitHub Desktop.
Save gerritjvv/e23d6098b91f86a1aca04ff0e73a869f to your computer and use it in GitHub Desktop.
helm avoid nil pointer on nested values and maps

When you nest values in yaml like

server:
  spec:
    cpu: 0.5

You use it in helm as {{ .Values.server.spec.cpu | default 0.5 }}, which works fine till you try the values as not defined
e.g

server:

Now {{ .Values.server.spec.cpu | default 0.5 }} will cause the template code to panic with a nil exception.

Ultimately the Helm templates are Golang templates and this is just how it works. There is no ?. operator.

The best and strangest undocumented solution is to wrap each level of nesting in (, ).

e.g.

{{ (((.Values.server).spec).cpu) | default 0.5 }}

This will not panic and you will get the expected behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment