Is there any way to get name of the file used in --set-file parameter during helm install/upgrade/template within the helm chart template files?

2/26/2020

helm template sample ./sample-chart --set-file configMaps="{app1.yaml,app2.yaml}"

Is the command i am using to set file as values to the chart. And in the chart i am using it set the files as config map.

configmap.yaml:

{{ range $configMap := .Values.configMaps }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ <<file-name> }}-cm
data: 
{{ $configMap | indent 2}}
{{ end }}

I want to set the config map name, the name of the files whose content is in use as data .i.e app1,app2 in this case. Is there any ways to do this?

-- Sarabesh n.r
configmap
kubernetes
kubernetes-helm

2 Answers

3/4/2020

Just an update on this issue, I have changed the format of the key in --set-file argument.

helm template sample ./sample-chart --set-file configMapFiles.app1-yaml.data="app1.yaml" --set-file configMapFiles.app2-yaml.data="app2.yaml"

And I have changed the template for configMap.yaml as

configmap.yaml :

{{ range $configMapName$configMap := .Values.configMapFiles }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{$configMapName}}-cm
data: 
{{$configMap | indent 2}}: |-
{{ $configMap.data | indent 4}}
---
{{ end }}

This allowed me to set filename in the metadata of the configMap.

-- Sarabesh n.r
Source: StackOverflow

2/26/2020

You can ask Helm to read the file itself when the template is instantiated. Instead of passing in the contents of the files, pass their names directly.

Adapting the basic example from the Helm documentation, you could write a ConfigMap

{{- $files := .Files }}
{{- range .Values.configMaps }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ . }}-cm
data:
  {{ . }}: |-
    {{ $files.Get . }}
{{- end }}

(The actual example puts all of the files into a single ConfigMap and that might be a little easier to manage.)

When you run this, you'd give the names of the files and not their contents at the command line (or an auxiliary values file)

helm template sample ./sample-chart --set configMaps="{app1.yaml,app2.yaml}"
-- David Maze
Source: StackOverflow