Reference value key with different value in helm/go template

12/19/2019

I want to know if it's possible to use a value as an object key of a different value like this?

...
spec:
  replicas: {{ .Values[.Release.Namespace].replicas }}
...

When my values.yaml is like:

production:
  replicas: 2
staging:
  replicas: 1

And I install like this:

helm install --namespace production my-release .

If not, is there any other way to achieve this?

-- Moshe
go-templates
kubernetes
kubernetes-helm

2 Answers

12/19/2019

You can use more than one value file when installing a helm chart. You can have a "default" values.yaml, and then have several values.env.yaml files with the specific settings for each environment.

So you would have:

values.yaml

someConfigForAllEnvironments: true

values.staging.yaml

replicas: 1

values.production.yaml

replicas: 2

template.yaml

...
spec:
  replicas: {{ .Values.replicas }}
...

And you would install with:

helm install --namespace production my-release -f values.production.yaml
-- Ángela
Source: StackOverflow

12/20/2019

Take look at similar case: helm-values. You use the index template function, though its values layout is little bit different from yours; {{ index (index .Values .Release.Namespace) "replicas" }} should work.

You can use more than one value file when installing a helm chart. You can have a "default" values.yaml, and then have several values.env.yaml files with the specific settings for each environment.

You could iterate over the "namespaces" and use the one that matches the current namespace.

-- MaggieO
Source: StackOverflow