how can i reference the namespace in values.yaml?

8/23/2019

I would like to be able to reference the current namespace in values.yaml to use it to suffix some values like this

# in values.yaml
someParam: someval-{{ .Release.Namespace }}

It much nicer to define it this way instead of going into all my templates and adding {{ .Release.Namespace }}. If I can do it in values.yaml it's much clearer and only needs to be defined in one place.

-- red888
kubernetes
kubernetes-helm

4 Answers

4/23/2020

There is a proposal in github for what you're asking: Proposal: Allow templating in values.yaml

From one of the latest comments:

This is the first thread I ran into, than also comment here... See also #2514

:) Thankfully, latest Helm manual says how to achieve this. https://helm.sh/docs/howto/charts_tips_and_tricks/#using-the-tpl-function

The trick is enclosing variable in " or in a yaml block |-, and than referencing it in a template as {{ tpl .Values.variable . }} This seems to make Helm happy.

Example:

$ cat Chart.yaml | grep appVersion appVersion: 0.0.1-SNAPSHOT-d2e2f42


$ cat platform/shared/t/values.yaml | grep -A2 image: image:    tag:
|-
    {{ .Chart.AppVersion }}


$ cat templates/deployment.yaml | grep image:
          image: "{{ .Values.image.repository }}:{{ tpl .Values.image.tag . }}"


$ helm template . --values platform/shared/t/values.betradar.yaml |
grep image
          image: "docker-registry.default.svc:5000/namespace/service:0.0.1-SNAPSHOT-d2e2f42"
          imagePullPolicy: Always
      image: busybox 

Otherwise there is an error thrown..

$ cat platform/shared/t/values.yaml | grep -A1 image: image:    tag:
{{ .Chart.AppVersion }}

1 $ helm template . --values platform/shared/t/values.yaml | grep
image Error: failed to parse platform/shared/t/values.yaml: error
converting YAML to JSON: yaml: invalid map key: map[interface
{}]interface {}{".Chart.AppVersion":interface {}(nil)}
-- Vusal
Source: StackOverflow

8/26/2019

Just to clarify:

As described by community: Amit Kumar Gupta and David Maze there is no good solution natively supported by helm in order to change this behavior without modifying templates. It looks that in your case (without modifying helm templates) the best solution it will be just using set with parameters during helm install.

like:

helm install --set foo=bar --set foo=newbar ./redis
-- Hanx
Source: StackOverflow

8/24/2019

You can use named templates to define re-usable helper templates. E.g.

In templates/_helpers.tpl:

{{- define "myChart.someParam" -}}someval-{{ .Release.Namespace }}{{- end -}}

In templates/configmap.yaml (for example):

apiVersion: v1
kind: ConfigMap
metadata:
  name: something
data:
  foo: {{ template "myChart.someParam" . }}

The result:

$ helm template . --namespace=bar
---
# Source: helm/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: something
data:
  foo: someval-bar
-- Amit Kumar Gupta
Source: StackOverflow

8/24/2019

If you know that someParam might contain templating constructs, Helm includes a tpl function that interprets it.

- name: SOME_VARIABLE
  value: {{ .Values.someParam | tpl }}

If a chart allows this it generally documents it. As a specific example, the helm/charts PostgreSQL chart documents that its configurationConfigMap setting is

ConfigMap with the PostgreSQL configuration files (Note: Overrides postgresqlConfiguration and pgHbaConfiguration). The value is evaluated as a template.

So for that specific value, you can put a template macro in the values.yaml file.

-- David Maze
Source: StackOverflow