Skip to content

Commit 3a7b475

Browse files
committed
Add pagination to describeInstances in aws
For large clusters and AWS projects, the describeInstances api call returns a 500 http error because the response exceed the limit in the AWS api. This means that, for example, for a classic loadbalancer, no new nodes get added to it, which could lead to service disruption over time due to node attrition. This commit attempts to solve that by changing the call to use the pagination method from the aws sdk
1 parent 790bad5 commit 3a7b475

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

pkg/cloudprovider/providers/aws/aws.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import (
4646
"gopkg.in/gcfg.v1"
4747
"k8s.io/klog"
4848

49-
"k8s.io/api/core/v1"
49+
v1 "k8s.io/api/core/v1"
5050
"k8s.io/apimachinery/pkg/api/resource"
5151
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
5252
"k8s.io/apimachinery/pkg/types"
@@ -280,6 +280,9 @@ type EC2 interface {
280280
// Query EC2 for instances matching the filter
281281
DescribeInstances(request *ec2.DescribeInstancesInput) ([]*ec2.Instance, error)
282282

283+
// Query EC2 for instances matching the filter with pagination
284+
DescribeInstancesPages(input *ec2.DescribeInstancesInput, fn func(*ec2.DescribeInstancesOutput, bool) bool) error
285+
283286
// Attach a volume to an instance
284287
AttachVolume(*ec2.AttachVolumeInput) (*ec2.VolumeAttachment, error)
285288
// Detach a volume from an instance it is attached to
@@ -902,6 +905,11 @@ func (s *awsSdkEC2) DescribeInstances(request *ec2.DescribeInstancesInput) ([]*e
902905
return results, nil
903906
}
904907

908+
// Implementation of EC2.Instances with pagination
909+
func (s *awsSdkEC2) DescribeInstancesPages(input *ec2.DescribeInstancesInput, fn func(*ec2.DescribeInstancesOutput, bool) bool) error {
910+
return s.ec2.DescribeInstancesPages(input, fn)
911+
}
912+
905913
// Implements EC2.DescribeSecurityGroups
906914
func (s *awsSdkEC2) DescribeSecurityGroups(request *ec2.DescribeSecurityGroupsInput) ([]*ec2.SecurityGroup, error) {
907915
// Security groups are paged
@@ -4503,17 +4511,21 @@ func (c *Cloud) describeInstances(filters []*ec2.Filter) ([]*ec2.Instance, error
45034511
Filters: filters,
45044512
}
45054513

4506-
response, err := c.ec2.DescribeInstances(request)
4514+
var matches []*ec2.Instance
4515+
err := c.ec2.DescribeInstancesPages(request, func(page *ec2.DescribeInstancesOutput, lastPage bool) bool {
4516+
for _, reservations := range page.Reservations {
4517+
for _, instance := range reservations.Instances {
4518+
if c.tagging.hasClusterTag(instance.Tags) {
4519+
matches = append(matches, instance)
4520+
}
4521+
}
4522+
}
4523+
return !lastPage
4524+
})
45074525
if err != nil {
45084526
return nil, err
45094527
}
45104528

4511-
var matches []*ec2.Instance
4512-
for _, instance := range response {
4513-
if c.tagging.hasClusterTag(instance.Tags) {
4514-
matches = append(matches, instance)
4515-
}
4516-
}
45174529
return matches, nil
45184530
}
45194531

pkg/cloudprovider/providers/aws/aws_fakes.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ func (ec2i *FakeEC2Impl) DescribeInstances(request *ec2.DescribeInstancesInput)
174174
return matches, nil
175175
}
176176

177+
// DescribeInstancesPages is not implemented but is required for interface conformance
178+
func (ec2i *FakeEC2Impl) DescribeInstancesPages(input *ec2.DescribeInstancesInput, fn func(*ec2.DescribeInstancesOutput, bool) bool) error {
179+
panic("Not implemented")
180+
}
177181
// AttachVolume is not implemented but is required for interface conformance
178182
func (ec2i *FakeEC2Impl) AttachVolume(request *ec2.AttachVolumeInput) (resp *ec2.VolumeAttachment, err error) {
179183
panic("Not implemented")

pkg/cloudprovider/providers/aws/instances.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ import (
2626

2727
"github.com/aws/aws-sdk-go/aws"
2828
"github.com/aws/aws-sdk-go/service/ec2"
29+
v1 "k8s.io/api/core/v1"
2930
"k8s.io/klog"
30-
31-
"k8s.io/api/core/v1"
3231
)
3332

3433
// awsInstanceRegMatch represents Regex Match for AWS instance.

0 commit comments

Comments
 (0)