-
Notifications
You must be signed in to change notification settings - Fork 4
/
storageclasses.go
101 lines (83 loc) · 2.23 KB
/
storageclasses.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
package main
import (
"log"
"net/http"
"strconv"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/labels"
)
type storageclassesresponse struct {
APIResponseBase
StorageClasses []string `json:"storageClasses"`
}
type defaultstorageclassresponse struct {
APIResponseBase
DefaultStorageClass string `json:"defaultStorageClass"`
}
// GetStorageClasses returns the list of storage classes for the cluster
func (s *server) GetStorageClasses(w http.ResponseWriter, r *http.Request) {
scs, err := s.listers.storageClasses.List(labels.Everything())
if err != nil {
s.error(w, r, err)
return
}
var storageclasses []string
// Identify the default storage class based on the annotations
for _, sc := range scs {
storageclasses = append(storageclasses, sc.Name)
}
log.Printf("found storage classes")
resp := &storageclassesresponse{
APIResponseBase: APIResponseBase{
Success: true,
Status: http.StatusOK,
},
StorageClasses: storageclasses,
}
s.respond(w, r, resp)
}
// GetDefaultStorageClass returns the default storage class for the cluster.
func (s *server) GetDefaultStorageClass(w http.ResponseWriter, r *http.Request) {
scs, err := s.listers.storageClasses.List(labels.Everything())
if err != nil {
s.error(w, r, err)
return
}
var defaultsc *storagev1.StorageClass
// Identify the default storage class based on the annotations
for _, sc := range scs {
if val, ok := sc.Annotations["storageclass.kubernetes.io/is-default-class"]; ok {
bval, err := strconv.ParseBool(val)
if err != nil {
log.Printf("failed to parse annotation value: %s - %v", sc.Name, err)
continue
}
if bval {
defaultsc = sc
break
}
}
if val, ok := sc.Annotations["storageclass.beta.kubernetes.io/is-default-class"]; ok {
bval, err := strconv.ParseBool(val)
if err != nil {
log.Printf("failed to parse annotation value: %s - %v", sc.Name, err)
continue
}
if bval {
defaultsc = sc
break
}
}
}
resp := &defaultstorageclassresponse{
APIResponseBase: APIResponseBase{
Success: true,
Status: http.StatusOK,
},
}
if defaultsc != nil {
log.Printf("found default storageclass %q", defaultsc.Name)
resp.DefaultStorageClass = defaultsc.Name
}
s.respond(w, r, resp)
}