Skip to content

Commit 13d7647

Browse files
Sneha-atsunnylovestiramisu
authored andcommitted
refactoring for GetDisk call
1 parent fd15fe1 commit 13d7647

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

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

+38-36
Original file line numberDiff line numberDiff line change
@@ -541,10 +541,10 @@ func parseMachineType(machineTypeUrl string) string {
541541
}
542542

543543
func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error, string) {
544-
diskToPublish := ""
544+
diskType := ""
545545
project, volKey, err := gceCS.validateControllerPublishVolumeRequest(ctx, req)
546546
if err != nil {
547-
return nil, err, diskToPublish
547+
return nil, err, diskType
548548
}
549549

550550
volumeID := req.GetVolumeId()
@@ -559,36 +559,36 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
559559
project, volKey, err = gceCS.CloudProvider.RepairUnderspecifiedVolumeKey(ctx, project, volKey)
560560
if err != nil {
561561
if gce.IsGCENotFoundError(err) {
562-
return nil, status.Errorf(codes.NotFound, "ControllerPublishVolume could not find volume with ID %v: %v", volumeID, err.Error()), diskToPublish
562+
return nil, status.Errorf(codes.NotFound, "ControllerPublishVolume could not find volume with ID %v: %v", volumeID, err.Error()), diskType
563563
}
564-
return nil, common.LoggedError("ControllerPublishVolume error repairing underspecified volume key: ", err), diskToPublish
564+
return nil, common.LoggedError("ControllerPublishVolume error repairing underspecified volume key: ", err), diskType
565565
}
566566

567567
// Acquires the lock for the volume on that node only, because we need to support the ability
568568
// to publish the same volume onto different nodes concurrently
569569
lockingVolumeID := fmt.Sprintf("%s/%s", nodeID, volumeID)
570570
if acquired := gceCS.volumeLocks.TryAcquire(lockingVolumeID); !acquired {
571-
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID), diskToPublish
571+
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID), diskType
572572
}
573573
defer gceCS.volumeLocks.Release(lockingVolumeID)
574574
disk, err := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)
575-
diskToPublish = metrics.GetDiskType(disk)
575+
diskType = metrics.GetDiskType(disk)
576576
if err != nil {
577577
if gce.IsGCENotFoundError(err) {
578-
return nil, status.Errorf(codes.NotFound, "Could not find disk %v: %v", volKey.String(), err.Error()), diskToPublish
578+
return nil, status.Errorf(codes.NotFound, "Could not find disk %v: %v", volKey.String(), err.Error()), diskType
579579
}
580-
return nil, status.Errorf(codes.Internal, "Failed to getDisk: %v", err.Error()), diskToPublish
580+
return nil, status.Errorf(codes.Internal, "Failed to getDisk: %v", err.Error()), diskType
581581
}
582582
instanceZone, instanceName, err := common.NodeIDToZoneAndName(nodeID)
583583
if err != nil {
584-
return nil, status.Errorf(codes.NotFound, "could not split nodeID: %v", err.Error()), diskToPublish
584+
return nil, status.Errorf(codes.NotFound, "could not split nodeID: %v", err.Error()), diskType
585585
}
586586
instance, err := gceCS.CloudProvider.GetInstanceOrError(ctx, instanceZone, instanceName)
587587
if err != nil {
588588
if gce.IsGCENotFoundError(err) {
589-
return nil, status.Errorf(codes.NotFound, "Could not find instance %v: %v", nodeID, err.Error()), diskToPublish
589+
return nil, status.Errorf(codes.NotFound, "Could not find instance %v: %v", nodeID, err.Error()), diskType
590590
}
591-
return nil, status.Errorf(codes.Internal, "Failed to get instance: %v", err.Error()), diskToPublish
591+
return nil, status.Errorf(codes.Internal, "Failed to get instance: %v", err.Error()), diskType
592592
}
593593

