user2309838
user2309838

Reputation: 307

how to send "if" statement in helm array template

I would like to have a if exists check in my array object of the helm template. My YAML looks like this:

servers:
- hosts: dev
  port: 443
{{- if .Values.stage.enabled -}}
- hosts: stage
  port: 443
{{- end -}}

I received error converting YAML to JSON: yaml: mapping values are not allowed in this context

Is nesting if condition not allowed in helm template? If so, how do I fix it?

Upvotes: 1

Views: 798

Answers (1)

David Maze
David Maze

Reputation: 159515

The - inside the curly braces causes the Go text/template engine to swallow the whitespace adjacent to it. If you run helm template on this (highly recommended) you will see output like

servers:
- hosts: dev
  port: 443- hosts: stage
  port: 443and then whatever comes next in the template

If you put conditionals on lines of their own like this, as a rule of thumb, it often works well to include the - at the start of the line (to remove the newline before it) but not at the end of the line (preserving the indentation on the next line).

servers:
- hosts: dev
  port: 443
{{- if .Values.stage.enabled }}{{/* <-- no `-` here */}}
- hosts: stage
  port: 443
{{- end }}{{/* <-- no `-` here either */}}

Upvotes: 4

Related Questions