This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathall.go
More file actions
213 lines (184 loc) · 6.3 KB
/
all.go
File metadata and controls
213 lines (184 loc) · 6.3 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package kclient
import (
"context"
"fmt"
"strings"
"time"
"golang.org/x/sync/errgroup"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/klog/v2"
)
// Code into this file is heavily inspired from https://github.com/ahmetb/kubectl-tree
// GetAllResourcesFromSelector returns all resources of any kind (including CRs) matching the given label selector
func (c *Client) GetAllResourcesFromSelector(selector string, ns string) ([]unstructured.Unstructured, error) {
apis, err := findAPIs(c.cachedDiscoveryClient)
if err != nil {
return nil, err
}
return getAllResources(c.DynamicClient, apis.list, ns, selector)
}
func getAllResources(client dynamic.Interface, apis []apiResource, ns string, selector string) ([]unstructured.Unstructured, error) {
var out []unstructured.Unstructured
outChan := make(chan []unstructured.Unstructured)
var apisOfInterest []apiResource
for _, api := range apis {
if !api.r.Namespaced {
klog.V(5).Infof("[query api] api (%s) is non-namespaced, skipping", api.r.Name)
continue
}
apisOfInterest = append(apisOfInterest, api)
}
start := time.Now()
group := new(errgroup.Group) // an error group errors when any of the go routines encounters an error
klog.V(2).Infof("starting to concurrently query %d APIs", len(apis))
for _, api := range apisOfInterest {
api := api // shadowing because go vet complains "loop variable api captured by func literal"
group.Go(func() error {
klog.V(5).Infof("[query api] start: %s", api.GroupVersionResource())
v, err := queryAPI(client, api, ns, selector)
if err != nil {
klog.V(5).Infof("[query api] error querying: %s, error=%v", api.GroupVersionResource(), err)
return err
}
outChan <- v
klog.V(5).Infof("[query api] done: %s, found %d apis", api.GroupVersionResource(), len(v))
return nil
})
}
klog.V(2).Infof("fired up all goroutines to query APIs")
errChan := make(chan error)
go func() {
err := group.Wait()
klog.V(2).Infof("all goroutines have returned in %v", time.Since(start))
close(outChan)
errChan <- err
}()
for v := range outChan {
out = append(out, v...)
}
klog.V(2).Infof("query result: objects=%d", len(out))
return out, <-errChan
}
func queryAPI(client dynamic.Interface, api apiResource, ns string, selector string) ([]unstructured.Unstructured, error) {
var out []unstructured.Unstructured
var next string
for {
var intf dynamic.ResourceInterface
nintf := client.Resource(api.GroupVersionResource())
intf = nintf.Namespace(ns)
resp, err := intf.List(context.TODO(), metav1.ListOptions{
Limit: 250,
Continue: next,
LabelSelector: selector,
})
if err != nil {
klog.V(5).Infof("listing resources failed (%s): %v", api.GroupVersionResource(), err)
return nil, nil
}
out = append(out, resp.Items...)
next = resp.GetContinue()
if next == "" {
break
}
}
return out, nil
}
type apiResource struct {
r metav1.APIResource
gv schema.GroupVersion
}
func (a apiResource) GroupVersionResource() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: a.gv.Group,
Version: a.gv.Version,
Resource: a.r.Name,
}
}
type resourceNameLookup map[string][]apiResource
type resourceMap struct {
list []apiResource
m resourceNameLookup
}
func findAPIs(client discovery.DiscoveryInterface) (*resourceMap, error) {
start := time.Now()
var resList []*metav1.APIResourceList
// TODO(feloy) Remove call to ServerGroups() when https://github.com/kubernetes/kubernetes/issues/116414 is fixed
// The call to ServerGroups() prevents from calling ServerPreferredResources() when ServerGroups() returns nil
// (which will make ServerPreferredResources() panic)
originalErrorHandlers := runtime.ErrorHandlers
runtime.ErrorHandlers = nil
groups, err := client.ServerGroups()
if groups == nil {
resList = nil
} else {
resList, err = client.ServerPreferredResources()
}
runtime.ErrorHandlers = originalErrorHandlers
if err != nil {
return nil, fmt.Errorf("failed to fetch api groups from kubernetes: %w", err)
}
klog.V(5).Infof("queried api discovery in %v", time.Since(start))
klog.V(5).Infof("found %d items (groups) in server-preferred APIResourceList", len(resList))
rm := &resourceMap{
m: make(resourceNameLookup),
}
for _, group := range resList {
klog.V(5).Infof("iterating over group %s/%s (%d apis)", group.GroupVersion, group.APIVersion, len(group.APIResources))
gv, err := schema.ParseGroupVersion(group.GroupVersion)
if err != nil {
return nil, fmt.Errorf("%q cannot be parsed into groupversion: %w", group.GroupVersion, err)
}
for _, apiRes := range group.APIResources {
klog.V(5).Infof(" api=%s namespaced=%v", apiRes.Name, apiRes.Namespaced)
if !contains(apiRes.Verbs, "list") {
klog.V(5).Infof(" api (%s) doesn't have required verb, skipping: %v", apiRes.Name, apiRes.Verbs)
continue
}
v := apiResource{
gv: gv,
r: apiRes,
}
names := apiNames(apiRes, gv)
klog.V(5).Infof("names: %s", strings.Join(names, ", "))
for _, name := range names {
rm.m[name] = append(rm.m[name], v)
}
rm.list = append(rm.list, v)
}
}
klog.V(5).Infof(" found %d apis", len(rm.m))
return rm, nil
}
func contains(v []string, s string) bool {
for _, vv := range v {
if vv == s {
return true
}
}
return false
}
// return all names that could refer to this APIResource
func apiNames(a metav1.APIResource, gv schema.GroupVersion) []string {
var out []string
singularName := a.SingularName
if singularName == "" {
// TODO(ahmetb): sometimes SingularName is empty (e.g. Deployment), use lowercase Kind as fallback - investigate why
singularName = strings.ToLower(a.Kind)
}
pluralName := a.Name
shortNames := a.ShortNames
names := append([]string{singularName, pluralName}, shortNames...)
for _, n := range names {
fmtBare := n // e.g. deployment
fmtWithGroup := strings.Join([]string{n, gv.Group}, ".") // e.g. deployment.apps
fmtWithGroupVersion := strings.Join([]string{n, gv.Version, gv.Group}, ".") // e.g. deployment.v1.apps
out = append(out,
fmtBare, fmtWithGroup, fmtWithGroupVersion)
}
return out
}