<p>I have a <code>env</code> variable called <code>app_conf_path</code> which points to a <code>\location\file.yaml</code> which in turn contains all the values required for the application to work.The application needs this <code>app_conf_path</code>  which has the location of   <code>file.yaml</code> to run the application. How can i create a <code>configmap</code> for this type of setup.  Right now i am having that <code>file.yaml</code> in a <code>persistentvolume</code> and have that <code>env</code> variable pointing to that <code>mountlocation</code>. I came to know about <code>configmaps</code> only recently. Any help on this would be appreciated.</p>

doc_noob
<blockquote>
  <p>I have a <code>env</code> variable called <code>app_conf_path</code> which points to a <code>\location\file.yaml</code> which in turn contains all the values required for the application to work.The application needs this <code>app_conf_path</code> which has the location of <code>file.yaml</code> to run the application. How can i create a <code>configmap</code> for this type of setup?</p>
</blockquote>

<p>I'll begin talking about the concepts of ConfigMaps:</p>

<ul>
<li>ConfigMap is a dictionary of configuration settings. It consists of key-value pairs of strings.</li>
<li>ConfigMaps are useful to keep your code separate from configuration.</li>
<li>You can generate a configmap <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-directories" rel="nofollow noreferrer">from directories</a>, <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-literal-values" rel="nofollow noreferrer">from literal values</a> or what we want: <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-files" rel="nofollow noreferrer">from a file</a>.</li>
<li><a href="https://kubernetes.io/docs/concepts/storage/volumes/#configmap" rel="nofollow noreferrer">ConfigMap</a> can be treated like a volume in kubernetes:

<blockquote>
  <ul>
  <li>The data stored in a <code>ConfigMap</code> object can be referenced in a volume of type <code>configMap</code> and then consumed by containerized applications running in a Pod.</li>
  <li>When referencing a <code>configMap</code> object, you can simply provide its name in the volume to reference it. You can also customize the path to use for a specific entry in the ConfigMap</li>
  </ul>
</blockquote></li>
</ul>

<p><strong>Creating a ConfigMap From File:</strong></p>

<ul>
<li>To create a configmap you run the command:</li>
</ul>

<p><code>kubectl create configmap &lt;CONFIGMAP_NAME&gt; --from-file=/location/file.yaml</code></p>

<ul>
<li>You can also add more than one file to a single configmap, just repeat the <code>--from-file</code> argument, example:</li>
</ul>

<pre><code>kubectl create configmap &lt;CONFIGMAP_NAME&gt; \
--from-file=path/db.properties \
--from-file=path/ui.properties
</code></pre>

<hr>

<blockquote>
  <p>I want to stop mounting the  <code>persistentvolume</code>  which has this  <code>file.yaml</code>  and the  <code>file.yaml</code>  is a simple  <code>yaml</code>  file with details of  <code>dbconnectionstrings</code>  and  <code>paths</code>  for other  <code>apps</code></p>
</blockquote>

<p>From the concepts we saw above, your intention to stop having to mount the file to a PV to serve the config file can be fully realized using a <code>ConfigMap</code>. </p>

<ul>
<li>I'd like to suggest you <a href="https://theithollow.com/2019/02/20/kubernetes-configmaps/" rel="nofollow noreferrer">The ITHollow ConfigMap Example</a>. I was going to use it here but your app is already built with a function to look for the configuration file outside. I'll leave this link so you can see how you could use a ConfigMap to other apps that needs external configuration and are not hardcoded to look for it in a specific file.</li>
</ul>

<hr>

<p><strong>Reproducible Example:</strong></p>

<ul>
<li><p>This will be a example to show you how to achieve the portion your question requires.</p>

<ul>
<li>It will be a simple <code>ubuntu</code> pod which has a config file mounted in <code>/tmp/file.yaml</code> and that file path will be a Env variable called <code>app_conf_path</code>.</li>
</ul></li>
<li><p>First, I'll create a file called <code>file.yaml</code> and add 3 values:</p></li>
</ul>

<pre><code>$ cat file.yaml 
key1: value1
key2: value2
key3: value3
</code></pre>

<p><strong>NOTE:</strong> <em>the name <code>file.yaml</code> is not very common, I'm using it to emulate your environment, usually we use something like <code>app.properties</code> and it does not require any previous structure, just all values in a <code>key:value</code> pair form, like in my example.</em> </p>

<ul>
<li>Now we will create the configmap called <code>app.config</code> from the file <code>file.yaml</code>. The file is on the same folder I'm running the command, thus I don't have to specify the full path:</li>
</ul>

<pre><code>$ kubectl create configmap app.config --from-file=file.yaml
configmap/app.config created
</code></pre>

<p><em>The filename becomes the reference inside the configmap and will be used later</em>.</p>

<ul>
<li>Let's see the configmap we created:</li>
</ul>

<pre><code>$ kubectl describe configmap app.config
Name:         app.config
Namespace:    default
Labels:       &lt;none&gt;
Annotations:  &lt;none&gt;

Data
====
file.yaml:
----
key1: value1
key2: value2
key3: value3

Events:  &lt;none&gt;
</code></pre>

<ul>
<li>Now your goal is to <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-volume" rel="nofollow noreferrer">add the configmap data to a volume</a>, and add the ENV variable that points <code>app_conf_path</code> to <code>/tmp/file.yaml</code>, here is the <code>app-deploy.yaml</code> for that:</li>
</ul>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: ubuntu
        image: ubuntu
        command: [ "/bin/bash", "-c", "--" ]
        args: [ "while true; do sleep 3000; done;" ]
        volumeMounts:
          - name: config-volume
            mountPath: /tmp
        env:
          - name: app_conf_path
            value: "/tmp/file.yaml"
      volumes:
        - name: config-volume
          configMap:
            name: app.config
</code></pre>

<p><strong>NOTE:</strong> This is a very interesting step. We create a <code>volume</code> using the <code>configmap</code> and we set the location desired to <code>mount</code> that <code>volume</code>. Each section of the <code>configmap</code> will be a file inside that folder. Since we created it from only 1 file, it's the only file that will be mounted. We also set the <code>ENV name</code> you need with the <code>value</code> as the path to the file. </p>

<ul>
<li>Now let's apply it and open a shell inside the pod with <code>kubectl exec -it &lt;POD_NAME&gt; -- /bin/bash</code> to see our result:</li>
</ul>

<pre><code>$ kubectl apply -f app-deploy.yaml 
deployment.apps/my-app created

$ kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
my-app-68b5b69fc8-xxqpw   1/1     Running   0          3s

$ kubectl exec -it my-app-68b5b69fc8-xxqpw -- /bin/bash

root@my-app-68b5b69fc8-xxqpw:/# printenv | grep app_conf_path
app_conf_path=/tmp/file.yaml

root@my-app-68b5b69fc8-xxqpw:/# cat $app_conf_path   
key1: value1
key2: value2
key3: value3
</code></pre>

<hr>

<p>Now we reached the goal of your request.</p>

<p>Inside the pod there is a configuration file called <code>file.yaml</code> with the configuration settings we used to generate the config file.</p>

<p>You don't have to worry about creating and maintaining the volume separately.</p>

<p>If you still have any question about it let me know in the comments.</p>
