Skip to content

Commit fe62f32

Browse files
Sneha-atsunnylovestiramisu
authored andcommitted
refactoring for GetDisk call
1 parent b41cab6 commit fe62f32

File tree

4 files changed

+39
-40
lines changed

4 files changed

+39
-40
lines changed

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

+37-36
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,10 @@ func parseMachineType(machineTypeUrl string) string {
485485
}
486486

487487
func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error, string) {
488-
diskToPublish := ""
488+
diskType := ""
489489
project, volKey, err := gceCS.validateControllerPublishVolumeRequest(ctx, req)
490490
if err != nil {
491-
return nil, err, diskToPublish
491+
return nil, err, diskType
492492
}
493493

494494
volumeID := req.GetVolumeId()
@@ -503,36 +503,36 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
503503
project, volKey, err = gceCS.CloudProvider.RepairUnderspecifiedVolumeKey(ctx, project, volKey)
504504
if err != nil {
505505
if gce.IsGCENotFoundError(err) {
506-
return nil, status.Errorf(codes.NotFound, "ControllerPublishVolume could not find volume with ID %v: %v", volumeID, err.Error()), diskToPublish
506+
return nil, status.Errorf(codes.NotFound, "ControllerPublishVolume could not find volume with ID %v: %v", volumeID, err.Error()), diskType
507507
}
508-
return nil, common.LoggedError("ControllerPublishVolume error repairing underspecified volume key: ", err), diskToPublish
508+
return nil, common.LoggedError("ControllerPublishVolume error repairing underspecified volume key: ", err), diskType
509509
}
510510

511511
// Acquires the lock for the volume on that node only, because we need to support the ability
512512
// to publish the same volume onto different nodes concurrently
513513
lockingVolumeID := fmt.Sprintf("%s/%s", nodeID, volumeID)
514514
if acquired := gceCS.volumeLocks.TryAcquire(lockingVolumeID); !acquired {
515-
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID), diskToPublish
515+
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID), diskType
516516
}
517517
defer gceCS.volumeLocks.Release(lockingVolumeID)
518518
disk, err := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)
519-
diskToPublish = metrics.GetDiskType(disk)
519+
diskType = metrics.GetDiskType(disk)
520520
if err != nil {
521521
if gce.IsGCENotFoundError(err) {
522-
return nil, status.Errorf(codes.NotFound, "Could not find disk %v: %v", volKey.String(), err.Error()), diskToPublish
522+
return nil, status.Errorf(codes.NotFound, "Could not find disk %v: %v", volKey.String(), err.Error()), diskType
523523
}
524-
return nil, status.Errorf(codes.Internal, "Failed to getDisk: %v", err.Error()), diskToPublish
524+
return nil, status.Errorf(codes.Internal, "Failed to getDisk: %v", err.Error()), diskType
525525
}
526526
instanceZone, instanceName, err := common.NodeIDToZoneAndName(nodeID)
527527
if err != nil {
528-
return nil, status.Errorf(codes.NotFound, "could not split nodeID: %v", err.Error()), diskToPublish
528+
return nil, status.Errorf(codes.NotFound, "could not split nodeID: %v", err.Error()), diskType
529529
}
530530
instance, err := gceCS.CloudProvider.GetInstanceOrError(ctx, instanceZone, instanceName)
531531
if err != nil {
532532
if gce.IsGCENotFoundError(err) {
533-
return nil, status.Errorf(codes.NotFound, "Could not find instance %v: %v", nodeID, err.Error()), diskToPublish
533+
return nil, status.Errorf(codes.NotFound, "Could not find instance %v: %v", nodeID, err.Error()), diskType
534534
}
535-
return nil, status.Errorf(codes.Internal, "Failed to get instance: %v", err.Error()), diskToPublish
535+
return nil, status.Errorf(codes.Internal, "Failed to get instance: %v", err.Error()), diskType
536536
}
537537

538538
readWrite := "READ_WRITE"
@@ -542,21 +542,21 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
542542

543543
deviceName, err := common.GetDeviceName(volKey)
544544
if err != nil {
545-
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error()), diskToPublish
545+
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error()), diskType
546546
}
547547

