/
list.go
54 lines (47 loc) · 1.66 KB
/
list.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
package persistentvolume
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
)
// List list all persistentvolumes in the k8s cluster, it simply call `ListAll`.
func (h *Handler) List() ([]*corev1.PersistentVolume, error) {
return h.ListAll()
}
// ListByLabel list persistentvolumes by labels.
// Multiple labels separated by comma(",") eg: "name=myapp,role=devops",
// and there is an "And" relationship between multiple labels.
func (h *Handler) ListByLabel(labels string) ([]*corev1.PersistentVolume, error) {
listOptions := h.Options.ListOptions.DeepCopy()
listOptions.LabelSelector = labels
pvList, err := h.clientset.CoreV1().PersistentVolumes().List(h.ctx, *listOptions)
if err != nil {
return nil, err
}
return extractList(pvList), nil
}
// ListByField list persistentvolumes by field, work like `kubectl get xxx --field-selector=xxx`.
func (h *Handler) ListByField(field string) ([]*corev1.PersistentVolume, error) {
fieldSelector, err := fields.ParseSelector(field)
if err != nil {
return nil, err
}
listOptions := h.Options.ListOptions.DeepCopy()
listOptions.FieldSelector = fieldSelector.String()
pvList, err := h.clientset.CoreV1().PersistentVolumes().List(h.ctx, *listOptions)
if err != nil {
return nil, err
}
return extractList(pvList), nil
}
// ListAll list all persistentvolumes in the k8s cluster.
func (h *Handler) ListAll() ([]*corev1.PersistentVolume, error) {
return h.ListByLabel("")
}
// extractList
func extractList(pvList *corev1.PersistentVolumeList) []*corev1.PersistentVolume {
var objList []*corev1.PersistentVolume
for i := range pvList.Items {
objList = append(objList, &pvList.Items[i])
}
return objList
}