How to figure out a pod's namespace from within a pod?

7/6/2017

My service X is running in a pod P with Namespace N. how do I find out namespace info from withing Service X using Kubernetes go client API?

-- aks
kubernetes
kubernetes-go-client

2 Answers

7/7/2017

I'm not sure what is meant by go client API. The pod's namespace is in /var/run/secrets/kubernetes.io/serviceaccount/namespace so this "service X" just needs to read this file.

Alternatively, you can also use the Downward API.

-- Eugene Chow
Source: StackOverflow

7/7/2017

I figured i can pass on pod, namespace and more using Environment variables while creating a new pod using go-client. The way to do it is by specifying environment variable key/val pair in PodSpec as below:

Env: []v1.EnvVar{
    {Name: "POD_NAME", Value: podname},
    {Name: "POD_NAMESPACE", Value: nsname},
},

then from within a pod, i can read this env. variables using os package.

-- aks
Source: StackOverflow