Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 583
guice Avatar asked Jan 01 '23 09:01

guice


2 Answers

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 }}
like image 191
Rafał Leszko Avatar answered Jan 27 '23 09:01

Rafał Leszko


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.

like image 31
guice Avatar answered Jan 27 '23 08:01

guice