Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current namespace of an in-cluster go Kubernetes client

Tags:

go

kubernetes

How do I get the current namespace of a deployment/service using the kubernetes client-go API? It doesn't seem to be in the client object or in the config.

like image 723
gkgkgkgk Avatar asked Nov 13 '18 14:11

gkgkgkgk


People also ask

How can I get current namespace in Kubernetes?

The most basic command for viewing Kubernetes objects via kubectl is get . If you run kubectl get <resource-name> you will get a listing of all resources in the current namespace. If you want to get a specific resource, you can use kubectl get <resource-name> <object-name> .

What is namespace in the cluster Kubernetes?

Namespaces are a way to organize clusters into virtual sub-clusters — they can be helpful when different teams or projects share a Kubernetes cluster. Any number of namespaces are supported within a cluster, each logically separated from others but with the ability to communicate with each other.

What is the default Kubernetes namespace?

The “default” Namespace In most Kubernetes distributions, the cluster comes out of the box with a Namespace called “default.” In fact, there are actually three namespaces that Kubernetes ships with: default, kube-system (used for Kubernetes components), and kube-public (used for public resources).


1 Answers

Add this environment variable in your deployment config.

 - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace

This is using the kubernetes downward api

like image 82
bappr Avatar answered Sep 28 '22 04:09

bappr