Andy
Andy

Reputation: 3522

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

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?

Upvotes: 6

Views: 25560

Answers (4)

PjoterS
PjoterS

Reputation: 14102

Background

This tutorial and question is quite old (Asked 1 year, 4 months ago). The currently used version of Helm is 3. It does not require Tiller.

As you stated in your question, you have to use Glob and put files into proper directories. I have tested this on my GKE cluster with Helm3.

$ helm version
version.BuildInfo{Version:"v3.2.1"

Solution - Steps

1. Create test-chart

I have created it in my /home/user directory

$ helm create test-chart
Creating test-chart
$ ls
postgress test-chart

2. Create directory where you will put/download file with configuration

$ cd test-chart/
$ ls
charts  Chart.yaml  templates  values.yaml
$ mkdir configmap
$ ls
charts  Chart.yaml  configmap  templates  values.yaml
$ cd configmap/

I have used example from Kubernetes docs.


$ wget https://kubernetes.io/examples/configmap/ui.properties
...
ui.properties                               100%[===========================================================================================>]      83  --.-KB/s    in 0s

2020-12-14 15:14:14 (687 KB/s) - ‘ui.properties’ saved [83/83]
...
user@cloudshell:~/test-chart/configmap (project)$ cat ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

3. Create ConfigMap file in templates directory

$ cd ~/test-chart/templates

Create configmap yaml like below.

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config
data:
  {{- (.Files.Glob "configmap/*").AsConfig | nindent 2 }}

4. Install chart / use --dry-run

Go to main directory ~/test-chart, where you have files like below

charts  Chart.yaml  configmap  templates  values.yaml

Now you can helm install chart or use --dry-run option before, to check how configuration YAMLs would looks like.

5. Output

$ helm install test . --dry-run
NAME: test
LAST DEPLOYED: Mon Dec 14 15:24:38 2020
NAMESPACE: default
STATUS: pending-install
REVISION: 1
HOOKS:
---
# Source: test-chart/templates/tests/test-connection.yaml
...
---
# Source: test-chart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config
data:
  ui.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice
---
# Source: test-chart/templates/service.yaml

Conclusion

File which will be base for configmap must be in the directory, which is on the same level as template directory and Chart.yaml. You also have to use Files.Glob and provide proper path to configuration file.

Upvotes: 9

张馆长
张馆长

Reputation: 1849

I test, this will work

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
  labels:
    app: {{ template "app.prefix" . }}
data:
  application.conf: |-
{{ .Files.Get "config/application.config" | nindent 4 }}

Upvotes: 1

Jay Carroll
Jay Carroll

Reputation: 55

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 }}

Upvotes: 0

cecunami
cecunami

Reputation: 1037

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!

Upvotes: 0

Related Questions