<p>So hopefully this makes sense to the non-Djangoers of the k8s community. I will try my best to explain the setup / reasoning.</p>
<p>With Django, we have LOTS of what are called management commands that we can run within the scope and environment of our Django app that can really help development and deployment. I'm sure most other frameworks have similar, if not identical, concepts.</p>
<p>An example would be the &quot;<code>python manage.py migrate</code>&quot; command that ensures our codebase (migration scripts) are applied to and reflect in the associated database.</p>
<p>There are approx. 30 - 50 core commands we can run, we can also create our own, as well as apply those from any installed third party applications.</p>
<p>Anyways. The most important takeaway is that there are a lot of commands we can and do run.</p>
<p>Now, I have the following k8s Job to run the &quot;migrate&quot; command:</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: asencis-web-migrate-job
spec:
  template:
    spec:
      containers:
        - name: asencis-web-migrate-job
          image: asencis/asencis-base:latest
          imagePullPolicy: Always
          command: ['python', 'manage.py', 'migrate']
          envFrom:
          - configMapRef:
              name: asencis-config
          - secretRef:
              name: asencis-secret
      restartPolicy: Never
  backoffLimit: 5
</code></pre>
<p>This job essentially runs the <code>python manage.py migrate</code> command within the application scope/environment. It works like a charm:</p>
<pre class="lang-sh prettyprint-override"><code>$ kubectl apply -f asencis-web-migrate-job.yaml
</code></pre>
<p>It's very useful in the deployment of the application when all of our tests have run, we can then build an image, &quot;rollout restart&quot; the cluster and then apply any migrations. It's incredibly seamless. (Not of my hat to the k8s core team for making such a useful product!)</p>
<p>Anyways.</p>
<p>My question is essentially this, <em><strong>can we apply an argument to the above <code>kubectl apply</code> command on the job, to run any command we like</strong></em>?</p>
<p>An example would be:</p>
<pre class="lang-sh prettyprint-override"><code>$ kubectl apply -f asencis-web-job.yaml --command python manage.py migrate
</code></pre>

Micheal J. Roberts
<p>You will probably need to build your own tool for this.</p>
<p>You could use a shell script around <a href="https://mikefarah.gitbook.io/yq/" rel="nofollow noreferrer">yq</a>, for example:</p>
<pre class="lang-sh prettyprint-override"><code>#!/bin/sh
yq eval \
    &quot;.spec.template.spec.containers.[0].command.[2]=\&quot;$1\&quot;' \
    template-web-job.yaml \
| kubectl apply -f-
</code></pre>
<p>You can fill in more parts of the Job this way: compute <code>.metadata.name</code> from <code>$USER-$1-$(date +%s)</code>, attach <code>labels:</code> to the Pod to find it later, and so on.</p>
<p>If this wasn't a one-off Job I might recommend a more Kubernetes-native tool like Helm or Kustomize.  Both tools need somewhat involved filesystem layouts, and then you need to pass the variables (script command, submitter) in some form; this wouldn't actually be easier than using <code>yq</code> to rewrite the YAML.  Helm is a little more oriented towards mostly-stable installations (it knows about the main application Deployment and how to update it in-place).</p>
<p>If you have Helm already, you could build a similar script around <code>helm template</code>.  Or if you have <code>jq</code> but not <code>yq</code> you could rewrite the Job in JSON <code>{&quot;apiVersion&quot;: &quot;batch/v1&quot;, &quot;kind&quot;: &quot;Job&quot;, ...}</code> but otherwise use the same script.</p>
