Search code examples
kuberneteskubernetes-helmgo-templates

Reference value key with different value in helm/go template


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?


Solution

  • 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.