forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
volumes.go
552 lines (484 loc) · 15.8 KB
/
volumes.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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.
*/
/*
* This test checks that various VolumeSources are working. For each volume
* type it creates a server pod, exporting simple 'index.html' file.
* Then it uses appropriate VolumeSource to import this file into a client pod
* and checks that the pod can see the file. It does so by importing the file
* into web server root and loadind the index.html from it.
*
* These tests work only when privileged containers are allowed, exporting
* various filesystems (NFS, GlusterFS, ...) usually needs some mounting or
* other privileged magic in the server pod.
*
* Note that the server containers are for testing purposes only and should not
* be used in production.
*/
package e2e
import (
"fmt"
"time"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
client "k8s.io/kubernetes/pkg/client/unversioned"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
// Configuration of one tests. The test consist of:
// - server pod - runs serverImage, exports ports[]
// - client pod - does not need any special configuration
type VolumeTestConfig struct {
namespace string
// Prefix of all pods. Typically the test name.
prefix string
// Name of container image for the server pod.
serverImage string
// Ports to export from the server pod. TCP only.
serverPorts []int
// Volumes needed to be mounted to the server container from the host
// map <host (source) path> -> <container (dst.) path>
volumes map[string]string
}
// Starts a container specified by config.serverImage and exports all
// config.serverPorts from it. The returned pod should be used to get the server
// IP address and create appropriate VolumeSource.
func startVolumeServer(client *client.Client, config VolumeTestConfig) *api.Pod {
podClient := client.Pods(config.namespace)
portCount := len(config.serverPorts)
serverPodPorts := make([]api.ContainerPort, portCount)
for i := 0; i < portCount; i++ {
portName := fmt.Sprintf("%s-%d", config.prefix, i)
serverPodPorts[i] = api.ContainerPort{
Name: portName,
ContainerPort: config.serverPorts[i],
Protocol: api.ProtocolTCP,
}
}
volumeCount := len(config.volumes)
volumes := make([]api.Volume, volumeCount)
mounts := make([]api.VolumeMount, volumeCount)
i := 0
for src, dst := range config.volumes {
mountName := fmt.Sprintf("path%d", i)
volumes[i].Name = mountName
volumes[i].VolumeSource.HostPath = &api.HostPathVolumeSource{
Path: src,
}
mounts[i].Name = mountName
mounts[i].ReadOnly = false
mounts[i].MountPath = dst
i++
}
By(fmt.Sprint("creating ", config.prefix, " server pod"))
privileged := new(bool)
*privileged = true
serverPod := &api.Pod{
TypeMeta: unversioned.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: api.ObjectMeta{
Name: config.prefix + "-server",
Labels: map[string]string{
"role": config.prefix + "-server",
},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: config.prefix + "-server",
Image: config.serverImage,
SecurityContext: &api.SecurityContext{
Privileged: privileged,
},
Ports: serverPodPorts,
VolumeMounts: mounts,
},
},
Volumes: volumes,
},
}
_, err := podClient.Create(serverPod)
expectNoError(err, "Failed to create %s pod: %v", serverPod.Name, err)
expectNoError(waitForPodRunningInNamespace(client, serverPod.Name, config.namespace))
By("locating the server pod")
pod, err := podClient.Get(serverPod.Name)
expectNoError(err, "Cannot locate the server pod %v: %v", serverPod.Name, err)
By("sleeping a bit to give the server time to start")
time.Sleep(20 * time.Second)
return pod
}
// Clean both server and client pods.
func volumeTestCleanup(client *client.Client, config VolumeTestConfig) {
By(fmt.Sprint("cleaning the environment after ", config.prefix))
defer GinkgoRecover()
podClient := client.Pods(config.namespace)
// ignore all errors, the pods may not be even created
podClient.Delete(config.prefix+"-client", nil)
podClient.Delete(config.prefix+"-server", nil)
}
// Start a client pod using given VolumeSource (exported by startVolumeServer())
// and check that the pod sees the data from the server pod.
func testVolumeClient(client *client.Client, config VolumeTestConfig, volume api.VolumeSource, expectedContent string) {
By(fmt.Sprint("starting ", config.prefix, " client"))
podClient := client.Pods(config.namespace)
clientPod := &api.Pod{
TypeMeta: unversioned.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: api.ObjectMeta{
Name: config.prefix + "-client",
Labels: map[string]string{
"role": config.prefix + "-client",
},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: config.prefix + "-client",
Image: "gcr.io/google_containers/nginx:1.7.9",
Ports: []api.ContainerPort{
{
Name: "web",
ContainerPort: 80,
Protocol: api.ProtocolTCP,
},
},
VolumeMounts: []api.VolumeMount{
{
Name: config.prefix + "-volume",
MountPath: "/usr/share/nginx/html",
},
},
},
},
Volumes: []api.Volume{
{
Name: config.prefix + "-volume",
VolumeSource: volume,
},
},
},
}
if _, err := podClient.Create(clientPod); err != nil {
Failf("Failed to create %s pod: %v", clientPod.Name, err)
}
expectNoError(waitForPodRunningInNamespace(client, clientPod.Name, config.namespace))
By("reading a web page from the client")
body, err := client.Get().
Namespace(config.namespace).
Prefix("proxy").
Resource("pods").
Name(clientPod.Name).
DoRaw()
expectNoError(err, "Cannot read web page: %v", err)
Logf("body: %v", string(body))
By("checking the page content")
Expect(body).To(ContainSubstring(expectedContent))
}
var _ = Describe("Volumes", func() {
clean := true // If 'false', the test won't clear its namespace (and pods and services) upon completion. Useful for debugging.
// filled in BeforeEach
var c *client.Client
var namespace *api.Namespace
BeforeEach(func() {
var err error
c, err = loadClient()
Expect(err).NotTo(HaveOccurred())
By("Building a namespace api object")
namespace, err = createTestingNS("volume", c)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
if clean {
if err := deleteNS(c, namespace.Name, 5*time.Minute /* namespace deletion timeout */); err != nil {
Failf("Couldn't delete ns %s", err)
}
}
})
////////////////////////////////////////////////////////////////////////
// NFS
////////////////////////////////////////////////////////////////////////
// Marked with [Skipped] to skip the test by default (see driver.go),
// the test needs privileged containers, which are disabled by default.
// Run the test with "go run hack/e2e.go ... --ginkgo.focus=Volume"
Describe("[Skipped] NFS", func() {
It("should be mountable", func() {
config := VolumeTestConfig{
namespace: namespace.Name,
prefix: "nfs",
serverImage: "gcr.io/google_containers/volume-nfs",
serverPorts: []int{2049},
}
defer func() {
if clean {
volumeTestCleanup(c, config)
}
}()
pod := startVolumeServer(c, config)
serverIP := pod.Status.PodIP
Logf("NFS server IP address: %v", serverIP)
volume := api.VolumeSource{
NFS: &api.NFSVolumeSource{
Server: serverIP,
Path: "/",
ReadOnly: true,
},
}
// Must match content of test/images/volumes-tester/nfs/index.html
testVolumeClient(c, config, volume, "Hello from NFS!")
})
})
////////////////////////////////////////////////////////////////////////
// Gluster
////////////////////////////////////////////////////////////////////////
// Marked with [Skipped] to skip the test by default (see driver.go),
// the test needs privileged containers, which are disabled by default.
// Run the test with "go run hack/e2e.go ... --ginkgo.focus=Volume"
Describe("[Skipped] GlusterFS", func() {
It("should be mountable", func() {
config := VolumeTestConfig{
namespace: namespace.Name,
prefix: "gluster",
serverImage: "gcr.io/google_containers/volume-gluster",
serverPorts: []int{24007, 24008, 49152},
}
defer func() {
if clean {
volumeTestCleanup(c, config)
}
}()
pod := startVolumeServer(c, config)
serverIP := pod.Status.PodIP
Logf("Gluster server IP address: %v", serverIP)
// create Endpoints for the server
endpoints := api.Endpoints{
TypeMeta: unversioned.TypeMeta{
Kind: "Endpoints",
APIVersion: "v1",
},
ObjectMeta: api.ObjectMeta{
Name: config.prefix + "-server",
},
Subsets: []api.EndpointSubset{
{
Addresses: []api.EndpointAddress{
{
IP: serverIP,
},
},
Ports: []api.EndpointPort{
{
Name: "gluster",
Port: 24007,
Protocol: api.ProtocolTCP,
},
},
},
},
}
endClient := c.Endpoints(config.namespace)
defer func() {
if clean {
endClient.Delete(config.prefix + "-server")
}
}()
if _, err := endClient.Create(&endpoints); err != nil {
Failf("Failed to create endpoints for Gluster server: %v", err)
}
volume := api.VolumeSource{
Glusterfs: &api.GlusterfsVolumeSource{
EndpointsName: config.prefix + "-server",
// 'test_vol' comes from test/images/volumes-tester/gluster/run_gluster.sh
Path: "test_vol",
ReadOnly: true,
},
}
// Must match content of test/images/volumes-tester/gluster/index.html
testVolumeClient(c, config, volume, "Hello from GlusterFS!")
})
})
////////////////////////////////////////////////////////////////////////
// iSCSI
////////////////////////////////////////////////////////////////////////
// Marked with [Skipped] to skip the test by default (see driver.go),
// the test needs privileged containers, which are disabled by default.
// Also, make sure that iscsiadm utility and iscsi target kernel modules
// are installed on all nodes!
// Run the test with "go run hack/e2e.go ... --ginkgo.focus=iSCSI"
Describe("[Skipped] iSCSI", func() {
It("should be mountable", func() {
config := VolumeTestConfig{
namespace: namespace.Name,
prefix: "iscsi",
serverImage: "gcr.io/google_containers/volume-iscsi",
serverPorts: []int{3260},
volumes: map[string]string{
// iSCSI container needs to insert modules from the host
"/lib/modules": "/lib/modules",
},
}
defer func() {
if clean {
volumeTestCleanup(c, config)
}
}()
pod := startVolumeServer(c, config)
serverIP := pod.Status.PodIP
Logf("iSCSI server IP address: %v", serverIP)
volume := api.VolumeSource{
ISCSI: &api.ISCSIVolumeSource{
TargetPortal: serverIP + ":3260",
// from test/images/volumes-tester/iscsi/initiatorname.iscsi
IQN: "iqn.2003-01.org.linux-iscsi.f21.x8664:sn.4b0aae584f7c",
Lun: 0,
FSType: "ext2",
ReadOnly: true,
},
}
// Must match content of test/images/volumes-tester/iscsi/block.tar.gz
testVolumeClient(c, config, volume, "Hello from iSCSI")
})
})
////////////////////////////////////////////////////////////////////////
// Ceph RBD
////////////////////////////////////////////////////////////////////////
// Marked with [Skipped] to skip the test by default (see driver.go),
// the test needs privileged containers, which are disabled by default.
// Run the test with "go run hack/e2e.go ... --ginkgo.focus=RBD"
// Run the test with "go run hack/e2e.go ... --ginkgo.focus=Volume"
Describe("[Skipped] Ceph RBD", func() {
It("should be mountable", func() {
config := VolumeTestConfig{
namespace: namespace.Name,
prefix: "rbd",
serverImage: "gcr.io/google_containers/volume-rbd",
serverPorts: []int{6789},
volumes: map[string]string{
// iSCSI container needs to insert modules from the host
"/lib/modules": "/lib/modules",
"/sys": "/sys",
},
}
defer func() {
if clean {
volumeTestCleanup(c, config)
}
}()
pod := startVolumeServer(c, config)
serverIP := pod.Status.PodIP
Logf("Ceph server IP address: %v", serverIP)
// create secrets for the server
secret := api.Secret{
TypeMeta: unversioned.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: api.ObjectMeta{
Name: config.prefix + "-secret",
},
Data: map[string][]byte{
// from test/images/volumes-tester/rbd/keyring
"key": []byte("AQDRrKNVbEevChAAEmRC+pW/KBVHxa0w/POILA=="),
},
}
secClient := c.Secrets(config.namespace)
defer func() {
if clean {
secClient.Delete(config.prefix + "-secret")
}
}()
if _, err := secClient.Create(&secret); err != nil {
Failf("Failed to create secrets for Ceph RBD: %v", err)
}
volume := api.VolumeSource{
RBD: &api.RBDVolumeSource{
CephMonitors: []string{serverIP},
RBDPool: "rbd",
RBDImage: "foo",
RadosUser: "admin",
SecretRef: &api.LocalObjectReference{
Name: config.prefix + "-secret",
},
FSType: "ext2",
ReadOnly: true,
},
}
// Must match content of test/images/volumes-tester/gluster/index.html
testVolumeClient(c, config, volume, "Hello from RBD")
})
})
////////////////////////////////////////////////////////////////////////
// Ceph
////////////////////////////////////////////////////////////////////////
// Marked with [Skipped] to skip the test by default (see driver.go),
// the test needs privileged containers, which are disabled by default.
// Run the test with "go run hack/e2e.go ... --ginkgo.focus=Volume"
Describe("[Skipped] CephFS", func() {
It("should be mountable", func() {
config := VolumeTestConfig{
namespace: namespace.Name,
prefix: "cephfs",
serverImage: "gcr.io/google_containers/volume-ceph",
serverPorts: []int{6789},
}
defer func() {
if clean {
volumeTestCleanup(c, config)
}
}()
pod := startVolumeServer(c, config)
serverIP := pod.Status.PodIP
Logf("Ceph server IP address: %v", serverIP)
By("sleeping a bit to give ceph server time to initialize")
time.Sleep(20 * time.Second)
// create ceph secret
secret := &api.Secret{
TypeMeta: unversioned.TypeMeta{
Kind: "Secret",
APIVersion: "v1beta3",
},
ObjectMeta: api.ObjectMeta{
Name: config.prefix + "-secret",
},
// Must use the ceph keyring at contrib/for-tests/volumes-ceph/ceph/init.sh
// and encode in base64
Data: map[string][]byte{
"key": []byte("AQAMgXhVwBCeDhAA9nlPaFyfUSatGD4drFWDvQ=="),
},
}
defer func() {
if clean {
if err := c.Secrets(namespace.Name).Delete(secret.Name); err != nil {
Failf("unable to delete secret %v: %v", secret.Name, err)
}
}
}()
var err error
if secret, err = c.Secrets(namespace.Name).Create(secret); err != nil {
Failf("unable to create test secret %s: %v", secret.Name, err)
}
volume := api.VolumeSource{
CephFS: &api.CephFSVolumeSource{
Monitors: []string{serverIP + ":6789"},
User: "kube",
SecretRef: &api.LocalObjectReference{Name: config.prefix + "-secret"},
ReadOnly: true,
},
}
// Must match content of contrib/for-tests/volumes-ceph/ceph/index.html
testVolumeClient(c, config, volume, "Hello Ceph!")
})
})
})