<p>I have a yml file that I took from a github repository.</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: scripts-cm
data:
  locustfile.py: |
    from locust import HttpLocust, TaskSet, task


    class UserTasks(TaskSet):

        @task
        def index(self):
            self.client.get(&quot;/&quot;)

        @task
        def stats(self):
            self.client.get(&quot;/stats/requests&quot;)


    class WebsiteUser(HttpLocust):
        task_set = UserTasks
</code></pre>
<p>But I don't want to set my configure management to do this as I have a separate locustfile.py and it is quite big. Is there a way to copy the file in the data attribute instead? or I have to use cat command?</p>

Moses Liao GZ
<blockquote>
<p>Is there a way to copy the file in the data attribute instead?</p>
</blockquote>
<p>Yes there is. You can create a <code>configMap</code> from a file and mount this configMap as a Volume to a <code>Pod</code>. I've prepared an example to show you the whole process.</p>
<p>Let's assume that you have your <code>locustfile.py</code> with only the Python script:</p>
<pre class="lang-py prettyprint-override"><code>from locust import HttpLocust, TaskSet, task


class UserTasks(TaskSet):

    @task
    def index(self):
        self.client.get(&quot;/&quot;)

    @task
    def stats(self):
        self.client.get(&quot;/stats/requests&quot;)


class WebsiteUser(HttpLocust):
    task_set = UserTasks
</code></pre>
<p>You will need to create a <code>configMap</code> from this file with following command:</p>
<ul>
<li><code>$ kubectl create configmap scripts-cm --from-file=locustfile.py</code></li>
</ul>
<p>After that you can mount this <code>configMap</code> as a <code>Volume</code> like in the example below:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: ubuntu
spec:
  containers:
    - name: ubuntu
      image: ubuntu
      command: 
      - sleep 
      - &quot;infinity&quot; 
      volumeMounts:
      - name: config-volume
        mountPath: /app/ # &lt;-- where you would like to mount your files from a configMap 
  volumes:
    - name: config-volume
      configMap:
        name: scripts-cm # &lt;-- name of your configMap
  restartPolicy: Never
</code></pre>
<p>You can then check if your files are placed correctly in the <code>/app</code> directory:</p>
<ul>
<li><code>$ kubectl exec -it ubuntu -- ls /app/</code></li>
</ul>
<pre class="lang-sh prettyprint-override"><code>locustfile.py
</code></pre>
<blockquote>
<p>A tip!</p>
<p>You can add multiple files to a <code>configMap</code> by</p>
<ul>
<li><code>--from-file=one.py --from-file=two.py ...</code></li>
</ul>
<p>You can also run <code>--from-file=.</code> (use all the files from directory).</p>
</blockquote>
<hr />
<p>Additional resources:</p>
<ul>
<li><em><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">Kubernetes.io: Configure pod container: Configure pod configmap</a></em></li>
<li><em><a href="https://kubernetes.io/docs/concepts/configuration/configmap/" rel="nofollow noreferrer">Kubernetes.io: Configmap</a></em></li>
</ul>
