<p>I'm currently learning Kubernetes and recently learnt about using ConfigMaps for a Containers environment variables.</p>
<p>Let's say I have the following simple ConfigMap:</p>
<pre><code>apiVersion: v1
data:
  MYSQL_ROOT_PASSWORD: password
kind: ConfigMap
metadata:
  creationTimestamp: null
  name: mycm
</code></pre>
<p>I know that a container of some deployment can consume this environment variable via:</p>
<p><code>kubectl set env deployment mydb --from=configmap/mycm</code></p>
<p>or by specifying it manually in the manifest like so:</p>
<pre><code>containers:
- env:
  - name: MYSQL_ROOT_PASSWORD
    valueFrom:
      configMapKeyRef:
        key: MYSQL_ROOT_PASSWORD
        name: mycm
</code></pre>
<p>However, this isn't what I am after, since I'd to manually change the environment variables each time the ConfigMap changes.</p>
<p>I am aware that mounting a ConfigMap to the Pod's volume allows for the auto-updating of ConfigMap values. I'm currently trying to find a way to set a Container's environment variables to those stored in the mounted config map.</p>
<p>So far I have the following YAML manifest:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mydb
  name: mydb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mydb
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: mydb
    spec:
      containers:
      - image: mariadb
        name: mariadb
        resources: {}
        args: [&quot;export MYSQL_ROOT_PASSWORD=$(cat /etc/config/MYSQL_ROOT_PASSWORD)&quot;]
        volumeMounts:
        - name: config-volume
          mountPath: /etc/config
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: temp
      volumes:
      - name: config-volume
        configMap:
          name: mycm
status: {}
</code></pre>
<p>I'm attempting to set the <code>MYSQL_ROOT_PASSWORD</code> to some temporary value, and then update it to mounted value as soon as the container starts via <code>args: [&quot;export MYSQL_ROOT_PASSWORD=$(cat /etc/config/MYSQL_ROOT_PASSWORD)&quot;]</code></p>
<p>As I somewhat expected, this didn't work, resulting in the following error:</p>
<p><code>/usr/local/bin/docker-entrypoint.sh: line 539: /export MYSQL_ROOT_PASSWORD=$(cat /etc/config/MYSQL_ROOT_PASSWORD): No such file or directory</code></p>
<p>I assume this is because the volume is mounted after the entrypoint. I tried adding a readiness probe to wait for the mount but this didn't work either:</p>
<pre><code>readinessProbe:
          exec:
            command: [&quot;sh&quot;, &quot;-c&quot;, &quot;test -f /etc/config/MYSQL_ROOT_PASSWORD&quot;]
          initialDelaySeconds: 5
          periodSeconds: 5
</code></pre>
<p>Is there any easy way to achieve what I'm trying to do, or is it impossible?</p>

Anthony Seager
<p>So I managed to find a solution, with a lot of inspiration from <a href="https://stackoverflow.com/a/75265250/13557241">this answer</a>.</p>
<p>Essentially, what I did was create a sidecar container based on the alpine K8s image that mounts the configmap and constantly watches for any changes, since the K8s API automatically updates the mounted configmap when the configmap is changed. This required the following script, <code>watch_passwd.sh</code>, which makes use of <code>inotifywait</code> to watch for changes and then uses the K8s API to rollout the changes accordingly:</p>
<pre class="lang-bash prettyprint-override"><code>update_passwd() {
    kubectl delete secret mysql-root-passwd &gt; /dev/null 2&gt;&amp;1
    kubectl create secret generic mysql-root-passwd --from-file=/etc/config/MYSQL_ROOT_PASSWORD
}

update_passwd

while true
do
    inotifywait -e modify &quot;/etc/config/MYSQL_ROOT_PASSWORD&quot;
    update_passwd
    kubectl rollout restart deployment $1
done

</code></pre>
<p>The Dockerfile is then:</p>
<pre><code>FROM docker.io/alpine/k8s:1.25.6
RUN apk update &amp;&amp; apk add inotify-tools
COPY watch_passwd.sh .
</code></pre>
<p>After building the image (locally in this case) as <em>mysidecar</em>, I create the ServiceAccount, Role, and RoleBinding outlined <a href="https://stackoverflow.com/a/75265250/13557241">here</a>, adding rules for deployments so that they can be restarted by the sidecar.</p>
<p>After this, I piece it all together to create the following YAML Manifest (note that <code>imagePullPolicy</code> is set to <code>Never</code>, since I created the image locally):</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: mydb
  name: mydb
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mydb
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: mydb
    spec:
      serviceAccountName: secretmaker
      containers:
      - image: mysidecar
        name: mysidecar
        imagePullPolicy: Never
        command:
          - /bin/sh
          - -c
          - |
            ./watch_passwd.sh $(DEPLOYMENT_NAME)
        env:
          - name: DEPLOYMENT_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.labels['app']
        volumeMounts:
        - name: config-volume
          mountPath: /etc/config
      - image: mariadb
        name: mariadb
        resources: {}
        envFrom:
        - secretRef:
            name: mysql-root-passwd
      volumes:
      - name: config-volume
        configMap:
          name: mycm
status: {}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: secretmaker
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  labels:
    app: mydb
  name: secretmaker
rules:
- apiGroups: [&quot;&quot;]
  resources: [&quot;secrets&quot;]
  verbs: [&quot;create&quot;, &quot;get&quot;, &quot;delete&quot;, &quot;list&quot;]
- apiGroups: [&quot;apps&quot;]
  resources: [&quot;deployments&quot;]
  verbs: [&quot;get&quot;, &quot;list&quot;, &quot;watch&quot;, &quot;update&quot;, &quot;patch&quot;]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  labels:
    app: mydb
  name: secretmaker
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: secretmaker
subjects:
- kind: ServiceAccount
  name: secretmaker
  namespace: default
---
</code></pre>
<p>It all works as expected! Hopefully this is able to help someone out in the future. Also, if anybody comes across this and has a better solution please feel free to let me know :)</p>
