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

Paginate all vpc calls. #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

bergmannf
Copy link
Contributor

Use pagination for all vpc metrics, so we don't miss out on any data.

@bergmannf
Copy link
Contributor Author

Should replace #43 (I just couldn't manage to update the other PR)

vpc.go Outdated
}
var unexpectedNumberOfVpcs error
var cidrBlockAssociations []*ec2.VpcCidrBlockAssociation
err := c.ec2.DescribeVpcsPagesWithContext(ctx, input, func(page *ec2.DescribeVpcsOutput, lastPage bool) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess pagination isn't really needed when we only want to get a single vpc. I'd rather leave this function unchanged as this makes it way harder to understand what it's doing.

vpc.go Outdated
}},
}}}
err := c.ec2.DescribeRouteTablesPagesWithContext(ctx, input, func(page *ec2.DescribeRouteTablesOutput, lastPage bool) bool {
routeTables = append(routeTables, page.RouteTables...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we're only interested in the number of tables, we can just count them instead of saving them all:

numRouteTables += len(page.RouteTables)

vpc.go Outdated

var vpcEndpoints []*ec2.VpcEndpoint
err := c.ec2.DescribeVpcEndpointsPagesWithContext(ctx, descEndpointsInput, func(page *ec2.DescribeVpcEndpointsOutput, lastPage bool) bool {
vpcEndpoints = append(vpcEndpoints, page.VpcEndpoints...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we only care about the number, i would not save all the individual endpoints:

numEndpoints += len(page.VpcEndpoints)

vpc.go Outdated
RouteTableIds: []*string{rtb.RouteTableId},
}, func(page *ec2.DescribeRouteTablesOutput, lastPage bool) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it quite awkward to do pagination on this call, as we're describing only a single routetable. It makes the function much harder to understand.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the unlikely case that the api call returns multiple pages, we error out anyway

vpc.go Outdated
describeVpcsOutput, err := c.ec2.DescribeVpcsWithContext(ctx, &ec2.DescribeVpcsInput{})
var vpcs []*ec2.Vpc
err := c.ec2.DescribeVpcsPagesWithContext(ctx, &ec2.DescribeVpcsInput{}, func(page *ec2.DescribeVpcsOutput, lastPage bool) bool {
vpcs = append(vpcs, page.Vpcs...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here, i'd suggest to only save the actual number of vpcs, and not the vpcs to make it clearer what we are attempting to do.

vpc.go Outdated
Name: aws.String("vpc-id"),
Values: []*string{vpc.VpcId},
}},
}}}, func(page *ec2.DescribeSubnetsOutput, lastPage bool) bool {
subnets = append(subnets, page.Subnets...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, i'd suggest to only save the actual number of subnets, and not the actual subnets so it's easier to understand what we are attempting.

vpc.go Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants