From f3cc3031f4358e181f5aa604834fb6ba68b25198 Mon Sep 17 00:00:00 2001 From: torredil Date: Thu, 11 Apr 2024 18:49:10 +0000 Subject: [PATCH] Add explicit AttachVolume call in WaitForAttachmentState Signed-off-by: torredil --- pkg/cloud/cloud.go | 9 +++++++++ pkg/cloud/cloud_test.go | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index 6b66987c21..5b3c2854ff 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -1024,6 +1024,15 @@ func (c *cloud) WaitForAttachmentState(ctx context.Context, volumeID, expectedSt // 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) { + request := &ec2.AttachVolumeInput{ + Device: aws.String(expectedDevice), + InstanceId: aws.String(expectedInstance), + VolumeId: aws.String(volumeID), + } + _, err := c.ec2.AttachVolume(ctx, request) + if err != nil { + return false, fmt.Errorf("WaitForAttachmentState AttachVolume error, expected device but be attached but was %s, volumeID=%q, instanceID=%q, Device=%q, err=%w", attachmentState, volumeID, expectedInstance, expectedDevice, err) + } return false, fmt.Errorf("attachment of disk %q failed, expected device to be attached but was %s", volumeID, attachmentState) } diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index 707521f5c3..125bd31a9e 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -3029,8 +3029,11 @@ func TestWaitForAttachmentState(t *testing.T) { defer cancel() switch tc.name { - case "success: detached", "failure: already assigned but wrong state": + 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": + 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 "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",