3

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

I tried something as below, but that does not work.

containers:
      - env:
        - name: JAVA_OPTS
          value: -Dappdynamics.agent.nodeName=$HOST-$spec.nodeName-spec.PodName

and

- name: appdynamics.agent.nodeName
  value= $HOST-$spec.nodeName-spec.PodName

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

mpalumbo7
  • 1,180
  • 10
  • 11
Sunil Chauraha
  • 525
  • 1
  • 7
  • 21

2 Answers2

10

You can get POD_NAME and POD_NAMESPACE passing them as environment variables via fieldRef.

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

EDIT: Added example env REFERENCE_EXAMPLE to show how to reference variables. Thanks to this answer for pointing out the $() interpolation.

You can reference supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP as mentioned in the documentation here.

However, CLUSTERNAME is not a standard property available. According to this PR #22043, the CLUSTERNAME should be injected to the .metadata field if using GCE.

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

mpalumbo7
  • 1,180
  • 10
  • 11
  • According to the docs here https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#envvarsource-v1-core only a few fields in the spec are supported. Got `field label not supported: status.restartPolicy`. – Praveen Sripati Oct 07 '18 at 01:20
  • Thanks for the reply but that did not help. Can I use something like this? - name: JAVA_OPTS value: -Xmx512m -Xms256m -Dappdynamics.agent.nodeName=$(metadata.name) – Sunil Chauraha Oct 07 '18 at 17:22
  • @SunilChauraha You need to reference the environment variable that Kuberenetes created, not the field in the manifest. So, your first `env` should be `- name: MY_NODE_NAME valueFrom: fieldRef: fieldPath: spec.nodeName` and the second would be `-name: JAVA_OPTS value: -Xmx512m -Xms256m -Dappdynamics.agent.nodeName=$(MY_NODE_NAME)` – mpalumbo7 Oct 08 '18 at 04:28
  • Thanks @PraveenSripati for confirming that. – mpalumbo7 Oct 08 '18 at 04:31
  • @SunilChauraha I'll edit the answer. Please mark the question as answered. Thanks. – mpalumbo7 Oct 08 '18 at 10:15
  • only name and namespace exist in metadata. CLUSTER_NAME is only for gce – Guillaume Cisco Feb 17 '23 at 10:38
2

Below format helped me, suggested by ewok2030 and Praveen. Only one thing to make sure that the variable should be declared before they are used as JAVA_OPTS.

containers:

   - env:

    - name: APPD_NODE_NAME
       valueFrom: 
        fieldRef:
          fieldPath: spec.nodeName
    - name: APPD_POD_NAMESPACE
      valueFrom:
        fieldRef:
          fieldPath: metadata.namespace
    - name: APP_POD_NAME
      valueFrom: 
        fieldRef:
          fieldPath: metadata.name
    - name: JAVA_OPTS
      value: -Xmx712m -Xms556m -Dpdp.logging.level=WARN -Dappdynamics.agent.nodeName=$(APPD_NODE_NAME)-$(APPD_POD_NAMESPACE)-$(APP_POD_NAME)
Sunil Chauraha
  • 525
  • 1
  • 7
  • 21