Create multiple configmaps on kubernetes with using yaml files

6/18/2021

I have a few yaml files that contains some values. I want to read that files while helm deploying and create configmaps for each of them.

I've added config file under the helm charts. ( Same level with templates folder )

chart structure

And then I've tried to create 'configmap-creator.yaml' which is located under the 'templates' folder.

I simply run 'helm upgrade --install ealpkar --namespace ealpkar --create-namespace .' It was complete successfully but there is only one configmap which is called 'config2-configmap'. I missed the first one ( config1-configmap )

Here is the 'configmap-creator.yaml'

{{- $files := .Files }}
{{- range $key, $value := .Files }}
{{- if hasPrefix "config/" $key }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $key | trimPrefix "config/" | replace ".yaml" "" | replace "_" "-" }}-configmap
data:
{{ $key | trimPrefix "config/" }}: {{ $files.Get $key | quote }}
{{- end }}
{{- end }}

Example of yaml file which is under 'config' folder;

  • config1.yaml

    dummy_product:
      ip: 10.10.10.10
      port: 22
  • config2.yaml

    dummy_product_2:
      ip: 10.10.10.20
      port: 22
-- ealpkar
configmap
kubernetes
kubernetes-helm

1 Answer

6/18/2021

Fix your template, adding a separator between objects.

{{- $files := .Files }}
{{- range $key, $value := .Files }}
{{- if hasPrefix "config/" $key }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ $key | trimPrefix "config/" | replace ".yaml" "" | replace "_" "-" }}-configmap
data:
  {{ $key | trimPrefix "config/" }}: {{ $files.Get $key | quote }}
{{- end }}
{{- end }}
-- SYN
Source: StackOverflow