问题
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.
回答1:
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
回答2:
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
andpgHbaConfiguration
). The value is evaluated as a template.
So for that specific value, you can put a template macro in the values.yaml
file.
回答3:
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
来源:https://stackoverflow.com/questions/57633472/how-can-i-reference-the-namespace-in-values-yaml