-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
94 lines (84 loc) · 2.7 KB
/
query.go
File metadata and controls
94 lines (84 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"context"
stderrors "errors"
"fmt"
"sync"
"time"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"k8s.io/klog"
)
// getAllResources finds all API objects in specified API resources in all namespaces (or non-namespaced).
func getAllResources(client dynamic.Interface, apis []apiResource, allNs bool) ([]unstructured.Unstructured, error) {
var mu sync.Mutex
var wg sync.WaitGroup
var out []unstructured.Unstructured
start := time.Now()
klog.V(2).Infof("starting to query %d APIs in concurrently", len(apis))
var errResult error
for _, api := range apis {
if !allNs && !api.r.Namespaced {
klog.V(4).Infof("[query api] api (%s) is non-namespaced, skipping", api.r.Name)
continue
}
wg.Add(1)
go func(a apiResource) {
defer wg.Done()
klog.V(4).Infof("[query api] start: %s", a.GroupVersionResource())
v, err := queryAPI(client, a, allNs)
if err != nil {
if errors.IsForbidden(err) {
// should not fail the overall process, but print an info message indicating the permission issue
klog.V(4).Infof("[query api] skipping forbidden resource: %s", a.GroupVersionResource())
klog.Infof("cannot query %s (forbidden), omitting from the tree", a.GroupVersionResource().GroupResource())
} else {
klog.V(4).Infof("[query api] error querying: %s, error=%v", a.GroupVersionResource(), err)
errResult = stderrors.Join(errResult, fmt.Errorf("failed to query the %s resources: %w", a.GroupVersionResource(), err))
}
return
}
mu.Lock()
out = append(out, v...)
mu.Unlock()
klog.V(4).Infof("[query api] done: %s, found %d apis", a.GroupVersionResource(), len(v))
}(api)
}
klog.V(2).Infof("fired up all goroutines to query APIs")
wg.Wait()
klog.V(2).Infof("all goroutines have returned in %v", time.Since(start))
klog.V(2).Infof("query result: error=%v, objects=%d", errResult, len(out))
return out, errResult
}
func queryAPI(client dynamic.Interface, api apiResource, allNs bool) ([]unstructured.Unstructured, error) {
var out []unstructured.Unstructured
var next string
var ns string
if !allNs {
ns = getNamespace()
}
for {
var intf dynamic.ResourceInterface
nintf := client.Resource(api.GroupVersionResource())
if !allNs {
intf = nintf.Namespace(ns)
} else {
intf = nintf
}
resp, err := intf.List(context.TODO(), metav1.ListOptions{
Limit: 250,
Continue: next,
})
if err != nil {
return nil, fmt.Errorf("listing resources failed (%s): %w", api.GroupVersionResource(), err)
}
out = append(out, resp.Items...)
next = resp.GetContinue()
if next == "" {
break
}
}
return out, nil
}