Skip to content

Commit

Permalink
Add code for filtering target group & load balancers by VPC ID (kuber…
Browse files Browse the repository at this point in the history
…netes-sigs#2157)

* Add code for filtering target group & LB by VPC ID; Add IT code

* Add detailed comments for tests
  • Loading branch information
Shreya027 authored Aug 11, 2021
1 parent 166c439 commit f861a8e
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 35 deletions.
2 changes: 1 addition & 1 deletion controllers/ingress/group_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewGroupReconciler(cloud aws.Cloud, k8sClient client.Client, eventRecorder
enhancedBackendBuilder := ingress.NewDefaultEnhancedBackendBuilder(k8sClient, annotationParser, authConfigBuilder)
referenceIndexer := ingress.NewDefaultReferenceIndexer(enhancedBackendBuilder, authConfigBuilder, logger)
trackingProvider := tracking.NewDefaultProvider(ingressTagPrefix, config.ClusterName)
elbv2TaggingManager := elbv2deploy.NewDefaultTaggingManager(cloud.ELBV2(), logger)
elbv2TaggingManager := elbv2deploy.NewDefaultTaggingManager(cloud.ELBV2(), cloud.VpcID(), logger)
modelBuilder := ingress.NewDefaultModelBuilder(k8sClient, eventRecorder,
cloud.EC2(), cloud.ACM(),
annotationParser, subnetsResolver,
Expand Down
2 changes: 1 addition & 1 deletion controllers/service/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewServiceReconciler(cloud aws.Cloud, k8sClient client.Client, eventRecorde

annotationParser := annotations.NewSuffixAnnotationParser(serviceAnnotationPrefix)
trackingProvider := tracking.NewDefaultProvider(serviceTagPrefix, config.ClusterName)
elbv2TaggingManager := elbv2.NewDefaultTaggingManager(cloud.ELBV2(), logger)
elbv2TaggingManager := elbv2.NewDefaultTaggingManager(cloud.ELBV2(), cloud.VpcID(), logger)
modelBuilder := service.NewDefaultModelBuilder(annotationParser, subnetsResolver, vpcResolver, trackingProvider,
elbv2TaggingManager, config.ClusterName, config.DefaultTags, config.ExternalManagedTags, config.DefaultSSLPolicy)
stackMarshaller := deploy.NewDefaultStackMarshaller()
Expand Down
48 changes: 27 additions & 21 deletions pkg/deploy/elbv2/tagging_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ type TaggingManager interface {
}

// NewDefaultTaggingManager constructs default TaggingManager.
func NewDefaultTaggingManager(elbv2Client services.ELBV2, logger logr.Logger) *defaultTaggingManager {
func NewDefaultTaggingManager(elbv2Client services.ELBV2, vpcID string, logger logr.Logger) *defaultTaggingManager {
return &defaultTaggingManager{
elbv2Client: elbv2Client,
logger: logger,

elbv2Client: elbv2Client,
vpcID: vpcID,
logger: logger,
describeTagsChunkSize: defaultDescribeTagsChunkSize,
}
}
Expand All @@ -106,9 +106,9 @@ var _ TaggingManager = &defaultTaggingManager{}
// default implementation for TaggingManager
// @TODO: use AWS Resource Groups Tagging API to optimize this implementation once it have PrivateLink support.
type defaultTaggingManager struct {
elbv2Client services.ELBV2
logger logr.Logger

elbv2Client services.ELBV2
vpcID string
logger logr.Logger
describeTagsChunkSize int
}

Expand Down Expand Up @@ -235,20 +235,23 @@ func (m *defaultTaggingManager) ListLoadBalancers(ctx context.Context, tagFilter
return nil, err
}

lbARNs := make([]string, 0, len(lbs))
lbByARN := make(map[string]*elbv2sdk.LoadBalancer, len(lbs))
lbARNsWithinVPC := make([]string, 0, len(lbs))
lbByARNWithinVPC := make(map[string]*elbv2sdk.LoadBalancer, len(lbs))
for _, lb := range lbs {
if awssdk.StringValue(lb.VpcId) != m.vpcID {
continue
}
lbARN := awssdk.StringValue(lb.LoadBalancerArn)
lbARNs = append(lbARNs, lbARN)
lbByARN[lbARN] = lb
lbARNsWithinVPC = append(lbARNsWithinVPC, lbARN)
lbByARNWithinVPC[lbARN] = lb
}
tagsByARN, err := m.describeResourceTags(ctx, lbARNs)
tagsByARN, err := m.describeResourceTags(ctx, lbARNsWithinVPC)
if err != nil {
return nil, err
}

var matchedLBs []LoadBalancerWithTags
for _, arn := range lbARNs {
for _, arn := range lbARNsWithinVPC {
tags := tagsByARN[arn]
matchedAnyTagFilter := false
for _, tagFilter := range tagFilters {
Expand All @@ -259,7 +262,7 @@ func (m *defaultTaggingManager) ListLoadBalancers(ctx context.Context, tagFilter
}
if matchedAnyTagFilter {
matchedLBs = append(matchedLBs, LoadBalancerWithTags{
LoadBalancer: lbByARN[arn],
LoadBalancer: lbByARNWithinVPC[arn],
Tags: tags,
})
}
Expand All @@ -274,20 +277,23 @@ func (m *defaultTaggingManager) ListTargetGroups(ctx context.Context, tagFilters
return nil, err
}

tgARNs := make([]string, 0, len(tgs))
tgByARN := make(map[string]*elbv2sdk.TargetGroup, len(tgs))
tgARNsWithinVPC := make([]string, 0, len(tgs))
tgByARNWithinVPC := make(map[string]*elbv2sdk.TargetGroup, len(tgs))
for _, tg := range tgs {
if awssdk.StringValue(tg.VpcId) != m.vpcID {
continue
}
tgARN := awssdk.StringValue(tg.TargetGroupArn)
tgARNs = append(tgARNs, tgARN)
tgByARN[tgARN] = tg
tgARNsWithinVPC = append(tgARNsWithinVPC, tgARN)
tgByARNWithinVPC[tgARN] = tg
}
tagsByARN, err := m.describeResourceTags(ctx, tgARNs)
tagsByARN, err := m.describeResourceTags(ctx, tgARNsWithinVPC)
if err != nil {
return nil, err
}

var matchedTGs []TargetGroupWithTags
for _, arn := range tgARNs {
for _, arn := range tgARNsWithinVPC {
tags := tagsByARN[arn]
matchedAnyTagFilter := false
for _, tagFilter := range tagFilters {
Expand All @@ -298,7 +304,7 @@ func (m *defaultTaggingManager) ListTargetGroups(ctx context.Context, tagFilters
}
if matchedAnyTagFilter {
matchedTGs = append(matchedTGs, TargetGroupWithTags{
TargetGroup: tgByARN[arn],
TargetGroup: tgByARNWithinVPC[arn],
Tags: tags,
})
}
Expand Down
Loading

0 comments on commit f861a8e

Please sign in to comment.