Commit 24c44e7a authored by Daniel Schonfeld's avatar Daniel Schonfeld

optimize ListRoutes to fetch instances only once per call

Issue #12121 - fixes courtesy of @justinsb - thank you
parent ee006e76
...@@ -2104,22 +2104,48 @@ func (s *AWSCloud) UpdateTCPLoadBalancer(name, region string, hosts []string) er ...@@ -2104,22 +2104,48 @@ func (s *AWSCloud) UpdateTCPLoadBalancer(name, region string, hosts []string) er
} }
// Returns the instance with the specified ID // Returns the instance with the specified ID
func (a *AWSCloud) getInstanceById(instanceID string) (*ec2.Instance, error) { // This function is currently unused, but seems very likely to be needed again
request := &ec2.DescribeInstancesInput{ func (a *AWSCloud) getInstanceByID(instanceID string) (*ec2.Instance, error) {
InstanceIds: []*string{&instanceID}, instances, err := a.getInstancesByIDs([]*string{&instanceID})
}
instances, err := a.ec2.DescribeInstances(request)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(instances) == 0 { if len(instances) == 0 {
return nil, fmt.Errorf("no instances found for instance: %s", instanceID) return nil, fmt.Errorf("no instances found for instance: %s", instanceID)
} }
if len(instances) > 1 { if len(instances) > 1 {
return nil, fmt.Errorf("multiple instances found for instance: %s", instanceID) 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 // TODO: Make efficient
......
...@@ -56,18 +56,36 @@ func (s *AWSCloud) ListRoutes(clusterName string) ([]*cloudprovider.Route, error ...@@ -56,18 +56,36 @@ func (s *AWSCloud) ListRoutes(clusterName string) ([]*cloudprovider.Route, error
} }
var routes []*cloudprovider.Route var routes []*cloudprovider.Route
var instanceIDs []*string
for _, r := range table.Routes { for _, r := range table.Routes {
instanceID := orEmpty(r.InstanceId) instanceID := orEmpty(r.InstanceId)
destinationCIDR := orEmpty(r.DestinationCidrBlock)
if instanceID == "" || destinationCIDR == "" { if instanceID == "" {
continue continue
} }
instance, err := s.getInstanceById(instanceID) instanceIDs = append(instanceIDs, &instanceID)
}
instances, err := s.getInstancesByIDs(instanceIDs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, r := range table.Routes {
instanceID := orEmpty(r.InstanceId)
destinationCIDR := orEmpty(r.DestinationCidrBlock)
if instanceID == "" || destinationCIDR == "" {
continue
}
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) instanceName := orEmpty(instance.PrivateDnsName)
routeName := clusterName + "-" + destinationCIDR routeName := clusterName + "-" + destinationCIDR
routes = append(routes, &cloudprovider.Route{Name: routeName, TargetInstance: instanceName, DestinationCIDR: destinationCIDR}) routes = append(routes, &cloudprovider.Route{Name: routeName, TargetInstance: instanceName, DestinationCIDR: destinationCIDR})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment