-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathunionpv.go
161 lines (142 loc) · 4.52 KB
/
unionpv.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
package beku
import (
"errors"
"reflect"
"k8s.io/api/core/v1"
)
// UnionPV output pvc and pv
type UnionPV struct {
pv *PersistentVolume
pvc *PersistentVolumeClaim
err error
}
// NewUnionPV create PersistentVolume,PersistentVolumeClaim and error
// and chain function call begin with this function.
func NewUnionPV() *UnionPV { return &UnionPV{pv: NewPV(), pvc: NewPVC()} }
// Finish Chain function call end with this function
// return Kubernetes resource object(PersistentVolume,PersistentVolumeClaim) and error
// In the function, it will check necessary parametersainput the default field
func (un *UnionPV) Finish() (pv *v1.PersistentVolume, pvc *v1.PersistentVolumeClaim, err error) {
un.verify()
if un.err != nil {
err = un.err
return
}
pv, err = un.pv.Finish()
if err != nil {
return
}
pvc, err = un.pvc.Finish()
return
}
// SetName set PersistentVolume and PersistentVolumeClaim name
func (un *UnionPV) SetName(name string) *UnionPV {
un.pv.SetName(name)
un.pvc.SetName(name)
return un
}
// SetNamespace set PersistentVolumeClaim namespace,
// and PersistentVolume can't set namespece because of no such attribute
func (un *UnionPV) SetNamespace(namespace string) *UnionPV {
un.pvc.SetNamespace(namespace)
return un
}
// SetNamespaceAndName set PersistentVolumeClaim namespace and name, set PersistentVolumeClaim name
// PersistentVolume can't set namespece because of no such attribute
func (un *UnionPV) SetNamespaceAndName(namespace, name string) *UnionPV {
un.pv.SetName(name)
un.pvc.SetNamespaceAndName(namespace, name)
return un
}
// SetAccessMode set PersistentVolume and PersistentVolumeClaim accessmode
func (un *UnionPV) SetAccessMode(mode PersistentVolumeAccessMode) *UnionPV {
un.pvc.SetAccessMode(mode)
un.pv.SetAccessMode(mode)
return un
}
// SetAccessModes PersistentVolume and PersistentVolumeClaim access modes
func (un *UnionPV) SetAccessModes(modes []PersistentVolumeAccessMode) *UnionPV {
un.pvc.SetAccessModes(modes)
un.pv.SetAccessModes(modes)
return un
}
// SetCapacity set PersistentVolume capacity and set PersistentVolumeClaim resource request
func (un *UnionPV) SetCapacity(capMaps map[ResourceName]string) *UnionPV {
un.pv.SetCapacity(capMaps)
un.pvc.SetResourceRequests(capMaps)
return un
}
// SetAnnotations get PersistentVolume and PersistentVolumeClaim annotations
func (un *UnionPV) SetAnnotations(annotations map[string]string) *UnionPV {
un.pv.SetAnnotations(annotations)
un.pvc.SetAnnotations(annotations)
return un
}
// SetVolumeMode set pvc volume mode Filesystem or Block
func (un *UnionPV) SetVolumeMode(volumeMode PersistentVolumeMode) *UnionPV {
un.pvc.SetVolumeMode(volumeMode)
return un
}
// SetLabels set PersistentVolume labels ,set PersistentVolumeClaim labels and set PersistentVolumeClaim selector
func (un *UnionPV) SetLabels(labels map[string]string) *UnionPV {
un.pv.SetLabels(labels)
un.pvc.SetLabels(labels)
un.pvc.SetSelector(labels)
return un
}
// SetNFS set PersistentVolume volume source is NFS
func (un *UnionPV) SetNFS(nfs *NFSVolumeSource) *UnionPV {
un.pv.SetNFS(nfs)
return un
}
// SetRBD set PersistentVolume volume source is RBD
func (un *UnionPV) SetRBD(rbd *RBDPersistentVolumeSource) *UnionPV {
un.pv.SetRBD(rbd)
return un
}
// Release release UnionPV on Kubernetes
func (un *UnionPV) Release() (pv *v1.PersistentVolume, pvc *v1.PersistentVolumeClaim, err error) {
pv, pvc, err = un.Finish()
if err != nil {
return
}
client, err := GetKubeClient()
if err != nil {
return
}
pv, err = client.CoreV1().PersistentVolumes().Create(pv)
if err != nil {
return
}
pvc, err = client.CoreV1().PersistentVolumeClaims(pvc.GetNamespace()).Create(pvc)
return
}
// verify check UnionPV necessary value, input the default field and input related data.
func (un *UnionPV) verify() {
if un.err != nil {
return
}
pvname, pvcname := un.pv.GetName(), un.pvc.GetName()
if !verifyString(pvname) || !verifyString(pvcname) {
un.err = errors.New("pvc or pv name is empty not allow")
return
}
//check labels and selector
pvlabels, pvclabels, pvcselector := un.pv.GetLabels(), un.pvc.GetLabels(), un.pvc.GetSelector()
if !reflect.DeepEqual(pvcselector, pvlabels) {
un.err = errors.New("UnionPV, it is not allow to pvc selector and pv labels not equal")
return
}
if !verifyString(un.pvc.GetNamespace()) {
un.SetNamespace("default")
}
if pvlabels == nil {
pvlabels = map[string]string{"name": pvname}
un.pv.SetLabels(pvlabels)
un.pvc.SetSelector(pvlabels)
}
if pvclabels == nil {
pvclabels = map[string]string{"name": pvcname}
un.pvc.SetLabels(pvclabels)
}
}