-
Notifications
You must be signed in to change notification settings - Fork 68
/
k8sdeprecated.go
72 lines (64 loc) · 2.26 KB
/
k8sdeprecated.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
package kubepug
import (
"context"
log "github.com/sirupsen/logrus"
apierrors "k8s.io/apimachinery/pkg/api/errors"
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/rest"
)
func listObjects(items []unstructured.Unstructured) (deprecatedItems []DeprecatedItem) {
for _, d := range items {
name := d.GetName()
namespace := d.GetNamespace()
if namespace != "" {
deprecatedItems = append(deprecatedItems, DeprecatedItem{Scope: "OBJECT", ObjectName: name, Namespace: namespace})
} else {
deprecatedItems = append(deprecatedItems, DeprecatedItem{Scope: "GLOBAL", ObjectName: name})
}
}
return deprecatedItems
}
// ListDeprecated receives the Map of Deprecated API and List the existent Deprecated Objects in the Cluster
func (KubeAPIs KubernetesAPIs) ListDeprecated(config *rest.Config, showDescription bool) (deprecated []DeprecatedAPI) {
client, err := dynamic.NewForConfig(config)
if err != nil {
log.Fatalf("Failed to create the K8s client while listing Deprecated objects")
}
for _, dpa := range KubeAPIs {
// We only want deprecated APIs :)
if !dpa.deprecated {
continue
}
log.Debugf("Listing objects for %s/%s/%s", dpa.group, dpa.version, dpa.name)
gvr := schema.GroupVersionResource{Group: dpa.group, Version: dpa.version, Resource: dpa.name}
list, err := client.Resource(gvr).List(context.TODO(), metav1.ListOptions{})
if apierrors.IsNotFound(err) {
continue
}
if apierrors.IsForbidden(err) {
log.Fatalf("Failed to list objects in the cluster. Permission denied! Please check if you have the proper authorization")
}
if err != nil {
log.Fatalf("Failed communicating with k8s while listing objects. \nError: %v", err)
}
if len(list.Items) > 0 {
log.Infof("Found %d deprecated objects of type %s/%s/%s", len(list.Items), dpa.group, dpa.version, dpa.name)
api := DeprecatedAPI{
Kind: dpa.kind,
Deprecated: dpa.deprecated,
Group: dpa.group,
Name: dpa.name,
Version: dpa.version,
}
if showDescription {
api.Description = dpa.description
}
api.Items = listObjects(list.Items)
deprecated = append(deprecated, api)
}
}
return deprecated
}