UPSTREAM: 45049: Log an EBS vol's instance when attaching fails because VolumeInUse
@@ -1471,6 +1471,28 @@ func (c *Cloud) getAwsInstance(nodeName types.NodeName) (*awsInstance, error) {
return awsInstance, nil
}
+// wrapAttachError wraps the error returned by an AttachVolume request with
+// additional information, if needed and possible.
+func wrapAttachError(err error, disk *awsDisk, instance string) error {
+ if awsError, ok := err.(awserr.Error); ok {
+ if awsError.Code() == "VolumeInUse" {
+ info, err := disk.describeVolume()
+ if err != nil {
+ glog.Errorf("Error describing volume %q: %v", disk.awsID, err)
+ } else {
+ for _, a := range info.Attachments {
+ if disk.awsID != awsVolumeID(aws.StringValue(a.VolumeId)) {
+ glog.Warningf("Expected to get attachment info of volume %q but instead got info of %q", disk.awsID, aws.StringValue(a.VolumeId))
+ } else if aws.StringValue(a.State) == "attached" {
+ return fmt.Errorf("Error attaching EBS volume %q to instance %q: %v. The volume is currently attached to instance %q", disk.awsID, instance, awsError, aws.StringValue(a.InstanceId))
+ }
+ }
+ }
+ }
+ }
+ return fmt.Errorf("Error attaching EBS volume %q to instance %q: %v", disk.awsID, instance, err)
+}
+
// AttachDisk implements Volumes.AttachDisk
func (c *Cloud) AttachDisk(diskName KubernetesVolumeID, nodeName types.NodeName, readOnly bool) (string, error) {
disk, err := newAWSDisk(c, diskName)
@@ -1527,7 +1549,7 @@ func (c *Cloud) AttachDisk(diskName KubernetesVolumeID, nodeName types.NodeName,
if err != nil {
attachEnded = true
// TODO: Check if the volume was concurrently attached?
- return "", fmt.Errorf("Error attaching EBS volume %q to instance %q: %v", disk.awsID, awsInstance.awsID, err)
+ return "", wrapAttachError(err, disk, awsInstance.awsID)
}
if da, ok := c.deviceAllocators[awsInstance.nodeName]; ok {
da.Deprioritize(mountDevice)
