How to get the current namespace in a Pod?

9/5/2017

One way I could think of is to set an environment which value is the namespace of the Pod when defining the Pod.
Getting the namespace dynamically without requiring changes for Pod will be better because it lessens the burden of constructing a Pod.

So is there a way to get current namespace in a Pod?

-- flyer
kubernetes
kubernetes-go-client

2 Answers

9/5/2017

Try the file:

/var/run/secrets/kubernetes.io/serviceaccount/namespace
-- Graham Dumpleton
Source: StackOverflow

9/6/2017

you don't need to set a static namespace env variable in the pod spec if you want to use env variables, you can use the "Downward API" for letting k8s fill it dynamically with the current namespace. See https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#the-downward-api

-- slintes
Source: StackOverflow