Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package elasticloadbalancing

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
ec2_2 "github.com/aws/aws-sdk-go-v2/service/ec2"
types2 "github.com/aws/aws-sdk-go-v2/service/ec2/types"
Expand Down Expand Up @@ -59,8 +60,9 @@ type ELBDetail struct {

func GetELBDetail(ctx context.Context, iService schema.ServiceInterface, res chan<- any) error {
elbClient := iService.(*collector.Services).ELB
ec2Client := iService.(*collector.Services).EC2

ELBDetails, err := describeELBDetails(ctx, elbClient)
ELBDetails, err := describeELBDetails(ctx, elbClient, ec2Client)
if err != nil {
log.CtxLogger(ctx).Warn("describeELBDetails error", zap.Error(err))
return err
Expand All @@ -73,22 +75,22 @@ func GetELBDetail(ctx context.Context, iService schema.ServiceInterface, res cha
return nil
}

func describeELBDetails(ctx context.Context, c *elasticloadbalancingv2.Client) (ELBDetails []ELBDetail, err error) {
elbs, err := describeELBs(ctx, c)
func describeELBDetails(ctx context.Context, elbClient *elasticloadbalancingv2.Client, ec2Client *ec2_2.Client) (ELBDetails []ELBDetail, err error) {
elbs, err := describeELBs(ctx, elbClient)
if err != nil {
return nil, err
}

for _, elb := range elbs {
ELBDetails = append(ELBDetails, ELBDetail{
ELB: elb,
VPC: ec2.DescribeVPCDetailsByFilters(ctx, ec2_2.NewFromConfig(aws.Config{Region: c.Options().Region, Credentials: c.Options().Credentials}), []types2.Filter{
VPC: ec2.DescribeVPCDetailsByFilters(ctx, ec2Client, []types2.Filter{
{
Name: aws.String("vpc-id"),
Values: []string{*elb.VpcId},
},
}),
SecurityGroups: ec2.DescribeSecurityGroupDetailsByFilters(ctx, ec2_2.NewFromConfig(aws.Config{Region: c.Options().Region, Credentials: c.Options().Credentials}), []types2.Filter{
SecurityGroups: ec2.DescribeSecurityGroupDetailsByFilters(ctx, ec2Client, []types2.Filter{
{
Name: aws.String("group-id"),
Values: elb.SecurityGroups,
Expand All @@ -100,7 +102,9 @@ func describeELBDetails(ctx context.Context, c *elasticloadbalancingv2.Client) (
}

func describeELBs(ctx context.Context, c *elasticloadbalancingv2.Client) (elbs []types.LoadBalancer, err error) {
input := &elasticloadbalancingv2.DescribeLoadBalancersInput{}
input := &elasticloadbalancingv2.DescribeLoadBalancersInput{
PageSize: aws.Int32(400),
Comment on lines +105 to +106
Copy link

Choose a reason for hiding this comment

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

suggestion: Hardcoded PageSize may not be optimal for all AWS accounts.

Making PageSize configurable or implementing pagination will help ensure all load balancers are retrieved, regardless of account size or future API changes.

Suggested implementation:

func describeELBs(ctx context.Context, c *elasticloadbalancingv2.Client, pageSize int32) (elbs []types.LoadBalancer, err error) {
	var marker *string
	elbs = []types.LoadBalancer{}
	for {
		input := &elasticloadbalancingv2.DescribeLoadBalancersInput{
			PageSize: aws.Int32(pageSize),
			Marker:   marker,
		}
		output, err := c.DescribeLoadBalancers(ctx, input)
		if err != nil {
			log.CtxLogger(ctx).Warn("DescribeLoadBalancers error", zap.Error(err))
			return nil, err
		}
		elbs = append(elbs, output.LoadBalancers...)
		if output.NextMarker == nil {
			break
		}
		marker = output.NextMarker
	}
  • You will need to update all calls to describeELBs to pass the desired pageSize (e.g., describeELBs(ctx, client, 400)).
  • Consider making pageSize a configurable value (e.g., from a config file or environment variable) if you want to avoid hardcoding it in function calls.

}
output, err := c.DescribeLoadBalancers(ctx, input)
if err != nil {
log.CtxLogger(ctx).Warn("DescribeLoadBalancers error", zap.Error(err))
Expand Down
5 changes: 4 additions & 1 deletion collector/aws/collector/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"bytes"
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
Expand Down Expand Up @@ -67,11 +69,11 @@ import (
"github.com/aws/smithy-go/logging"
"github.com/core-sdk/log"
"github.com/core-sdk/schema"
"time"
)

// Services contains regional client of AWS services
type Services struct {
Region string

Choose a reason for hiding this comment

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

medium

The Region field has been added to the Services struct, but it is not used anywhere in the codebase. To keep the code clean and avoid potential confusion, it's best to remove this unused field.

EC2 *ec2.Client
IAM *iam.Client
S3 *s3.Client
Expand Down Expand Up @@ -173,6 +175,7 @@ func (s *Services) InitServices(cloudAccountParam schema.CloudAccountParam) (err
s.ElastiCache = initElastiCacheClient(cfg)
case ELB:
s.ELB = initELBClient(cfg)
s.EC2 = initEC2Client(cfg)
case CLB:
s.CLB = initCLBClient(cfg)
case FSxFileSystem:
Expand Down
Loading