Skip to content

Commit 0b13b82

Browse files
committed
refactoring for GetDisk call
1 parent 13133cc commit 0b13b82

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

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

+37-36
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,10 @@ func parseMachineType(machineTypeUrl string) string {
542542
}
543543

544544
func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Context, req *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error, string) {
545-
diskToPublish := ""
545+
diskType := ""
546546
project, volKey, err := gceCS.validateControllerPublishVolumeRequest(ctx, req)
547547
if err != nil {
548-
return nil, err, diskToPublish
548+
return nil, err, diskType
549549
}
550550

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

568568
// Acquires the lock for the volume on that node only, because we need to support the ability
569569
// to publish the same volume onto different nodes concurrently
570570
lockingVolumeID := fmt.Sprintf("%s/%s", nodeID, volumeID)
571571
if acquired := gceCS.volumeLocks.TryAcquire(lockingVolumeID); !acquired {
572-
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID), diskToPublish
572+
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID), diskType
573573
}
574574
defer gceCS.volumeLocks.Release(lockingVolumeID)
575575
disk, err := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)
576-
diskToPublish = metrics.GetDiskType(disk)
576+
diskType = metrics.GetDiskType(disk)
577577
if err != nil {
578578
if gce.IsGCENotFoundError(err) {
579-
return nil, status.Errorf(codes.NotFound, "Could not find disk %v: %v", volKey.String(), err.Error()), diskToPublish
579+
return nil, status.Errorf(codes.NotFound, "Could not find disk %v: %v", volKey.String(), err.Error()), diskType
580580
}
581-
return nil, status.Errorf(codes.Internal, "Failed to getDisk: %v", err.Error()), diskToPublish
581+
return nil, status.Errorf(codes.Internal, "Failed to getDisk: %v", err.Error()), diskType
582582
}
583583
instanceZone, instanceName, err := common.NodeIDToZoneAndName(nodeID)
584584
if err != nil {
585-
return nil, status.Errorf(codes.NotFound, "could not split nodeID: %v", err.Error()), diskToPublish
585+
return nil, status.Errorf(codes.NotFound, "could not split nodeID: %v", err.Error()), diskType
586586
}
587587
instance, err := gceCS.CloudProvider.GetInstanceOrError(ctx, instanceZone, instanceName)
588588
if err != nil {
589589
if gce.IsGCENotFoundError(err) {
590-
return nil, status.Errorf(codes.NotFound, "Could not find instance %v: %v", nodeID, err.Error()), diskToPublish
590+
return nil, status.Errorf(codes.NotFound, "Could not find instance %v: %v", nodeID, err.Error()), diskType
591591
}
592-
return nil, status.Errorf(codes.Internal, "Failed to get instance: %v", err.Error()), diskToPublish
592+
return nil, status.Errorf(codes.Internal, "Failed to get instance: %v", err.Error()), diskType
593593
}
594594

595595
readWrite := "READ_WRITE"
@@ -599,21 +599,21 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
599599

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

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

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

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

