Load contents of yml as configmap in k8s using helm

11/22/2019

Is it possible to load the contents of yml as configamp in k8s using Helm Charts? Helm Chart for ConfigMap

{{- define "common.configmap.tpl" -}}
kind: ConfigMap
apiVersion: v1
{{ template "common.metadata" . }}
  namespace: {{ .Values.namespace | default "default" }}
data:
{{ range $path, $bytes := .Files.Glob "config/*.yml" }}
  {{- $.Files.Get $path | indent 2 }}
{{ end }}
{{- end }}
{{- define "common.configmap" -}}
{{- template "common.util.merge" (append . "common.configmap.tpl") -}}
{{- end }}

Sample application.yml.

server:
  port: 8080
  name: localhost
sample:
    groupa:
        param1: value1base
        param2: value2base
    higherparam: valuehigherbase

expected configmap

apiVersion: v1
data:
  server_port: 8080
  server_name: localhost
  sample_groupa_param1: value1base
  sample_groupa_param2: value2base
  sample_higherparam: valuehigherbase
kind: ConfigMap
metadata:
  labels:
    app: test
    chart: test-1.0.0
  name: test-cm
  namespace: default
-- Sunil Gajula
configmap
kubernetes
kubernetes-helm

1 Answer

11/22/2019

This docs might be of help Accessing Files Inside Templates.

You should be able to tweak this to work for you:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  config.json: |-
    {
{{- range $key, $val := .Values.json }}
{{ $key | quote | indent 6}}: {{ $val | quote }}
{{- end}}
    }

values.yaml

json:
  key1: val1
  key2: val2
  key3: val3
apiVersion: v1
kind: ConfigMap
metadata:
  name: mangy-hare-configmap
data:
  config.json: |-
    {
      "key1": "val1"
      "key2": "val2"
      "key3": "val3"
    }

Or you can try loading the yaml using following way:

{{ (.Files.Glob "files/indexes.conf").AsConfig | indent 2 }}
-- Crou
Source: StackOverflow