Skip to content

Commit b78ebed

Browse files
Sneha-atsunnylovestiramisu
authored andcommitted
refactoring for GetDisk call
1 parent 69f4ca3 commit b78ebed

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

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

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,10 @@ func parseMachineType(machineTypeUrl string) string {
549549
}
550550

551551
func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error, string) {
552-
diskToPublish := ""
552+
diskType := ""
553553
project, volKey, err := gceCS.validateControllerPublishVolumeRequest(ctx, req)
554554
if err != nil {
555-
return nil, err, diskToPublish
555+
return nil, err, diskType
556556
}
557557

558558
volumeID := req.GetVolumeId()
@@ -567,36 +567,36 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
567567
project, volKey, err = gceCS.CloudProvider.RepairUnderspecifiedVolumeKey(ctx, project, volKey)
568568
if err != nil {
569569
if gce.IsGCENotFoundError(err) {
570-
return nil, status.Errorf(codes.NotFound, "ControllerPublishVolume could not find volume with ID %v: %v", volumeID, err.Error()), diskToPublish
570+
return nil, status.Errorf(codes.NotFound, "ControllerPublishVolume could not find volume with ID %v: %v", volumeID, err.Error()), diskType
571571
}
572-
return nil, common.LoggedError("ControllerPublishVolume error repairing underspecified volume key: ", err), diskToPublish
572+
return nil, common.LoggedError("ControllerPublishVolume error repairing underspecified volume key: ", err), diskType
573573
}
574574

575575
// Acquires the lock for the volume on that node only, because we need to support the ability
576576
// to publish the same volume onto different nodes concurrently
577577
lockingVolumeID := fmt.Sprintf("%s/%s", nodeID, volumeID)
578578
if acquired := gceCS.volumeLocks.TryAcquire(lockingVolumeID); !acquired {
579-
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID), diskToPublish
579+
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID), diskType
580580
}
581581
defer gceCS.volumeLocks.Release(lockingVolumeID)
582582
disk, err := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)
583-
diskToPublish = metrics.GetDiskType(disk)
583+
diskType = metrics.GetDiskType(disk)
584584
if err != nil {
585585
if gce.IsGCENotFoundError(err) {
586-
return nil, status.Errorf(codes.NotFound, "Could not find disk %v: %v", volKey.String(), err.Error()), diskToPublish
586+
return nil, status.Errorf(codes.NotFound, "Could not find disk %v: %v", volKey.String(), err.Error()), diskType
587587
}
588-
return nil, status.Errorf(codes.Internal, "Failed to getDisk: %v", err.Error()), diskToPublish
588+
return nil, status.Errorf(codes.Internal, "Failed to getDisk: %v", err.Error()), diskType
589589
}
590590
instanceZone, instanceName, err := common.NodeIDToZoneAndName(nodeID)
591591
if err != nil {
592-
return nil, status.Errorf(codes.NotFound, "could not split nodeID: %v", err.Error()), diskToPublish
592+
return nil, status.Errorf(codes.NotFound, "could not split nodeID: %v", err.Error()), diskType
593593
}
594594
instance, err := gceCS.CloudProvider.GetInstanceOrError(ctx, instanceZone, instanceName)
595595
if err != nil {
596596
if gce.IsGCENotFoundError(err) {
597-
return nil, status.Errorf(codes.NotFound, "Could not find instance %v: %v", nodeID, err.Error()), diskToPublish
597+
return nil, status.Errorf(codes.NotFound, "Could not find instance %v: %v", nodeID, err.Error()), diskType
598598
}
599-
return nil, status.Errorf(codes.Internal, "Failed to get instance: %v", err.Error()), diskToPublish
599+
return nil, status.Errorf(codes.Internal, "Failed to get instance: %v", err.Error()), diskType
600600
}
601601

602602
readWrite := "READ_WRITE"
@@ -606,21 +606,21 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
606606

