How to create a config map from a file in helm?

7/26/2019

I have a container running in a pod which requires a single config file. The deployment/service etc are deployed using helm, and ideally I'd like to use the same method to setup the config, and if possible, I'd like to use helm's templating engine to template the config file.

I came across this: https://www.nclouds.com/blog/simplify-kubernetes-deployments-helm-part-3-creating-configmaps-secrets/

I have the following file structure:

/chart
  /templates
    my-config-map.yaml
  /config
    application.config

and my-config-map.yaml contains the following:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
  labels:
    app: {{ template "app.prefix" . }}
data:
  {{ (tpl (.Files.Glob "config/*").AsConfig . ) | indent 2 }}

When I run this command:

kubectl get configmaps my-config -n my-namespace -o yaml

I get:

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2019-07-26T11:11:05Z
  labels:
    app: my-app
  name: my-config
  namespace: my-namespace
  resourceVersion: "2697856"
  selfLink: <selflink>
  uid: 0fe63ba8-af96-11e9-a73e-42010af00273

Note that it does not appear to have any data in it. However if I create it from the command line using this command:

kubectl --namespace my-namespace create configmap my-config --from-file=application.conf

I get this, which does appear to contain the data:

apiVersion: v1
data:
  application.conf: |-
    conf {
      ...
kind: ConfigMap
metadata:
  creationTimestamp: 2019-07-26T11:00:59Z
  name: my-config
  namespace: my-namespace
  resourceVersion: "2695174"
  selfLink: <selflink>

What exactly am I doing wrong?

-- Andy
google-cloud-platform
google-kubernetes-engine
kubernetes
kubernetes-helm

2 Answers

7/26/2019

My guess is that Helm is looking for the "config" under the "templates" folder.

Try moving the config folder under templates:

/chart
  /templates
    my-config-map.yaml
    /config
      application.config

or change

(.Files.Glob "config/*") 

to

(.Files.Glob "../config/*")

Also, keep in mind that the tpl function is introduced in Helm 2.5.

Hope that helps!

-- cecunami
Source: StackOverflow

2/3/2020

For what it's worth, you are indenting by 2, but have already indented by two in the pasted code. So, effectively indenting by 4. You want:

data:
{{ (tpl (.Files.Glob "config/*").AsConfig . ) | indent 2 }}
-- Jay Carroll
Source: StackOverflow