<p>I have a requirement to pass  cluster, namespace and pod name to AppDynamics agent from my container deployed in Kubernetes cluster.</p>

<p>I tried something as below, but that does not work.</p>

<pre><code>containers:
      - env:
        - name: JAVA_OPTS
          value: -Dappdynamics.agent.nodeName=$HOST-$spec.nodeName-spec.PodName
</code></pre>

<p>and </p>

<pre><code>- name: appdynamics.agent.nodeName
  value= $HOST-$spec.nodeName-spec.PodName
</code></pre>

<p>Could anyone please help me here how to collect the detail and pass to AppD.
Thanks in advance.</p>

Sunil Chauraha
<p>You can get <code>POD_NAME</code> and <code>POD_NAMESPACE</code> passing them as environment variables via <code>fieldRef</code>. </p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: test-env
spec:
  containers:
    - name: test-container
      image: my-test-image:latest
      env:
        - name: MY_NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: MY_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: MY_POD_SERVICE_ACCOUNT
          valueFrom:
            fieldRef:
              fieldPath: spec.serviceAccountName
        - name: REFERENCE_EXAMPLE
          value: "/$(MY_NODE_NAME)/$(MY_POD_NAMESPACE)/$(MY_POD_NAME)/data.log"
  restartPolicy: Never
</code></pre>

<p><strong>EDIT</strong>: <em>Added example env <code>REFERENCE_EXAMPLE</code> to show how to reference variables. Thanks to <a href="https://stackoverflow.com/questions/49582349/kubernetes-how-to-refer-to-one-environment-variable-from-another">this</a> answer for pointing out the <code>$()</code> interpolation.</em></p>

<p>You can reference <code>supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP</code> as mentioned in the documentation <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#envvarsource-v1-core" rel="noreferrer">here</a>.</p>

<p>However, <code>CLUSTERNAME</code> is not a standard property available. According to this <a href="https://github.com/kubernetes/kubernetes/pull/22043" rel="noreferrer">PR #22043</a>, the <code>CLUSTERNAME</code> should be injected to the <code>.metadata</code> field if using GCE.</p>

<p>Otherwise, you'll have to specific the <code>CLUSTERNAME</code> manually in the <code>.metadata</code> field  and then use <code>fieldRef</code> to inject it as an environment variable.</p>
