<p>I am trying to copy the first container argument in a pod definition to an environment variable. This is my pod template file.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: test
    image: nginx
    command: [ &quot;/bin/bash&quot;, &quot;-c&quot;, &quot;--&quot; ]
    args: [&quot;sleep 30&quot;]
    env:
      - name: &quot;TEST_ENV&quot;
        valueFrom:
          fieldRef:
            fieldPath: spec.containers[0].args[0]
  restartPolicy: Never
</code></pre>
<p>But on executing <code>kubectl apply</code>, I get the following error.</p>
<p><code>The Pod &quot;myapp-pod&quot; is invalid: spec.containers[0].env[0].valueFrom.fieldRef.fieldPath: Invalid value: &quot;spec.containers[0].args[0]&quot;: error converting fieldPath: field label not supported: spec.containers[0].args[0]</code></p>
<p>What is the correct way to reference this argument?</p>

Harshith Bolar
<p>Only a limited number of fields are available using <code>fieldRef</code>.</p>
<p>From <a href="https://kubernetes.io/docs/concepts/workloads/pods/downward-api/#available-fields" rel="nofollow noreferrer">the documentation</a>:</p>
<blockquote>
<p>Only some Kubernetes API fields are available through the downward API. This section lists which fields you can make available.</p>
<h2>Information available via fieldRef</h2>
<ul>
<li><code>metadata.name</code></li>
<li><code>metadata.namespace</code></li>
<li><code>metadata.uid</code></li>
<li><code>metadata.annotations['&lt;KEY&gt;']</code></li>
<li><code>metadata.labels['&lt;KEY&gt;']</code></li>
</ul>
<p>The following information is available through environment variables but not as a downwardAPI volume fieldRef:</p>
<ul>
<li><code>spec.serviceAccountName</code></li>
<li><code>spec.nodeName</code></li>
<li><code>status.hostIP</code></li>
<li><code>status.podIP</code></li>
</ul>
<p>The following information is available through a downwardAPI volume fieldRef, but not as environment variables:</p>
<ul>
<li><code>metadata.labels</code></li>
<li><code>metadata.annotations</code></li>
</ul>
</blockquote>
<hr />
<p>In any case, since the container arguments are available to whatever service you're running inside the container, you should be able to retrieve them from your codes argument array/list/argv/etc.</p>
