-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvc-handler.go
97 lines (85 loc) · 2.89 KB
/
pvc-handler.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
package main
import (
"context"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
func PvcReady(pvcs *apiv1.PersistentVolumeClaim,
pvcName string,
tmpString string,
labelName string,
cap string) {
storageclassName := STORAGECLASS
// assemble pvc name
pvcName = pvcName + "-pvc-" + tmpString
// assemble resource limit
resourceLimit := make(map[apiv1.ResourceName]resource.Quantity)
totalClaimedQuant := resource.MustParse(cap)
resourceLimit[apiv1.ResourceStorage] = totalClaimedQuant
*pvcs = apiv1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: pvcName,
Labels: map[string]string{labelName: labelName},
Annotations: nil,
},
Spec: apiv1.PersistentVolumeClaimSpec{
AccessModes: []apiv1.PersistentVolumeAccessMode{apiv1.ReadWriteMany},
/*Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{labelName: labelName}, // used to binding with pv, if pv create manual
},*/
Resources: apiv1.ResourceRequirements{
Requests: resourceLimit,
},
VolumeName: "", // binding to which PV
StorageClassName: &storageclassName,
VolumeMode: nil, // by default is the raw block
},
}
}
func Create_pvc(pvcClient v1.PersistentVolumeClaimInterface,
pvcName string,
tmpString string,
labelName string,
caps string) {
var pvcs apiv1.PersistentVolumeClaim
PvcReady(&pvcs, pvcName, tmpString, labelName, caps)
resultPVC, err := pvcClient.Create(context.TODO(), &pvcs, metav1.CreateOptions{})
if err != nil {
Error.Println("create the pvc err : ", err)
}
Trace.Printf("created %s\n", resultPVC.GetObjectMeta().GetName())
}
func List_pvc(pvcClient v1.PersistentVolumeClaimInterface, labelName string) {
Trace.Println("list persistentvolumeclaim...")
list, err := pvcClient.List(context.TODO(), metav1.ListOptions{LabelSelector: labelName})
if err != nil {
Error.Println("list pvc err: ", err)
}
for _, s := range list.Items {
Trace.Printf(" * [%s] pvc in [%s] with [%v] label\n", s.Name, s.Namespace, s.Labels)
}
}
func Delete_pvc(pvcClient v1.PersistentVolumeClaimInterface, pvcName string, labelName string, gracePeriodSeconds *int64) {
deletePolicy := metav1.DeletePropagationForeground
if pvcName != "" {
if err := pvcClient.Delete(context.TODO(), pvcName, metav1.DeleteOptions{
GracePeriodSeconds: gracePeriodSeconds,
PropagationPolicy: &deletePolicy,
}); err != nil {
Error.Println("delete pvc err:", err)
}
Trace.Printf("deleted pvc %s\n", pvcName)
} else {
if err := pvcClient.DeleteCollection(context.TODO(), metav1.DeleteOptions{
GracePeriodSeconds: gracePeriodSeconds,
PropagationPolicy: &deletePolicy,
}, metav1.ListOptions{
LabelSelector: labelName,
}); err != nil {
Error.Println("delete pvcs err:", err)
}
Trace.Printf("delete all pvcs under label: %s\n", labelName)
}
}