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 .Values.debug.info }}
  # 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 .Values.aws.lambda .Values.aws.ecs }}
  • Same applies for a or expression:
{{ if or .Values.aws.lambda .Values.aws.ecs }}
  • We can test for equality using the eq operator with the same exact syntax:
{{ if eq .Values.aws.defaultAccount "blue" }}
  • And inequality:
{{ if ne .Values.aws.defaultAccount "blue" }}
  • Along with other string functions, we can check if some string is contained on some other with the contains function:
{{ if contains .Values.aws.environment "test" }}
  • To negate an expression it's more intuitive:
{{ if not .Values.aws.lambda.enabled }}

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

{{ if not (contains .Values.aws.environment "test") }}

Posted on 14/07/2021

Categories