/
pvc.go
279 lines (254 loc) · 8.53 KB
/
pvc.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
/*
Copyright @ 2021 bocloud <fushaosong@beyondcent.com>.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"encoding/json"
"fmt"
"github.com/carina-io/carina/utils/log"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
const (
xfsPvcName = "csi-carina-pvc1"
ext4PvcName = "csi-carina-pvc3"
baseCapacity = 7
expandCapacity = 14
)
var pvc1 = `
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: csi-carina-pvc1
namespace: carina
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 7Gi
storageClassName: csi-carina-sc1
volumeMode: Filesystem
`
var pvc2 = `
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: csi-carina-pvc2
namespace: carina
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 7Gi
storageClassName: csi-carina-sc2
volumeMode: Filesystem
`
var pvc3 = `
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: csi-carina-pvc3
namespace: carina
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 7Gi
storageClassName: csi-carina-sc3
volumeMode: Filesystem
`
var pvc4 = `
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: csi-carina-pvc4
namespace: carina
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 7Gi
storageClassName: csi-carina-sc4
volumeMode: Filesystem
`
func testCreatePvc() {
It("create pvc with xfs", func() {
pvcName := "csi-carina-pvc1"
stdout, stderr, err := kubectlWithInput([]byte(pvc1), "apply", "-f", "-")
Expect(err).ShouldNot(HaveOccurred(), "stdout=%s, stderr=%s", stdout, stderr)
By("Waiting for pvc pending")
Eventually(func() error {
stdout, stderr, err = kubectl("get", "pvc", pvcName, "-o", "json", "-n", NameSpace)
if err != nil {
return fmt.Errorf("failed to create PVC. stdout: %s, stderr: %s, err: %v", stdout, stderr, err)
}
var pvc corev1.PersistentVolumeClaim
err = json.Unmarshal(stdout, &pvc)
if err != nil {
return fmt.Errorf("unmarshal error: stdout=%s", stdout)
}
if pvc.Status.Phase != corev1.ClaimPending {
return fmt.Errorf("pvc status error: %s, %s", pvcName, pvc.Status.Phase)
}
return nil
}).Should(Succeed())
})
It("create pvc without disk group", func() {
pvcName := "csi-carina-pvc2"
stdout, stderr, err := kubectlWithInput([]byte(pvc2), "apply", "-f", "-")
Expect(err).ShouldNot(HaveOccurred(), "stdout=%s, stderr=%s", stdout, stderr)
By("Waiting for pvc pending")
Eventually(func() error {
stdout, stderr, err = kubectl("get", "pvc", pvcName, "-o", "json", "-n", NameSpace)
if err != nil {
return fmt.Errorf("failed to create PVC. stdout: %s, stderr: %s, err: %v", stdout, stderr, err)
}
var pvc corev1.PersistentVolumeClaim
err = json.Unmarshal(stdout, &pvc)
if err != nil {
return fmt.Errorf("unmarshal error: stdout=%s", stdout)
}
if pvc.Status.Phase != corev1.ClaimPending {
return fmt.Errorf("pvc status error: %s, %s", pvcName, pvc.Status.Phase)
}
return nil
}).Should(Succeed())
})
It("create pvc with ext4", func() {
pvcName := "csi-carina-pvc3"
stdout, stderr, err := kubectlWithInput([]byte(pvc3), "apply", "-f", "-")
Expect(err).ShouldNot(HaveOccurred(), "stdout=%s, stderr=%s", stdout, stderr)
By("Waiting for pvc pending")
Eventually(func() error {
stdout, stderr, err = kubectl("get", "pvc", pvcName, "-o", "json", "-n", NameSpace)
if err != nil {
return fmt.Errorf("failed to create PVC. stdout: %s, stderr: %s, err: %v", stdout, stderr, err)
}
var pvc corev1.PersistentVolumeClaim
err = json.Unmarshal(stdout, &pvc)
if err != nil {
return fmt.Errorf("unmarshal error: stdout=%s", stdout)
}
if pvc.Status.Phase != corev1.ClaimPending {
return fmt.Errorf("pvc status error: %s, %s", pvcName, pvc.Status.Phase)
}
return nil
}).Should(Succeed())
})
It("create pvc with immediate", func() {
pvcName := "csi-carina-pvc4"
stdout, stderr, err := kubectlWithInput([]byte(pvc4), "apply", "-f", "-")
Expect(err).ShouldNot(HaveOccurred(), "stdout=%s, stderr=%s", stdout, stderr)
By("Waiting for pvc ready")
nodeName, diskGroup := "", ""
Eventually(func() error {
stdout, stderr, err = kubectl("get", "pvc", pvcName, "-o", "json", "-n", NameSpace)
if err != nil {
return fmt.Errorf("failed to create PVC. stdout: %s, stderr: %s, err: %v", stdout, stderr, err)
}
var pvc corev1.PersistentVolumeClaim
err = json.Unmarshal(stdout, &pvc)
if err != nil {
return fmt.Errorf("unmarshal error: stdout=%s", stdout)
}
if pvc.Status.Phase != corev1.ClaimBound {
log.Infof("pvc status error: %s, %s", pvcName, pvc.Status.Phase)
return fmt.Errorf("pvc status: %s, %s", pvcName, pvc.Status.Phase)
}
By("get pv info")
stdout, stderr, err = kubectl("get", "pv", pvc.Spec.VolumeName, "-o", "json")
if err != nil {
return fmt.Errorf("failed to get pv. stdout: %s, stderr: %s, err: %v", stdout, stderr, err)
}
var pv corev1.PersistentVolume
err = json.Unmarshal(stdout, &pv)
if err != nil {
return fmt.Errorf("unmarshal error: stdout=%s", stdout)
}
nodeName = pv.Spec.CSI.VolumeAttributes["carina.storage.io/node"]
diskGroup = pv.Spec.CSI.VolumeAttributes["carina.storage.io/disk-group-name"]
log.Info("pv check success")
return nil
}).Should(Succeed())
Eventually(func() error {
By("disk capacity check")
stdout, stderr, err = kubectl("get", "node", nodeName, "-o", "json")
if err != nil {
return fmt.Errorf("failed to get node. stdout: %s, stderr: %s, err: %v", stdout, stderr, err)
}
var node corev1.Node
err = json.Unmarshal(stdout, &node)
if err != nil {
log.Infof("failed to ummarshal node. stdout: %s, stderr: %s, err: %v", stdout, stderr, err)
return fmt.Errorf("failed to ummarshal node. stdout: %s, stderr: %s, err: %v", stdout, stderr, err)
}
capacity := node.Status.Capacity.Name(corev1.ResourceName(fmt.Sprintf("carina.storage.io/%s", diskGroup)), resource.BinarySI).Value()
allocatable := node.Status.Allocatable.Name(corev1.ResourceName(fmt.Sprintf("carina.storage.io/%s", diskGroup)), resource.BinarySI).Value()
if capacity != allocatable+10+7 {
log.Infof("failed to allocatable node. capacity: %d, allocatable: %d", capacity, allocatable)
return fmt.Errorf("failed to allocatable node. capacity: %d, allocatable: %d", capacity, allocatable)
}
log.Infof("success to allocatable node. capacity: %d, allocatable: %d", capacity, allocatable)
return nil
}).Should(Succeed())
})
}
func testDeletePvc() {
It("delete pvc", func() {
pvcName := "csi-carina-pvc1"
stdout, stderr, err := kubectl("delete", "pvc", pvcName, "-n", NameSpace)
Expect(err).ShouldNot(HaveOccurred(), "stdout=%s, stderr=%s", stdout, stderr)
Eventually(func() error {
stdout, stderr, err = kubectl("get", "pvc", pvcName, "-n", NameSpace)
if err != nil {
return err
}
return nil
}).Should(HaveOccurred())
pvcName = "csi-carina-pvc2"
stdout, stderr, err = kubectl("delete", "pvc", pvcName, "-n", NameSpace)
Expect(err).ShouldNot(HaveOccurred(), "stdout=%s, stderr=%s", stdout, stderr)
Eventually(func() error {
stdout, stderr, err = kubectl("get", "pvc", pvcName, "-n", NameSpace)
if err != nil {
return err
}
return nil
}).Should(HaveOccurred())
pvcName = "csi-carina-pvc3"
stdout, stderr, err = kubectl("delete", "pvc", pvcName, "-n", NameSpace)
Expect(err).ShouldNot(HaveOccurred(), "stdout=%s, stderr=%s", stdout, stderr)
Eventually(func() error {
stdout, stderr, err = kubectl("get", "pvc", pvcName, "-n", NameSpace)
if err != nil {
return err
}
return nil
}).Should(HaveOccurred())
pvcName = "csi-carina-pvc4"
stdout, stderr, err = kubectl("delete", "pvc", pvcName, "-n", NameSpace)
Expect(err).ShouldNot(HaveOccurred(), "stdout=%s, stderr=%s", stdout, stderr)
Eventually(func() error {
stdout, stderr, err = kubectl("get", "pvc", pvcName, "-n", NameSpace)
if err != nil {
return err
}
return nil
}).Should(HaveOccurred())
})
}