in pkg/providers/v1/aws.go [3059:3127]
func (c *Cloud) DisksAreAttached(nodeDisks map[types.NodeName][]KubernetesVolumeID) (map[types.NodeName]map[KubernetesVolumeID]bool, error) {
attached := make(map[types.NodeName]map[KubernetesVolumeID]bool)
if len(nodeDisks) == 0 {
return attached, nil
}
nodeNames := []string{}
for nodeName, diskNames := range nodeDisks {
for _, diskName := range diskNames {
setNodeDisk(attached, diskName, nodeName, false)
}
nodeNames = append(nodeNames, mapNodeNameToPrivateDNSName(nodeName))
}
// Note that we get instances regardless of state.
// This means there might be multiple nodes with the same node names.
awsInstances, err := c.getInstancesByNodeNames(nodeNames)
if err != nil {
// When there is an error fetching instance information
// it is safer to return nil and let volume information not be touched.
return nil, err
}
if len(awsInstances) == 0 {
klog.V(2).Infof("DisksAreAttached found no instances matching node names; will assume disks not attached")
return attached, nil
}
// Note that we check that the volume is attached to the correct node, not that it is attached to _a_ node
for _, awsInstance := range awsInstances {
nodeName := mapInstanceToNodeName(awsInstance)
diskNames := nodeDisks[nodeName]
if len(diskNames) == 0 {
continue
}
awsInstanceState := "<nil>"
if awsInstance != nil && awsInstance.State != nil {
awsInstanceState = aws.StringValue(awsInstance.State.Name)
}
if awsInstanceState == "terminated" {
// Instance is terminated, safe to assume volumes not attached
// Note that we keep volumes attached to instances in other states (most notably, stopped)
continue
}
idToDiskName := make(map[EBSVolumeID]KubernetesVolumeID)
for _, diskName := range diskNames {
volumeID, err := diskName.MapToAWSVolumeID()
if err != nil {
return nil, fmt.Errorf("error mapping volume spec %q to aws id: %v", diskName, err)
}
idToDiskName[volumeID] = diskName
}
for _, blockDevice := range awsInstance.BlockDeviceMappings {
volumeID := EBSVolumeID(aws.StringValue(blockDevice.Ebs.VolumeId))
diskName, found := idToDiskName[volumeID]
if found {
// Disk is still attached to node
setNodeDisk(attached, diskName, nodeName, true)
}
}
}
return attached, nil
}