forked from dirathea/kubectl-unused-volumes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkload.go
124 lines (101 loc) · 2.56 KB
/
workload.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
package workload
import (
"fmt"
"github.com/ava-labs/kubectl-unused-volumes/pkg/api"
appsV1 "k8s.io/api/apps/v1"
batchV1 "k8s.io/api/batch/v1"
)
type deployment struct {
appsV1.Deployment
}
func (d deployment) IsEmpty() bool {
return d.Status.Replicas == 0
}
func (d deployment) GetVolumeNames() (volumeNames []string) {
volumes := d.Spec.Template.Spec.Volumes
for _, vol := range volumes {
if vol.PersistentVolumeClaim != nil {
volumeNames = append(volumeNames, vol.PersistentVolumeClaim.ClaimName)
}
}
return
}
func (d deployment) GetName() string {
return d.Name
}
func deploymentWorkload(workload appsV1.Deployment) api.Workload {
return deployment{workload}
}
type daemonSet struct {
appsV1.DaemonSet
}
func (d daemonSet) GetVolumeNames() (volumeNames []string) {
volumes := d.Spec.Template.Spec.Volumes
for _, vol := range volumes {
if vol.PersistentVolumeClaim != nil {
volumeNames = append(volumeNames, vol.PersistentVolumeClaim.ClaimName)
}
}
return
}
func (d daemonSet) IsEmpty() bool {
return d.Status.DesiredNumberScheduled == 0
}
func (d daemonSet) GetName() string {
return d.Name
}
func daemonsetWorkload(workload appsV1.DaemonSet) api.Workload {
return daemonSet{workload}
}
type statefulSet struct {
appsV1.StatefulSet
}
func (s statefulSet) GetVolumeNames() (volumeNames []string) {
volumes := s.Spec.Template.Spec.Volumes
for _, vol := range volumes {
if vol.PersistentVolumeClaim != nil {
volumeNames = append(volumeNames, vol.PersistentVolumeClaim.ClaimName)
}
}
// Statefulset has dynamic pvc object
pvcTemplates := s.Spec.VolumeClaimTemplates
for _, vol := range pvcTemplates {
pvcPrefixName := fmt.Sprintf("%s-%s-", vol.GetName(), s.GetName())
volumeNames = append(volumeNames, pvcPrefixName)
}
return
}
func (s statefulSet) IsEmpty() bool {
return s.Status.Replicas == 0
}
func (s statefulSet) GetName() string {
return s.Name
}
func statefulsetWorkload(workload appsV1.StatefulSet) api.Workload {
return statefulSet{workload}
}
type job struct {
batchV1.Job
}
func (j job) GetVolumeNames() (volumeNames []string) {
volumes := j.Spec.Template.Spec.Volumes
for _, vol := range volumes {
if vol.PersistentVolumeClaim != nil {
volumeNames = append(volumeNames, vol.PersistentVolumeClaim.ClaimName)
}
}
return
}
func (j job) IsEmpty() bool {
completions := int32(1)
if j.Spec.Completions != nil {
completions = *j.Spec.Completions
}
return j.Status.Succeeded == completions
}
func (j job) GetName() string {
return j.Name
}
func jobWorkload(workload batchV1.Job) api.Workload {
return job{workload}
}