Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ListRoutes to fetch instances only once per call #19337

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 33 additions & 7 deletions pkg/cloudprovider/providers/aws/aws.go
Expand Up @@ -2104,22 +2104,48 @@ func (s *AWSCloud) UpdateTCPLoadBalancer(name, region string, hosts []string) er
}

// Returns the instance with the specified ID
func (a *AWSCloud) getInstanceById(instanceID string) (*ec2.Instance, error) {
request := &ec2.DescribeInstancesInput{
InstanceIds: []*string{&instanceID},
}

instances, err := a.ec2.DescribeInstances(request)
// This function is currently unused, but seems very likely to be needed again
func (a *AWSCloud) getInstanceByID(instanceID string) (*ec2.Instance, error) {
instances, err := a.getInstancesByIDs([]*string{&instanceID})
if err != nil {
return nil, err
}

if len(instances) == 0 {
return nil, fmt.Errorf("no instances found for instance: %s", instanceID)
}
if len(instances) > 1 {
return nil, fmt.Errorf("multiple instances found for instance: %s", instanceID)
}
return instances[0], nil

return instances[instanceID], nil
}

func (a *AWSCloud) getInstancesByIDs(instanceIDs []*string) (map[string]*ec2.Instance, error) {
instancesByID := make(map[string]*ec2.Instance)
if len(instanceIDs) == 0 {
return instancesByID, nil
}

request := &ec2.DescribeInstancesInput{
InstanceIds: instanceIDs,
}

instances, err := a.ec2.DescribeInstances(request)
if err != nil {
return nil, err
}

for _, instance := range instances {
instanceID := orEmpty(instance.InstanceId)
if instanceID == "" {
continue
}

instancesByID[instanceID] = instance
}

return instancesByID, nil
}

// TODO: Make efficient
Expand Down
24 changes: 21 additions & 3 deletions pkg/cloudprovider/providers/aws/aws_routes.go
Expand Up @@ -56,6 +56,23 @@ func (s *AWSCloud) ListRoutes(clusterName string) ([]*cloudprovider.Route, error
}

var routes []*cloudprovider.Route
var instanceIDs []*string

for _, r := range table.Routes {
instanceID := orEmpty(r.InstanceId)

if instanceID == "" {
continue
}

instanceIDs = append(instanceIDs, &instanceID)
}

instances, err := s.getInstancesByIDs(instanceIDs)
if err != nil {
return nil, err
}

for _, r := range table.Routes {
instanceID := orEmpty(r.InstanceId)
destinationCIDR := orEmpty(r.DestinationCidrBlock)
Expand All @@ -64,9 +81,10 @@ func (s *AWSCloud) ListRoutes(clusterName string) ([]*cloudprovider.Route, error
continue
}

instance, err := s.getInstanceById(instanceID)
if err != nil {
return nil, err
instance, found := instances[instanceID]
if !found {
glog.Warningf("unable to find instance ID %s in the list of instances being routed to", instanceID)
continue
}
instanceName := orEmpty(instance.PrivateDnsName)
routeName := clusterName + "-" + destinationCIDR
Expand Down