Skip to content

Commit

Permalink
Only bail to AttachVolume if volume is detached
Browse files Browse the repository at this point in the history
Signed-off-by: Connor Catlett <[email protected]>
  • Loading branch information
ConnorJC3 committed Oct 14, 2024
1 parent 38f0b20 commit 0ef0e76
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ func (c *cloud) WaitForAttachmentState(ctx context.Context, volumeID, expectedSt
// if we expected volume to be attached and it was reported as already attached via DescribeInstance call
// but DescribeVolume told us volume is detached, we will short-circuit this long wait loop and return error
// so as AttachDisk can be retried without waiting for 20 minutes.
if (expectedState == volumeAttachedState) && alreadyAssigned && (attachmentState != expectedState) {
if (expectedState == volumeAttachedState) && alreadyAssigned && (attachmentState == volumeDetachedState) {
request := &ec2.AttachVolumeInput{
Device: aws.String(expectedDevice),
InstanceId: aws.String(expectedInstance),
Expand Down
16 changes: 14 additions & 2 deletions pkg/cloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3301,14 +3301,23 @@ func TestWaitForAttachmentState(t *testing.T) {
expectError: true,
},
{
name: "failure: already assigned but wrong state",
name: "failure: already assigned but detached state",
volumeID: "vol-test-1234",
expectedState: volumeAttachedState,
expectedInstance: "1234",
expectedDevice: defaultPath,
alreadyAssigned: true,
expectError: true,
},
{
name: "failure: already assigned but attaching state",
volumeID: "vol-test-1234",
expectedState: volumeAttachedState,
expectedInstance: "1234",
expectedDevice: defaultPath,
alreadyAssigned: true,
expectError: false,
},
{
name: "failure: multiple attachments with Multi-Attach disabled",
volumeID: "vol-test-1234",
Expand Down Expand Up @@ -3353,9 +3362,12 @@ func TestWaitForAttachmentState(t *testing.T) {
switch tc.name {
case "success: detached":
mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(&ec2.DescribeVolumesOutput{Volumes: []types.Volume{detachedVol}}, nil).AnyTimes()
case "failure: already assigned but wrong state":
case "failure: already assigned but detached state":
mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(&ec2.DescribeVolumesOutput{Volumes: []types.Volume{detachedVol}}, nil)
mockEC2.EXPECT().AttachVolume(gomock.Any(), gomock.Any()).Return(nil, nil)
case "failure: already assigned but attaching state":
mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(&ec2.DescribeVolumesOutput{Volumes: []types.Volume{attachingVol}}, nil)
mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(&ec2.DescribeVolumesOutput{Volumes: []types.Volume{attachedVol}}, nil)
case "success: disk not found, assumed detached", "failure: disk not found, expected attached":
mockEC2.EXPECT().DescribeVolumes(gomock.Any(), gomock.Any()).Return(nil, &smithy.GenericAPIError{
Code: "InvalidVolume.NotFound",
Expand Down

0 comments on commit 0ef0e76

Please sign in to comment.