Skip to content

Commit 5181cdc

Browse files
authored
Merge pull request #111 from davidz627/feature/diskNameCollisions
Resolve disk name collisions for attached regional disks
2 parents 58f20f5 + 3dec96e commit 5181cdc

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
@@ -40,6 +40,8 @@ const (
4040
nodeIDZoneValue = 0
4141
nodeIDNameValue = 1
4242
nodeIDTotalElements = 2
43+
44+
regionalDeviceNameSuffix = "_regional"
4345
)
4446

4547
func BytesToGb(bytes int64) int64 {
@@ -104,3 +106,14 @@ func GetRegionFromZones(zones []string) (string, error) {
104106
}
105107
return regions.UnsortedList()[0], nil
106108
}
109+
110+
func GetDeviceName(volKey *meta.Key) (string, error) {
111+
switch volKey.Type() {
112+
case meta.Zonal:
113+
return volKey.Name, nil
114+
case meta.Regional:
115+
return volKey.Name + regionalDeviceNameSuffix, nil
116+
default:
117+
return "", fmt.Errorf("volume key %v neither zonal nor regional", volKey.Name)
118+
}
119+
}

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

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

321+
deviceName, err := common.GetDeviceName(volKey)
322+
if err != nil {
323+
return fmt.Errorf("failed to get device name: %v", err)
324+
}
321325
attachedDiskV1 := &compute.AttachedDisk{
322-
DeviceName: disk.GetName(),
326+
DeviceName: deviceName,
323327
Kind: disk.GetKind(),
324328
Mode: readWrite,
325329
Source: source,
@@ -338,7 +342,12 @@ func (cloud *CloudProvider) AttachDisk(ctx context.Context, disk *CloudDisk, vol
338342
}
339343

340344
func (cloud *CloudProvider) DetachDisk(ctx context.Context, volKey *meta.Key, instanceZone, instanceName string) error {
341-
op, err := cloud.service.Instances.DetachDisk(cloud.project, instanceZone, instanceName, volKey.Name).Context(ctx).Do()
345+
deviceName, err := common.GetDeviceName(volKey)
346+
if err != nil {
347+
return err
348+
}
349+
350+
op, err := cloud.service.Instances.DetachDisk(cloud.project, instanceZone, instanceName, deviceName).Context(ctx).Do()
342351
if err != nil {
343352
return err
344353
}

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

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

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

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

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

317323
if !attached {
318324
// Volume is not attached to node. Success!
@@ -649,19 +655,19 @@ func getRequestCapacity(capRange *csi.CapacityRange) (int64, error) {
649655
return capBytes, nil
650656
}
651657

652-
func diskIsAttached(volume *gce.CloudDisk, instance *compute.Instance) bool {
658+
func diskIsAttached(deviceName string, instance *compute.Instance) bool {
653659
for _, disk := range instance.Disks {
654-
if disk.DeviceName == volume.GetName() {
660+
if disk.DeviceName == deviceName {
655661
// Disk is attached to node
656662
return true
657663
}
658664
}
659665
return false
660666
}
661667

662-
func diskIsAttachedAndCompatible(volume *gce.CloudDisk, instance *compute.Instance, volumeCapability *csi.VolumeCapability, readWrite string) (bool, error) {
668+
func diskIsAttachedAndCompatible(deviceName string, instance *compute.Instance, volumeCapability *csi.VolumeCapability, readWrite string) (bool, error) {
663669
for _, disk := range instance.Disks {
664-
if disk.DeviceName == volume.GetName() {
670+
if disk.DeviceName == deviceName {
665671
// Disk is attached to node
666672
if disk.Mode != readWrite {
667673
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
@@ -849,15 +849,13 @@ func TestGetRequestCapacity(t *testing.T) {
849849
func TestDiskIsAttached(t *testing.T) {
850850
testCases := []struct {
851851
name string
852-
disk *compute.Disk
852+
deviceName string
853853
instance *compute.Instance
854854
expAttached bool
855855
}{
856856
{
857-
name: "normal-attached",
858-
disk: &compute.Disk{
859-
Name: "test-disk",
860-
},
857+
name: "normal-attached",
858+
deviceName: "test-disk",
861859
instance: &compute.Instance{
862860
Disks: []*compute.AttachedDisk{
863861
{
@@ -868,10 +866,8 @@ func TestDiskIsAttached(t *testing.T) {
868866
expAttached: true,
869867
},
870868
{
871-
name: "normal-not-attached",
872-
disk: &compute.Disk{
873-
Name: "test-disk",
874-
},
869+
name: "normal-not-attached",
870+
deviceName: "test-disk",
875871
instance: &compute.Instance{
876872
Disks: []*compute.AttachedDisk{
877873
{
@@ -884,7 +880,7 @@ func TestDiskIsAttached(t *testing.T) {
884880
}
885881
for _, tc := range testCases {
886882
t.Logf("test case: %s", tc.name)
887-
if attached := diskIsAttached(gce.ZonalCloudDisk(tc.disk), tc.instance); attached != tc.expAttached {
883+
if attached := diskIsAttached(tc.deviceName, tc.instance); attached != tc.expAttached {
888884
t.Errorf("Expected disk attached to be %v, but got %v", tc.expAttached, attached)
889885
}
890886
}
@@ -893,17 +889,15 @@ func TestDiskIsAttached(t *testing.T) {
893889
func TestDiskIsAttachedAndCompatible(t *testing.T) {
894890
testCases := []struct {
895891
name string
896-
disk *compute.Disk
892+
deviceName string
897893
instance *compute.Instance
898894
mode string
899895
expAttached bool
900896
expErr bool
901897
}{
902898
{
903-
name: "normal-attached",
904-
disk: &compute.Disk{
905-
Name: "test-disk",
906-
},
899+
name: "normal-attached",
900+
deviceName: "test-disk",
907901
instance: &compute.Instance{
908902
Disks: []*compute.AttachedDisk{
909903
{
@@ -916,10 +910,8 @@ func TestDiskIsAttachedAndCompatible(t *testing.T) {
916910
expAttached: true,
917911
},
918912
{
919-
name: "normal-not-attached",
920-
disk: &compute.Disk{
921-
Name: "test-disk",
922-
},
913+
name: "normal-not-attached",
914+
deviceName: "test-disk",
923915
instance: &compute.Instance{
924916
Disks: []*compute.AttachedDisk{
925917
{
@@ -932,10 +924,8 @@ func TestDiskIsAttachedAndCompatible(t *testing.T) {
932924
expAttached: false,
933925
},
934926
{
935-
name: "incompatible mode",
936-
disk: &compute.Disk{
937-
Name: "test-disk",
938-
},
927+
name: "incompatible mode",
928+
deviceName: "test-disk",
939929
instance: &compute.Instance{
940930
Disks: []*compute.AttachedDisk{
941931
{
@@ -951,7 +941,7 @@ func TestDiskIsAttachedAndCompatible(t *testing.T) {
951941
}
952942
for _, tc := range testCases {
953943
t.Logf("test case: %s", tc.name)
954-
attached, err := diskIsAttachedAndCompatible(gce.ZonalCloudDisk(tc.disk), tc.instance, nil, tc.mode)
944+
attached, err := diskIsAttachedAndCompatible(tc.deviceName, tc.instance, nil, tc.mode)
955945
if err != nil && !tc.expErr {
956946
t.Errorf("Did not expect error but got: %v", err)
957947
}

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)