607607
deviceName, err := common.GetDeviceName(volKey)
608608
if err != nil {
609-
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error()), diskToPublish
609+
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error()), diskType
610610
}
611611

612612
attached, err := diskIsAttachedAndCompatible(deviceName, instance, volumeCapability, readWrite)
613613
if err != nil {
614-
return nil, status.Errorf(codes.AlreadyExists, "Disk %v already published to node %v but incompatible: %v", volKey.Name, nodeID, err.Error()), diskToPublish
614+
return nil, status.Errorf(codes.AlreadyExists, "Disk %v already published to node %v but incompatible: %v", volKey.Name, nodeID, err.Error()), diskType
615615
}
616616
if attached {
617617
// Volume is attached to node. Success!
618618
klog.V(4).Infof("ControllerPublishVolume succeeded for disk %v to instance %v, already attached.", volKey, nodeID)
619-
return pubVolResp, nil, diskToPublish
619+
return pubVolResp, nil, diskType
620620
}
621621
instanceZone, instanceName, err = common.NodeIDToZoneAndName(nodeID)
622622
if err != nil {
623-
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error()), diskToPublish
623+
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error()), diskType
624624
}
625625
err = gceCS.CloudProvider.AttachDisk(ctx, project, volKey, readWrite, attachableDiskTypePersistent, instanceZone, instanceName)
626626
if err != nil {
@@ -629,18 +629,18 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
629629
// If we encountered an UnsupportedDiskError, rewrite the error message to be more user friendly.
630630
// The error message from GCE is phrased around disk create on VM creation, not runtime attach.
631631
machineType := parseMachineType(instance.MachineType)
632-
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
632+
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
633633
}
634-
return nil, status.Errorf(codes.Internal, "Failed to Attach: %v", err.Error()), diskToPublish
634+
return nil, status.Errorf(codes.Internal, "Failed to Attach: %v", err.Error()), diskType
635635
}
636636

637637
err = gceCS.CloudProvider.WaitForAttach(ctx, project, volKey, instanceZone, instanceName)
638638
if err != nil {
639-
return nil, status.Errorf(codes.Internal, "Errored during WaitForAttach: %v", err.Error()), diskToPublish
639+
return nil, status.Errorf(codes.Internal, "Errored during WaitForAttach: %v", err.Error()), diskType
640640
}
641641

642642
klog.V(4).Infof("ControllerPublishVolume succeeded for disk %v to instance %v", volKey, nodeID)
643-
return pubVolResp, nil, diskToPublish
643+
return pubVolResp, nil, diskType
644644
}
645645

