Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes helm, Files.Get and variables

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?

like image 405
Magick Avatar asked Oct 01 '18 00:10

Magick


People also ask

How do I get YAML file from Helm chart?

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.

How do you use variables in Helm?

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 .

Where are Helm files stored?

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.


Video Answer


2 Answers

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 }}
like image 124
David Maze Avatar answered Oct 18 '22 19:10

David Maze


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 }}

like image 28
redzack Avatar answered Oct 18 '22 18:10

redzack