Search code examples
kuberneteskubernetes-helm

How can I generate a configmap from a directory of files that need to be templated?


I can generate the ConfigMap from the directory but they are not translating the template directives or values. Below is an example of the Release.Namespace template directive not being output in the ConfigMap.

.
|____Chart.yaml
|____charts
|____.helmignore
|____templates
| |____my-scripts.yaml
|____values.yaml
|____test-files
  |____test1.txt
---
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-scripts
  namespace: {{ .Release.Namespace }}
data:
  test.txt: |-
{{ .Files.Get "test-files/test1.txt" | indent 4}}
# test-files/test1.txt
test file
{{ .Release.Namespace }}

When I run helm install . --dry-run --debug --namespace this-should-print here's what I'm getting vs what I'm expecting:

Actual:

---
# Source: test/templates/my-scripts.yaml
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-scripts
  namespace: test
data:
  test.txt: |-
    # test-files/test1.txt
    test file
    {{ .Release.Namespace }}

Expected:

---
# Source: test/templates/my-scripts.yaml
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-scripts
  namespace: test
data:
  test.txt: |-
    # test-files/test1.txt
    test file
    this-should-print

Alternatively, I would be interested in every file in a specified directory being output in the format like:

<filename>: |-
  <content>

Solution

  • I've found a way of doing it using the tpl function:

    ---
    # templates/myscripts.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-scripts
      namespace: {{ .Release.Namespace }}
    data:
      test.txt: |-
    {{ tpl ( .Files.Get "test-files/test1.txt" ) . | indent 4 }}
    

    The new output is exactly as expected:

    # templates/myscripts.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-scripts
      namespace: this-should-print
    data:
      test.txt: |-
        # test-files/test1.txt
        test file
        this-should-print
    

    And for bonus points, getting all files from a directory without having to update this list within the config map:

    ---
    # templates/myscripts.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-scripts
      namespace: {{ .Release.Namespace }}
    data:
    {{ tpl (.Files.Glob "groovy-scripts/*").AsConfig . | indent 4 }}