Create configmaps from files recursively
Asked Answered
F

4

15

I have multiple configuration files in two directories. For example,

  • conf.d/parentconf1.conf
  • con.d/node1/child1.conf
  • conf.d/node2/child2.conf

I need to mount these configuration files in the same directory structure to kubernetes pod using ConfigMap.

Tried using the

kubectl create configmap --from-file=./conf.d --from-file=./conf.d/node1/child1.conf --from-file=./conf.d/node2/child2.conf. 

Config map created, as expected, cannot express the nested directory structure.

Is it possible to create ConfigMap recursively from folders and still retain the folder structure in the name of the key entry for the ConfigMap - since the intention is to mount these ConfigMaps into pods?

Frightfully answered 22/4, 2019 at 6:34 Comment(0)
R
14

Unfortunately, reflecting directory structure in configmap is not supported currently. Workaround is to express directory hierarchy like this:

apiVersion: v1
kind: ConfigMap
metadata:
   name: testconfig
data:
  file1: |
    This is file1
  file2: |
    This is file2 in subdir directory
---
apiVersion: v1
kind: Pod
metadata:
  name: testpod
spec:
  restartPolicy: Never
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: [ "/bin/sh","-c", "sleep 1000" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: testconfig
        items:
        - key: file2
          path: subdir/file2
        - key: file1
          path: file1
Reber answered 22/4, 2019 at 7:11 Comment(2)
These configuration files are multi-pipline configuration files for logstash. So, there will be 10-12 files already (bound to grow). Currently, I used the approach to mount the nested directory to a different config-map using kubectl. Going with the work-around.Frightfully
Are there a way to mount one additional nested file in the subdir, but the rest of files - as a first-level mount? I have 1 file in a subdirectory and a bunch of them on the first level, so it's not really convenient to list all the files.Thicken
P
14

An automatable workaround: tar your files, map the tar configmap volume file in /tmp, and untar it at the start of the container.

Create the tar:

tar -cvf conf-d.tar ./conf.d
kubectl create configmap conf-d --from-file=conf-d.tar
rm conf-d.tar

and in your pod.yml, add the tar -xf before your command, or before your default image command:

    command: [ "/bin/sh","-c", "tar -xf /tmp/conf-d.tar -C /etc/ && sleep 1000" ]
    volumeMounts:
      - mountPath: /tmp/conf-d.tar
        name: nginx-config-volume
        subPath: conf-d.tar
Pneuma answered 9/1, 2020 at 18:29 Comment(0)
V
6

When writing templates for Helm charts the built-in tooling can be used to create a config map or secret with all files in a directory.

Directory structure:

test
├── bar
│   └── init.sh
├── foo
│   ├── some.sh
│   └── thing.sh
└── README

Helm config map template:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  {{- $files := .Files }}
  {{- range $path, $_ := .Files.Glob "test/**" }}
  {{ $path | replace "/" "." }}: |
{{ $files.Get $path | indent 4 }}
  {{- end }}

Result:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  test.bar.init.sh: |
    echo foo
  test.foo.some.sh: |
    echo foo
  test.foo.thing.sh: |
    echo foo
  test.README: |
    # My title

Tested with helm 3.7.1

Vowel answered 10/11, 2021 at 22:46 Comment(0)
E
0

While the three existing answers are great, here's another approach.

ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config
data:
  config.toml: |
    # Configuration settings for the application
    setting1 = "value1"
    setting2 = "value2"
  keystore-key.json: |
    {
      "key": "your-key-content"
    }
  password.txt: |
    your-password-content

Pod

apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: example-container
    image: your-container-image
    volumeMounts:
    - name: config-volume
      mountPath: /app/config/config.toml
      subPath: config.toml
    - name: config-volume
      mountPath: /app/config/keystore/key.json
      subPath: keystore-key.json
    - name: config-volume
      mountPath: /app/config/secrets/password.txt
      subPath: password.txt
  volumes:
  - name: config-volume
    configMap:
      name: example-config

For cases when the number of files isn't high.

Ebenezer answered 5/8 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.