548548
attached, err := diskIsAttachedAndCompatible(deviceName, instance, volumeCapability, readWrite)
549549
if err != nil {
550-
return nil, status.Errorf(codes.AlreadyExists, "Disk %v already published to node %v but incompatible: %v", volKey.Name, nodeID, err.Error()), diskToPublish
550+
return nil, status.Errorf(codes.AlreadyExists, "Disk %v already published to node %v but incompatible: %v", volKey.Name, nodeID, err.Error()), diskType
551551
}
552552
if attached {
553553
// Volume is attached to node. Success!
554554
klog.V(4).Infof("ControllerPublishVolume succeeded for disk %v to instance %v, already attached.", volKey, nodeID)
555-
return pubVolResp, nil, diskToPublish
555+
return pubVolResp, nil, diskType
556556
}
557557
instanceZone, instanceName, err = common.NodeIDToZoneAndName(nodeID)
558558
if err != nil {
559-
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error()), diskToPublish
559+
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error()), diskType
560560
}
561561
err = gceCS.CloudProvider.AttachDisk(ctx, project, volKey, readWrite, attachableDiskTypePersistent, instanceZone, instanceName)
562562
if err != nil {
@@ -565,18 +565,18 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
565565
// If we encountered an UnsupportedDiskError, rewrite the error message to be more user friendly.
566566
// The error message from GCE is phrased around disk create on VM creation, not runtime attach.
567567
machineType := parseMachineType(instance.MachineType)
568-
return nil, status.Errorf(codes.InvalidArgument, "'%s' is not a compatible disk type with the machine type %s, please review the GCP online documentation for available persistent disk options", udErr.DiskType, machineType), diskToPublish
568+
return nil, status.Errorf(codes.InvalidArgument, "'%s' is not a compatible disk type with the machine type %s, please review the GCP online documentation for available persistent disk options", udErr.DiskType, machineType), diskType
569569
}
570-
return nil, status.Errorf(codes.Internal, "Failed to Attach: %v", err.Error()), diskToPublish
570+
return nil, status.Errorf(codes.Internal, "Failed to Attach: %v", err.Error()), diskType
571571
}
572572

573573
err = gceCS.CloudProvider.WaitForAttach(ctx, project, volKey, instanceZone, instanceName)
574574
if err != nil {
575-
return nil, status.Errorf(codes.Internal, "Errored during WaitForAttach: %v", err.Error()), diskToPublish
575+
return nil, status.Errorf(codes.Internal, "Errored during WaitForAttach: %v", err.Error()), diskType
576576
}
577577

578578
klog.V(4).Infof("ControllerPublishVolume succeeded for disk %v to instance %v", volKey, nodeID)
579-
return pubVolResp, nil, diskToPublish
579+
return pubVolResp, nil, diskType
580580
}
581581

