Skip to content

Commit 9797034

Browse files
feloyahmetb
authored andcommitted
Query namespaced resources in current namespace by default (#22)
* Add a flag --all-namespaces * Document * No global variables anymore * Fixes * remove unused allNs
1 parent 9d5bde0 commit 9797034

File tree

5 files changed

+59
-16
lines changed

5 files changed

+59
-16
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ Example (Agones Fleet):
2424

2525
![example Kubernetes object hierarchy with Agones Fleet](assets/example-3.png)
2626

27+
## Flags
28+
29+
By default, the plugin will only search "namespaced" objects in the same
30+
namespace as the specified object.
31+
32+
You can use the `-A` or `--all-namespaces` flag to search namespaced and
33+
non-namespaced objects in all namespaces.
34+
2735
## Author
2836

2937
Ahmet Alp Balkan [@ahmetb](https://twitter.com/ahmetb).

cmd/kubectl-tree/apis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ func findAPIs(client discovery.DiscoveryInterface) (*resourceMap, error) {
6767
klog.V(4).Infof(" api (%s) doesn't have required verb, skipping: %v", apiRes.Name, apiRes.Verbs)
6868
continue
6969
}
70-
7170
v := apiResource{
7271
gv: gv,
7372
r: apiRes,
@@ -80,6 +79,7 @@ func findAPIs(client discovery.DiscoveryInterface) (*resourceMap, error) {
8079
rm.list = append(rm.list, v)
8180
}
8281
}
82+
klog.V(5).Infof(" found %d apis", len(rm.m))
8383
return rm, nil
8484
}
8585

cmd/kubectl-tree/namespace.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
func getNamespace() string {
4+
if v := *cf.Namespace; v != "" {
5+
return v
6+
}
7+
clientConfig := cf.ToRawKubeConfigLoader()
8+
defaultNamespace, _, err := clientConfig.Namespace()
9+
if err != nil {
10+
defaultNamespace = "default"
11+
}
12+
return defaultNamespace
13+
}

cmd/kubectl-tree/query.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
// getAllResources finds all API objects in specified API resources in all namespaces (or non-namespaced).
15-
func getAllResources(client dynamic.Interface, apis []apiResource) ([]unstructured.Unstructured, error) {
15+
func getAllResources(client dynamic.Interface, apis []apiResource, allNs bool) ([]unstructured.Unstructured, error) {
1616
var mu sync.Mutex
1717
var wg sync.WaitGroup
1818
var out []unstructured.Unstructured
@@ -22,11 +22,15 @@ func getAllResources(client dynamic.Interface, apis []apiResource) ([]unstructur
2222

2323
var errResult error
2424
for _, api := range apis {
25+
if !allNs && !api.r.Namespaced {
26+
klog.V(4).Infof("[query api] api (%s) is non-namespaced, skipping", api.r.Name)
27+
continue
28+
}
2529
wg.Add(1)
2630
go func(a apiResource) {
2731
defer wg.Done()
2832
klog.V(4).Infof("[query api] start: %s", a.GroupVersionResource())
29-
v, err := queryAPI(client, a)
33+
v, err := queryAPI(client, a, allNs)
3034
if err != nil {
3135
klog.V(4).Infof("[query api] error querying: %s, error=%v", a.GroupVersionResource(), err)
3236
errResult = err
@@ -46,12 +50,24 @@ func getAllResources(client dynamic.Interface, apis []apiResource) ([]unstructur
4650
return out, errResult
4751
}
4852

49-
func queryAPI(client dynamic.Interface, api apiResource) ([]unstructured.Unstructured, error) {
53+
func queryAPI(client dynamic.Interface, api apiResource, allNs bool) ([]unstructured.Unstructured, error) {
5054
var out []unstructured.Unstructured
5155

5256
var next string
57+
var ns string
58+
59+
if !allNs {
60+
ns = getNamespace()
61+
}
5362
for {
54-
resp, err := client.Resource(api.GroupVersionResource()).List(metav1.ListOptions{
63+
var intf dynamic.ResourceInterface
64+
nintf := client.Resource(api.GroupVersionResource())
65+
if !allNs {
66+
intf = nintf.Namespace(ns)
67+
} else {
68+
intf = nintf
69+
}
70+
resp, err := intf.List(metav1.ListOptions{
5571
Limit: 250,
5672
Continue: next,
5773
})

cmd/kubectl-tree/rootcmd.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ import (
3030
"k8s.io/klog"
3131
)
3232

33+
const (
34+
allNamespacesFlag = "all-namespaces"
35+
)
36+
3337
var cf *genericclioptions.ConfigFlags
3438

3539
// This variable is populated by goreleaser
@@ -57,7 +61,13 @@ func versionString() string {
5761
return "v" + version
5862
}
5963

60-
func run(_ *cobra.Command, args []string) error {
64+
func run(command *cobra.Command, args []string) error {
65+
66+
allNs, err := command.Flags().GetBool(allNamespacesFlag)
67+
if err != nil {
68+
allNs = false
69+
}
70+
6171
restConfig, err := cf.ToRESTConfig()
6272
if err != nil {
6373
return err
@@ -102,15 +112,8 @@ func run(_ *cobra.Command, args []string) error {
102112
api = apiResults[0]
103113
}
104114

105-
ns := *cf.Namespace
106-
if ns == "" {
107-
clientConfig := cf.ToRawKubeConfigLoader()
108-
defaultNamespace, _, err := clientConfig.Namespace()
109-
if err != nil {
110-
defaultNamespace = "default"
111-
}
112-
ns = defaultNamespace
113-
}
115+
ns := getNamespace()
116+
klog.V(2).Infof("namespace=%s allNamespaces=%v", ns, allNs)
114117

115118
obj, err := dyn.Resource(api.GroupVersionResource()).Namespace(ns).Get(name, metav1.GetOptions{})
116119
if err != nil {
@@ -120,7 +123,7 @@ func run(_ *cobra.Command, args []string) error {
120123
klog.V(5).Infof("target parent object: %#v", obj)
121124

122125
klog.V(2).Infof("querying all api objects")
123-
apiObjects, err := getAllResources(dyn, apis.resources())
126+
apiObjects, err := getAllResources(dyn, apis.resources(), allNs)
124127
if err != nil {
125128
return fmt.Errorf("error while querying api objects: %w", err)
126129
}
@@ -148,6 +151,9 @@ func init() {
148151
})
149152

150153
cf = genericclioptions.NewConfigFlags(true)
154+
155+
rootCmd.Flags().BoolP(allNamespacesFlag, "A", false, "query all objects in all API groups, both namespaced and non-namespaced")
156+
151157
cf.AddFlags(rootCmd.Flags())
152158
if err := flag.Set("logtostderr", "true"); err != nil {
153159
fmt.Fprintf(os.Stderr, "failed to set logtostderr flag: %v\n", err)

0 commit comments

Comments
 (0)