MCPcopy
hub / github.com/ahmetb/kubectl-tree / getAllResources

Function getAllResources

cmd/kubectl-tree/query.go:18–60  ·  view source on GitHub ↗

getAllResources finds all API objects in specified API resources in all namespaces (or non-namespaced).

(client dynamic.Interface, apis []apiResource, allNs bool, labelSelector string)

Source from the content-addressed store, hash-verified

16
17// getAllResources finds all API objects in specified API resources in all namespaces (or non-namespaced).
18func getAllResources(client dynamic.Interface, apis []apiResource, allNs bool, labelSelector string) ([]unstructured.Unstructured, error) {
19 var mu sync.Mutex
20 var wg sync.WaitGroup
21 var out []unstructured.Unstructured
22
23 start := time.Now()
24 klog.V(2).Infof("starting to query %d APIs in concurrently", len(apis))
25
26 var errResult error
27 for _, api := range apis {
28 if !allNs && !api.r.Namespaced {
29 klog.V(4).Infof("[query api] api (%s) is non-namespaced, skipping", api.r.Name)
30 continue
31 }
32 wg.Add(1)
33 go func(a apiResource) {
34 defer wg.Done()
35 klog.V(4).Infof("[query api] start: %s", a.GroupVersionResource())
36 v, err := queryAPI(client, a, allNs, labelSelector)
37 if err != nil {
38 if errors.IsForbidden(err) {
39 // should not fail the overall process, but print an info message indicating the permission issue
40 klog.V(4).Infof("[query api] skipping forbidden resource: %s", a.GroupVersionResource())
41 klog.Infof("cannot query %s (forbidden), omitting from the tree", a.GroupVersionResource().GroupResource())
42 } else {
43 klog.V(4).Infof("[query api] error querying: %s, error=%v", a.GroupVersionResource(), err)
44 errResult = stderrors.Join(errResult, fmt.Errorf("failed to query the %s resources: %w", a.GroupVersionResource(), err))
45 }
46 return
47 }
48 mu.Lock()
49 out = append(out, v...)
50 mu.Unlock()
51 klog.V(4).Infof("[query api] done: %s, found %d apis", a.GroupVersionResource(), len(v))
52 }(api)
53 }
54
55 klog.V(2).Infof("fired up all goroutines to query APIs")
56 wg.Wait()
57 klog.V(2).Infof("all goroutines have returned in %v", time.Since(start))
58 klog.V(2).Infof("query result: error=%v, objects=%d", errResult, len(out))
59 return out, errResult
60}
61
62func queryAPI(client dynamic.Interface, api apiResource, allNs bool, labelSelector string) ([]unstructured.Unstructured, error) {
63 var out []unstructured.Unstructured

Callers 1

runFunction · 0.85

Calls 2

queryAPIFunction · 0.85
GroupVersionResourceMethod · 0.80

Tested by

no test coverage detected