@@ -1171,16 +1171,16 @@ func (i *awsInstance) describeInstance() (*ec2.Instance, error) {
11711171// Gets the mountDevice already assigned to the volume, or assigns an unused mountDevice.
11721172// If the volume is already assigned, this will return the existing mountDevice with alreadyAttached=true.
11731173// Otherwise the mountDevice is assigned by finding the first available mountDevice, and it is returned with alreadyAttached=false.
1174- func (c * Cloud ) getMountDevice (i * awsInstance , volumeID awsVolumeID , assign bool ) (assigned mountDevice , alreadyAttached bool , err error ) {
1174+ func (c * Cloud ) getMountDevice (
1175+ i * awsInstance ,
1176+ info * ec2.Instance ,
1177+ volumeID awsVolumeID ,
1178+ assign bool ) (assigned mountDevice , alreadyAttached bool , err error ) {
11751179 instanceType := i .getInstanceType ()
11761180 if instanceType == nil {
11771181 return "" , false , fmt .Errorf ("could not get instance type for instance: %s" , i .awsID )
11781182 }
11791183
1180- info , err := i .describeInstance ()
1181- if err != nil {
1182- return "" , false , err
1183- }
11841184 deviceMappings := map [mountDevice ]awsVolumeID {}
11851185 for _ , blockDevice := range info .BlockDeviceMappings {
11861186 name := aws .StringValue (blockDevice .DeviceName )
@@ -1438,7 +1438,7 @@ func (c *Cloud) AttachDisk(diskName KubernetesVolumeID, nodeName types.NodeName,
14381438 return "" , err
14391439 }
14401440
1441- awsInstance , err := c .getAwsInstance (nodeName )
1441+ awsInstance , info , err := c .getFullInstance (nodeName )
14421442 if err != nil {
14431443 return "" , fmt .Errorf ("error finding instance %s: %v" , nodeName , err )
14441444 }
@@ -1465,7 +1465,7 @@ func (c *Cloud) AttachDisk(diskName KubernetesVolumeID, nodeName types.NodeName,
14651465 }
14661466 }()
14671467
1468- mountDevice , alreadyAttached , err = c .getMountDevice (awsInstance , disk .awsID , true )
1468+ mountDevice , alreadyAttached , err = c .getMountDevice (awsInstance , info , disk .awsID , true )
14691469 if err != nil {
14701470 return "" , err
14711471 }
@@ -1525,7 +1525,7 @@ func (c *Cloud) DetachDisk(diskName KubernetesVolumeID, nodeName types.NodeName)
15251525 return "" , err
15261526 }
15271527
1528- awsInstance , err := c .getAwsInstance (nodeName )
1528+ awsInstance , info , err := c .getFullInstance (nodeName )
15291529 if err != nil {
15301530 if err == cloudprovider .InstanceNotFound {
15311531 // If instance no longer exists, safe to assume volume is not attached.
@@ -1539,7 +1539,7 @@ func (c *Cloud) DetachDisk(diskName KubernetesVolumeID, nodeName types.NodeName)
15391539 return "" , err
15401540 }
15411541
1542- mountDevice , alreadyAttached , err := c .getMountDevice (awsInstance , disk .awsID , false )
1542+ mountDevice , alreadyAttached , err := c .getMountDevice (awsInstance , info , disk .awsID , false )
15431543 if err != nil {
15441544 return "" , err
15451545 }
@@ -1723,7 +1723,7 @@ func (c *Cloud) GetDiskPath(volumeName KubernetesVolumeID) (string, error) {
17231723
17241724// DiskIsAttached implements Volumes.DiskIsAttached
17251725func (c * Cloud ) DiskIsAttached (diskName KubernetesVolumeID , nodeName types.NodeName ) (bool , error ) {
1726- awsInstance , err := c .getAwsInstance (nodeName )
1726+ _ , instance , err := c .getFullInstance (nodeName )
17271727 if err != nil {
17281728 if err == cloudprovider .InstanceNotFound {
17291729 // If instance no longer exists, safe to assume volume is not attached.
@@ -1742,11 +1742,7 @@ func (c *Cloud) DiskIsAttached(diskName KubernetesVolumeID, nodeName types.NodeN
17421742 return false , fmt .Errorf ("error mapping volume spec %q to aws id: %v" , diskName , err )
17431743 }
17441744
1745- info , err := awsInstance .describeInstance ()
1746- if err != nil {
1747- return false , err
1748- }
1749- for _ , blockDevice := range info .BlockDeviceMappings {
1745+ for _ , blockDevice := range instance .BlockDeviceMappings {
17501746 id := awsVolumeID (aws .StringValue (blockDevice .Ebs .VolumeId ))
17511747 if id == diskID {
17521748 return true , nil
@@ -1766,7 +1762,7 @@ func (c *Cloud) DisksAreAttached(diskNames []KubernetesVolumeID, nodeName types.
17661762 idToDiskName [volumeID ] = diskName
17671763 attached [diskName ] = false
17681764 }
1769- awsInstance , err := c .getAwsInstance (nodeName )
1765+ _ , instance , err := c .getFullInstance (nodeName )
17701766 if err != nil {
17711767 if err == cloudprovider .InstanceNotFound {
17721768 // If instance no longer exists, safe to assume volume is not attached.
@@ -1779,11 +1775,7 @@ func (c *Cloud) DisksAreAttached(diskNames []KubernetesVolumeID, nodeName types.
17791775
17801776 return attached , err
17811777 }
1782- info , err := awsInstance .describeInstance ()
1783- if err != nil {
1784- return attached , err
1785- }
1786- for _ , blockDevice := range info .BlockDeviceMappings {
1778+ for _ , blockDevice := range instance .BlockDeviceMappings {
17871779 volumeID := awsVolumeID (aws .StringValue (blockDevice .Ebs .VolumeId ))
17881780 diskName , found := idToDiskName [volumeID ]
17891781 if found {
@@ -3124,7 +3116,7 @@ func (c *Cloud) getInstanceByID(instanceID string) (*ec2.Instance, error) {
31243116 }
31253117
31263118 if len (instances ) == 0 {
3127- return nil , fmt . Errorf ( "no instances found for instance: %s" , instanceID )
3119+ return nil , cloudprovider . InstanceNotFound
31283120 }
31293121 if len (instances ) > 1 {
31303122 return nil , fmt .Errorf ("multiple instances found for instance: %s" , instanceID )
@@ -3255,6 +3247,19 @@ func (c *Cloud) getInstanceByNodeName(nodeName types.NodeName) (*ec2.Instance, e
32553247 return instance , err
32563248}
32573249
3250+ func (c * Cloud ) getFullInstance (nodeName types.NodeName ) (* awsInstance , * ec2.Instance , error ) {
3251+ if nodeName == "" {
3252+ instance , err := c .getInstanceByID (c .selfAWSInstance .awsID )
3253+ return c .selfAWSInstance , instance , err
3254+ }
3255+ instance , err := c .getInstanceByNodeName (nodeName )
3256+ if err != nil {
3257+ return nil , nil , err
3258+ }
3259+ awsInstance := newAWSInstance (c .ec2 , instance )
3260+ return awsInstance , instance , err
3261+ }
3262+
32583263// Add additional filters, to match on our tags
32593264// This lets us run multiple k8s clusters in a single EC2 AZ
32603265func (c * Cloud ) addFilters (filters []* ec2.Filter ) []* ec2.Filter {
0 commit comments