<p><strong>Problem</strong></p>

<ul>
<li>We want to reuse the same image</li>
<li>We have different static files (e.g. css) that should vary among different deployments. The folder-structure of these files should be preserved.</li>
</ul>

<p>e.g:</p>

<ol>
<li>test1.xml</li>
<li>test2.xml</li>
<li>layout

<ol> 3.1 test1.css</ol>

<ol> 3.1 test2.css</ol></li>
</ol>

<p>We need to put those files once (at startup) into the Pod.</p>

<p>As far as I know, I can only create one ConfigMap of all files in one directory-level and a I have to recreate the structure.</p>

<p>e.g. </p>

<pre><code>kubectl create configmap style-files --from-file=.
</code></pre>

<p>In our example above, this command would only create a ConfigMap for the xml-files.</p>

<p><strong>Expected Behaviour</strong></p>

<p>Something that behaves like a ConfigMap, but is able to create a single ConfigMap that contains all files in the current directory and all files of the subdirectory while preserving the folder structure.</p>

<p><strong>Question</strong></p>

<p>Is there some concept that is made for my use case or do I have to use ConfigMaps?</p>

Andre Hartmann
<p>Example files in the directory:</p>

<pre><code>.
├── test21.css
├── test22.css
├── test2.xml
└── test.xml

Create configmap:
    kubectl create configmap example --from-file=./
</code></pre>

<p>example configmap:</p>

<pre><code>apiVersion: v1
data:
  test.xml: |
    test1
    test1
  test2.xml: |
    test2
    test2
  test21.css: |
    test21
    test21
  test22.css: |
    test22
    test22
kind: ConfigMap
</code></pre>

<p>Example pod with the volume where ConfigMap keys are projected: </p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: busy
spec:
  containers:
  - name: busybox
    image: k8s.gcr.io/busybox
    command: ["/bin/sh"]
    args: ["-c", "sleep 200"]
    volumeMounts:
    - mountPath: /test
      name: data1
  volumes:
    - name: data1
      configMap:
        name: example
        items:
        - key: test.xml
          path: test.xml
        - key: test2.xml
          path: test2.xml
        - key: test21.css
          path: layout/test21.css
        - key: test22.css
          path: layout/test22.css
</code></pre>

<p>Note:</p>

<blockquote>
  <p>You can project keys to specific paths and specific permissions on a per-file basis.</p>
  
  <p>You can combine this example with different sources like secrets and configmaps using <a href="https://kubernetes.io/docs/concepts/storage/volumes/#projected" rel="nofollow noreferrer">projected volume</a>:
  A projected volume maps several existing volume sources into the same directory.</p>
</blockquote>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: busy
spec:
  containers:
  - name: busybox
    image: k8s.gcr.io/busybox
    command: ["/bin/sh"]
    args: ["-c", "sleep 200"]
    volumeMounts:
    - mountPath: /test
      name: data1
  volumes:
    - name: data1
      projected:
        sources:
        - configMap:
            name: example
            items:
              - key: test.xml
                path: test.xml
              - key: test2.xml
                path: test2.xml
              - key: test21.css
                path: layout/test21.css
              - key: test22.css
                path: layout/test22.css
</code></pre>

<p>Another approach is to use zip/jar file as configmap (configmap support binary file) so after mounting it can be unzipped into desired path inside your container or using init container to prepare appropriate folder structure or build images with repopulated data.</p>

<p>Resources:</p>

<ul>
<li>Configure a <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-projected-volume-storage/#configure-a-projected-volume-for-a-pod" rel="nofollow noreferrer">projected volume for a pod</a> </li>
<li>Configure a <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">Pod to Use a ConfigMap</a> </li>
<li><a href="https://unofficial-kubernetes.readthedocs.io/en/latest/tasks/configure-pod-container/configmap/" rel="nofollow noreferrer">unofficial-kubernetes</a> </li>
<li>Getting Started with <a href="https://dev.to/jmarhee/getting-started-with-kubernetes-initcontainers-and-volume-pre-population-j83" rel="nofollow noreferrer">Kubernetes InitContainers and Volume pre-population</a> </li>
</ul>

<p>Hope this help.</p>
