-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
337 lines (281 loc) · 8.17 KB
/
api.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
package volm
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type VolumeInfo struct {
Name string `json:"name"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Status v1.PersistentVolumeClaimStatus `json:"status"`
Spec v1.PersistentVolumeClaimSpec `json:"spec"`
Terminating bool `json:"terminating"`
TerminatingSince *metaV1.Time `json:"terminatingSince,omitempty"`
UsedBy []PodInfo `json:"usedBy"`
}
type PodInfo struct {
Name string `json:"name"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Phase v1.PodPhase `json:"phase"`
StartTime *metaV1.Time `json:"startTime"`
Terminating bool `json:"terminating"`
TerminatingSince *metaV1.Time `json:"terminatingSince,omitempty"`
}
// Config configures the API
type Config struct {
Service string
Version string
Log *zap.Logger
Cs *kubernetes.Clientset
PVCNamespace string
PVCSelector string
}
// API is primary object implementing the core API methods
// and HTTP handlers
type API struct {
*Config
LogErrors prometheus.Counter
PVCSelectorMap map[string]string
PodStore *PodStore
PVCStore *PVCStore
}
// NewApi constructs an API object and populates it with
// configuration along with setting defaults where required.
func NewApi(cfg *Config) (*API, error) {
a := &API{Config: cfg}
// default logger if none specified
if a.Log == nil {
zapCfg := zap.NewProductionConfig()
logger, err := zapCfg.Build()
if err != nil {
os.Exit(1)
}
a.Log = logger
}
a.PVCSelectorMap = map[string]string{}
if a.PVCSelector != "" {
kvs := strings.Split(a.PVCSelector, ",")
for _, kv := range kvs {
kv := strings.Split(kv, "=")
if len(kv) < 2 {
a.Log.Fatal("Malformed PVC selector")
os.Exit(1)
}
a.PVCSelectorMap[kv[0]] = kv[1]
}
}
podStore, err := NewPodStore(&PodStoreConfig{
Namespace: a.PVCNamespace,
Log: a.Log,
Cs: a.Cs,
})
if err != nil {
return a, err
}
a.PodStore = podStore
pvcStore, err := NewPVCStore(&PVCStoreConfig{
Namespace: a.PVCNamespace,
Log: a.Log,
Cs: a.Cs,
})
if err != nil {
return a, err
}
a.PVCStore = pvcStore
return a, nil
}
// IsNotFound returns true if the error is a errors.StatusError
// matching metaV1.StatusReasonNotFound this function allows us
// to log more critical errors and pass status information such
// at 404s and 500s through the REST API.
func IsNotFound(err error) bool {
if statusError, isStatus := err.(*errors.StatusError); isStatus && statusError.Status().Reason == metaV1.StatusReasonNotFound {
return true
}
return false
}
// OkHandler is provided for created a default slash route for the
// HTTP API and returns basic version, node and service name.
func (a *API) OkHandler(version string, mode string, service string) gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"version": version, "mode": mode, "service": service})
}
}
func (a *API) ListPVCHandler() gin.HandlerFunc {
return func(c *gin.Context) {
pvcList, err := a.GetPVCList()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, pvcList)
}
}
func (a *API) GetPVCList() ([]VolumeInfo, error) {
vols := make([]VolumeInfo, 0)
// get all pods
pods := a.PodStore.GetPods()
for _, pvc := range a.PVCStore.GetPVCs() {
var terminating bool
var terminatingSince *metaV1.Time
selectorPass := true
// ensure PVC meets selector criteria
for k, v := range a.PVCSelectorMap {
if _, ok := pvc.Labels[k]; !ok {
selectorPass = false
}
if pvc.Labels[k] != v {
selectorPass = false
}
}
if !selectorPass {
continue
}
podList, err := a.GetPodsInfoByPVC(pods, pvc.Name)
if err != nil {
return vols, err
}
// See https://github.com/kubernetes/kubernetes/issues/22839
// on terminating status
if pvc.DeletionTimestamp != nil {
terminating = true
terminatingSince = pvc.DeletionTimestamp
}
vols = append(vols, VolumeInfo{
Name: pvc.Name,
Labels: pvc.Labels,
Annotations: pvc.Annotations,
Status: pvc.Status,
Spec: pvc.Spec,
Terminating: terminating,
TerminatingSince: terminatingSince,
UsedBy: podList,
})
}
return vols, nil
}
func (a *API) GetPVCHandler() gin.HandlerFunc {
return func(c *gin.Context) {
pvc, err := a.GetPVC(c.Param("name"))
if err != nil && err.Error() == "not found" {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, pvc)
}
}
func (a *API) GetPVC(name string) (VolumeInfo, error) {
volInfo := VolumeInfo{}
pvc := a.PVCStore.GetPVC(name)
if pvc == nil {
return volInfo, fmt.Errorf("not found")
}
// ensure PVC meets selector criteria
for k, v := range a.PVCSelectorMap {
if _, ok := pvc.Labels[k]; !ok {
return volInfo, fmt.Errorf("PVC labels does not contain key %s", k)
}
if pvc.Labels[k] != v {
return volInfo, fmt.Errorf("PVC label %s does not contain value %s", k, v)
}
}
volInfo.Name = pvc.Name
volInfo.Labels = pvc.Labels
volInfo.Annotations = pvc.Annotations
volInfo.Spec = pvc.Spec
volInfo.Status = pvc.Status
// See https://github.com/kubernetes/kubernetes/issues/22839
// on terminating status
if pvc.DeletionTimestamp != nil {
volInfo.Terminating = true
volInfo.TerminatingSince = pvc.DeletionTimestamp
}
pods := a.PodStore.GetPods()
podList, err := a.GetPodsInfoByPVC(pods, pvc.Name)
if err != nil {
return VolumeInfo{}, err
}
volInfo.UsedBy = podList
return volInfo, err
}
func (a *API) DeletePVCHandler() gin.HandlerFunc {
return func(c *gin.Context) {
err := a.DeletePVC(c.Param("name"))
if IsNotFound(err) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": true})
}
}
func (a *API) DeletePVC(name string) error {
ctx := context.Background()
pvcClient := a.Cs.CoreV1().PersistentVolumeClaims(a.PVCNamespace)
pvc, err := pvcClient.Get(ctx, name, metaV1.GetOptions{})
if IsNotFound(err) {
return err
}
if err != nil {
a.Log.Error("GetPVC got error invoking pvcClient.Get", zap.Error(err))
return err
}
// ensure PVC meets selector criteria
for k, v := range a.PVCSelectorMap {
if _, ok := pvc.Labels[k]; !ok {
return fmt.Errorf("PVC labels does not contain key %s", k)
}
if pvc.Labels[k] != v {
return fmt.Errorf("PVC label %s does not contain value %s", k, v)
}
}
err = pvcClient.Delete(ctx, name, metaV1.DeleteOptions{})
if err != nil {
a.Log.Error("DeletePVC got error invoking pvcClient.Delete", zap.Error(err))
return err
}
return nil
}
func (a *API) GetPodsInfoByPVC(pods []v1.Pod, pvcName string) ([]PodInfo, error) {
var podInfoList []PodInfo
for _, pod := range pods {
for _, v := range pod.Spec.Volumes {
if v.PersistentVolumeClaim != nil && v.PersistentVolumeClaim.ClaimName == pvcName {
var terminating bool
var terminatingSince *metaV1.Time
if pod.DeletionTimestamp != nil {
terminating = true
terminatingSince = pod.DeletionTimestamp
}
podInfoList = append(podInfoList, PodInfo{
Name: pod.Name,
Labels: pod.Labels,
Annotations: pod.Annotations,
Phase: pod.Status.Phase,
StartTime: pod.Status.StartTime,
Terminating: terminating,
TerminatingSince: terminatingSince,
})
}
}
}
return podInfoList, nil
}