Dynamic ConfigMap Helm Template
thank you for the response. I have something different in mind. My new code makes it a little bit more clearly.
{{ range $k, $v := .Values.configs }}apiVersion: v1kind: ConfigMapmetadata: name: configmap namespace: {{ $.Values.namespace }} labels: app: "{{base $v}}"data: key: {{$k}} value: {{$v}}{{ $.Files.Get $v }}{{ end }}
I have a Loop over the ConfigMap. My values.yaml looks like
configs name: configs/file1 name: configs/file2
The values are in a separate folder configs, one file per configmap.
The current problem is, that the result is one ConfigMap with the values of file2. I would expect two ConfigMaps. What is wrong here in my template.
Thank you very much.
Generally Tiller
renders all templates that are in templates/
directory.So if I correctly understand your question - you can start with below easy example:
1)create test chart and remove all predefined templates
helm create testchartrm -rf testchart/templates/*
2) create 2 Configmaps yaml files in templates/
configmap1.yaml:
apiVersion: v1kind: ConfigMapmetadata: name: {{ .Release.Name }}-configmap-1data: myvalue: "My Drinks:" drink1: {{ .Values.config1test.drink1 }} drink2: {{ .Values.config1test.drink2 }} drink3: {{ .Values.config1test.drink3 }}
configmap2.yaml:
apiVersion: v1kind: ConfigMapmetadata: name: {{ .Release.Name }}-configmap-2data: myvalue: "My food:" food1: {{ .Values.config2test.food1 }} food2: {{ .Values.config2test.food2 }} food3: {{ .Values.config2test.food3 }}
3) create values files(1 or more depending on your implementation. I created 2)
myvals1.yaml:
config1test: drink1: coffee drink2: tea drink3: juice
myvals2.yaml:
config2test: food1: meat food2: fish food3: salad
4) test template rendering before applying:
helm install --dry-run --debug -f ./testchart/myvals1.yaml -f ./testchart/myvals2.yaml ./testchart[debug] Created tunnel using local port: '37605'[debug] SERVER: "127.0.0.1:37605"[debug] Original chart version: ""[debug] CHART PATH: /home/vkryvoruchko/testchartNAME: tan-frogREVISION: 1RELEASED: Fri Feb 1 13:10:46 2019CHART: testchart-0.1.0USER-SUPPLIED VALUES:config1test: drink1: coffee drink2: tea drink3: juiceconfig2test: food1: meat food2: fish food3: saladCOMPUTED VALUES:affinity: {}config1test: drink1: coffee drink2: tea drink3: juiceconfig2test: food1: meat food2: fish food3: saladfullnameOverride: ""image: pullPolicy: IfNotPresent repository: nginx tag: stableingress: annotations: {} enabled: false hosts: - chart-example.local paths: [] tls: []nameOverride: ""nodeSelector: {}replicaCount: 1resources: {}service: port: 80 type: ClusterIPtolerations: []HOOKS:MANIFEST:---# Source: testchart/templates/configmap1.yamlapiVersion: v1kind: ConfigMapmetadata: name: tan-frog-configmap-1data: myvalue: "My Drinks:" drink1: coffee drink2: tea drink3: juice---# Source: testchart/templates/configmap2.yamlapiVersion: v1kind: ConfigMapmetadata: name: tan-frog-configmap-2data: myvalue: "My food:" food1: meat food2: fish food3: salad
5) Install chart
helm install -f ./testchart/myvals1.yaml -f ./testchart/myvals2.yaml ./testchartNAME: unsung-grizzlyLAST DEPLOYED: Fri Feb 1 13:13:15 2019NAMESPACE: defaultSTATUS: DEPLOYEDRESOURCES:==> v1/ConfigMapNAME DATA AGEunsung-grizzly-configmap-1 4 0sunsung-grizzly-configmap-2 4 0s
6) Verify ConfigMaps:
kubectl get configmaps -o wideNAME DATA AGEunsung-grizzly-configmap-1 4 61sunsung-grizzly-configmap-2 4 61s