From 974d47ecf8c269593130d4518c126e39e27a6786 Mon Sep 17 00:00:00 2001 From: Michelle Au Date: Thu, 15 Nov 2018 14:01:34 -0800 Subject: [PATCH] Remove devicePath dependency for CSI volumes --- pkg/volume/csi/csi_attacher.go | 5 ++- pkg/volume/csi/csi_attacher_test.go | 67 +++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/pkg/volume/csi/csi_attacher.go b/pkg/volume/csi/csi_attacher.go index a5ca331a8c..c66792af0f 100644 --- a/pkg/volume/csi/csi_attacher.go +++ b/pkg/volume/csi/csi_attacher.go @@ -119,16 +119,19 @@ func (c *csiAttacher) Attach(spec *volume.Spec, nodeName types.NodeName) (string klog.V(4).Info(log("attacher.Attach finished OK with VolumeAttachment object [%s]", attachID)) + // TODO(71164): In 1.15, return empty devicePath return attachID, nil } -func (c *csiAttacher) WaitForAttach(spec *volume.Spec, attachID string, pod *v1.Pod, timeout time.Duration) (string, error) { +func (c *csiAttacher) WaitForAttach(spec *volume.Spec, _ string, pod *v1.Pod, timeout time.Duration) (string, error) { source, err := getCSISourceFromSpec(spec) if err != nil { klog.Error(log("attacher.WaitForAttach failed to extract CSI volume source: %v", err)) return "", err } + attachID := getAttachmentName(source.VolumeHandle, source.Driver, string(c.plugin.host.GetNodeName())) + skip, err := c.plugin.skipAttach(source.Driver) if err != nil { klog.Error(log("attacher.Attach failed to find if driver is attachable: %v", err)) diff --git a/pkg/volume/csi/csi_attacher_test.go b/pkg/volume/csi/csi_attacher_test.go index 72c0c236e2..c34008b624 100644 --- a/pkg/volume/csi/csi_attacher_test.go +++ b/pkg/volume/csi/csi_attacher_test.go @@ -332,6 +332,73 @@ func TestAttacherWaitForVolumeAttachmentWithCSIDriver(t *testing.T) { } } +func TestAttacherWaitForAttach(t *testing.T) { + tests := []struct { + name string + driver string + makeAttachment func() *storage.VolumeAttachment + expectedAttachID string + expectError bool + }{ + { + name: "successful attach", + driver: "attachable", + makeAttachment: func() *storage.VolumeAttachment { + + testAttachID := getAttachmentName("test-vol", "attachable", "node") + successfulAttachment := makeTestAttachment(testAttachID, "node", "test-pv") + successfulAttachment.Status.Attached = true + return successfulAttachment + }, + expectedAttachID: getAttachmentName("test-vol", "attachable", "node"), + expectError: false, + }, + { + name: "failed attach", + driver: "attachable", + expectError: true, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + plug, _, tmpDir, _ := newTestWatchPlugin(t, nil) + defer os.RemoveAll(tmpDir) + + attacher, err := plug.NewAttacher() + if err != nil { + t.Fatalf("failed to create new attacher: %v", err) + } + csiAttacher := attacher.(*csiAttacher) + spec := volume.NewSpecFromPersistentVolume(makeTestPV("test-pv", 10, test.driver, "test-vol"), false) + + if test.makeAttachment != nil { + attachment := test.makeAttachment() + _, err = csiAttacher.k8s.StorageV1beta1().VolumeAttachments().Create(attachment) + if err != nil { + t.Fatalf("failed to create VolumeAttachment: %v", err) + } + gotAttachment, err := csiAttacher.k8s.StorageV1beta1().VolumeAttachments().Get(attachment.Name, meta.GetOptions{}) + if err != nil { + t.Fatalf("failed to get created VolumeAttachment: %v", err) + } + t.Logf("created test VolumeAttachment %+v", gotAttachment) + } + + attachID, err := csiAttacher.WaitForAttach(spec, "", nil, time.Second) + if err != nil && !test.expectError { + t.Errorf("Unexpected error: %s", err) + } + if err == nil && test.expectError { + t.Errorf("Expected error, got none") + } + if attachID != test.expectedAttachID { + t.Errorf("Expected attachID %q, got %q", test.expectedAttachID, attachID) + } + }) + } +} + func TestAttacherWaitForVolumeAttachment(t *testing.T) { nodeName := "test-node" testCases := []struct {