Create configmaps from files recursively

4/22/2019

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?

-- dexter2305
configmap
kubernetes

2 Answers

1/9/2020

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
-- Vincent J
Source: StackOverflow

4/22/2019

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
-- Vasily Angapov
Source: StackOverflow