<p>I have a kubernetes deployment that starts a pod that includes a <code>runAsUser</code> key in its <code>securityContext</code>. I was hoping I could stick this value in the environment of an initContainer using <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-pod-fields-as-values-for-environment-variables" rel="nofollow noreferrer">valueFrom</a>, like this:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: testdeployment
spec:
  template:
    spec:
      containers:
        - name: myservice
          image: myimage
          securityContext:
            runAsUser: 1000
      initContainers:
        - name: initialize_things
          image: myimage
          env:
            - name: CONTAINER_UID
              valueFrom:
                fieldRef:
                  fieldPath: spec.containers[0].securityContext.runAsUser
</code></pre>
<p>That doesn't seem to work:</p>
<blockquote>
<p>The Deployment &quot;testdeployment&quot; is invalid: spec.template.spec.initContainers[0].env[0].valueFrom.fieldRef.fieldPath: Invalid value: &quot;spec.containers[0].securityContext.runAsUser&quot;: error converting fieldPath: field label not supported: spec.containers[0].securityContext.runAsUser</p>
</blockquote>
<p>Is there any way to make this work? I'm trying to reduce the number of places I'm hardcoding that UID.</p>

<p>I think you cant make this work because <a href="https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#the-downward-api" rel="nofollow noreferrer">The downward API</a> <strong>doesnt support</strong> <code>spec.containers[0].securityContext.runAsUser</code> as a field.</p>
<p>Btw, in your case more logically was to put full path , I mean <code>spec.template.spec.containers[0].securityContext.runAsUser</code>, but anyway, it wont help</p>
<p>As per <a href="https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#capabilities-of-the-downward-api" rel="nofollow noreferrer">Capabilities of the Downward API</a> - you are able to use only few fields</p>
<pre><code>Information available via fieldRef:
metadata.name 
metadata.namespace
metadata.uid 
metadata.labels['&lt;KEY&gt;']
metadata.annotations['&lt;KEY&gt;'] 

In addition, the following information is available through downwardAPI volume fieldRef:    
metadata.labels
metadata.annotations 

The following information is available through environment variables:    
status.podIP 
spec.serviceAccountName 
spec.nodeName 
status.hostIP
</code></pre>
<p>You can find very similar issue on github closed: <a href="https://github.com/kubernetes/kubernetes/issues/84492" rel="nofollow noreferrer">how to get imageID in container</a></p>
