-
Notifications
You must be signed in to change notification settings - Fork 4
/
persistentvolumeclaims.go
367 lines (312 loc) · 9.22 KB
/
persistentvolumeclaims.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"context"
"fmt"
"log"
"net/http"
"sort"
"time"
kf_v1 "github.com/StatCan/kubeflow-apis/apis/kubeflow/v1"
"github.com/gorilla/mux"
"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
type pvcresponse struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Status pvcStatus `json:"status"`
Age time.Time `json:"age"`
Capacity resource.Quantity `json:"capacity"`
Modes []corev1.PersistentVolumeAccessMode `json:"modes"`
Class string `json:"class"`
Notebooks []string `json:"notebooks"`
Labels map[string]string `json:"labels"`
}
type pvcsresponse struct {
APIResponseBase
PersistentVolumeClaims []pvcresponse `json:"pvcs"`
}
type getpvcresponse struct {
APIResponseBase
Pvc corev1.PersistentVolumeClaim `json:"pvc"`
Notebooks []string `json:"notebooks"`
}
type pvcpodsresponse struct {
APIResponseBase
Pods []corev1.Pod `json:"pods"`
}
type pvceventsresponse struct {
APIResponseBase
Events []corev1.Event `json:"events"`
}
// pvcPhase is the phase of a PVC
type pvcPhase string
// status represents the status of a PVC.
type pvcStatus struct {
Message string `json:"message"`
Phase pvcPhase `json:"phase"`
State string `json:"state"`
Key string `json:"key"`
EventMessage string `json:"eventMessage"`
}
const (
PvcPhaseReady pvcPhase = "ready"
PvcPhaseWaiting pvcPhase = "waiting"
PvcPhaseWarning pvcPhase = "warning"
PvcPhaseError pvcPhase = "error"
PvcPhaseUninitialized pvcPhase = "uninitialized"
PvcPhaseUnavailable pvcPhase = "unavailable"
PvcPhaseTerminating pvcPhase = "terminating"
PvcPhaseStopped pvcPhase = "stopped"
)
// Set the status of the pvc
// https://github.com/kubeflow/kubeflow/blob/v1.7.0/components/crud-web-apps/volumes/backend/apps/common/status.py#L4
func GetPvcStatus(pvc *corev1.PersistentVolumeClaim, allevents []*corev1.Event) pvcStatus {
// If pvc is being deleted
if pvc.DeletionTimestamp != nil {
return pvcStatus{
Message: "Deleting Volume...",
Phase: PvcPhaseTerminating,
Key: "pvcDeleting",
}
}
if pvc.Status.Phase == "Bound" {
return pvcStatus{
Message: "Bound",
Phase: PvcPhaseReady,
Key: "pvcBound",
}
}
// The PVC is in Pending state, we check the Events to find out why
evs := make([]*corev1.Event, 0)
for _, event := range allevents {
if event.InvolvedObject.Kind != "PersistentVolumeClaim" || event.InvolvedObject.Name != pvc.Name {
continue
}
evs = append(evs, event)
}
sort.Sort(eventsByTimestamp(evs))
// If there are no events, then the PVC was just created
if len(evs) == 0 {
return pvcStatus{
Message: "Provisioning Volume...",
Phase: PvcPhaseWaiting,
Key: "pvcProvisioning",
}
}
msg := fmt.Sprintf("Pending: %s", evs[0].Message)
key := "pvcPending"
eventMsg := evs[0].Message
state := evs[0].Reason
var phase pvcPhase
if evs[0].Reason == "WaitForFirstConsumer" {
phase = PvcPhaseUnavailable
msg = "Pending: This volume will be bound when its first consumer" +
" is created. E.g., when you first browse its contents, or" +
" attach it to a notebook server"
key = "pvcPendingUnavailable"
} else if evs[0].Reason == "Provisioning" {
phase = PvcPhaseWaiting
} else if evs[0].Reason == "FailedBinding" {
phase = PvcPhaseWarning
} else if evs[0].Type == "Warning" {
phase = PvcPhaseWarning
} else if evs[0].Type == "Normal" {
phase = PvcPhaseReady
}
return pvcStatus{
Message: msg,
Phase: phase,
State: state,
Key: key,
EventMessage: eventMsg,
}
}
func GetNotebookPvcs(nb *kf_v1.Notebook) []string {
pvcs := make([]string, 0)
if len(nb.Spec.Template.Spec.Volumes) == 0 {
return pvcs
}
vols := nb.Spec.Template.Spec.Volumes
for _, vol := range vols {
if vol.PersistentVolumeClaim == nil {
continue
}
pvcs = append(pvcs, vol.PersistentVolumeClaim.ClaimName)
}
return pvcs
}
func GetNotebooksUsingPvc(pvc string, notebooks []*kf_v1.Notebook) []string {
//Return a list of Notebooks that are using the given PVC.
mountedNotebooks := make([]string, 0)
for _, nb := range notebooks {
pvcs := GetNotebookPvcs(nb)
if slices.Contains(pvcs, pvc) {
mountedNotebooks = append(mountedNotebooks, nb.Name)
}
}
return mountedNotebooks
}
// GetPersistentVolumeClaims returns the PVCs in the requested namespace.
func (s *server) GetPersistentVolumeClaims(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
namespace := vars["namespace"]
log.Printf("loading persistent volume claims for %q", namespace)
pvcs, err := s.listers.persistentVolumeClaims.PersistentVolumeClaims(namespace).List(labels.Everything())
if err != nil {
s.error(w, r, err)
return
}
sort.Sort(persistentVolumeClaimsByName(pvcs))
notebooks, err := s.listers.notebooks.Notebooks(namespace).List(labels.Everything())
if err != nil {
s.error(w, r, err)
return
}
sort.Sort(notebooksByName(notebooks))
resp := pvcsresponse{
APIResponseBase: APIResponseBase{
Success: true,
Status: http.StatusOK,
},
PersistentVolumeClaims: make([]pvcresponse, 0),
}
for _, pvc := range pvcs {
size := pvc.Status.Capacity.Storage()
if size == nil {
size = pvc.Spec.Resources.Requests.Storage()
}
allevents, err := s.listers.events.Events(pvc.Namespace).List(labels.Everything())
if err != nil {
log.Printf("failed to load events for %s/%s: %v", pvc.Namespace, pvc.Name, err)
}
status := GetPvcStatus(pvc, allevents)
notebooksList := GetNotebooksUsingPvc(pvc.Name, notebooks)
resp.PersistentVolumeClaims = append(resp.PersistentVolumeClaims, pvcresponse{
Name: pvc.Name,
Namespace: pvc.Namespace,
Status: status,
Age: pvc.CreationTimestamp.Time,
Capacity: *size,
Modes: pvc.Spec.AccessModes,
Class: *pvc.Spec.StorageClassName,
Notebooks: notebooksList,
Labels: pvc.Labels,
})
}
s.respond(w, r, &resp)
}
// TODO: Delete pvc
func (s *server) DeletePvc(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
namespace := vars["namespace"]
pvc := vars["pvc"]
log.Printf("deleting pvc %q for %q", pvc, namespace)
propagation := v1.DeletePropagationForeground
err := s.clientsets.kubernetes.CoreV1().PersistentVolumeClaims(namespace).Delete(r.Context(), pvc, v1.DeleteOptions{
PropagationPolicy: &propagation,
})
if err != nil {
s.error(w, r, err)
return
}
s.respond(w, r, &APIResponseBase{
Success: true,
Status: http.StatusOK,
})
}
func (s *server) GetPvc(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
namespace := vars["namespace"]
pvc := vars["pvc"]
log.Printf("getting pvc %q for %q", pvc, namespace)
vol, err := s.listers.persistentVolumeClaims.PersistentVolumeClaims(namespace).Get(pvc)
if err != nil {
s.error(w, r, err)
return
}
notebooks, err := s.listers.notebooks.Notebooks(namespace).List(labels.Everything())
if err != nil {
s.error(w, r, err)
return
}
notebooksList := GetNotebooksUsingPvc(vol.Name, notebooks)
resp := &getpvcresponse{
APIResponseBase: APIResponseBase{
Success: true,
Status: http.StatusOK,
},
Pvc: *vol,
Notebooks: notebooksList,
}
s.respond(w, r, resp)
}
func getPodPvcs(pod corev1.Pod) []string {
/*
Return a list of PVC name that the given Pod
is using. If it doesn't use any, then an empty list will
be returned.
*/
pvcs := make([]string, 0)
if len(pod.Spec.Volumes) == 0 {
return pvcs
}
vols := pod.Spec.Volumes
for _, vol := range vols {
//Check if the volume is a pvc
if vol.PersistentVolumeClaim != nil {
pvcs = append(pvcs, vol.PersistentVolumeClaim.ClaimName)
}
}
return pvcs
}
func (s *server) GetPvcPods(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
namespace := vars["namespace"]
pvc := vars["pvc"]
log.Printf("getting pods with pvc %q for %q", pvc, namespace)
allpods, err := s.listers.pods.Pods(namespace).List(labels.Everything())
if err != nil {
s.error(w, r, err)
return
}
mountedPods := make([]corev1.Pod, 0)
for _, pod := range allpods {
pvcs := getPodPvcs(*pod)
if slices.Contains(pvcs, pvc) {
mountedPods = append(mountedPods, *pod)
}
}
resp := &pvcpodsresponse{
APIResponseBase: APIResponseBase{
Success: true,
Status: http.StatusOK,
},
Pods: mountedPods,
}
s.respond(w, r, resp)
}
func (s *server) GetPvcEvents(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
namespace := vars["namespace"]
pvc := vars["pvc"]
log.Printf("getting events in %q in %q", pvc, namespace)
eventOpts := v1.ListOptions{
FieldSelector: "involvedObject.kind=PersistentVolumeClaim,involvedObject.name=" + pvc,
}
events, err := s.clientsets.kubernetes.CoreV1().Events(namespace).List(context.TODO(), eventOpts)
if err != nil {
log.Printf("failed to load events for %s/%s: %v", namespace, pvc, err)
}
resp := &pvceventsresponse{
APIResponseBase: APIResponseBase{
Success: true,
Status: http.StatusOK,
},
Events: events.Items,
}
s.respond(w, r, resp)
}