-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (164 loc) · 4.02 KB
/
main.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
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
package main
import (
"fmt"
"strings"
"time"
"gomodules.xyz/sets"
ksets "gomodules.xyz/sets/kubernetes"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
apiv1 "kmodules.xyz/client-go/api/v1"
ctrl "sigs.k8s.io/controller-runtime"
)
func main() {
gkSet := ksets.NewGroupKind(
schema.GroupKind{
Group: "admissionregistration.k8s.io",
Kind: "ValidatingWebhookConfiguration",
},
schema.GroupKind{
Group: "events.k8s.io",
Kind: "Event",
},
schema.GroupKind{
Group: "storage.k8s.io",
Kind: "VolumeAttachment",
},
schema.GroupKind{
Group: "admissionregistration.k8s.io",
Kind: "MutatingWebhookConfiguration",
},
schema.GroupKind{
Group: "",
Kind: "PodTemplate",
},
schema.GroupKind{
Group: "apps",
Kind: "ControllerRevision",
},
schema.GroupKind{
Group: "apiextensions.k8s.io",
Kind: "CustomResourceDefinition",
},
schema.GroupKind{
Group: "flowcontrol.apiserver.k8s.io",
Kind: "PriorityLevelConfiguration",
},
schema.GroupKind{
Group: "",
Kind: "Event",
})
cfg := ctrl.GetConfigOrDie()
kc := kubernetes.NewForConfigOrDie(cfg)
//rsLists, err := kc.Discovery().ServerPreferredResources()
//if err != nil {
// panic(err)
//}
//result, err := restmapper.GetAPIGroupResources(kc.Discovery())
//if err != nil {
// panic(err)
//}
//data, err := json.MarshalIndent(result, "", " ")
//if err != nil {
// panic(err)
//}
//fmt.Println(string(data))
resourceChannel := make(chan apiv1.ResourceID, 100)
resourceTracker := map[schema.GroupVersionKind]apiv1.ResourceID{}
//for _, rsList := range rsLists {
// for _, rs := range rsList.APIResources {
// // skip sub resource
// if strings.ContainsRune(rs.Name, '/') {
// continue
// }
//
// // if resource can't be listed or read (get) skip it
// verbs := sets.NewString(rs.Verbs...)
// if !verbs.HasAll("list", "get", "watch") {
// continue
// }
//
// gvk := schema.FromAPIVersionAndKind(rsList.GroupVersion, rs.Kind)
// if gkSet.Has(gvk.GroupKind()) {
// continue
// }
//
// rid := apiv1.ResourceID{
// Group: gvk.Group,
// Version: gvk.Version,
// Name: rs.Name,
// Kind: rs.Kind,
// Scope: apiv1.ClusterScoped,
// }
// if rs.Namespaced {
// rid.Scope = apiv1.NamespaceScoped
// }
// if _, found := resourceTracker[gvk]; !found {
// resourceTracker[gvk] = rid
// resourceChannel <- rid
// }
// }
//}
//
//result := make([]apiv1.ResourceID, 0, len(resourceTracker))
//for _, rs := range resourceTracker {
// result = append(result, rs)
//}
//
//data2, err := json.MarshalIndent(result, "", " ")
//if err != nil {
// panic(err)
//}
//fmt.Println(string(data2))
go func() {
for rid := range resourceChannel {
fmt.Println(rid.GroupVersionKind())
}
}()
err := wait.PollImmediateUntil(60*time.Second, func() (done bool, err error) {
rsLists, err := kc.Discovery().ServerPreferredResources()
if err != nil && !discovery.IsGroupDiscoveryFailedError(err) {
klog.ErrorS(err, "failed to list server preferred resources")
return false, nil
}
for _, rsList := range rsLists {
for _, rs := range rsList.APIResources {
// skip sub resource
if strings.ContainsRune(rs.Name, '/') {
continue
}
// if resource can't be listed or read (get) skip it
verbs := sets.NewString(rs.Verbs...)
if !verbs.HasAll("list", "get", "watch") {
continue
}
gvk := schema.FromAPIVersionAndKind(rsList.GroupVersion, rs.Kind)
if gkSet.Has(gvk.GroupKind()) {
continue
}
rid := apiv1.ResourceID{
Group: gvk.Group,
Version: gvk.Version,
Name: rs.Name,
Kind: rs.Kind,
Scope: apiv1.ClusterScoped,
}
if rs.Namespaced {
rid.Scope = apiv1.NamespaceScoped
}
if _, found := resourceTracker[gvk]; !found {
resourceTracker[gvk] = rid
resourceChannel <- rid
}
}
}
return false, nil
}, nil)
if err != nil {
panic(err)
}
select {}
}