Ivan
Ivan

Reputation: 2340

Is there a way to not specify namespace in kubernetes when referring to a uniquely named item?

Is it possible to avoid specifying namespace if I'd like to run e.g. kubectl describe deployment <deployment_name> for a deployment that is unique? Or is namespace argument is always mandatory?

Upvotes: 2

Views: 2482

Answers (3)

reith
reith

Reputation: 2088

To list such resources, you can use Field Selector on name:

kubectl get deployments --all-namespaces --field-selector='metadata.name=<deployment_name>'

To describe:

kubectl get deployments --all-namespaces --field-selector='metadata.name=<deployment_name>' -oyaml | kubectl describe -f -

Upvotes: 4

anemyte
anemyte

Reputation: 20306

When you do not use a namespace argument in a command, it is taken from active context in your kubeconfig. As far as I know there is no way to perform a search over other namespaces for the requested object if none is found in the current.

Though you can set the default namespace in the context. It is helpful if you are going to execute multiple commands in the same namespace. To do so:

kubectl config set-context --current --namespace=<insert-namespace-name-here>

After that it'll use the given namespace by default.

Upvotes: 2

whites11
whites11

Reputation: 13350

It is mandatory. The point is that kubectl is using the Kubernetes API to get information about a deployment.

The URL it needs to call to respond to kubectl describe deployment <deployment_name> is:

GET /apis/apps/v1/namespaces/{namespace}/deployments/{deployment_name}

Therefore the namespace information is needed to build the URL.

Theoretically, the kubectl tool could list all deployments in the cluster and retrieving the namespace but that's not the case currently (and I doubt it'll ever be).

Upvotes: 3

Related Questions