Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Helm - Templating variables in values.yaml

I'm trying to template variables from a map inside the values.yaml into my final Kubernetes ConfigMap YAML.

I've read through https://github.com/helm/helm/issues/2492 and https://helm.sh/docs/chart_template_guide/ but can't seem to find an answer.

For some context, this is roughly what I'm trying to do:

values.yaml

config:
  key1: value
  key2: value-{{ .Release.Name }}

configmap.yaml

kind: ConfigMap
data:
  config-file: |
    {{- range $key, $value := .Values.config }}
    {{ $key }} = {{ $value }}
    {{- end }}

Where the desired output with would be:

helm template --name v1 mychart/

kind: ConfigMap
data:
  config-file: |
    key1 = value
    key2 = value-v1

I've tried a few variations using template functions and pipelining, but to no avail:

{{ $key }} = {{ tpl $value . }}
{{ $key }} = {{ $value | tpl . }}
{{ $key }} = {{ tpl $value $ }}
like image 680
ahstn Avatar asked May 02 '19 18:05

ahstn


3 Answers

The above would also have worked in this way

values.yaml

config:
  key1: "value"
  key2: "value-{{ .Release.Name }}"

configmap.yaml

kind: ConfigMap
data:
  config-file: |
    {{- range $key, $value := .Values.config }}
    {{ $key }} = {{ tpl $value $ }}
    {{- end }}

What I changed was : I put value in quotes in value.yaml and used template tpl in the config map.

like image 127
Amrut Prabhu Avatar answered Sep 24 '22 01:09

Amrut Prabhu


Managed to solve this using the following syntax:

configmap.yaml

kind: ConfigMap
data:
  config-file: |
    {{- range $key, $value := .Values.config }}
    {{ $key }} = {{ tpl ($value | toString) $ }}
    {{- end }}
like image 35
ahstn Avatar answered Sep 23 '22 01:09

ahstn


I'll refer to the question's title regarding templating variables in helm and suggest another option to use on values.yaml which is YAML Anchors.

Docs reference

As written in here:

The YAML spec provides a way to store a reference to a value, and later refer to that value by reference. YAML refers to this as "anchoring":

coffee: "yes, please"
favorite: &favoriteCoffee "Cappucino"
coffees:
  - Latte
  - *favoriteCoffee
  - Espresso

In the above, &favoriteCoffee sets a reference to Cappuccino.

Later, that reference is used as *favoriteCoffee.
So coffees becomes Latte, Cappuccino, Espresso.

A more practical example

Referring to a common image setup (Registry and PullPolicy) in all values.yaml.

Notice how the default values are being set at Global.Image next to the reference definition which starts with &:

Global:
  Image:
    Registry: &global-docker-registry "12345678910.dkr.ecr.us-west-2.amazonaws.com" # <--- Default value
    PullPolicy: &global-pull-policy "IfNotPresent" # <--- Default value

Nginx:
  Image:
    Registry: *global-docker-registry
    PullPolicy: *global-pull-policy
    Version: 1.21.4
    Port: 80

MySql:
  Image:
    Registry: *global-docker-registry
    PullPolicy: *global-pull-policy
    Name: mysql
    Version: 8.0.27
    Port: 3306
like image 14
RtmY Avatar answered Sep 20 '22 01:09

RtmY