594594
readWrite := "READ_WRITE"
@@ -598,21 +598,21 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
598598

599599
deviceName, err := common.GetDeviceName(volKey)
600600
if err != nil {
601-
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error()), diskToPublish
601+
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error()), diskType
602602
}
603603

604604
attached, err := diskIsAttachedAndCompatible(deviceName, instance, volumeCapability, readWrite)
605605
if err != nil {
606-
return nil, status.Errorf(codes.AlreadyExists, "Disk %v already published to node %v but incompatible: %v", volKey.Name, nodeID, err.Error()), diskToPublish
606+
return nil, status.Errorf(codes.AlreadyExists, "Disk %v already published to node %v but incompatible: %v", volKey.Name, nodeID, err.Error()), diskType
607607
}
608608
if attached {
609609
// Volume is attached to node. Success!
610610
klog.V(4).Infof("ControllerPublishVolume succeeded for disk %v to instance %v, already attached.", volKey, nodeID)
611-
return pubVolResp, nil, diskToPublish
611+
return pubVolResp, nil, diskType
612612
}
613613
instanceZone, instanceName, err = common.NodeIDToZoneAndName(nodeID)
614614
if err != nil {
615-
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error()), diskToPublish
615+
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error()), diskType
616616
}
617617
err = gceCS.CloudProvider.AttachDisk(ctx, project, volKey, readWrite, attachableDiskTypePersistent, instanceZone, instanceName)
618618
if err != nil {
@@ -621,18 +621,18 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
621621
// If we encountered an UnsupportedDiskError, rewrite the error message to be more user friendly.
622622
// The error message from GCE is phrased around disk create on VM creation, not runtime attach.
623623
machineType := parseMachineType(instance.MachineType)
624-
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
624+
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
625625
}
626-
return nil, status.Errorf(codes.Internal, "Failed to Attach: %v", err.Error()), diskToPublish
626+
return nil, status.Errorf(codes.Internal, "Failed to Attach: %v", err.Error()), diskType
627627
}
628628

629629
err = gceCS.CloudProvider.WaitForAttach(ctx, project, volKey, instanceZone, instanceName)
630630
if err != nil {
631-
return nil, status.Errorf(codes.Internal, "Errored during WaitForAttach: %v", err.Error()), diskToPublish
631+
return nil, status.Errorf(codes.Internal, "Errored during WaitForAttach: %v", err.Error()), diskType
632632
}
633633

634634
klog.V(4).Infof("ControllerPublishVolume succeeded for disk %v to instance %v", volKey, nodeID)
635-
return pubVolResp, nil, diskToPublish
635+
return pubVolResp, nil, diskType
636636
}
637637

