<p>I have a K8s deployment yaml definition where few env variables are declared. I want to declare an environment variable whose value to be referenced from a specific volume's mountpath.</p>
<p>For example, I want it as shown in below yaml from line #42 to #45. (Note: I have used it as an example only. I know it is not correct). Is it possible to achieve this and how?</p>
<pre><code> 37     spec:
 38       containers:
 39         - name: appplugin
 40           image: {{APP_VERSION}}
 41           env:
 42           - name: INFRA_ACCESS_IP
 43             valueFrom:
 44               fieldRef:
 45                 fieldPath: status.hostIP
 42           - name: LOG_BASE_DIR
 43             valueFrom:
 44               fieldRef:
 45                 fieldPath: volumeMounts.app-logs.mountPath.subPath
 46           envFrom:
 47           - configMapRef:
 48               name: infra-config
 49           - configMapRef:
 50               name: core-config
 51           ports:
 52           - containerPort: 9095
 53           volumeMounts:
 54           - name: app-certs
 55             mountPath: /etc/secrets/certs
 56             readOnly: true
 57           - name: app-logs
 58             mountPath: /var/log/toHost/
 59             subPath: app-logs
</code></pre>

AnilJ
<p>Not directly.  If you look at the API documentation for the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#envvarsource-v1-core" rel="nofollow noreferrer">EnvVarSource</a> object, you can see that a limited number of fields are supported for the <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">downward API</a>; generally only the metadata fields, the service-account name, the dynamic IP and node information, and the resource limits.</p>
<p>In the context of the file you show, the file path is fixed and you don't need a dynamic lookup.  Since each container has an isolated filesystem, it's a little unlikely you'll actually change this path in different deployments, and it will work to just specify that path directly:</p>
<pre class="lang-yaml prettyprint-override"><code>env:
  - name: LOG_BASE_DIR
    value: app-logs
volumeMounts:
  - name: app-logs
    mountPath: /var/log/toHost/
    subPath: app-logs
</code></pre>
<p>If you're using a templating tool like <a href="https://helm.sh/" rel="nofollow noreferrer">Helm</a> you can make this value configurable at deploy time.  Helm has the notion of &quot;values&quot; that are configurable at deployment time, and can inject those values (or do much more complex manipulation) when it installs things.  You could use this to set the path inside the container if you had a reason to:</p>
<pre class="lang-yaml prettyprint-override"><code>image: {{ .Values.appVersion }}
env:
  - name: LOG_BASE_DIR
    value: {{ .Values.logBaseDir | default &quot;app-logs&quot; }}
volumeMounts:
  - name: app-logs
    mountPath: /var/log/toHost/
    subPath: {{ .Values.logBaseDir | default &quot;app-logs&quot; }}
</code></pre>
<p>(For logs specifically, it might be better to skip this configuration entirely and just send logs to your process's stdout.  Then <code>kubectl logs</code> can retrieve them later.  You can also <a href="https://kubernetes.io/docs/concepts/cluster-administration/logging/" rel="nofollow noreferrer">deploy a log collector as a DaemonSet</a> that will capture these logs to some other system.)</p>
