Skip to content

Commit 044b315

Browse files
committed
All check for instanceID
In case DescribeVolumes returns stale attachment and the volume was previously attached to a different node.
1 parent 4dd6fd4 commit 044b315

File tree

1 file changed

+20
-10
lines changed
  • staging/src/k8s.io/legacy-cloud-providers/aws

1 file changed

+20
-10
lines changed

staging/src/k8s.io/legacy-cloud-providers/aws/aws.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,7 +2105,7 @@ func (c *Cloud) applyUnSchedulableTaint(nodeName types.NodeName, reason string)
21052105

21062106
// waitForAttachmentStatus polls until the attachment status is the expected value
21072107
// On success, it returns the last attachment state.
2108-
func (d *awsDisk) waitForAttachmentStatus(status string, expectedDevice string) (*ec2.VolumeAttachment, error) {
2108+
func (d *awsDisk) waitForAttachmentStatus(status string, expectedInstance, expectedDevice string) (*ec2.VolumeAttachment, error) {
21092109
backoff := wait.Backoff{
21102110
Duration: volumeAttachmentStatusPollDelay,
21112111
Factor: volumeAttachmentStatusFactor,
@@ -2176,14 +2176,24 @@ func (d *awsDisk) waitForAttachmentStatus(status string, expectedDevice string)
21762176
if attachmentStatus == "" {
21772177
attachmentStatus = "detached"
21782178
}
2179-
if attachment != nil && expectedDevice != "" {
2179+
if attachment != nil {
2180+
// AWS eventual consistency can go back in time.
2181+
// For example, we're waiting for a volume to be attached as /dev/xvdba, but AWS can tell us it's
2182+
// attached as /dev/xvdbb, where it was attached before and it was already detached.
2183+
// Retry couple of times, hoping AWS starts reporting the right status.
21802184
device := aws.StringValue(attachment.Device)
2181-
if device != "" && device != expectedDevice {
2182-
// AWS eventual consistency can go back in time.
2183-
// For example, we're waiting for a volume to be attached as /dev/xvdba, but AWS can tell us it's
2184-
// attached as /dev/xvdbb, where it was attached before and it was already detached.
2185-
// Retry couple of times, hoping AWS starts reporting the right status.
2186-
klog.Warningf("Expected device %s %s, but found device %s %s", expectedDevice, status, device, attachmentStatus)
2185+
if expectedDevice != "" && device != "" && device != expectedDevice {
2186+
klog.Warningf("Expected device %s %s for volume %s, but found device %s %s", expectedDevice, status, d.name, device, attachmentStatus)
2187+
errorCount++
2188+
if errorCount > volumeAttachmentStatusConsecutiveErrorLimit {
2189+
// report the error
2190+
return false, fmt.Errorf("attachment of disk %q failed: requested device %q but found %q", d.name, expectedDevice, device)
2191+
}
2192+
return false, nil
2193+
}
2194+
instanceID := aws.StringValue(attachment.InstanceId)
2195+
if expectedInstance != "" && instanceID != "" && instanceID != expectedInstance {
2196+
klog.Warningf("Expected instance %s/%s for volume %s, but found instance %s/%s", expectedInstance, status, d.name, instanceID, attachmentStatus)
21872197
errorCount++
21882198
if errorCount > volumeAttachmentStatusConsecutiveErrorLimit {
21892199
// report the error
@@ -2338,7 +2348,7 @@ func (c *Cloud) AttachDisk(diskName KubernetesVolumeID, nodeName types.NodeName)
23382348
klog.V(2).Infof("AttachVolume volume=%q instance=%q request returned %v", disk.awsID, awsInstance.awsID, attachResponse)
23392349
}
23402350

2341-
attachment, err := disk.waitForAttachmentStatus("attached", ec2Device)
2351+
attachment, err := disk.waitForAttachmentStatus("attached", awsInstance.awsID, ec2Device)
23422352

23432353
if err != nil {
23442354
if err == wait.ErrWaitTimeout {
@@ -2416,7 +2426,7 @@ func (c *Cloud) DetachDisk(diskName KubernetesVolumeID, nodeName types.NodeName)
24162426
return "", errors.New("no response from DetachVolume")
24172427
}
24182428

2419-
attachment, err := diskInfo.disk.waitForAttachmentStatus("detached", "")
2429+
attachment, err := diskInfo.disk.waitForAttachmentStatus("detached", awsInstance.awsID, "")
24202430
if err != nil {
24212431
return "", err
24222432
}

0 commit comments

Comments
 (0)