0

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>
Forin
  • 1,549
  • 2
  • 20
  • 44
  • *does not work as expected* means ... it rebooted your computer? it drank all your beer? Please [edit your question](https://stackoverflow.com/posts/63581972/edit) and say what did happen and what you expected to happen instead – mdaniel Aug 25 '20 at 15:59
  • @mdaniel edited, my bad – Forin Aug 26 '20 at 07:14

2 Answers2

2

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.

Matt
  • 7,419
  • 1
  • 11
  • 22
  • Works as I wanted, thanks! Additonal question: is there a way to get FIles without the need to define them in `values.yaml`? I've seen there is a `.Files.Glob` wich may be useful. – Forin Aug 26 '20 at 08:36
  • 2
    Yes you can do it with `.Files.Glob`. If you have troubles with using it, preferably create a new question for it to be compliant with stackOverflow guidlines. – Matt Aug 26 '20 at 09:03
0

Try something as below

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

My Chart structure

├── Chart.yaml
├── charts
├── files
│   ├── file1
│   └── file2
├── templates
│   ├── configmap.yaml
│   ├── deployment.yaml
│   ├── secret.yaml
│   └── service.yaml
└── values.yaml
redInk
  • 699
  • 5
  • 11