-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
65 lines (56 loc) · 1.41 KB
/
discovery.go
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
package main
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
type discovery interface {
discover() ([]schema.GroupVersionResource, error)
}
type dumpDiscovery struct {
clusterConfig *rest.Config
}
func newDiscovery(clusterConfig *rest.Config) discovery {
return &dumpDiscovery{
clusterConfig: clusterConfig,
}
}
func (d *dumpDiscovery) discover() ([]schema.GroupVersionResource, error) {
clientset, err := kubernetes.NewForConfig(d.clusterConfig)
if err != nil {
return nil, err
}
lists, err := clientset.DiscoveryClient.ServerPreferredResources()
if err != nil {
return nil, err
}
return d.parseGvrs(lists)
}
func (d *dumpDiscovery) parseGvrs(lists []*metav1.APIResourceList) ([]schema.GroupVersionResource, error) {
gvrs := []schema.GroupVersionResource{}
for _, list := range lists {
gv, err := schema.ParseGroupVersion(list.GroupVersion)
if err != nil {
return nil, err
}
for _, apiResource := range list.APIResources {
if d.hasWatchVerb(apiResource.Verbs) {
gvrs = append(gvrs, schema.GroupVersionResource{
Group: gv.Group,
Version: gv.Version,
Resource: apiResource.Name,
})
}
}
}
return gvrs, nil
}
func (d *dumpDiscovery) hasWatchVerb(verbs []string) bool {
for _, verb := range verbs {
if verb == "watch" {
return true
}
}
return false
}