Search code examples
kuberneteskubernetes-helm

Inject file into Helm template


I have a list of properties defined in values.yaml as follows:

files:
 - "file1"
 - "file2"

Then in my template I want to create config maps out of my values.

I came up with the following template:

{{- range $value := .Values.files }}
---
apiVersion: v1
kind: ConfigMap
metadata: 
    name: {{ $value }}
data:
    {{ $value }}: {{ .Files.Get (printf "%s/%s" "files" $value) | indent 4 }}
{{- end }}

As you can see I want to have configmaps with same name as files. I mixed up several parts of documentation, however my template does not work as expected.

How can I achieve configmap creation through templates?

//EDIT I expect to have the following ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata: 
    name: file1
data:
    file1: <file1 content>
---
apiVersion: v1
kind: ConfigMap
metadata: 
    name: file2
data:
    file2: <file2 content>

Solution

  • Try the following:

    {{- $files := .Files }}
    {{- range $value := .Values.files }}
    ---
    apiVersion: v1
    kind: ConfigMap
    metadata: 
      name: {{ $value }}
      data:
        {{ $value }}: |
    {{ $files.Get (printf "%s/%s" "files" $value) | indent 6 }}
    {{- end }}
    

    You problem seems to be with incorrect indentation. Make sure the line with $files.Get starts with no spaces.

    And I also added {{- $files := .Files }} to access .Files, because for some reason range is changeing the . scope.

    I have found this example in documentation but it seems to work only if file's content are one-line. So if your files are one line only, then you should check the example.

    Also notice the | after {{ $value }}:. You need it because file content is a multiline string. Check this StackQuestion on how to use multiline strings in yaml.