Create more configMaps via Go templating
Go programming: Create more configMaps via Go templating with working code examples.
The following Helm template snippet uses Go templating to iterate over all JSON files in a dashboards/ directory and generate a separate Kubernetes ConfigMap for each one. This is commonly used to provision Grafana dashboards automatically via sidecar.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{{ range $path, $_ := .Files.Glob "dashboards/*.json" }}
{{- $dashboardName := trimSuffix ".json" $path | base | replace "_" "-" | lower -}}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: "{{ template "grafana.fullname" $ }}-{{ $dashboardName }}"
namespace: {{ template "grafana.namespace" $ }}
labels:
{{- include "grafana.labels" $ | nindent 4 }}
grafana_dashboard: "1"
data:
{{ $dashboardName }}.json: |-
{{ $.Files.Get $path | indent 4}}
{{ end }}
This post is licensed under CC BY 4.0 by the author.