Helm – Templating variables in values.yaml

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

Leave a Comment