1

"helm check if key exist and then evaluate if it's value is true on single line condition" In Helm version 3.10 will the following condition below work as intended or I can achieve it in some other way?

{{- if and (hasKey .Values.enableJsConfigMap) (eq .Values.enableJsConfigMap.enabled true) }}

3 Answers3

3

This wouldn't work. The second condition will be evaluated regardless of the first. So if the key is not defined there will be an error. Use default instead:

{{- if eq (default .Values.enableJsConfigMap false) true }}
# Do something if enableJsConfigMap exists and its value is true
{{- end }}
user42196
  • 31
  • 2
0

Use dig. dig is the helm equivalent of bar?.foo, in that it doesn't error if bar doesn't have a foo. https://helm.sh/docs/chart_template_guide/function_list/#dig

The least unreadable syntax of dig uses pipelining: dictionary | dig "key" "childDictKey" "defaultValue". You can have any number of childDictKeys, including none.

{{- if eq (.Values | dig "enableJsConfigMap" "enabled" "false") "true" }}
{{- /*do something*/ }}
{{- end }}
Syfer Polski
  • 176
  • 3
0

Th best construction i use:

{{- if (.Values.enableJsConfigMap).enabled }}
{{- /*do something*/ }}
{{- end }}

Also you can make any number of childs like this:

{{- if (((((((.Values.a).b).c).d).e).f).g).k }}

I have no idea why is it working.
Looks like Helm will try to check brackets from inner to outer.
If some thing will be absent, it will return nill to "if". I assume, it's memory consumption operation, because helm can create new objects instead of allocators here - i'm not sure.
But i never met a problem with this.

Zerginwan
  • 1
  • 1