<p>I have a python program that can control a Kubernetes cluster (from outside). During that program execution, it obtains a byte array.</p>

<p>I have full pod spec ready to be created.</p>

<p>I need to modify the pod spec (adding an init container) so that when the main container starts, there is a file somewhere with those exact bytes.</p>

<p>What's the easiest way to do that?</p>

Ark-kun
<p>If I understand your question correctly, you want to run a Python script that will extract or derive a byte array from somewhere before your Pod starts, and writes this byte array to a file for your actual application to read from when running inside the Pod.</p>

<p>I can see 2 ways to achieve this:</p>

<ul>
<li>Slightly modify your Docker image to run your script as an entrypoint and then running your application (command: &amp; args: in your Pod spec). You would ship both together and won't need an initContainer.</li>
<li>Or as you were tending for: use a combination of initContainer and Volumes</li>
</ul>

<p>For the later:</p>

<pre><code>template:
  spec:
    volumes:
    - name: byte-array
      emptyDir: {}
    initContainers:
        - name: byte-array-generator
          image: your/init-image:latest
          command: ["/usr/bin/python", "byte_array_generator.py"]
          volumeMounts:
            - mountPath: /my/byte-array/
              name: byte-array
    containers:
    - name: application
      image: your/actual-app:latest
      volumeMounts:
        - name: byte-array
          mountPath: /byte-array/
</code></pre>

<p>I condensed all 3 parts:</p>

<ul>
<li>1 empty volume definition used to pass over the file</li>
<li>1 initContainer with your script generating the byte array and writing it to disk in let's say /my/byte-array/bytearray.bin (where the volume has been mounted)</li>
<li>1 actual container running your application and reading the byte array from /byte-array/bytearray.bin (where the volume has been mounted)</li>
</ul>

<p>One important note from there you also need to take into consideration is: if you mount your volume on a pre-existing folder with actual files, they will all "overwritten" by your volume. The source will take the destination's place.
You might be able to prevent that using <code>subPath</code> but I never tried with this way, I only know it works if you mount ConfigMaps as volumes.</p>

<p><strong><em>Edit</strong>: the answer to your comment is too long</em></p>

<p>Outside the container or outside the Kubernetes cluster ? Maybe there is a misconception, so just in case: an initContainer doesn't have to use the same image as your Pod's. You could even load your script as a ConfigMap and mount it into the initContainer using Python base image to run it if you want...</p>

<p>But if really your script has to be running outside of the cluster and send a file enabling your Pod to start, I'd suggest you add a logic to your byte generation that will output it to a file taking the Pod hostname for example (from Kubernetes API), and scp it to the Kubernetes node running it (pulled from Kubernetes API too) into a know destination. Just define a folder on <strong>each</strong> of your nodes, like <code>/var/data/your_app/</code> and mount it on all your pods.</p>

<pre><code>volumes:
  - hostPath:
       path: /var/data/your_app
      type: Directory
    name: bite-arrays
</code></pre>

<p>then mount <code>bite-arrays</code> wherever you want in whatever container needs to read it by reusing its hostname (to allow you to scale if necessary).
Since you said your script is controlling the cluster, I assume it's already talking to Kubernetes' API... You might also want to create a logic to cleanup left-overs...</p>

<p>Or maybe we got it all wrong and somehow your script is also generating and applying the Pod spec on the fly, which in this case could just be solved by an environment variable or a ConfigMap shipped alongside...</p>

<p><strong>Pod spec:</strong></p>

<pre><code>volumes:
  - name: byte-array
    configMap:
      name: your-app-bytes

volumeMounts:
  - name: byte-array
    mountPath: /data/your-app/byte-array
    readOnly: true
    subPath: byte-array
</code></pre>

<p><strong>ConfigMap:</strong></p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: your-app-bytes
data:
  byte-array: |-
    WHATEVERBYTESAREGENERATEDHERE
</code></pre>