639639
func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context, req *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
@@ -644,18 +644,17 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
644644
gceCS.Metrics.RecordOperationErrorMetrics("ControllerUnpublishVolume", err, diskTypeForMetric)
645645
}
646646
}()
647-
project, volKey, err := gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
647+
_, _, err = gceCS.validateControllerUnpublishVolumeRequest(ctx, req)
648648
if err != nil {
649649
return nil, err
650650
}
651+
err = status.Errorf(codes.InvalidArgument, "error message")
651652
// Only valid requests will be queued
652653
backoffId := gceCS.errorBackoff.backoffId(req.NodeId, req.VolumeId)
653654
if gceCS.errorBackoff.blocking(backoffId) {
654655
return nil, status.Errorf(codes.Unavailable, "ControllerUnpublish not permitted on node %q due to backoff condition", req.NodeId)
655656
}
656-
diskToUnpublish, _ := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)
657-
diskTypeForMetric = metrics.GetDiskType(diskToUnpublish)
658-
resp, err := gceCS.executeControllerUnpublishVolume(ctx, req)
657+
resp, err, diskTypeForMetric := gceCS.executeControllerUnpublishVolume(ctx, req)
659658
if err != nil {
660659
klog.Infof("For node %s adding backoff due to error for volume %s", req.NodeId, req.VolumeId)
661660
gceCS.errorBackoff.next(backoffId)
@@ -685,11 +684,12 @@ func (gceCS *GCEControllerServer) validateControllerUnpublishVolumeRequest(ctx c
685684
return project, volKey, nil
686685
}
687686

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

691691
if err != nil {
692-
return nil, err
692+
return nil, err, diskType
693693
}
694694

695695
volumeID := req.GetVolumeId()
@@ -698,52 +698,53 @@ func (gceCS *GCEControllerServer) executeControllerUnpublishVolume(ctx context.C
698698
if err != nil {
699699
if gce.IsGCENotFoundError(err) {
700700
klog.Warningf("Treating volume %v as unpublished because it could not be found", volumeID)
701-
return &csi.ControllerUnpublishVolumeResponse{}, nil
701+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
702702
}
703-
return nil, common.LoggedError("ControllerUnpublishVolume error repairing underspecified volume key: ", err)
703+
return nil, common.LoggedError("ControllerUnpublishVolume error repairing underspecified volume key: ", err), diskType
704704
}
705705

706706
// Acquires the lock for the volume on that node only, because we need to support the ability
707707
// to unpublish the same volume from different nodes concurrently
708708
lockingVolumeID := fmt.Sprintf("%s/%s", nodeID, volumeID)
709709
if acquired := gceCS.volumeLocks.TryAcquire(lockingVolumeID); !acquired {
710-
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID)
710+
return nil, status.Errorf(codes.Aborted, common.VolumeOperationAlreadyExistsFmt, lockingVolumeID), diskType
711711
}
712712
defer gceCS.volumeLocks.Release(lockingVolumeID)
713-
713+
diskToUnpublish, _ := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)
714+
diskType = metrics.GetDiskType(diskToUnpublish)
714715
instanceZone, instanceName, err := common.NodeIDToZoneAndName(nodeID)
715716
if err != nil {
716-
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error())
717+
return nil, status.Errorf(codes.InvalidArgument, "could not split nodeID: %v", err.Error()), diskType
717718
}
718719
instance, err := gceCS.CloudProvider.GetInstanceOrError(ctx, instanceZone, instanceName)
719720
if err != nil {
720721
if gce.IsGCENotFoundError(err) {
721722
// Node not existing on GCE means that disk has been detached
722723
klog.Warningf("Treating volume %v as unpublished because node %v could not be found", volKey.String(), instanceName)
723-
return &csi.ControllerUnpublishVolumeResponse{}, nil
724+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
724725
}
725-
return nil, status.Errorf(codes.Internal, "error getting instance: %v", err.Error())
726+
return nil, status.Errorf(codes.Internal, "error getting instance: %v", err.Error()), diskType
726727
}
727728

728729
deviceName, err := common.GetDeviceName(volKey)
729730
if err != nil {
730-
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error())
731+
return nil, status.Errorf(codes.Internal, "error getting device name: %v", err.Error()), diskType
731732
}
732733

733734
attached := diskIsAttached(deviceName, instance)
734735

735736
if !attached {
736737
// Volume is not attached to node. Success!
737738
klog.V(4).Infof("ControllerUnpublishVolume succeeded for disk %v from node %v. Already not attached.", volKey, nodeID)
738-
return &csi.ControllerUnpublishVolumeResponse{}, nil
739+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
739740
}
740741
err = gceCS.CloudProvider.DetachDisk(ctx, project, deviceName, instanceZone, instanceName)
741742
if err != nil {
742-
return nil, common.LoggedError("Failed to detach: ", err)
743+
return nil, common.LoggedError("Failed to detach: ", err), diskType
743744
}
744745

745746
klog.V(4).Infof("ControllerUnpublishVolume succeeded for disk %v from node %v", volKey, nodeID)
746-
return &csi.ControllerUnpublishVolumeResponse{}, nil
747+
return &csi.ControllerUnpublishVolumeResponse{}, nil, diskType
747748
}
748749

749750
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)