646646
func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
@@ -651,18 +651,17 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
651651
gceCS.Metrics.RecordOperationErrorMetrics("ControllerUnpublishVolume", err, diskTypeForMetric)
652652
}
653653
}()
654-
project, volKey, err := gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
654+
_, _, err = gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
655655
if err != nil {
656656
return nil, err
657657
}
658+
err = status.Errorf(codes.InvalidArgument, "error message")
658659
// Only valid requests will be queued
659660
backoffId := gceCS.errorBackoff.backoffId(req.NodeId, req.VolumeId)
660661
if gceCS.errorBackoff.blocking(backoffId) {
661662
return nil, status.Errorf(codes.Unavailable, "ControllerUnpublish not permitted on node %q due to backoff condition", req.NodeId)
662663
}
663-
diskToUnpublish, _ := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)
664-
diskTypeForMetric = metrics.GetDiskType(diskToUnpublish)
665-
resp, err := gceCS.executeControllerUnpublishVolume(ctx, req)
664+
resp, err, diskTypeForMetric := gceCS.executeControllerUnpublishVolume(ctx, req)
666665
if err != nil {
667666
klog.Infof("For node %s adding backoff due to error for volume %s", req.NodeId, req.VolumeId)
668667
gceCS.errorBackoff.next(backoffId)
@@ -692,64 +691,67 @@ func (gceCS *GCEControllerServer) validateControllerUnpublishVolumeRequest(ctx c
692691
return project, volKey, nil
693692
}
694693

695-
func (gceCS *GCEControllerServer) executeControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
694+
func (gceCS *GCEControllerServer) executeControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error, string) {
695+
var diskType string
696696
project, volKey, err := gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
697697

698698
if err != nil {
699-
return nil, err
699+
return nil, err, diskType
700700
}
701701

702702
volumeID := req.GetVolumeId()
703703
nodeID := req.GetNodeId()
704704
project, volKey, err = gceCS.CloudProvider.RepairUnderspecifiedVolumeKey(ctx, project, volKey)
705705
if err != nil {
706706
if gce.IsGCENotFoundError(err) {
707-
return nil, status.Errorf(codes.NotFound, "ControllerUnpublishVolume could not find volume with ID %v: %v", volumeID, err.Error())
707+
klog.Warningf("Treating volume %v as unpublished because it could not be found", volumeID)
708+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
708709
}
709-
return nil, common.LoggedError("ControllerUnpublishVolume error repairing underspecified volume key: ", err)
710+
return nil, common.LoggedError("ControllerUnpublishVolume error repairing underspecified volume key: ", err), diskType
710711
}
711712

712713
// Acquires the lock for the volume on that node only, because we need to support the ability
713714
// to unpublish the same volume from different nodes concurrently
714715
lockingVolumeID := fmt.Sprintf("%s/%s", nodeID, volumeID)
715716
if acquired := gceCS.volumeLocks.TryAcquire(lockingVolumeID); !acquired {
716-
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID)
717+
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID), diskType
717718
}
718719
defer gceCS.volumeLocks.Release(lockingVolumeID)
719-
720+
diskToUnpublish, _ := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)
721+
diskType = metrics.GetDiskType(diskToUnpublish)
720722
instanceZone, instanceName, err := common.NodeIDToZoneAndName(nodeID)
721723
if err != nil {
722-
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error())
724+
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error()), diskType
723725
}
724726
instance, err := gceCS.CloudProvider.GetInstanceOrError(ctx, instanceZone, instanceName)
725727
if err != nil {
726728
if gce.IsGCENotFoundError(err) {
727729
// Node not existing on GCE means that disk has been detached
728730
klog.Warningf("Treating volume %v as unpublished because node %v could not be found", volKey.String(), instanceName)
729-
return &csi.ControllerUnpublishVolumeResponse{}, nil
731+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
730732
}
731-
return nil, status.Errorf(codes.Internal, "error getting instance: %v", err.Error())
733+
return nil, status.Errorf(codes.Internal, "error getting instance: %v", err.Error()), diskType
732734
}
733735

734736
deviceName, err := common.GetDeviceName(volKey)
735737
if err != nil {
736-
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error())
738+
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error()), diskType
737739
}
738740

739741
attached := diskIsAttached(deviceName, instance)
740742

741743
if !attached {
742744
// Volume is not attached to node. Success!
743745
klog.V(4).Infof("ControllerUnpublishVolume succeeded for disk %v from node %v. Already not attached.", volKey, nodeID)
744-
return &csi.ControllerUnpublishVolumeResponse{}, nil
746+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
745747
}
746748
err = gceCS.CloudProvider.DetachDisk(ctx, project, deviceName, instanceZone, instanceName)
747749
if err != nil {
748-
return nil, common.LoggedError("Failed to detach: ", err)
750+
return nil, common.LoggedError("Failed to detach: ", err), diskType
749751
}
750752

751753
klog.V(4).Infof("ControllerUnpublishVolume succeeded for disk %v from node %v", volKey, nodeID)
752-
return &csi.ControllerUnpublishVolumeResponse{}, nil
754+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
753755
}
754756

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

pkg/metrics/metrics.go

Lines changed: 0 additions & 2 deletions
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)