Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
spkane opened this issue Dec 3, 2021 · 7 comments

Comments

@spkane
Copy link

spkane commented Dec 3, 2021

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
@ahmetb
Copy link
Owner

ahmetb commented Dec 4, 2021

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.

@HectorB-2020
Copy link

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
Copy link
Owner

ahmetb commented Feb 12, 2023

@HectorB-2020 whats the output for

kubectl get deploy coredns -A

@HectorB-2020
Copy link

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
Copy link
Owner

ahmetb commented Feb 13, 2023

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.

@HectorB-2020
Copy link

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

@spkane
Copy link
Author

spkane commented Feb 13, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants