I'm trying to dynamically specify the name of a file to include in a configmap.yaml, using the Helm templating language.
Here is an example:
{{- $filename := .Values.KRB5_REALM -}}
apiVersion: v1
data:
# When the config map is mounted as a volume, these will be created as files.
krb5.conf: |
{{ .Files.Get $filename".krb5.conf" | indent 4 }}
kind: ConfigMap
metadata:
name: {{ template "myapp.fullname" . }}
labels:
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
app: {{ template "myapp.name" . }}
environment: {{ .Values.environment }}
The above code results in an error.
I've tried several variations but without any success, such as:
{{ .Files.Get .Values.KRB5_REALM".krb5.conf" | indent 4 }}
How can I resolve this issue?
You can use the helm native commands to achieve this. In helm, there is a command called helm template . Using the template command you can convert any helm chart to a YAML manifest. The resultant manifest file will have all the default values set in the helm values.
In Helm templates, a variable is a named reference to another object. It follows the form $name . Variables are assigned with a special assignment operator: := . We can rewrite the above to use a variable for Release.Name .
All template files are stored in a chart's templates/ folder. When Helm renders the charts, it will pass every file in that directory through the template engine.
The usual way to assemble strings like this is with the Go text/template printf
function. One way to do it could be:
{{ printf "%s.krb5.conf" .Values.KRB5_REALM | .Files.Get | indent 4 }}
Or you could parenthesize the expression:
{{ .Files.Get (printf "%s.krb5.conf" .Values.KRB5_REALM) | indent 4 }}
Somehow i stumbled upon this post and thanks to @david maze, i would like to add few things. So what if there are more than two arguments and the file is in a directory how to use it then. My use-case was to add all the configuration files those were in json into a separate directory named config that was created inside the helm directory. This is how i rolled it out. Hope this helps:
The values.yaml file
config_service_version: v1
config_service_dir: config
service: test
The configmap.yaml file
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Values.service }}-{{ .Values.config_service_version }}
data:
config.json: |-
{{ .Files.Get (printf "%s/%s-%s.json" .Values.config_service_dir .Values.service .Values.config_service_version ) | indent 4 }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With