Helm Charts - How do I use `default` on undefined object property values?

1/3/2020

Using Helm, I was under the impression default would be the fallback if a variable is not defined. However, it doesn't appear Helm can get to values in sub-object hashes:

  type: {{ default "NodePort" .Values.fpm.service.type }}

If .Values.fpm.service or service.type is not defined, it should use 9000.

However, attempting to template this throws a nil pointer error:

 <.Values.fpm.service.type>: nil pointer evaluating interface {}.type

Is there a way to simply perform this level of variable testing? Or am I subjected to an if/else test?

The intent of this is to optionally define .fpm.service (and [..].type) within your values.yaml file.

(I'm building a Helm Library chart to handle optional definitions by main charts)

-- guice
kubernetes-helm

2 Answers

1/3/2020

According to the official Helm doc (Using Default Function), the syntax is different and you should use it this way:

type: {{ .Values.fpm.service.type | default "NodePort" | quote }}
-- RafaƂ Leszko
Source: StackOverflow

1/3/2020

Doesn't look like there's really a good way to stop Helm from trying to dive into non-existing objects. I moved into a single line if condition, and it worked:

  type: {{ if .Values.fpm.service -}} {{ .default "NodePort" .Values.fpm.service.type | quote }} {{- else -}} "NodePort" {{- end }}

This way, I check if fpm.service exists first, before trying .type check. It works, whether .service and .service.type is or is not defined.

-- guice
Source: StackOverflow