<p>I have a kubernetes service, written in Go, and am using <code>client-go</code> to access the kubernetes apis.</p>
<p>I need the <code>Pod</code> of the service's own pod.</p>
<p>The <code>PodInterface</code> allows me to iterate all pods, but what I need is a &quot;self&quot; concept to get the currently running pod that is executing my code.</p>
<p>It appears by reading <code>/var/run/secrets/kubernetes.io/serviceaccount/namespace</code> and searching pods in the namespace for the one matching <code>hostname</code>, I can determine the &quot;self&quot; pod.</p>
<p>Is this the proper solution?</p>

jws
<p>Expose the <code>POD_NAME</code> and <code>POD_NAMESPACE</code> to your pod as environment variables. Later use those values to get your own pod object.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: dapi-envars-fieldref
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ &quot;sh&quot;, &quot;-c&quot;]
      args:
      - while true; do
          echo -en '\n';
          printenv MY_POD_NAME MY_POD_NAMESPACE;
          sleep 10;
        done;
      env:
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: MY_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
  restartPolicy: Never
</code></pre>
<p><strong>Ref:</strong> <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">environment-variable-expose-pod-information</a></p>
