<p>I have a Pod or Job yaml spec file (I can edit it) and I want to launch it from my local machine (e.g. using <code>kubectl create -f my_spec.yaml</code>)</p>

<p>The spec declares a volume mount. There would be a file in that volume that I want to use as value for an environment variable.</p>

<p>I want to make it so that the volume file contents ends up in the environment variable (without me jumping through hoops by somehow "downloading" the file to my local machine and inserting it in the spec).</p>

<p>P.S. It's obvious how to do that if you have control over the <code>command</code> of the <code>container</code>. But in case of launching arbitrary image, I have no control over the <code>command</code> attribute as I do not know it.</p>

<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  generateName: puzzle
spec:
  template:
    spec:
      containers:
      - name: main
        image: arbitrary-image
        env:
        - name: my_var
          valueFrom: &lt;Contents of /mnt/my_var_value.txt&gt;
        volumeMounts:
        - name: my-vol
          path: /mnt
      volumes:
      - name: my-vol
        persistentVolumeClaim:
          claimName: my-pvc
</code></pre>

<p>You can create deployment with kubectl endless loop which will constantly poll volume and update configmap from it. After that you can mount created configmap into your pod. It's a little bit hacky but will work and update your configmap automatically. The only requirement is that PV must be ReadWriteMany or ReadOnlyMany (but in that case you can mount it in read-only mode to all pods).</p>

<pre><code>apiVersion: v1
kind: ServiceAccount
metadata:
  name: cm-creator
  namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: cm-creator
rules:
- apiGroups: [""]
  resources: ["configmaps"]
  verbs: ["create", "update", "get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: cm-creator
  namespace: default
subjects:
- kind: User
  name: system:serviceaccount:default:cm-creator
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: cm-creator
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cm-creator
  namespace: default
  labels:
    app: cm-creator
spec:
  replicas: 1
  serviceAccountName: cm-creator
  selector:
    matchLabels:
      app: cm-creator
  template:
    metadata:
      labels:
        app: cm-creator
    spec:
      containers:
      - name: cm-creator
        image: bitnami/kubectl
        command:
        - /bin/bash
        - -c
        args:
        - while true; 
            kubectl create cm myconfig --from-file=my_var=/mnt/my_var_value.txt --dry-run -o yaml | kubectl apply -f-;
            sleep 60;
          done
        volumeMounts:
        - name: my-vol
          path: /mnt
          readOnly: true
      volumes:
      - name: my-vol
        persistentVolumeClaim:
          claimName: my-pvc
</code></pre>
