Skip to content

Commit 6115fc4

Browse files
committed
Use cluster-driver-registrar for CSIDriverRegistry tests
Signed-off-by: Jose A. Rivera <[email protected]>
1 parent 07428f7 commit 6115fc4

File tree

9 files changed

+235
-98
lines changed

9 files changed

+235
-98
lines changed

test/e2e/storage/csi_volumes.go

Lines changed: 61 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import (
2222
"fmt"
2323
"regexp"
2424
"strings"
25+
"time"
2526

2627
"k8s.io/api/core/v1"
2728
storagev1 "k8s.io/api/storage/v1"
2829
"k8s.io/apimachinery/pkg/api/errors"
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3031
"k8s.io/apimachinery/pkg/util/sets"
3132
clientset "k8s.io/client-go/kubernetes"
32-
csiv1alpha1 "k8s.io/csi-api/pkg/apis/csi/v1alpha1"
3333
csiclient "k8s.io/csi-api/pkg/client/clientset/versioned"
3434
"k8s.io/kubernetes/test/e2e/framework"
3535
"k8s.io/kubernetes/test/e2e/framework/podlogs"
@@ -196,57 +196,49 @@ var _ = utils.SIGDescribe("CSI Volumes", func() {
196196
})
197197

198198
// The CSIDriverRegistry feature gate is needed for this test in Kubernetes 1.12.
199-
Context("CSI attach test using HostPath driver [Feature:CSIDriverRegistry]", func() {
199+
200+
Context("CSI attach test using mock driver [Feature:CSIDriverRegistry]", func() {
200201
var (
201202
driver testsuites.TestDriver
202203
)
203204

204-
BeforeEach(func() {
205-
config := testsuites.TestConfig{
206-
Framework: f,
207-
Prefix: "csi-attach",
208-
}
209-
driver = drivers.InitHostPathCSIDriver(config)
210-
driver.CreateDriver()
211-
})
212-
213-
AfterEach(func() {
214-
driver.CleanupDriver()
215-
})
216-
217205
tests := []struct {
218-
name string
219-
driverAttachable bool
220-
driverExists bool
221-
expectVolumeAttachment bool
206+
name string
207+
driverAttachable bool
208+
driverExists bool
222209
}{
223210
{
224-
name: "non-attachable volume does not need VolumeAttachment",
225-
driverAttachable: false,
226-
driverExists: true,
227-
expectVolumeAttachment: false,
211+
name: "should not require VolumeAttach for drivers without attachment",
212+
driverAttachable: false,
213+
driverExists: true,
228214
},
229215
{
230-
name: "attachable volume needs VolumeAttachment",
231-
driverAttachable: true,
232-
driverExists: true,
233-
expectVolumeAttachment: true,
216+
name: "should require VolumeAttach for drivers with attachment",
217+
driverAttachable: true,
218+
driverExists: true,
234219
},
235220
{
236-
name: "volume with no CSI driver needs VolumeAttachment",
237-
driverExists: false,
238-
expectVolumeAttachment: true,
221+
name: "should preserve attachment policy when no CSIDriver present",
222+
driverAttachable: true,
223+
driverExists: false,
239224
},
240225
}
241226

242227
for _, t := range tests {
243228
test := t
244229
It(test.name, func() {
230+
By("Deploying mock CSI driver")
231+
config := testsuites.TestConfig{
232+
Framework: f,
233+
Prefix: "csi-attach",
234+
}
235+
236+
driver = drivers.InitMockCSIDriver(config, test.driverExists, test.driverAttachable, nil)
237+
driver.CreateDriver()
238+
245239
if test.driverExists {
246-
csiDriver := createCSIDriver(csics, testsuites.GetUniqueDriverName(driver), test.driverAttachable, nil)
247-
if csiDriver != nil {
248-
defer csics.CsiV1alpha1().CSIDrivers().Delete(csiDriver.Name, nil)
249-
}
240+
defer destroyCSIDriver(csics, driver)
241+
defer driver.CleanupDriver()
250242
}
251243

252244
By("Creating pod")
@@ -270,7 +262,8 @@ var _ = utils.SIGDescribe("CSI Volumes", func() {
270262
defer cs.StorageV1().StorageClasses().Delete(class.Name, nil)
271263
}
272264
if claim != nil {
273-
defer cs.CoreV1().PersistentVolumeClaims(ns.Name).Delete(claim.Name, nil)
265+
// Fully delete PV before deleting CSI driver
266+
defer deleteVolume(cs, claim)
274267
}
275268
if pod != nil {
276269
// Fully delete (=unmount) the pod before deleting CSI driver
@@ -284,48 +277,34 @@ var _ = utils.SIGDescribe("CSI Volumes", func() {
284277
framework.ExpectNoError(err, "Failed to start pod: %v", err)
285278

286279
By("Checking if VolumeAttachment was created for the pod")
287-
// Check that VolumeAttachment does not exist
288280
handle := getVolumeHandle(cs, claim)
289281
attachmentHash := sha256.Sum256([]byte(fmt.Sprintf("%s%s%s", handle, scTest.Provisioner, nodeName)))
290282
attachmentName := fmt.Sprintf("csi-%x", attachmentHash)
291283
_, err = cs.StorageV1beta1().VolumeAttachments().Get(attachmentName, metav1.GetOptions{})
292284
if err != nil {
293285
if errors.IsNotFound(err) {
294-
if test.expectVolumeAttachment {
286+
if test.driverAttachable {
295287
framework.ExpectNoError(err, "Expected VolumeAttachment but none was found")
296288
}
297289
} else {
298290
framework.ExpectNoError(err, "Failed to find VolumeAttachment")
299291
}
300292
}
301-
if !test.expectVolumeAttachment {
293+
if !test.driverAttachable {
302294
Expect(err).To(HaveOccurred(), "Unexpected VolumeAttachment found")
303295
}
304296
})
305297
}
306298
})
307299

308-
Context("CSI workload information [Feature:CSIDriverRegistry]", func() {
300+
Context("CSI workload information using mock driver [Feature:CSIDriverRegistry]", func() {
309301
var (
310302
driver testsuites.TestDriver
311303
podInfoV1 = "v1"
312304
podInfoUnknown = "unknown"
313305
podInfoEmpty = ""
314306
)
315307

316-
BeforeEach(func() {
317-
config := testsuites.TestConfig{
318-
Framework: f,
319-
Prefix: "csi-workload",
320-
}
321-
driver = drivers.InitMockCSIDriver(config)
322-
driver.CreateDriver()
323-
})
324-
325-
AfterEach(func() {
326-
driver.CleanupDriver()
327-
})
328-
329308
tests := []struct {
330309
name string
331310
podInfoOnMountVersion *string
@@ -365,11 +344,18 @@ var _ = utils.SIGDescribe("CSI Volumes", func() {
365344
for _, t := range tests {
366345
test := t
367346
It(test.name, func() {
347+
By("Deploying mock CSI driver")
348+
config := testsuites.TestConfig{
349+
Framework: f,
350+
Prefix: "csi-workload",
351+
}
352+
353+
driver = drivers.InitMockCSIDriver(config, test.driverExists, true, test.podInfoOnMountVersion)
354+
driver.CreateDriver()
355+
368356
if test.driverExists {
369-
csiDriver := createCSIDriver(csics, testsuites.GetUniqueDriverName(driver), true, test.podInfoOnMountVersion)
370-
if csiDriver != nil {
371-
defer csics.CsiV1alpha1().CSIDrivers().Delete(csiDriver.Name, nil)
372-
}
357+
defer destroyCSIDriver(csics, driver)
358+
defer driver.CleanupDriver()
373359
}
374360

375361
By("Creating pod")
@@ -397,7 +383,8 @@ var _ = utils.SIGDescribe("CSI Volumes", func() {
397383
defer cs.StorageV1().StorageClasses().Delete(class.Name, nil)
398384
}
399385
if claim != nil {
400-
defer cs.CoreV1().PersistentVolumeClaims(ns.Name).Delete(claim.Name, nil)
386+
// Fully delete PV before deleting CSI driver
387+
defer deleteVolume(cs, claim)
401388
}
402389
if pod != nil {
403390
// Fully delete (=unmount) the pod before deleting CSI driver
@@ -472,20 +459,15 @@ func testTopologyNegative(cs clientset.Interface, suffix, namespace string, dela
472459
}
473460
}
474461

475-
func createCSIDriver(csics csiclient.Interface, name string, attachable bool, podInfoOnMountVersion *string) *csiv1alpha1.CSIDriver {
476-
By("Creating CSIDriver instance")
477-
driver := &csiv1alpha1.CSIDriver{
478-
ObjectMeta: metav1.ObjectMeta{
479-
Name: name,
480-
},
481-
Spec: csiv1alpha1.CSIDriverSpec{
482-
AttachRequired: &attachable,
483-
PodInfoOnMountVersion: podInfoOnMountVersion,
484-
},
462+
func destroyCSIDriver(csics csiclient.Interface, driver testsuites.TestDriver) {
463+
driverName := testsuites.GetUniqueDriverName(driver)
464+
driverGet, err := csics.CsiV1alpha1().CSIDrivers().Get(driverName, metav1.GetOptions{})
465+
if err == nil {
466+
framework.Logf("deleting %s.%s: %s", driverGet.TypeMeta.APIVersion, driverGet.TypeMeta.Kind, driverGet.ObjectMeta.Name)
467+
// Uncomment the following line to get full dump of CSIDriver object
468+
// framework.Logf("%s", framework.PrettyPrint(driverGet))
469+
csics.CsiV1alpha1().CSIDrivers().Delete(driverName, nil)
485470
}
486-
driver, err := csics.CsiV1alpha1().CSIDrivers().Create(driver)
487-
framework.ExpectNoError(err, "Failed to create CSIDriver: %v", err)
488-
return driver
489471
}
490472

491473
func getVolumeHandle(cs clientset.Interface, claim *v1.PersistentVolumeClaim) string {
@@ -508,6 +490,15 @@ func getVolumeHandle(cs clientset.Interface, claim *v1.PersistentVolumeClaim) st
508490
return pv.Spec.CSI.VolumeHandle
509491
}
510492

493+
func deleteVolume(cs clientset.Interface, claim *v1.PersistentVolumeClaim) {
494+
// re-get the claim to the latest state with bound volume
495+
claim, err := cs.CoreV1().PersistentVolumeClaims(claim.Namespace).Get(claim.Name, metav1.GetOptions{})
496+
if err == nil {
497+
cs.CoreV1().PersistentVolumeClaims(claim.Namespace).Delete(claim.Name, nil)
498+
framework.WaitForPersistentVolumeDeleted(cs, claim.Spec.VolumeName, 2*time.Second, 2*time.Minute)
499+
}
500+
}
501+
511502
func startPausePod(cs clientset.Interface, t testsuites.StorageClassTest, node testsuites.NodeSelection, ns string) (*storagev1.StorageClass, *v1.PersistentVolumeClaim, *v1.Pod) {
512503
class := newStorageClass(t, ns, "")
513504
class, err := cs.StorageV1().StorageClasses().Create(class)

test/e2e/storage/drivers/csi.go

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,39 @@ func (h *hostpathCSIDriver) CleanupDriver() {
166166

167167
// mockCSI
168168
type mockCSIDriver struct {
169-
cleanup func()
170-
driverInfo testsuites.DriverInfo
169+
cleanup func()
170+
driverInfo testsuites.DriverInfo
171+
manifests []string
172+
podInfoVersion *string
171173
}
172174

173175
var _ testsuites.TestDriver = &mockCSIDriver{}
174176
var _ testsuites.DynamicPVTestDriver = &mockCSIDriver{}
175177

176178
// InitMockCSIDriver returns a mockCSIDriver that implements TestDriver interface
177-
func InitMockCSIDriver(config testsuites.TestConfig) testsuites.TestDriver {
179+
func InitMockCSIDriver(config testsuites.TestConfig, registerDriver, driverAttachable bool, podInfoVersion *string) testsuites.TestDriver {
180+
driverManifests := []string{
181+
"test/e2e/testing-manifests/storage-csi/cluster-driver-registrar/rbac.yaml",
182+
"test/e2e/testing-manifests/storage-csi/driver-registrar/rbac.yaml",
183+
"test/e2e/testing-manifests/storage-csi/external-attacher/rbac.yaml",
184+
"test/e2e/testing-manifests/storage-csi/external-provisioner/rbac.yaml",
185+
"test/e2e/testing-manifests/storage-csi/mock/csi-mock-rbac.yaml",
186+
"test/e2e/testing-manifests/storage-csi/mock/csi-storageclass.yaml",
187+
"test/e2e/testing-manifests/storage-csi/mock/csi-mock-driver.yaml",
188+
}
189+
190+
config.ServerConfig = &framework.VolumeTestConfig{}
191+
192+
if registerDriver {
193+
driverManifests = append(driverManifests, "test/e2e/testing-manifests/storage-csi/mock/csi-mock-cluster-driver-registrar.yaml")
194+
}
195+
196+
if driverAttachable {
197+
driverManifests = append(driverManifests, "test/e2e/testing-manifests/storage-csi/mock/csi-mock-driver-attacher.yaml")
198+
} else {
199+
config.ServerConfig.ServerArgs = append(config.ServerConfig.ServerArgs, "--disable-attach")
200+
}
201+
178202
return &mockCSIDriver{
179203
driverInfo: testsuites.DriverInfo{
180204
Name: "csi-mock",
@@ -190,6 +214,8 @@ func InitMockCSIDriver(config testsuites.TestConfig) testsuites.TestDriver {
190214
},
191215
Config: config,
192216
},
217+
manifests: driverManifests,
218+
podInfoVersion: podInfoVersion,
193219
}
194220
}
195221

@@ -223,26 +249,28 @@ func (m *mockCSIDriver) CreateDriver() {
223249
node := nodes.Items[rand.Intn(len(nodes.Items))]
224250
m.driverInfo.Config.ClientNodeName = node.Name
225251

252+
containerArgs := []string{"--name=csi-mock-" + f.UniqueName}
253+
254+
if m.driverInfo.Config.ServerConfig != nil && m.driverInfo.Config.ServerConfig.ServerArgs != nil {
255+
containerArgs = append(containerArgs, m.driverInfo.Config.ServerConfig.ServerArgs...)
256+
}
257+
226258
// TODO (?): the storage.csi.image.version and storage.csi.image.registry
227259
// settings are ignored for this test. We could patch the image definitions.
228260
o := utils.PatchCSIOptions{
229-
OldDriverName: "csi-mock",
230-
NewDriverName: "csi-mock-" + f.UniqueName,
231-
DriverContainerName: "mock",
232-
DriverContainerArguments: []string{"--name=csi-mock-" + f.UniqueName},
233-
ProvisionerContainerName: "csi-provisioner",
234-
NodeName: m.driverInfo.Config.ClientNodeName,
261+
OldDriverName: "csi-mock",
262+
NewDriverName: "csi-mock-" + f.UniqueName,
263+
DriverContainerName: "mock",
264+
DriverContainerArguments: containerArgs,
265+
ProvisionerContainerName: "csi-provisioner",
266+
ClusterRegistrarContainerName: "csi-cluster-driver-registrar",
267+
NodeName: m.driverInfo.Config.ClientNodeName,
268+
PodInfoVersion: m.podInfoVersion,
235269
}
236270
cleanup, err := f.CreateFromManifests(func(item interface{}) error {
237271
return utils.PatchCSIDeployment(f, o, item)
238272
},
239-
"test/e2e/testing-manifests/storage-csi/driver-registrar/rbac.yaml",
240-
"test/e2e/testing-manifests/storage-csi/external-attacher/rbac.yaml",
241-
"test/e2e/testing-manifests/storage-csi/external-provisioner/rbac.yaml",
242-
"test/e2e/testing-manifests/storage-csi/mock/csi-mock-rbac.yaml",
243-
"test/e2e/testing-manifests/storage-csi/mock/csi-storageclass.yaml",
244-
"test/e2e/testing-manifests/storage-csi/mock/csi-mock-driver.yaml",
245-
)
273+
m.manifests...)
246274
m.cleanup = cleanup
247275
if err != nil {
248276
framework.Failf("deploying csi mock driver: %v", err)

test/e2e/storage/utils/deployment.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func PatchCSIDeployment(f *framework.Framework, o PatchCSIOptions, object interf
9797
// Driver name is expected to be the same
9898
// as the snapshotter here.
9999
container.Args = append(container.Args, "--snapshotter="+o.NewDriverName)
100+
case o.ClusterRegistrarContainerName:
101+
if o.PodInfoVersion != nil {
102+
container.Args = append(container.Args, "--pod-info-mount-version="+*o.PodInfoVersion)
103+
}
100104
}
101105
}
102106
}
@@ -153,6 +157,12 @@ type PatchCSIOptions struct {
153157
// If non-empty, --snapshotter with new name will be appended
154158
// to the argument list.
155159
SnapshotterContainerName string
160+
// The name of the container which has the cluster-driver-registrar
161+
// binary.
162+
ClusterRegistrarContainerName string
156163
// If non-empty, all pods are forced to run on this node.
157164
NodeName string
165+
// If not nil, the argument to pass to the cluster-driver-registrar's
166+
// pod-info-mount-version argument.
167+
PodInfoVersion *string
158168
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The original file is (or will be) https://github.com/kubernetes-csi/cluster-driver-registrar/blob/master/deploy/kubernetes/rbac.yaml
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This YAML file contains all RBAC objects that are necessary to run
2+
# cluster-driver-registrar.
3+
#
4+
# In production, each CSI driver deployment has to be customized:
5+
# - to avoid conflicts, use non-default namespace and different names
6+
# for non-namespaced entities like the ClusterRole
7+
8+
apiVersion: v1
9+
kind: ServiceAccount
10+
metadata:
11+
name: csi-cluster-driver-registrar
12+
# replace with non-default namespace name
13+
namespace: default
14+
15+
---
16+
kind: ClusterRole
17+
apiVersion: rbac.authorization.k8s.io/v1
18+
metadata:
19+
name: cluster-driver-registrar-runner
20+
rules:
21+
- apiGroups: ["csi.storage.k8s.io"]
22+
resources: ["csidrivers"]
23+
verbs: ["create", "delete"]
24+
25+
---
26+
kind: ClusterRoleBinding
27+
apiVersion: rbac.authorization.k8s.io/v1
28+
metadata:
29+
name: csi-cluster-driver-registrar-role
30+
subjects:
31+
- kind: ServiceAccount
32+
name: csi-cluster-driver-registrar
33+
# replace with non-default namespace name
34+
namespace: default
35+
roleRef:
36+
kind: ClusterRole
37+
name: cluster-driver-registrar-runner
38+
apiGroup: rbac.authorization.k8s.io

0 commit comments

Comments
 (0)