<p>I have a k8s deployment which run a Flask app, and I require that app to now his own application name (which is in <code>metadata.labels.app</code>). Then I would like to be able to get that information from the running pod, is it possible ?</p>
<p>I already tried to <code>bash</code> into the running pod and run <code>printenv</code> but there was no information about that precise deployment.</p>
<p>I also checked with the kubernetes client for python but it seems to be impossible to get only informations about current deployment.</p>
<p>I am missing something ? Is it even possible ?</p>
<p>Thank for your help</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: my-app
    env: production
  name: my-app
  namespace: production
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
      env: production
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: my-app
        env: production
    spec:
      containers:
        - image: gcr.io/github.com/my-company/my-app
          imagePullPolicy: IfNotPresent
          name: my-app-1
          resources:
            limits:
              cpu: 100m
              memory: 200Mi
            requests:
              cpu: 10m
              memory: 100Mi

</code></pre>

FairPluto
<p>You can expose information using env vars or files, see</p>
<ul>
<li><a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="noreferrer">https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/</a></li>
<li><a href="https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/" rel="noreferrer">https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/</a></li>
</ul>
<p>Example with single label:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: label-as-var
  labels:
    app: my-app
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [&quot;sh&quot;, &quot;-c&quot;]
      args:
        - while true; do
          echo &quot;$MY_APP&quot;;
          sleep 10;
          done;
      env:
        - name: MY_APP
          valueFrom:
            fieldRef:
              fieldPath: metadata.labels['app']
</code></pre>
<p>result:</p>
<pre><code>kubectl logs label-as-var
my-app
</code></pre>
<p>Example with downward api from docs, where all labels are accessible in file /etc/podinfo/labels</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: kubernetes-downwardapi-volume-example
  labels:
    zone: us-est-coast
    cluster: test-cluster1
    rack: rack-22
  annotations:
    build: two
    builder: john-doe
spec:
  containers:
    - name: client-container
      image: k8s.gcr.io/busybox
      command: [&quot;sh&quot;, &quot;-c&quot;]
      args:
      - while true; do
          if [[ -e /etc/podinfo/labels ]]; then
            echo -en '\n\n'; cat /etc/podinfo/labels; fi;
          if [[ -e /etc/podinfo/annotations ]]; then
            echo -en '\n\n'; cat /etc/podinfo/annotations; fi;
          sleep 5;
        done;
      volumeMounts:
        - name: podinfo
          mountPath: /etc/podinfo
  volumes:
    - name: podinfo
      downwardAPI:
        items:
          - path: &quot;labels&quot;
            fieldRef:
              fieldPath: metadata.labels
          - path: &quot;annotations&quot;
            fieldRef:
              fieldPath: metadata.annotations
</code></pre>
