forked from dirathea/kubectl-unused-volumes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.go
117 lines (101 loc) · 2.92 KB
/
plugin.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
package plugin
import (
"context"
"strings"
"github.com/ava-labs/kubectl-unused-volumes/pkg/api"
"github.com/ava-labs/kubectl-unused-volumes/pkg/workload"
"github.com/gosuri/uitable"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
v1 "k8s.io/api/core/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
)
type Options struct {
KubernetesConfigFlags *genericclioptions.ConfigFlags
NoHeaders bool
AllNamespaces bool
}
func RunPlugin(options Options) (output string, err error) {
// log := logger.NewLogger()
config, err := options.KubernetesConfigFlags.ToRESTConfig()
if err != nil {
err = errors.Wrap(err, "failed to read kubeconfig")
return
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
err = errors.Wrap(err, "failed to create clientset")
return
}
volumes, err := GetVolumes(context.Background(), clientset, *options.KubernetesConfigFlags.Namespace)
if err != nil {
err = errors.Wrap(err, "failed to get all pvc in namespaces")
return
}
allResources := []func(ctx context.Context, client *kubernetes.Clientset, namespace string) ([]api.Workload, error){
workload.GetAllDaemonSet,
workload.GetAllDeployment,
workload.GetAllJobs,
workload.GetAllStatefulset,
}
workloads := []api.Workload{}
fetchGroup, _ := errgroup.WithContext(context.Background())
for _, f := range allResources {
fetcherFunction := f
fetchGroup.Go(func() error {
lists, err := fetcherFunction(context.Background(), clientset, *options.KubernetesConfigFlags.Namespace)
if err == nil {
workloads = append(workloads, lists...)
}
return err
})
}
// Wait for all HTTP fetches to complete.
if err = fetchGroup.Wait(); err != nil {
err = errors.Wrap(err, "failed to get all pvc in namespaces")
return
}
for _, wk := range workloads {
if !wk.IsEmpty() {
removeVolume(volumes, wk)
} else {
markVolumeAsZeroReplica(volumes, wk)
}
}
table := uitable.New()
if !options.NoHeaders {
table.AddRow("Name", "Volume Name", "Size", "Reason", "Used By")
}
for _, p := range volumes {
if p != nil {
storageSize := p.Spec.Resources.Requests[v1.ResourceStorage]
table.AddRow(p.Name, p.Spec.VolumeName, storageSize.String(), p.Reason, workload.Join(p.Workloads, ","))
}
}
output = table.String()
return
}
func removeVolume(volumes []*api.Volume, workload api.Workload) {
for _, claim := range workload.GetVolumeNames() {
for idx, vol := range volumes {
if vol != nil {
if strings.HasPrefix(vol.GetName(), claim) {
volumes[idx] = nil
}
}
}
}
}
func markVolumeAsZeroReplica(volumes []*api.Volume, workload api.Workload) {
for _, claim := range workload.GetVolumeNames() {
for idx, vol := range volumes {
if vol != nil {
if strings.HasPrefix(vol.GetName(), claim) {
volumes[idx].Reason = api.WORKLOAD_HAS_ZERO_REPLICAS
volumes[idx].Workloads = append(volumes[idx].Workloads, workload)
}
}
}
}
}