<p>I have a requirement where i push bunch of key value pairs to a text/json file. Post that, i want to import the key value data into a configMap and consume this configMap within a POD using kubernetes-client API's.</p>

<p>Any pointers on how to get this done would be great.</p>

<p>TIA</p>

<p>You can do it in two ways.</p>

<h3>Create ConfigMap from file as is.</h3>

<p>In this case you will get ConfigMap with filename as a key and filedata as a value.</p>

<p>For example, you have file <code>your-file.json</code> with content <code>{key1: value1, key2: value2, keyN: valueN}</code>. </p>

<p>And <code>your-file.txt</code> with content </p>

<p><code>
key1: value1
key2: value2
keyN: valueN</code></p>

<pre><code>kubectl create configmap name-of-your-configmap --from-file=your-file.json
kubectl create configmap name-of-your-configmap-2 --from-file=your-file.txt
</code></pre>

<p>As result:</p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: name-of-your-configmap
data:
  your-file.json: |
    {key1: value1, key2: value2, keyN: valueN}

apiVersion: v1
kind: ConfigMap
metadata:
  name: name-of-your-configmap-2
data:
  your-file.txt: |
    key1: value1
    key2: value2
    keyN: valueN
</code></pre>

<p>After this you can mount any of ConfigMaps to a Pod, for example let's mount <code>your-file.json</code>:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh","-c","cat /etc/config/keys" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: name-of-your-configmap
        items:
        - key: your-file.json
          path: keys
restartPolicy: Never
</code></pre>

<p>Now you can get any information from your <code>/etc/config/your-file.json</code> inside the Pod. Remember that data is read-only.</p>

<h2>Create ConfigMap from file with environment variables.</h2>

<p>You can use special syntax to define pairs of <code>key: value</code> in file. 
These syntax rules apply:</p>

<ul>
<li>Each line in a file has to be in VAR=VAL format.</li>
<li>Lines beginning with # (i.e. comments) are ignored.</li>
<li>Blank lines are ignored.</li>
<li>There is no special handling of quotation marks (i.e. they will be part of the ConfigMap value)).</li>
</ul>

<p>You have file <code>your-env-file.txt</code> with content</p>

<p><code>key1=value1
key2=value2
keyN=valueN
</code></p>

<pre><code>kubectl create configmap name-of-your-configmap-3 --from-env-file=you-env-file.txt
</code></pre>

<p>As result:</p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: name-of-your-configmap-3
data:
  key1: value1
  key2: value2
  keyN: valueN
</code></pre>

<p>Now you can use ConfigMap data as Pod environment variables:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod-2
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: name-of-your-configmap-3
              key: key1
        - name: LOG_LEVEL
          valueFrom:
            configMapKeyRef:
              name: name-of-your-configmap-3
              key: key2
        - name: SOME_VAR
          valueFrom:
            configMapKeyRef:
              name: name-of-your-configmap-3
              key: keyN
  restartPolicy: Never
</code></pre>

<p>Now you can use these variables inside the Pod.</p>

<p>For more information check for <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="noreferrer">documentation</a> </p>
