Helm Chart - Range for Files.Get function to get content from file and put them in ConfigMap

9/16/2021

Im trying to use recursion in order to be adding contents of files in the ConfigMap.yaml but i dont seem to get it right. I dont get the content of the file no matter what i try. The location of the file is correct because i can get the content without recursion. {{.Files.Get "config/gitlab.conf" | indent 4 -}}

Any help would be greatly appreciated! Thank you

ConfigMap.yaml:

{{- if .Values.volConfigMap}}
{{- range .Values.volConfigMap }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .config.configName }}
  namespace: {{ $.Release.Namespace }}
data:
{{- if  .config.file }}
{{- range $path, $config := .config.file }}
  {{ $path }}: |
{{ tpl ($.Files.Get $config) $ | indent 4 -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}

Values.yaml:

config:
        configName: gitlab-conf
        file:
          logstash.conf: config/gitlab.conf

OUTPUT:

apiVersion: v1
kind: ConfigMap
metadata:
  name: gitlab-conf
  namespace: elk
data:
  logstash.conf: |
-- antonisnyc
helm3
kubernetes
kubernetes-helm

1 Answer

9/17/2021

The |- marker in YAML takes a multi-line string. This can be a useful technique for embedding big blocks of data inside of your manifests, as exemplified here.

{{- if .Values.volConfigMap }}
{{- range .Values.volConfigMap }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .config.configName }}
  namespace: {{ $.Release.Namespace }}
data:
  {{- if .config.file }}
  {{- range $k, $v := .config.file }}
  {{ $k }}: |-
    {{ $.Files.Get $v | nindent 4 }}
  {{- end -}}
  {{- end -}}
{{- end -}}
{{- end -}}

helm -|

-- z.x
Source: StackOverflow