-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
417 lines (353 loc) · 14.6 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/yaml"
)
var (
// will be replaced during the build process
version = "undefined"
commit = "undefined"
date = "undefined"
)
func lookupEnvString(key string, defaultVal string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return defaultVal
}
func lookupEnvBool(key string, defaultVal bool) bool {
if val, ok := os.LookupEnv(key); ok {
parsed, err := strconv.ParseBool(val)
if err != nil {
log.Fatalf("failed parsing %q as bool (%q): %v", val, key, err)
}
return parsed
}
return defaultVal
}
func lookupEnvUint64(key string, defaultVal uint64) uint64 {
if val, ok := os.LookupEnv(key); ok {
parsed, err := strconv.ParseUint(val, 10, 64)
if err != nil {
log.Fatalf("failed parsing %q as uint64 (%q): %v", val, key, err)
}
return parsed
}
return defaultVal
}
func main() {
start := time.Now()
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("failed getting user home dir: %v\n", err)
}
var (
kubeConfigPath = flag.String("config", lookupEnvString("CONFIG", filepath.Join(homeDir, ".kube", "config")), "path to the kubeconfig, empty for in-cluster config")
kubeContext = flag.String("context", lookupEnvString("CONTEXT", ""), "context from the kubeconfig, empty for default")
outdirFlag = flag.String("dir", lookupEnvString("DIR", "dump"), "output directory for the dumps")
labelsFlag = flag.String("labels", lookupEnvString("LABELS", ""), "dump resources with the given labels (e.g. key1=value1,key2=value2), empty for all")
ignoreLabelsFlag = flag.String("ignore-labels", lookupEnvString("IGNORE_LABELS", ""), "ignore resources with the given labels (e.g. key1=value1,key2=value2)")
resourcesFlag = flag.String("resources", lookupEnvString("RESOURCES", ""), "resources to dump (e.g. 'configmaps,secrets'), empty for all")
ignoreResourcesFlag = flag.String("ignore-resources", lookupEnvString("IGNORE_RESOURCES", ""), "resources to ignore (e.g. 'configmaps,secrets')")
namespacesFlag = flag.String("namespaces", lookupEnvString("NAMESPACES", ""), "namespaces to dump (e.g. 'ns1,ns2'), empty for all")
ignoreNamespacesFlag = flag.String("ignore-namespaces", lookupEnvString("IGNORE_NAMESPACES", ""), "namespaces to ignore (e.g. 'ns1,ns2')")
groupsFlag = flag.String("groups", lookupEnvString("GROUPS", ""), "groups to dump (e.g. 'metrics.k8s.io,coordination.k8s.io'), empty for all")
ignoreGroupsFlag = flag.String("ignore-groups", lookupEnvString("IGNORE_GROUPS", ""), "groups to ignore (e.g. 'metrics.k8s.io,coordination.k8s.io')")
clusterscopedFlag = flag.Bool("clusterscoped", lookupEnvBool("CLUSTERSCOPED", true), "dump cluster-wide resources")
namespacedFlag = flag.Bool("namespaced", lookupEnvBool("NAMESPACED", true), "dump namespaced resources")
statelessFlag = flag.Bool("stateless", lookupEnvBool("STATELESS", true), "remove fields containing a state of the resource")
versionFlag = flag.Bool("version", lookupEnvBool("VERSION", false), fmt.Sprintf("print version information of this release (%v)", version))
maxThreadsFlag = flag.Uint64("threads", lookupEnvUint64("THREADS", 10), "maximum number of threads (minimum 1)")
verbosityFlag = flag.Uint64("verbosity", lookupEnvUint64("VERBOSITY", 1), "verbosity of the output (0-3)")
)
flag.Parse()
if *versionFlag || *verbosityFlag > 1 {
fmt.Printf("version: %v\n", version)
fmt.Printf("commit: %v\n", commit)
fmt.Printf("date: %v\n", date)
if *versionFlag {
os.Exit(0)
}
}
if *maxThreadsFlag <= 0 {
log.Fatalln("minimum number of threads is 1")
}
kubeConfig, err := buildConfigFromFlags(*kubeContext, *kubeConfigPath)
if err != nil {
log.Fatalf("failed getting Kubernetes config: %v\n", err)
}
clientset, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
log.Fatalf("failed getting Kubernetes clientset: %v\n", err)
}
groups, err := clientset.DiscoveryClient.ServerGroups()
if err != nil {
log.Fatalf("failed getting server groups: %v\n", err)
}
dynamicClient, err := dynamic.NewForConfig(kubeConfig)
if err != nil {
log.Fatalf("failed creating dynamic client: %v\n", err)
}
var (
writtenFiles uint64
waitGroup sync.WaitGroup
threadGuard = make(chan struct{}, *maxThreadsFlag)
wantLabels = parseLabelsFlag(*labelsFlag)
wantResources = strings.Split(strings.ToLower(*resourcesFlag), ",")
wantNamespaces = strings.Split(strings.ToLower(*namespacesFlag), ",")
wantGroups = strings.Split(strings.ToLower(*groupsFlag), ",")
ignoreLabels = parseLabelsFlag(*ignoreLabelsFlag)
ignoreResources = strings.Split(strings.ToLower(*ignoreResourcesFlag), ",")
ignoreNamespaces = strings.Split(strings.ToLower(*ignoreNamespacesFlag), ",")
ignoreGroups = strings.Split(strings.ToLower(*ignoreGroupsFlag), ",")
)
for _, group := range groups.Groups {
if skipGroup(group, wantGroups, ignoreGroups) {
continue
}
for _, version := range group.Versions {
resources, err := clientset.DiscoveryClient.ServerResourcesForGroupVersion(version.GroupVersion)
if err != nil {
log.Printf("failed getting resources for %q: %v\n", version.GroupVersion, err)
continue
}
waitGroup.Add(len(resources.APIResources))
for _, res := range resources.APIResources {
threadGuard <- struct{}{} // would block if guard channel is already filled
go func(res metav1.APIResource, group metav1.APIGroup, version metav1.GroupVersionForDiscovery) {
defer func() {
waitGroup.Done()
<-threadGuard
}()
if skipResource(res, wantResources, ignoreResources) {
return
}
gvr := schema.GroupVersionResource{
Group: group.Name,
Version: version.Version,
Resource: res.Name,
}
if *verbosityFlag > 1 {
fmt.Printf("processing group=%v resource=%v\n", gvr.Group, gvr.Resource)
}
unstrList, err := dynamicClient.Resource(gvr).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Printf("failed listing %v: %v\n", gvr.String(), err)
return
}
for _, item := range unstrList.Items {
if skipItem(item, *namespacedFlag, *clusterscopedFlag, wantNamespaces, ignoreNamespaces) {
continue
}
if skipLabels(item.GetLabels(), wantLabels, ignoreLabels) {
continue
}
// Use a combination of resource and group name as it might not be unique otherwise.
// Example content of the variables:
// resource: "pod" group: ""
// resource: "pod" group: "metrics.k8s.io"
resourceAndGroup := strings.TrimSuffix(fmt.Sprintf("%s.%s", res.Name, group.Name), ".")
if *verbosityFlag > 2 {
fmt.Printf("processing manifest group=%v version=%v resource=%v namespace=%v name=%q\n", gvr.Group, gvr.Version, gvr.Resource, item.GetNamespace(), item.GetName())
}
if err := writeYAML(*outdirFlag, resourceAndGroup, item, *statelessFlag); err != nil {
log.Printf("failed writing %v/%v: %v\n", item.GetNamespace(), item.GetName(), err)
continue
}
atomic.AddUint64(&writtenFiles, 1)
}
}(res, group, version)
}
}
}
waitGroup.Wait()
if *verbosityFlag > 0 {
fmt.Printf("loaded %d manifests in %v\n", writtenFiles, time.Since(start).Round(1*time.Millisecond))
}
}
func parseLabelsFlag(labelsFlag string) map[string]string {
wantLabelsKeyValue := strings.Split(labelsFlag, ",")
if len(wantLabelsKeyValue) == 1 && wantLabelsKeyValue[0] == "" {
return nil
}
wantLabels := make(map[string]string)
for _, keyVal := range wantLabelsKeyValue {
key, val, ok := strings.Cut(keyVal, "=")
if !ok {
log.Fatalf("failed parsing (ignore-)labels flag at %q\n", keyVal)
}
wantLabels[key] = val
}
return wantLabels
}
func skipGroup(group metav1.APIGroup, wantGroups, ignoreGroups []string) bool {
// check if we got the specified group (if any groups were specified)
if len(wantGroups) > 0 && wantGroups[0] != "" && !slices.Contains(wantGroups, group.Name) {
return true
}
// check if we got a group to ignore (if any groups were specified)
if len(ignoreGroups) > 0 && ignoreGroups[0] != "" && slices.Contains(ignoreGroups, group.Name) {
return true
}
return false
}
func skipResource(res metav1.APIResource, wantResources, ignoreResources []string) bool {
// check if we can even 'list' the resource
if !slices.Contains(res.Verbs, "list") {
return true
}
// skip subresources
// TODO: maybe there is a better way to not get them in the first place
if strings.Contains(res.Name, "/") {
return true
}
// check if we got the specified resources (if any resources were specified)
if len(wantResources) > 0 && wantResources[0] != "" && !slices.Contains(wantResources, res.Name) {
return true
}
// check if we got a resource to ignore (if any resources were specified)
if len(ignoreResources) > 0 && ignoreResources[0] != "" && slices.Contains(ignoreResources, res.Name) {
return true
}
return false
}
func skipItem(item unstructured.Unstructured, namespaced, clusterscoped bool, wantNamespaces, ignoreNamespaces []string) bool {
// item with namespace but we skip namespaced items
if item.GetNamespace() != "" && !namespaced {
return true
}
// item clusterscoped but we skip them
if item.GetNamespace() == "" && !clusterscoped {
return true
}
// specific namespaces specied but doesn't match
if len(wantNamespaces) > 0 && wantNamespaces[0] != "" && !slices.Contains(wantNamespaces, item.GetNamespace()) {
return true
}
// ignore specific namespaces and it matches
if len(ignoreNamespaces) > 0 && ignoreNamespaces[0] != "" && slices.Contains(ignoreNamespaces, item.GetNamespace()) {
return true
}
return false
}
func skipLabels(got, want, ignore map[string]string) bool {
return skipLabelsWant(got, want) || skipLabelsIgnore(got, ignore)
}
func skipLabelsWant(got, want map[string]string) bool {
if len(want) == 0 {
return false
}
for wantKey, wantVal := range want {
for gotKey, gotVal := range got {
if wantKey == gotKey {
return wantVal != gotVal
}
}
}
return true
}
func skipLabelsIgnore(got, ignore map[string]string) bool {
if len(ignore) == 0 {
return false
}
for ignoreKey, ignoreVal := range ignore {
for gotKey, gotVal := range got {
if ignoreKey == gotKey {
return ignoreVal == gotVal
}
}
}
return false
}
func writeYAML(outDir, resourceAndGroup string, item unstructured.Unstructured, stateless bool) error {
if stateless {
cleanState(item)
}
yamlBytes, err := yaml.Marshal(item.Object)
if err != nil {
return fmt.Errorf("failed marshalling: %v", err)
}
namespace := "clusterscoped"
if item.GetNamespace() != "" {
namespace = filepath.Join("namespaced", item.GetNamespace())
}
dir := filepath.Join(outDir, namespace, resourceAndGroup)
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
return fmt.Errorf("failed creating dir %q: %v", dir, err)
}
objName := strings.ReplaceAll(item.GetName(), ":", "_") // windows compatibility
filename := filepath.Join(dir, objName) + ".yaml"
if err = os.WriteFile(filename, yamlBytes, os.ModePerm); err != nil {
return fmt.Errorf("failed writing file %q: %v", filename, err)
}
return nil
}
func cleanState(item unstructured.Unstructured) {
// partially based on https://github.com/WoozyMasta/kube-dump/blob/f1ae560a8b9da8dba1c28619f38089d40d0d2357/kube-dump#L334
// cluster-scoped and namespaced
unstructured.RemoveNestedField(item.Object, "metadata", "annotations", "control-plane.alpha.kubernetes.io/leader")
unstructured.RemoveNestedField(item.Object, "metadata", "annotations", "kubectl.kubernetes.io/last-applied-configuration")
unstructured.RemoveNestedField(item.Object, "metadata", "creationTimestamp")
unstructured.RemoveNestedField(item.Object, "metadata", "finalizers")
unstructured.RemoveNestedField(item.Object, "metadata", "generation")
unstructured.RemoveNestedField(item.Object, "metadata", "managedFields")
unstructured.RemoveNestedField(item.Object, "metadata", "resourceVersion")
unstructured.RemoveNestedField(item.Object, "metadata", "selfLink")
unstructured.RemoveNestedField(item.Object, "metadata", "ownerReferences")
unstructured.RemoveNestedField(item.Object, "metadata", "uid")
unstructured.RemoveNestedField(item.Object, "status")
if item.GetNamespace() == "" {
// cluster-scoped only
} else {
// namespaced only
unstructured.RemoveNestedField(item.Object, "metadata", "annotations", "autoscaling.alpha.kubernetes.io/conditions")
unstructured.RemoveNestedField(item.Object, "metadata", "annotations", "autoscaling.alpha.kubernetes.io/current-metrics")
unstructured.RemoveNestedField(item.Object, "metadata", "annotations", "deployment.kubernetes.io/revision")
unstructured.RemoveNestedField(item.Object, "metadata", "annotations", "kubernetes.io/config.seen")
unstructured.RemoveNestedField(item.Object, "metadata", "annotations", "kubernetes.io/service-account.uid")
unstructured.RemoveNestedField(item.Object, "metadata", "annotations", "pv.kubernetes.io/bind-completed")
unstructured.RemoveNestedField(item.Object, "metadata", "annotations", "pv.kubernetes.io/bound-by-controller")
unstructured.RemoveNestedField(item.Object, "metadata", "clusterIP")
unstructured.RemoveNestedField(item.Object, "metadata", "progressDeadlineSeconds")
unstructured.RemoveNestedField(item.Object, "metadata", "revisionHistoryLimit")
unstructured.RemoveNestedField(item.Object, "metadata", "spec", "metadata", "annotations", "kubectl.kubernetes.io/restartedAt")
unstructured.RemoveNestedField(item.Object, "metadata", "spec", "metadata", "creationTimestamp")
unstructured.RemoveNestedField(item.Object, "spec", "volumeName")
unstructured.RemoveNestedField(item.Object, "spec", "volumeMode")
}
}
// https://github.com/kubernetes/client-go/issues/192#issuecomment-349564767
func buildConfigFromFlags(context, kubeconfigPath string) (*rest.Config, error) {
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
&clientcmd.ConfigOverrides{
CurrentContext: context,
}).ClientConfig()
if err != nil {
return config, err
}
// https://kubernetes.io/blog/2020/09/03/warnings/#customize-client-handling
config = rest.CopyConfig(config)
config.WarningHandler = rest.NoWarnings{}
config.QPS = 100
config.Burst = 300
return config, nil
}