Skip to content

Commit 3dec96e

Browse files
committed
Resolve disk name collisions for attached regional disks
1 parent a1d6f88 commit 3dec96e

File tree

7 files changed

+67
-45
lines changed

7 files changed

+67
-45
lines changed

pkg/common/utils.go

+13
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const (
3838
nodeIDZoneValue = 0
3939
nodeIDNameValue = 1
4040
nodeIDTotalElements = 2
41+
42+
regionalDeviceNameSuffix = "_regional"
4143
)
4244

4345
func BytesToGb(bytes int64) int64 {
@@ -90,3 +92,14 @@ func GetRegionFromZones(zones []string) (string, error) {
9092
}
9193
return regions.UnsortedList()[0], nil
9294
}
95+
96+
func GetDeviceName(volKey *meta.Key) (string, error) {
97+
switch volKey.Type() {
98+
case meta.Zonal:
99+
return volKey.Name, nil
100+
case meta.Regional:
101+
return volKey.Name + regionalDeviceNameSuffix, nil
102+
default:
103+
return "", fmt.Errorf("volume key %v neither zonal nor regional", volKey.Name)
104+
}
105+
}

pkg/gce-cloud-provider/compute/gce-compute.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,12 @@ func (cloud *CloudProvider) deleteRegionalDisk(ctx context.Context, region, name
300300
func (cloud *CloudProvider) AttachDisk(ctx context.Context, disk *CloudDisk, volKey *meta.Key, readWrite, diskType, instanceZone, instanceName string) error {
301301
source := cloud.GetDiskSourceURI(disk, volKey)
302302

303+
deviceName, err := common.GetDeviceName(volKey)
304+
if err != nil {
305+
return fmt.Errorf("failed to get device name: %v", err)
306+
}
303307
attachedDiskV1 := &compute.AttachedDisk{
304-
DeviceName: disk.GetName(),
308+
DeviceName: deviceName,
305309
Kind: disk.GetKind(),
306310
Mode: readWrite,
307311
Source: source,
@@ -320,7 +324,12 @@ func (cloud *CloudProvider) AttachDisk(ctx context.Context, disk *CloudDisk, vol
320324
}
321325

322326
func (cloud *CloudProvider) DetachDisk(ctx context.Context, volKey *meta.Key, instanceZone, instanceName string) error {
323-
op, err := cloud.service.Instances.DetachDisk(cloud.project, instanceZone, instanceName, volKey.Name).Context(ctx).Do()
327+
deviceName, err := common.GetDeviceName(volKey)
328+
if err != nil {
329+
return err
330+
}
331+
332+
op, err := cloud.service.Instances.DetachDisk(cloud.project, instanceZone, instanceName, deviceName).Context(ctx).Do()
324333
if err != nil {
325334
return err
326335
}

pkg/gce-pd-csi-driver/controller.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,12 @@ func (gceCS *GCEControllerServer) ControllerPublishVolume(ctx context.Context, r
251251
readWrite = "READ_ONLY"
252252
}
253253

254-
attached, err := diskIsAttachedAndCompatible(disk, instance, volumeCapability, readWrite)
254+
deviceName, err := common.GetDeviceName(volKey)
255+
if err != nil {
256+
return nil, status.Error(codes.Internal, fmt.Sprintf("error getting device name: %v", err))
257+
}
258+
259+
attached, err := diskIsAttachedAndCompatible(deviceName, instance, volumeCapability, readWrite)
255260
if err != nil {
256261
return nil, status.Error(codes.AlreadyExists, fmt.Sprintf("Disk %v already published to node %v but incompatbile: %v", volKey.Name, nodeID, err))
257262
}
@@ -298,10 +303,6 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
298303
return nil, err
299304
}
300305

301-
disk, err := gceCS.CloudProvider.GetDisk(ctx, volKey)
302-
if err != nil {
303-
return nil, err
304-
}
305306
instanceZone, instanceName, err := common.NodeIDToZoneAndName(nodeID)
306307
if err != nil {
307308
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("could not split nodeID: %v", err))
@@ -311,7 +312,12 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
311312
return nil, err
312313
}
313314

314-
attached := diskIsAttached(disk, instance)
315+
deviceName, err := common.GetDeviceName(volKey)
316+
if err != nil {
317+
return nil, status.Error(codes.Internal, fmt.Sprintf("error getting device name: %v", err))
318+
}
319+
320+
attached := diskIsAttached(deviceName, instance)
315321

316322
if !attached {
317323
// Volume is not attached to node. Success!
@@ -473,19 +479,19 @@ func getRequestCapacity(capRange *csi.CapacityRange) (int64, error) {
473479
return capBytes, nil
474480
}
475481

476-
func diskIsAttached(volume *gce.CloudDisk, instance *compute.Instance) bool {
482+
func diskIsAttached(deviceName string, instance *compute.Instance) bool {
477483
for _, disk := range instance.Disks {
478-
if disk.DeviceName == volume.GetName() {
484+
if disk.DeviceName == deviceName {
479485
// Disk is attached to node
480486
return true
481487
}
482488
}
483489
return false
484490
}
485491

486-
func diskIsAttachedAndCompatible(volume *gce.CloudDisk, instance *compute.Instance, volumeCapability *csi.VolumeCapability, readWrite string) (bool, error) {
492+
func diskIsAttachedAndCompatible(deviceName string, instance *compute.Instance, volumeCapability *csi.VolumeCapability, readWrite string) (bool, error) {
487493
for _, disk := range instance.Disks {
488-
if disk.DeviceName == volume.GetName() {
494+
if disk.DeviceName == deviceName {
489495
// Disk is attached to node
490496
if disk.Mode != readWrite {
491497
return true, fmt.Errorf("disk mode does not match. Got %v. Want %v", disk.Mode, readWrite)

pkg/gce-pd-csi-driver/controller_test.go

+14-24
Original file line numberDiff line numberDiff line change
@@ -604,15 +604,13 @@ func TestGetRequestCapacity(t *testing.T) {
604604
func TestDiskIsAttached(t *testing.T) {
605605
testCases := []struct {
606606
name string
607-
disk *compute.Disk
607+
deviceName string
608608
instance *compute.Instance
609609
expAttached bool
610610
}{
611611
{
612-
name: "normal-attached",
613-
disk: &compute.Disk{
614-
Name: "test-disk",
615-
},
612+
name: "normal-attached",
613+
deviceName: "test-disk",
616614
instance: &compute.Instance{
617615
Disks: []*compute.AttachedDisk{
618616
{
@@ -623,10 +621,8 @@ func TestDiskIsAttached(t *testing.T) {
623621
expAttached: true,
624622
},
625623
{
626-
name: "normal-not-attached",
627-
disk: &compute.Disk{
628-
Name: "test-disk",
629-
},
624+
name: "normal-not-attached",
625+
deviceName: "test-disk",
630626
instance: &compute.Instance{
631627
Disks: []*compute.AttachedDisk{
632628
{
@@ -639,7 +635,7 @@ func TestDiskIsAttached(t *testing.T) {
639635
}
640636
for _, tc := range testCases {
641637
t.Logf("test case: %s", tc.name)
642-
if attached := diskIsAttached(gce.ZonalCloudDisk(tc.disk), tc.instance); attached != tc.expAttached {
638+
if attached := diskIsAttached(tc.deviceName, tc.instance); attached != tc.expAttached {
643639
t.Errorf("Expected disk attached to be %v, but got %v", tc.expAttached, attached)
644640
}
645641
}
@@ -648,17 +644,15 @@ func TestDiskIsAttached(t *testing.T) {
648644
func TestDiskIsAttachedAndCompatible(t *testing.T) {
649645
testCases := []struct {
650646
name string
651-
disk *compute.Disk
647+
deviceName string
652648
instance *compute.Instance
653649
mode string
654650
expAttached bool
655651
expErr bool
656652
}{
657653
{
658-
name: "normal-attached",
659-
disk: &compute.Disk{
660-
Name: "test-disk",
661-
},
654+
name: "normal-attached",
655+
deviceName: "test-disk",
662656
instance: &compute.Instance{
663657
Disks: []*compute.AttachedDisk{
664658
{
@@ -671,10 +665,8 @@ func TestDiskIsAttachedAndCompatible(t *testing.T) {
671665
expAttached: true,
672666
},
673667
{
674-
name: "normal-not-attached",
675-
disk: &compute.Disk{
676-
Name: "test-disk",
677-
},
668+
name: "normal-not-attached",
669+
deviceName: "test-disk",
678670
instance: &compute.Instance{
679671
Disks: []*compute.AttachedDisk{
680672
{
@@ -687,10 +679,8 @@ func TestDiskIsAttachedAndCompatible(t *testing.T) {
687679
expAttached: false,
688680
},
689681
{
690-
name: "incompatible mode",
691-
disk: &compute.Disk{
692-
Name: "test-disk",
693-
},
682+
name: "incompatible mode",
683+
deviceName: "test-disk",
694684
instance: &compute.Instance{
695685
Disks: []*compute.AttachedDisk{
696686
{
@@ -706,7 +696,7 @@ func TestDiskIsAttachedAndCompatible(t *testing.T) {
706696
}
707697
for _, tc := range testCases {
708698
t.Logf("test case: %s", tc.name)
709-
attached, err := diskIsAttachedAndCompatible(gce.ZonalCloudDisk(tc.disk), tc.instance, nil, tc.mode)
699+
attached, err := diskIsAttachedAndCompatible(tc.deviceName, tc.instance, nil, tc.mode)
710700
if err != nil && !tc.expErr {
711701
t.Errorf("Did not expect error but got: %v", err)
712702
}

pkg/gce-pd-csi-driver/node.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ func (ns *GCENodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStage
177177
// TODO(#83): Get real partitions
178178
partition := ""
179179

180-
devicePaths := ns.DeviceUtils.GetDiskByIdPaths(volumeKey.Name, partition)
180+
deviceName, err := common.GetDeviceName(volumeKey)
181+
if err != nil {
182+
status.Error(codes.Internal, fmt.Sprintf("error getting device name: %v", err))
183+
}
184+
185+
devicePaths := ns.DeviceUtils.GetDiskByIdPaths(deviceName, partition)
181186
devicePath, err := ns.DeviceUtils.VerifyDevicePath(devicePaths)
182187

183188
if err != nil {

pkg/mount-manager/device-utils.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const (
5454
type DeviceUtils interface {
5555
// GetDiskByIdPaths returns a list of all possible paths for a
5656
// given Persistent Disk
57-
GetDiskByIdPaths(pdName string, partition string) []string
57+
GetDiskByIdPaths(deviceName string, partition string) []string
5858

5959
// VerifyDevicePath returns the first of the list of device paths that
6060
// exists on the machine, or an empty string if none exists
@@ -71,12 +71,12 @@ func NewDeviceUtils() *deviceUtils {
7171
}
7272

7373
// Returns list of all /dev/disk/by-id/* paths for given PD.
74-
func (m *deviceUtils) GetDiskByIdPaths(pdName string, partition string) []string {
74+
func (m *deviceUtils) GetDiskByIdPaths(deviceName string, partition string) []string {
7575
devicePaths := []string{
76-
path.Join(diskByIdPath, diskGooglePrefix+pdName),
77-
path.Join(diskByIdPath, diskScsiGooglePrefix+pdName),
78-
path.Join(diskByHostIdPath, diskScsiGooglePrefix+pdName),
79-
path.Join(diskByHostIdPath, diskScsiGooglePrefix+pdName),
76+
path.Join(diskByIdPath, diskGooglePrefix+deviceName),
77+
path.Join(diskByIdPath, diskScsiGooglePrefix+deviceName),
78+
path.Join(diskByHostIdPath, diskScsiGooglePrefix+deviceName),
79+
path.Join(diskByHostIdPath, diskScsiGooglePrefix+deviceName),
8080
}
8181

8282
if partition != "" {

test/run-e2e-local.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/bin/bash
22

3-
#set -o nounset
3+
set -o nounset
44
set -o errexit
55

66
readonly PKGDIR=sigs.k8s.io/gcp-compute-persistent-disk-csi-driver
77

8-
98
ginkgo -v "test/e2e/tests" --logtostderr -- --project ${PROJECT} --service-account ${IAM_NAME}

0 commit comments

Comments
 (0)