Conditional expressions in helm

helm expressions if else

2 min read | by Jordi Prats

Maybe one of the main challenges about helm is the complexity of it's template rendering engine: It's not very intuitive

  • If we want to use an if/else construct it works as we are used to:
{{ if .Values.debug }}
  # Do something
{{ else }}
  # Do something else
{{ end }}
  • We can have elseif nesting expressions:
{{ if }}
  # Do something
{{ else if .Values.debug.err }}
  # Do something else
{{ else }}
  # Default
{{ end }}

When working with more complex expressions is when we will start to feel the pain. Since they have been implemented as functions, the resulting expression looks like follows:

  • And and expression will look like this:
{{ if and }}
  • Same applies for a or expression:
{{ if or }}
  • We can test for equality using the eq operator with the same exact syntax:
{{ if eq "blue" }}
  • And inequality:
{{ if ne "blue" }}
  • Along with other string functions, we can check if some string is contained on some other with the contains function:
{{ if contains "test" }}
  • To negate an expression it's more intuitive:
{{ if not }}

If you need to negate the result of a comparison you can nest both expressions:

{{ if not (contains "test") }}

Posted on 14/07/2021