Dynamic ConfigMap Helm Template

2/1/2019

I would like to create serveral ConfiMaps with one helm telmplate. Therefore I have created a folder for the configs/values and one configfile per ConfigMap. I have read the helm template guide and found nothing helpful for my problem. Maybe I missunderstood the possibilities of helm.

Afterwards there is a possibility to create one configmap from serveral files:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  {{- $files := .Files }}
  {{- range tuple "file1.yaml" "file2.yaml" }}
  {{ $files.Get . }}
  {{- end }}

Any recommendations would be helpful, Thanks,

Best wishes

-- soa
kubernetes
kubernetes-helm
templates

2 Answers

2/1/2019

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 testchart
rm -rf testchart/templates/*

2) create 2 Configmaps yaml files in templates/

configmap1.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap-1
data:
  myvalue: "My Drinks:"
  drink1: {{ .Values.config1test.drink1 }}
  drink2: {{ .Values.config1test.drink2 }}
  drink3: {{ .Values.config1test.drink3 }}

configmap2.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap-2
data:
  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/testchart

NAME:   tan-frog
REVISION: 1
RELEASED: Fri Feb  1 13:10:46 2019
CHART: testchart-0.1.0
USER-SUPPLIED VALUES:
config1test:
  drink1: coffee
  drink2: tea
  drink3: juice
config2test:
  food1: meat
  food2: fish
  food3: salad

COMPUTED VALUES:
affinity: {}
config1test:
  drink1: coffee
  drink2: tea
  drink3: juice
config2test:
  food1: meat
  food2: fish
  food3: salad
fullnameOverride: ""
image:
  pullPolicy: IfNotPresent
  repository: nginx
  tag: stable
ingress:
  annotations: {}
  enabled: false
  hosts:
  - chart-example.local
  paths: []
  tls: []
nameOverride: ""
nodeSelector: {}
replicaCount: 1
resources: {}
service:
  port: 80
  type: ClusterIP
tolerations: []

HOOKS:
MANIFEST:


---
# Source: testchart/templates/configmap1.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: tan-frog-configmap-1
data:
  myvalue: "My Drinks:"
  drink1: coffee
  drink2: tea
  drink3: juice
---
# Source: testchart/templates/configmap2.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: tan-frog-configmap-2
data:
  myvalue: "My food:"
  food1: meat
  food2: fish
  food3: salad

5) Install chart

helm install -f ./testchart/myvals1.yaml -f ./testchart/myvals2.yaml ./testchart

NAME:   unsung-grizzly
LAST DEPLOYED: Fri Feb  1 13:13:15 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/ConfigMap
NAME                        DATA  AGE
unsung-grizzly-configmap-1  4     0s
unsung-grizzly-configmap-2  4     0s

6) Verify ConfigMaps:

kubectl get configmaps -o wide

NAME                         DATA   AGE
unsung-grizzly-configmap-1   4      61s
unsung-grizzly-configmap-2   4      61s
-- VKR
Source: StackOverflow

2/2/2019

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: v1
kind: ConfigMap
metadata:
  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.

-- soa
Source: StackOverflow