<p>I am using go-client to access k8s resources in my environment. There are APIs to get/list pods, namespaces, etc.</p>
<p>How do I access the pod that I am currently running on?</p>

Kaushal Agrawal
<p>You can <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">Expose Pod Information to Containers Through Environment Variables</a> 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">pod fields</a>:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: dapi-envars-fieldref
spec:
  containers:
    - name: test-container
      ...
      ...
      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
  restartPolicy: Never
</code></pre>
<p>then simply look up these env vars in your Go code:</p>
<pre><code>log.Printf(&quot;MY_POD_NAME: %q&quot;, os.Getenv(&quot;MY_POD_NAME&quot;))
</code></pre>
