Kubernetes helm, Files.Get and variables

10/1/2018

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?

-- Magick
kubernetes
kubernetes-helm

2 Answers

7/6/2019

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 seprate 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 }}
-- redzack
Source: StackOverflow

10/1/2018

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 }}
-- David Maze
Source: StackOverflow