582582
func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
@@ -587,18 +587,17 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
587587
gceCS.Metrics.RecordOperationErrorMetrics("ControllerUnpublishVolume", err, diskTypeForMetric)
588588
}
589589
}()
590-
project, volKey, err := gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
590+
_, _, err = gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
591591
if err != nil {
592592
return nil, err
593593
}
594+
err = status.Errorf(codes.InvalidArgument, "error message")
594595
// Only valid requests will be queued
595596
backoffId := gceCS.errorBackoff.backoffId(req.NodeId, req.VolumeId)
596597
if gceCS.errorBackoff.blocking(backoffId) {
597598
return nil, status.Errorf(codes.Unavailable, "ControllerUnpublish not permitted on node %q due to backoff condition", req.NodeId)
598599
}
599-
diskToUnpublish, _ := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)
600-
diskTypeForMetric = metrics.GetDiskType(diskToUnpublish)
601-
resp, err := gceCS.executeControllerUnpublishVolume(ctx, req)
600+
resp, err, diskTypeForMetric := gceCS.executeControllerUnpublishVolume(ctx, req)
602601
if err != nil {
603602
klog.Infof("For node %s adding backoff due to error for volume %s", req.NodeId, req.VolumeId)
604603
gceCS.errorBackoff.next(backoffId)
@@ -628,11 +627,12 @@ func (gceCS *GCEControllerServer) validateControllerUnpublishVolumeRequest(ctx c
628627
return project, volKey, nil
629628
}
630629

631-
func (gceCS *GCEControllerServer) executeControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
630+
func (gceCS *GCEControllerServer) executeControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error, string) {
631+
var diskType string
632632
project, volKey, err := gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
633633

634634
if err != nil {
635-
return nil, err
635+
return nil, err, diskType
636636
}
637637

638638
volumeID := req.GetVolumeId()
@@ -641,52 +641,53 @@ func (gceCS *GCEControllerServer) executeControllerUnpublishVolume(ctx context.C
641641
if err != nil {
642642
if gce.IsGCENotFoundError(err) {
643643
klog.Warningf("Treating volume %v as unpublished because it could not be found", volumeID)
644-
return &csi.ControllerUnpublishVolumeResponse{}, nil
644+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
645645
}
646-
return nil, common.LoggedError("ControllerUnpublishVolume error repairing underspecified volume key: ", err)
646+
return nil, common.LoggedError("ControllerUnpublishVolume error repairing underspecified volume key: ", err), diskType
647647
}
648648

649649
// Acquires the lock for the volume on that node only, because we need to support the ability
650650
// to unpublish the same volume from different nodes concurrently
651651
lockingVolumeID := fmt.Sprintf("%s/%s", nodeID, volumeID)
652652
if acquired := gceCS.volumeLocks.TryAcquire(lockingVolumeID); !acquired {
653-
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID)
653+
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID), diskType
654654
}
655655
defer gceCS.volumeLocks.Release(lockingVolumeID)
656-
656+
diskToUnpublish, _ := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)
657+
diskType = metrics.GetDiskType(diskToUnpublish)
657658
instanceZone, instanceName, err := common.NodeIDToZoneAndName(nodeID)
658659
if err != nil {
659-
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error())
660+
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error()), diskType
660661
}
661662
instance, err := gceCS.CloudProvider.GetInstanceOrError(ctx, instanceZone, instanceName)
662663
if err != nil {
663664
if gce.IsGCENotFoundError(err) {
664665
// Node not existing on GCE means that disk has been detached
665666
klog.Warningf("Treating volume %v as unpublished because node %v could not be found", volKey.String(), instanceName)
666-
return &csi.ControllerUnpublishVolumeResponse{}, nil
667+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
667668
}
668-
return nil, status.Errorf(codes.Internal, "error getting instance: %v", err.Error())
669+
return nil, status.Errorf(codes.Internal, "error getting instance: %v", err.Error()), diskType
669670
}
670671

671672
deviceName, err := common.GetDeviceName(volKey)
672673
if err != nil {
673-
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error())
674+
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error()), diskType
674675
}
675676

676677
attached := diskIsAttached(deviceName, instance)
677678

678679
if !attached {
679680
// Volume is not attached to node. Success!
680681
klog.V(4).Infof("ControllerUnpublishVolume succeeded for disk %v from node %v. Already not attached.", volKey, nodeID)
681-
return &csi.ControllerUnpublishVolumeResponse{}, nil
682+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
682683
}
683684
err = gceCS.CloudProvider.DetachDisk(ctx, project, deviceName, instanceZone, instanceName)
684685
if err != nil {
685-
return nil, common.LoggedError("Failed to detach: ", err)
686+
return nil, common.LoggedError("Failed to detach: ", err), diskType
686687
}
687688

688689
klog.V(4).Infof("ControllerUnpublishVolume succeeded for disk %v from node %v", volKey, nodeID)
689-
return &csi.ControllerUnpublishVolumeResponse{}, nil
690+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
690691
}
691692

692693
func (gceCS *GCEControllerServer) ValidateVolumeCapabilities(ctx context.Context, req *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,4 @@ func containsZone(zones []string, zone string) bool {
211211
}
212212

213213
return false
214-
}
214+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,4 @@ func TestGetReadOnlyFromCapabilities(t *testing.T) {
290290
}
291291
}
292292
}
293-
}
293+
}

pkg/metrics/metrics.go

-2
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ func GetDiskType(disk *gce.CloudDisk) string {
151151
var diskType string
152152
if disk != nil {
153153
diskType = disk.GetPDType()
154-
} else {
155-
diskType = ""
156154
}
157155
return diskType
158156
}

0 commit comments

Comments
 (0)