diff --git a/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/aws/aws.go b/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/aws/aws.go index 70ab2d77ebc4..201487831f0c 100644 --- a/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/aws/aws.go +++ b/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/aws/aws.go @@ -1167,16 +1167,16 @@ func (i *awsInstance) describeInstance() (*ec2.Instance, error) { // Gets the mountDevice already assigned to the volume, or assigns an unused mountDevice. // If the volume is already assigned, this will return the existing mountDevice with alreadyAttached=true. // Otherwise the mountDevice is assigned by finding the first available mountDevice, and it is returned with alreadyAttached=false. -func (c *Cloud) getMountDevice(i *awsInstance, volumeID awsVolumeID, assign bool) (assigned mountDevice, alreadyAttached bool, err error) { +func (c *Cloud) getMountDevice( + i *awsInstance, + info *ec2.Instance, + volumeID awsVolumeID, + assign bool) (assigned mountDevice, alreadyAttached bool, err error) { instanceType := i.getInstanceType() if instanceType == nil { return "", false, fmt.Errorf("could not get instance type for instance: %s", i.awsID) } - info, err := i.describeInstance() - if err != nil { - return "", false, err - } deviceMappings := map[mountDevice]awsVolumeID{} for _, blockDevice := range info.BlockDeviceMappings { name := aws.StringValue(blockDevice.DeviceName) @@ -1437,7 +1437,7 @@ func (c *Cloud) AttachDisk(diskName KubernetesVolumeID, nodeName types.NodeName, return "", err } - awsInstance, err := c.getAwsInstance(nodeName) + awsInstance, info, err := c.getFullInstance(nodeName) if err != nil { return "", fmt.Errorf("error finding instance %s: %v", nodeName, err) } @@ -1464,7 +1464,7 @@ func (c *Cloud) AttachDisk(diskName KubernetesVolumeID, nodeName types.NodeName, } }() - mountDevice, alreadyAttached, err = c.getMountDevice(awsInstance, disk.awsID, true) + mountDevice, alreadyAttached, err = c.getMountDevice(awsInstance, info, disk.awsID, true) if err != nil { return "", err } @@ -1524,7 +1524,7 @@ func (c *Cloud) DetachDisk(diskName KubernetesVolumeID, nodeName types.NodeName) return "", err } - awsInstance, err := c.getAwsInstance(nodeName) + awsInstance, info, err := c.getFullInstance(nodeName) if err != nil { if err == cloudprovider.InstanceNotFound { // If instance no longer exists, safe to assume volume is not attached. @@ -1538,7 +1538,7 @@ func (c *Cloud) DetachDisk(diskName KubernetesVolumeID, nodeName types.NodeName) return "", err } - mountDevice, alreadyAttached, err := c.getMountDevice(awsInstance, disk.awsID, false) + mountDevice, alreadyAttached, err := c.getMountDevice(awsInstance, info, disk.awsID, false) if err != nil { return "", err } @@ -1722,7 +1722,7 @@ func (c *Cloud) GetDiskPath(volumeName KubernetesVolumeID) (string, error) { // DiskIsAttached implements Volumes.DiskIsAttached func (c *Cloud) DiskIsAttached(diskName KubernetesVolumeID, nodeName types.NodeName) (bool, error) { - awsInstance, err := c.getAwsInstance(nodeName) + _, instance, err := c.getFullInstance(nodeName) if err != nil { if err == cloudprovider.InstanceNotFound { // If instance no longer exists, safe to assume volume is not attached. @@ -1741,11 +1741,7 @@ func (c *Cloud) DiskIsAttached(diskName KubernetesVolumeID, nodeName types.NodeN return false, fmt.Errorf("error mapping volume spec %q to aws id: %v", diskName, err) } - info, err := awsInstance.describeInstance() - if err != nil { - return false, err - } - for _, blockDevice := range info.BlockDeviceMappings { + for _, blockDevice := range instance.BlockDeviceMappings { id := awsVolumeID(aws.StringValue(blockDevice.Ebs.VolumeId)) if id == diskID { return true, nil @@ -1765,7 +1761,7 @@ func (c *Cloud) DisksAreAttached(diskNames []KubernetesVolumeID, nodeName types. idToDiskName[volumeID] = diskName attached[diskName] = false } - awsInstance, err := c.getAwsInstance(nodeName) + _, instance, err := c.getFullInstance(nodeName) if err != nil { if err == cloudprovider.InstanceNotFound { // If instance no longer exists, safe to assume volume is not attached. @@ -1778,11 +1774,7 @@ func (c *Cloud) DisksAreAttached(diskNames []KubernetesVolumeID, nodeName types. return attached, err } - info, err := awsInstance.describeInstance() - if err != nil { - return attached, err - } - for _, blockDevice := range info.BlockDeviceMappings { + for _, blockDevice := range instance.BlockDeviceMappings { volumeID := awsVolumeID(aws.StringValue(blockDevice.Ebs.VolumeId)) diskName, found := idToDiskName[volumeID] if found { @@ -3123,7 +3115,7 @@ func (c *Cloud) getInstanceByID(instanceID string) (*ec2.Instance, error) { } if len(instances) == 0 { - return nil, fmt.Errorf("no instances found for instance: %s", instanceID) + return nil, cloudprovider.InstanceNotFound } if len(instances) > 1 { return nil, fmt.Errorf("multiple instances found for instance: %s", instanceID) @@ -3254,6 +3246,19 @@ func (c *Cloud) getInstanceByNodeName(nodeName types.NodeName) (*ec2.Instance, e return instance, err } +func (c *Cloud) getFullInstance(nodeName types.NodeName) (*awsInstance, *ec2.Instance, error) { + if nodeName == "" { + instance, err := c.getInstanceByID(c.selfAWSInstance.awsID) + return c.selfAWSInstance, instance, err + } + instance, err := c.getInstanceByNodeName(nodeName) + if err != nil { + return nil, nil, err + } + awsInstance := newAWSInstance(c.ec2, instance) + return awsInstance, instance, err +} + // Add additional filters, to match on our tags // This lets us run multiple k8s clusters in a single EC2 AZ func (c *Cloud) addFilters(filters []*ec2.Filter) []*ec2.Filter {