Skip to content

Commit 2bc84b3

Browse files
committed
feat: report RESOURCE_EXHAUSTED when max volume
Also fix already mount error code
1 parent 5c734b9 commit 2bc84b3

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

internal/driver/controllerserver_helper.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010

1111
"github.com/container-storage-interface/spec/lib/go/csi"
1212
"github.com/linode/linodego"
13-
"google.golang.org/grpc/codes"
14-
"google.golang.org/grpc/status"
1513

1614
linodevolumes "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-volumes"
1715
"github.com/linode/linode-blockstorage-csi-driver/pkg/logger"
@@ -667,13 +665,17 @@ func (cs *ControllerServer) attachVolume(ctx context.Context, volumeID, linodeID
667665
PersistAcrossBoots: &persist,
668666
})
669667
if err != nil {
670-
code := codes.Internal // Default error code is Internal.
671-
// Check if the error indicates that the volume is already attached.
668+
// https://github.com/container-storage-interface/spec/blob/master/spec.md#controllerpublishvolume-errors
672669
var apiErr *linodego.Error
673-
if errors.As(err, &apiErr) && strings.Contains(apiErr.Message, "is already attached") {
674-
code = codes.Unavailable // Allow a retry if the volume is already attached: race condition can occur here
670+
if errors.As(err, &apiErr) {
671+
switch {
672+
case strings.Contains(apiErr.Message, "is already attached"):
673+
return errAlreadyAttached
674+
case strings.Contains(apiErr.Message, "Maximum number of block storage volumes are attached to this Linode"):
675+
return errMaxAttachments
676+
}
675677
}
676-
return status.Errorf(code, "attach volume: %v", err)
678+
return errInternal("attach volume %d to Linode %d: %v", volumeID, linodeID, err)
677679
}
678680
return nil // Return nil if the volume is successfully attached.
679681
}

internal/driver/errors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ var (
3434
// attachments allowed for the instance, call errMaxVolumeAttachments.
3535
errMaxAttachments = status.Error(codes.ResourceExhausted, "max number of volumes already attached to instance")
3636

37+
// errAlreadyAttached is used to indicate that a volume is already attached
38+
// to a Linode instance.
39+
errAlreadyAttached = status.Error(codes.FailedPrecondition, "volume is already attached")
40+
3741
// errResizeDown indicates a request would result in a volume being resized
3842
// to be smaller than it currently is.
3943
//
@@ -61,10 +65,6 @@ func errRegionMismatch(gotRegion, wantRegion string) error {
6165
return status.Errorf(codes.InvalidArgument, "source volume is in region %q, needs to be in region %q", gotRegion, wantRegion)
6266
}
6367

64-
func errMaxVolumeAttachments(numAttachments int) error {
65-
return status.Errorf(codes.ResourceExhausted, "max number of volumes (%d) already attached to instance", numAttachments)
66-
}
67-
6868
func errInstanceNotFound(linodeID int) error {
6969
return status.Errorf(codes.NotFound, "linode instance %d not found", linodeID)
7070
}

0 commit comments

Comments
 (0)