Is it possible to define variables use if/else condition in helm chart?
Asked Answered
C

3

24

In my values.yaml I have:

isLocal: false
localEnv:
  url: abcd
prodEnv:
  url: defg

Then I have a service.yaml:

{{- if .Values.isLocal }}
{{- $env := .Values.localEnv }}
{{- else}}
{{- $env := .Values.prodEnv }}
{{- end}}
url: {{ $env.url}}

Unfortunately I got a 'undefined variable "$env"' error, does anyone have idea how to achieve this? Thanks!

Cavanaugh answered 22/8, 2019 at 0:54 Comment(0)
G
19

The Go text/template documentation notes:

A variable's scope extends to the "end" action of the control structure ("if", "with", or "range") in which it is declared....

so your declarations of $env go out of scope at the end of the {{ if }}...{{ end }} block, which isn't so useful.

Helm also includes (almost all of) a support template library called Sprig which includes a ternary function, which acts sort of like an inline "if" statement. For your example you could write

{{- $env := ternary .Values.localEnv .Values.prodEnv .Values.isLocal -}}

(Also consider just writing totally separate Helm values files for each environment, and installing your chart with helm install -f prod.yaml, instead of trying to encapsulate every possible environment in a single file.)

Gates answered 22/8, 2019 at 1:1 Comment(3)
It's a good idea to use one Helm values for each environment, but how to deal if we have a commune values between 'prod.yaml ' and 'local.yaml' ?Jame
You can use multiple -f options: helm install -f common.yaml -f prod.yaml ...Gates
Note that if you'll use instead of .Values.prodEnv some expression (i.e.: get .someObject .Values.localEnv # .Values.localEnv contains an attribute name) is will be executed before test expression!Serendipity
P
48

One more way would be to create the variable before starting the if/else block. For example:

{{- $env := .Values.prodEnv -}}
{{ if .Values.isLocal }}
  {{- $env = .Values.localEnv -}}
{{ end}}

Notice that I've not used the := operator inside the if block as the variable is already created.

and then

url: {{ $env.url}}
Paediatrics answered 9/3, 2021 at 14:1 Comment(2)
Thank you as well!Colyer
I prefer this answer since it more specifically answers the question principle (using if/else blocks, as this could be extended to more complex logic).Moriyama
G
19

The Go text/template documentation notes:

A variable's scope extends to the "end" action of the control structure ("if", "with", or "range") in which it is declared....

so your declarations of $env go out of scope at the end of the {{ if }}...{{ end }} block, which isn't so useful.

Helm also includes (almost all of) a support template library called Sprig which includes a ternary function, which acts sort of like an inline "if" statement. For your example you could write

{{- $env := ternary .Values.localEnv .Values.prodEnv .Values.isLocal -}}

(Also consider just writing totally separate Helm values files for each environment, and installing your chart with helm install -f prod.yaml, instead of trying to encapsulate every possible environment in a single file.)

Gates answered 22/8, 2019 at 1:1 Comment(3)
It's a good idea to use one Helm values for each environment, but how to deal if we have a commune values between 'prod.yaml ' and 'local.yaml' ?Jame
You can use multiple -f options: helm install -f common.yaml -f prod.yaml ...Gates
Note that if you'll use instead of .Values.prodEnv some expression (i.e.: get .someObject .Values.localEnv # .Values.localEnv contains an attribute name) is will be executed before test expression!Serendipity
W
16

You actually can define the variable $env outside the if block and assign its value using = with if block.

{{- $env := "" }}
{{- if .Values.isLocal }}
{{- $env = .Values.localEnv }}
{{- else}}
{{- $env = .Values.prodEnv }}
{{- end}}
url: {{ $env.url}}

Please note: := will declare the variable while assigning the value. On the other hand, = will assign the value only. If you use := in if, it will declare a new local variable $env which will not impact the value of $env declared outside the if block.

Wallop answered 3/10, 2021 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.