Skip to content

Commit d8b6609

Browse files
authored
Merge pull request #45 from DataDog/alberto.mardomingo/describe-instances-pagination
Add pagination to describeInstances in aws
2 parents 790bad5 + 9af2cee commit d8b6609

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

.github/workflows/dd-build.yml

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,9 @@ jobs:
1515
matrix:
1616
platform: ["linux/arm64","linux/amd64"]
1717
steps:
18-
- name: Maximize build space
19-
uses: easimon/maximize-build-space@master
20-
with:
21-
root-reserve-mb: 512
22-
swap-size-mb: 1024
23-
remove-dotnet: 'true'
24-
remove-android: 'true'
25-
remove-haskell: 'true'
26-
- name: FreeSpace
27-
run: |
28-
echo "Free space:"
29-
df -h
3018
- uses: actions/checkout@v2
3119
with:
3220
fetch-depth: 0
33-
- name: Set up Go
34-
uses: actions/setup-go@v2
35-
with:
36-
go-version: 1.15
3721
- name: Build recent
3822
env:
3923
KUBE_BUILD_PLATFORMS: ${{ matrix.platform }}
@@ -93,9 +77,9 @@ jobs:
9377
strategy:
9478
matrix:
9579
assets: [
96-
"kubernetes-client",
97-
"kubernetes-node",
98-
"kubernetes-server"
80+
"kubernetes-client",
81+
"kubernetes-node",
82+
"kubernetes-server"
9983
]
10084
platform: ["linux-arm64","linux-amd64"]
10185
steps:

pkg/cloudprovider/providers/aws/aws.go

Lines changed: 25 additions & 9 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
@@ -4499,21 +4507,29 @@ func (c *Cloud) getInstancesByNodeNames(nodeNames []string, states ...string) ([
44994507

45004508
// TODO: Move to instanceCache
45014509
func (c *Cloud) describeInstances(filters []*ec2.Filter) ([]*ec2.Instance, error) {
4510+
// Set the Maxresults to the maximum allowed per the AWS doc
4511+
// https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#DescribeInstancesInput
4512+
maxResults := int64(1000)
45024513
request := &ec2.DescribeInstancesInput{
4503-
Filters: filters,
4514+
Filters: filters,
4515+
MaxResults: &maxResults,
45044516
}
45054517

4506-
response, err := c.ec2.DescribeInstances(request)
4518+
var matches []*ec2.Instance
4519+
err := c.ec2.DescribeInstancesPages(request, func(page *ec2.DescribeInstancesOutput, lastPage bool) bool {
4520+
for _, reservations := range page.Reservations {
4521+
for _, instance := range reservations.Instances {
4522+
if c.tagging.hasClusterTag(instance.Tags) {
4523+
matches = append(matches, instance)
4524+
}
4525+
}
4526+
}
4527+
return !lastPage
4528+
})
45074529
if err != nil {
45084530
return nil, err
45094531
}
45104532

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

pkg/cloudprovider/providers/aws/aws_fakes.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ 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+
}
181+
177182
// AttachVolume is not implemented but is required for interface conformance
178183
func (ec2i *FakeEC2Impl) AttachVolume(request *ec2.AttachVolumeInput) (resp *ec2.VolumeAttachment, err error) {
179184
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)