ahmetb/kubectl-tree

Enhancement: Add cli switch to search all ALL un-namespaced objects (can be used in addition to the -n switch)

Opened this issue · 7 comments

I am working with a controller that manages a CRD which creates a namespace and then creates some additional resources in that namespace. With the current options, I am forced to search all namespaces to get the complete list of resources that this CRD resource owns.

  • A default search doesn't find anything (even though there is one non-namespaced child resource)
$ kubectl tree requests.project.example.com request-test

No resources are owned by this object through ownerReferences.
  • A single namespace search finds some of the resources, but does not list the one non-namespaced child resource.
$ kubectl tree requests.project.example.com request-test -n request-test

NAMESPACE     NAME                              READY  REASON  AGE
              Request/request-test              -              10m
request-test  ├─LimitRange/pod-resource-limits  -              10m
request-test  ├─ResourceQuota/pod-count         -              10m
request-test  └─RoleBinding/admin               -              10m
  • A search of all the namespaces finally finds all of the child resources, but it is overkill for what is actually needed.
$ kubectl tree requests.project.example.com request-test -A

NAMESPACE     NAME                              READY  REASON  AGE
              Request/request-test              -              6m48s
request-test  ├─LimitRange/pod-resource-limits  -              6m48s
              ├─Namespace/request-test          -              6m48s
request-test  ├─ResourceQuota/pod-count         -              6m48s
request-test  └─RoleBinding/admin               -              6m47s

It would be great to add a cli option like -C/--cluster-resources that could be used to specifically request that non-namespaced resources be checked. This should make it possible to find everything, without performing a overly broad search possible, using something like this:

$ kubectl tree requests.project.example.com request-test -n request-test -C

NAMESPACE     NAME                              READY  REASON  AGE
              Request/request-test              -              6m48s
request-test  ├─LimitRange/pod-resource-limits  -              6m48s
              ├─Namespace/request-test          -              6m48s
request-test  ├─ResourceQuota/pod-count         -              6m48s
request-test  └─RoleBinding/admin               -              6m47s

thanks for the proposal. out of curiosity: are there examples of this from other CLI tools for Kubernetes?

I agree with the idea of this, but want to be consistent in terms of naming of this option for the sake of consistency.

Hello @ahmetb! Seems like this flag -A is not working as expected, I'm afraid.

I had a fresh KinD installation for testing purposes. It contains only a few deployments, please note the flag -A:

kubectl get deploy -A
NAMESPACE            NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
kube-system          coredns                  2/2     2            2           82m
local-path-storage   local-path-provisioner   1/1     1            1           82m

This plugin correctly shows tree-like structure when namespace is mentioned explicitly.

kubectl tree deploy coredns -n kube-system
NAMESPACE    NAME                              READY  REASON  AGE
kube-system  Deployment/coredns                -              84m
kube-system  └─ReplicaSet/coredns-565d847f94   -              84m
kube-system    ├─Pod/coredns-565d847f94-66mtj  True           84m
kube-system    └─Pod/coredns-565d847f94-bgqxs  True           84m

Unfortunately this doesn't happen with -A:

kubectl tree deploy coredns -A
Error: failed to get deploy/coredns: deployments.apps "coredns" not found

Based on on the --help I recon -A is to be supported already, isn't?

kubectl tree --help
Show sub-resources of the Kubernetes object

Usage:
  kubectl tree KIND NAME [flags]

Examples:
  kubectl tree deployment my-app
  kubectl tree kservice.v1.serving.knative.dev my-app

Flags:
  -A, --all-namespaces                 query all objects in all API groups, both namespaced and non-namespaced
ahmetb commented

@HectorB-2020 whats the output for

kubectl get deploy coredns -A

Well, this is interesting: 👍

kubectl get deploy coredns -A
error: a resource cannot be retrieved by name across all namespaces

In fact this combination is never used with exact name. Usually generic results are filtered with parameters like --selector or passed through grep, you know.
On the other hand plain kubectl tree is not accepted without name.

kubectl tree deploy -A
Error: specify the kubernetes object in KIND NAME or KIND/NAME form

Is there a scenario when -A should work?

ahmetb commented

This plugin currently takes a single object and constructs the object hierarchy from there. So if multiple objects with the same KIND/NAME are found while searching all namespaces, we either error out (and force user to qualify by namespace) or pick a namespace for the user (I like to avoid "magic").

Or we can change the tool to print the tree for multiple root objects across different namespaces. Which might be ok.

In this case, we overloaded -A/--all-namespaces flag to mean "cluster-scoped objects". In hindsight, another flag name could've been better.

Thanks @ahmetb! It's clear now. 💯
👍

spkane commented

thanks for the proposal. out of curiosity: are there examples of this from other CLI tools for Kubernetes?

I agree with the idea of this, but want to be consistent in terms of naming of this option for the sake of consistency.

I am not aware of equivalent functionality in something like kubectl. I could imagine this either being a switch or simply being that standard behavior (show all resources in the namespace, plus all related cluster-wide resources).