638638
func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
@@ -643,18 +643,17 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
643643
gceCS.Metrics.RecordOperationErrorMetrics("ControllerUnpublishVolume", err, diskTypeForMetric)
644644
}
645645
}()
646-
project, volKey, err := gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
646+
_, _, err = gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
647647
if err != nil {
648648
return nil, err
649649
}
650+
err = status.Errorf(codes.InvalidArgument, "error message")
650651
// Only valid requests will be queued
651652
backoffId := gceCS.errorBackoff.backoffId(req.NodeId, req.VolumeId)
652653
if gceCS.errorBackoff.blocking(backoffId) {
653654
return nil, status.Errorf(codes.Unavailable, "ControllerUnpublish not permitted on node %q due to backoff condition", req.NodeId)
654655
}
655-
diskToUnpublish, _ := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)
656-
diskTypeForMetric = metrics.GetDiskType(diskToUnpublish)
657-
resp, err := gceCS.executeControllerUnpublishVolume(ctx, req)
656+
resp, err, diskTypeForMetric := gceCS.executeControllerUnpublishVolume(ctx, req)
658657
if err != nil {
659658
klog.Infof("For node %s adding backoff due to error for volume %s", req.NodeId, req.VolumeId)
660659
gceCS.errorBackoff.next(backoffId)
@@ -684,64 +683,67 @@ func (gceCS *GCEControllerServer) validateControllerUnpublishVolumeRequest(ctx c
684683
return project, volKey, nil
685684
}
686685

687-
func (gceCS *GCEControllerServer) executeControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
686+
func (gceCS *GCEControllerServer) executeControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error, string) {
687+
var diskType string
688688
project, volKey, err := gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
689689

690690
if err != nil {
691-
return nil, err
691+
return nil, err, diskType
692692
}
693693

694694
volumeID := req.GetVolumeId()
695695
nodeID := req.GetNodeId()
696696
project, volKey, err = gceCS.CloudProvider.RepairUnderspecifiedVolumeKey(ctx, project, volKey)
697697
if err != nil {
698698
if gce.IsGCENotFoundError(err) {
699-
return nil, status.Errorf(codes.NotFound, "ControllerUnpublishVolume could not find volume with ID %v: %v", volumeID, err.Error())
699+
klog.Warningf("Treating volume %v as unpublished because it could not be found", volumeID)
700+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
700701
}
701-
return nil, common.LoggedError("ControllerUnpublishVolume error repairing underspecified volume key: ", err)
702+
return nil, common.LoggedError("ControllerUnpublishVolume error repairing underspecified volume key: ", err), diskType
702703
}
703704

704705
// Acquires the lock for the volume on that node only, because we need to support the ability
705706
// to unpublish the same volume from different nodes concurrently
706707
lockingVolumeID := fmt.Sprintf("%s/%s", nodeID, volumeID)
707708
if acquired := gceCS.volumeLocks.TryAcquire(lockingVolumeID); !acquired {
708-
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID)
709+
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID), diskType
709710
}
710711
defer gceCS.volumeLocks.Release(lockingVolumeID)
711-
712+
diskToUnpublish, _ := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)
713+
diskType = metrics.GetDiskType(diskToUnpublish)
712714
instanceZone, instanceName, err := common.NodeIDToZoneAndName(nodeID)
713715
if err != nil {
714-
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error())
716+
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error()), diskType
715717
}
716718
instance, err := gceCS.CloudProvider.GetInstanceOrError(ctx, instanceZone, instanceName)
717719
if err != nil {
718720
if gce.IsGCENotFoundError(err) {
719721
// Node not existing on GCE means that disk has been detached
720722
klog.Warningf("Treating volume %v as unpublished because node %v could not be found", volKey.String(), instanceName)
721-
return &csi.ControllerUnpublishVolumeResponse{}, nil
723+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
722724
}
723-
return nil, status.Errorf(codes.Internal, "error getting instance: %v", err.Error())
725+
return nil, status.Errorf(codes.Internal, "error getting instance: %v", err.Error()), diskType
724726
}
725727

726728
deviceName, err := common.GetDeviceName(volKey)
727729
if err != nil {
728-
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error())
730+
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error()), diskType
729731
}
730732

731733
attached := diskIsAttached(deviceName, instance)
732734

733735
if !attached {
734736
// Volume is not attached to node. Success!
735737
klog.V(4).Infof("ControllerUnpublishVolume succeeded for disk %v from node %v. Already not attached.", volKey, nodeID)
736-
return &csi.ControllerUnpublishVolumeResponse{}, nil
738+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
737739
}
738740
err = gceCS.CloudProvider.DetachDisk(ctx, project, deviceName, instanceZone, instanceName)
739741
if err != nil {
740-
return nil, common.LoggedError("Failed to detach: ", err)
742+
return nil, common.LoggedError("Failed to detach: ", err), diskType
741743
}
742744

743745
klog.V(4).Infof("ControllerUnpublishVolume succeeded for disk %v from node %v", volKey, nodeID)
744-
return &csi.ControllerUnpublishVolumeResponse{}, nil
746+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
745747
}
746748

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

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)