-
Notifications
You must be signed in to change notification settings - Fork 5
/
tools.go
173 lines (163 loc) · 5.03 KB
/
tools.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
package persistentvolumeclaim
import (
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
)
// GetStatus get the status phase of the persistentvolumeclaim.
// All supported status are: Pending, Bound, Lost
// pending used for PersistentVolumeClaims that are not yet bound.
// Bound used for PersistentVolumeClaims that are bound.
// Lost used for PersistentVolumeClaims that lost their underlying.
func (h *Handler) GetStatus(object interface{}) (phase string, err error) {
switch val := object.(type) {
case string:
pvc, err := h.Get(val)
if err != nil {
return "", err
}
return string(pvc.Status.Phase), nil
case *corev1.PersistentVolumeClaim:
return string(val.Status.Phase), nil
case corev1.PersistentVolumeClaim:
return string(val.Status.Phase), nil
default:
return "", ErrInvalidToolsType
}
}
// GetVolume simply calls GetPV.
func (h *Handler) GetVolume(object interface{}) (string, error) {
return h.GetPV(object)
}
// GetPV get the pv name of the persistentvolumeclaim
func (h *Handler) GetPV(object interface{}) (string, error) {
switch val := object.(type) {
case string:
pvc, err := h.Get(val)
if err != nil {
return "", err
}
return pvc.Spec.VolumeName, nil
case *corev1.PersistentVolumeClaim:
return val.Spec.VolumeName, nil
case corev1.PersistentVolumeClaim:
return val.Spec.VolumeName, nil
default:
return "", ErrInvalidToolsType
}
}
// GetCapacity get the storage capacity of the persistentvolumeclaim.
func (h *Handler) GetCapacity(object interface{}) (int64, error) {
switch val := object.(type) {
case string:
pvc, err := h.Get(val)
if err != nil {
return 0, err
}
return h.getCapacity(pvc), nil
case *corev1.PersistentVolumeClaim:
return h.getCapacity(val), nil
case corev1.PersistentVolumeClaim:
return h.getCapacity(&val), nil
default:
return 0, ErrInvalidToolsType
}
}
func (h *Handler) getCapacity(pvc *corev1.PersistentVolumeClaim) int64 {
storage := pvc.Status.Capacity[corev1.ResourceName(corev1.ResourceStorage)]
//capacity = storage.Value()
//capacity = storage.MilliValue()
//capacity = storage.ScaledValue(resource.Kilo)
//capacity = storage.ScaledValue(resource.Mega)
//capacity = storage.ScaledValue(resource.Giga)
//capacity = storage.ScaledValue(resource.Tera)
//capacity = storage.ScaledValue(resource.Peta)
//capacity = storage.ScaledValue(resource.Exa)
return storage.Value()
}
// GetAccessModes get the access modes of the persistentvolumeclaim.
func (h *Handler) GetAccessModes(object interface{}) ([]string, error) {
switch val := object.(type) {
case string:
pvc, err := h.Get(val)
if err != nil {
return nil, err
}
return h.getAccessModes(pvc), nil
case *corev1.PersistentVolumeClaim:
return h.getAccessModes(val), nil
case corev1.PersistentVolumeClaim:
return h.getAccessModes(&val), nil
default:
return nil, ErrInvalidToolsType
}
}
func (h *Handler) getAccessModes(pvc *corev1.PersistentVolumeClaim) []string {
var accessModes []string
for _, accessMode := range pvc.Status.AccessModes {
accessModes = append(accessModes, string(accessMode))
}
return accessModes
}
// GetStorageClass get the storageclass name of the persistentvolumeclaim.
func (h *Handler) GetStorageClass(object interface{}) (string, error) {
switch val := object.(type) {
case string:
pvc, err := h.Get(val)
if err != nil {
return "", err
}
if pvc.Spec.StorageClassName == nil {
return "", fmt.Errorf("pvc/%s doesn't have storageclass", pvc.Name)
}
return *(pvc.Spec.StorageClassName), nil
case *corev1.PersistentVolumeClaim:
if val.Spec.StorageClassName == nil {
return "", fmt.Errorf("pvc/%s doesn't have storageclass", val.Name)
}
return *(val.Spec.StorageClassName), nil
case corev1.PersistentVolumeClaim:
if val.Spec.StorageClassName == nil {
return "", fmt.Errorf("pvc/%s doesn't have storageclass", val.Name)
}
return *(val.Spec.StorageClassName), nil
default:
return "", ErrInvalidToolsType
}
}
// GetAge returns age of the persistentvolumeclaim.
func (h *Handler) GetAge(object interface{}) (time.Duration, error) {
switch val := object.(type) {
case string:
pvc, err := h.Get(val)
if err != nil {
return time.Duration(0), err
}
return time.Now().Sub(pvc.CreationTimestamp.Time), nil
case *corev1.PersistentVolumeClaim:
return time.Now().Sub(val.CreationTimestamp.Time), nil
case corev1.PersistentVolumeClaim:
return time.Now().Sub(val.CreationTimestamp.Time), nil
default:
return time.Duration(0), ErrInvalidToolsType
}
}
// GetVolumeMode get volume mode of the persistentvolumeclaim.
// volumeMode defines what type of volume is required by the claim.
// Value of Filesystem is implied when not included in claim spec.
func (h *Handler) GetVolumeMode(object interface{}) (string, error) {
switch val := object.(type) {
case string:
pvc, err := h.Get(val)
if err != nil {
return "", err
}
return string(*pvc.Spec.VolumeMode), nil
case *corev1.PersistentVolumeClaim:
return string(*val.Spec.VolumeMode), nil
case corev1.PersistentVolumeClaim:
return string(*val.Spec.VolumeMode), nil
default:
return "", ErrInvalidToolsType
}
}