Skip to content

Commit d4d8428

Browse files
committed
refactor(nlb): move constants to shared constants file
1 parent c61b20f commit d4d8428

File tree

5 files changed

+73
-32
lines changed

5 files changed

+73
-32
lines changed

pkg/ingress/model_build_frontend_nlb.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99

1010
"sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants"
11+
"sigs.k8s.io/aws-load-balancer-controller/pkg/shared_utils"
1112

1213
awssdk "github.com/aws/aws-sdk-go-v2/aws"
1314
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
@@ -211,6 +212,45 @@ func (t *defaultModelBuildTask) buildFrontendNlb(ctx context.Context, scheme elb
211212
return nil
212213
}
213214

215+
func (t *defaultModelBuildTask) buildFrontendNlbAttributes(ctx context.Context) ([]elbv2model.LoadBalancerAttribute, error) {
216+
loadBalancerAttributes, err := t.getFrontendNlbAttributes()
217+
if err != nil {
218+
return []elbv2model.LoadBalancerAttribute{}, err
219+
}
220+
return shared_utils.MakeAttributesSliceFromMap(loadBalancerAttributes), nil
221+
}
222+
223+
func (t *defaultModelBuildTask) getFrontendNlbAttributes() (any, any) {
224+
var chosenAttributes map[string]string
225+
for _, member := range t.ingGroup.Members {
226+
var attributes map[string]string
227+
if _, err := t.annotationParser.ParseStringMapAnnotation(annotations.SvcLBSuffixLoadBalancerAttributes, &attributes, member.Ing.Annotations); err != nil {
228+
return nil, err
229+
}
230+
if chosenAttributes == nil {
231+
chosenAttributes = attributes
232+
} else {
233+
if !cmp.Equal(chosenAttributes, attributes) {
234+
return nil, errors.Errorf("conflicting frontend NLB attributes: %v | %v", chosenAttributes, attributes)
235+
}
236+
}
237+
}
238+
239+
dnsRecordClientRoutingPolicy, exists := chosenAttributes[lbAttrsLoadBalancingDnsClientRoutingPolicy]
240+
if exists {
241+
switch dnsRecordClientRoutingPolicy {
242+
case availabilityZoneAffinity:
243+
case partialAvailabilityZoneAffinity:
244+
case anyAvailabilityZone:
245+
default:
246+
return nil, errors.Errorf("invalid dns_record.client_routing_policy set in annotation %s: got '%s' expected one of ['%s', '%s', '%s']",
247+
annotations.SvcLBSuffixLoadBalancerAttributes, dnsRecordClientRoutingPolicy,
248+
anyAvailabilityZone, partialAvailabilityZoneAffinity, availabilityZoneAffinity)
249+
}
250+
}
251+
return chosenAttributes, nil
252+
}
253+
214254
func (t *defaultModelBuildTask) buildFrontendNlbSpec(ctx context.Context, scheme elbv2model.LoadBalancerScheme,
215255
alb *elbv2model.LoadBalancer) (elbv2model.LoadBalancerSpec, error) {
216256
securityGroups, err := t.buildFrontendNlbSecurityGroups(ctx)

pkg/service/model_build_load_balancer.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,7 @@ import (
2727
)
2828

2929
const (
30-
lbAttrsAccessLogsS3Enabled = "access_logs.s3.enabled"
31-
lbAttrsAccessLogsS3Bucket = "access_logs.s3.bucket"
32-
lbAttrsAccessLogsS3Prefix = "access_logs.s3.prefix"
33-
lbAttrsLoadBalancingCrossZoneEnabled = "load_balancing.cross_zone.enabled"
34-
lbAttrsLoadBalancingDnsClientRoutingPolicy = "dns_record.client_routing_policy"
35-
availabilityZoneAffinity = "availability_zone_affinity"
36-
partialAvailabilityZoneAffinity = "partial_availability_zone_affinity"
37-
anyAvailabilityZone = "any_availability_zone"
30+
resourceIDLoadBalancer = "LoadBalancer"
3831
)
3932

4033
func (t *defaultModelBuildTask) buildLoadBalancer(ctx context.Context, scheme elbv2model.LoadBalancerScheme) error {
@@ -532,16 +525,16 @@ func (t *defaultModelBuildTask) getLoadBalancerAttributes() (map[string]string,
532525
if _, err := t.annotationParser.ParseStringMapAnnotation(annotations.SvcLBSuffixLoadBalancerAttributes, &attributes, t.service.Annotations); err != nil {
533526
return nil, err
534527
}
535-
dnsRecordClientRoutingPolicy, exists := attributes[lbAttrsLoadBalancingDnsClientRoutingPolicy]
528+
dnsRecordClientRoutingPolicy, exists := attributes[shared_constants.LBAttributeLoadBalancingDnsClientRoutingPolicy]
536529
if exists {
537530
switch dnsRecordClientRoutingPolicy {
538-
case availabilityZoneAffinity:
539-
case partialAvailabilityZoneAffinity:
540-
case anyAvailabilityZone:
531+
case shared_constants.LBAttributeAvailabilityZoneAffinity:
532+
case shared_constants.LBAttributePartialAvailabilityZoneAffinity:
533+
case shared_constants.LBAttributeAnyAvailabilityZone:
541534
default:
542535
return nil, errors.Errorf("invalid dns_record.client_routing_policy set in annotation %s: got '%s' expected one of ['%s', '%s', '%s']",
543536
annotations.SvcLBSuffixLoadBalancerAttributes, dnsRecordClientRoutingPolicy,
544-
anyAvailabilityZone, partialAvailabilityZoneAffinity, availabilityZoneAffinity)
537+
shared_constants.LBAttributeAnyAvailabilityZone, shared_constants.LBAttributePartialAvailabilityZoneAffinity, shared_constants.LBAttributeAvailabilityZoneAffinity)
545538
}
546539
}
547540
return attributes, nil
@@ -559,20 +552,20 @@ func (t *defaultModelBuildTask) getAnnotationSpecificLbAttributes() (map[string]
559552
return nil, err
560553
}
561554
if exists && accessLogEnabled {
562-
annotationSpecificAttrs[lbAttrsAccessLogsS3Enabled] = strconv.FormatBool(accessLogEnabled)
555+
annotationSpecificAttrs[shared_constants.LBAttributeAccessLogsS3Enabled] = strconv.FormatBool(accessLogEnabled)
563556
if exists := t.annotationParser.ParseStringAnnotation(annotations.SvcLBSuffixAccessLogS3BucketName, &bucketName, t.service.Annotations); exists {
564-
annotationSpecificAttrs[lbAttrsAccessLogsS3Bucket] = bucketName
557+
annotationSpecificAttrs[shared_constants.LBAttributeAccessLogsS3Bucket] = bucketName
565558
}
566559
if exists := t.annotationParser.ParseStringAnnotation(annotations.SvcLBSuffixAccessLogS3BucketPrefix, &bucketPrefix, t.service.Annotations); exists {
567-
annotationSpecificAttrs[lbAttrsAccessLogsS3Prefix] = bucketPrefix
560+
annotationSpecificAttrs[shared_constants.LBAttributeAccessLogsS3Prefix] = bucketPrefix
568561
}
569562
}
570563
exists, err = t.annotationParser.ParseBoolAnnotation(annotations.SvcLBSuffixCrossZoneLoadBalancingEnabled, &crossZoneEnabled, t.service.Annotations)
571564
if err != nil {
572565
return nil, err
573566
}
574567
if exists {
575-
annotationSpecificAttrs[lbAttrsLoadBalancingCrossZoneEnabled] = strconv.FormatBool(crossZoneEnabled)
568+
annotationSpecificAttrs[shared_constants.LBAttributeLoadBalancingCrossZoneEnabled] = strconv.FormatBool(crossZoneEnabled)
576569
}
577570
return annotationSpecificAttrs, nil
578571
}

pkg/service/model_build_load_balancer_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ func Test_defaultModelBuilderTask_buildLBAttributes(t *testing.T) {
6161
wantError: false,
6262
wantValue: []elbv2.LoadBalancerAttribute{
6363
{
64-
Key: lbAttrsAccessLogsS3Enabled,
64+
Key: shared_constants.LBAttributeAccessLogsS3Enabled,
6565
Value: "true",
6666
},
6767
{
68-
Key: lbAttrsAccessLogsS3Bucket,
68+
Key: shared_constants.LBAttributeAccessLogsS3Bucket,
6969
Value: "nlb-bucket",
7070
},
7171
{
72-
Key: lbAttrsAccessLogsS3Prefix,
72+
Key: shared_constants.LBAttributeAccessLogsS3Prefix,
7373
Value: "bkt-pfx",
7474
},
7575
{
76-
Key: lbAttrsLoadBalancingCrossZoneEnabled,
76+
Key: shared_constants.LBAttributeLoadBalancingCrossZoneEnabled,
7777
Value: "true",
7878
},
7979
{
@@ -95,28 +95,28 @@ func Test_defaultModelBuilderTask_buildLBAttributes(t *testing.T) {
9595
wantError: false,
9696
wantValue: []elbv2.LoadBalancerAttribute{
9797
{
98-
Key: lbAttrsAccessLogsS3Enabled,
98+
Key: shared_constants.LBAttributeAccessLogsS3Enabled,
9999
Value: "true",
100100
},
101101
{
102-
Key: lbAttrsAccessLogsS3Bucket,
102+
Key: shared_constants.LBAttributeAccessLogsS3Bucket,
103103
Value: "nlb-bucket",
104104
},
105105
{
106-
Key: lbAttrsAccessLogsS3Prefix,
106+
Key: shared_constants.LBAttributeAccessLogsS3Prefix,
107107
Value: "bkt-pfx",
108108
},
109109
{
110-
Key: lbAttrsLoadBalancingCrossZoneEnabled,
110+
Key: shared_constants.LBAttributeLoadBalancingCrossZoneEnabled,
111111
Value: "true",
112112
},
113113
{
114114
Key: shared_constants.LBAttributeDeletionProtection,
115115
Value: "true",
116116
},
117117
{
118-
Key: lbAttrsLoadBalancingDnsClientRoutingPolicy,
119-
Value: availabilityZoneAffinity,
118+
Key: shared_constants.LBAttributeLoadBalancingDnsClientRoutingPolicy,
119+
Value: shared_constants.LBAttributeAvailabilityZoneAffinity,
120120
},
121121
},
122122
},
@@ -137,19 +137,19 @@ func Test_defaultModelBuilderTask_buildLBAttributes(t *testing.T) {
137137
wantError: false,
138138
wantValue: []elbv2.LoadBalancerAttribute{
139139
{
140-
Key: lbAttrsAccessLogsS3Enabled,
140+
Key: shared_constants.LBAttributeAccessLogsS3Enabled,
141141
Value: "true",
142142
},
143143
{
144-
Key: lbAttrsAccessLogsS3Bucket,
144+
Key: shared_constants.LBAttributeAccessLogsS3Bucket,
145145
Value: "overridden-nlb-bucket",
146146
},
147147
{
148-
Key: lbAttrsAccessLogsS3Prefix,
148+
Key: shared_constants.LBAttributeAccessLogsS3Prefix,
149149
Value: "overridden-bkt-pfx",
150150
},
151151
{
152-
Key: lbAttrsLoadBalancingCrossZoneEnabled,
152+
Key: shared_constants.LBAttributeLoadBalancingCrossZoneEnabled,
153153
Value: "false",
154154
},
155155
},

pkg/shared_constants/attributes.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ package shared_constants
22

33
const (
44
// LBAttributeDeletionProtection deletion protection attribute name
5-
LBAttributeDeletionProtection = "deletion_protection.enabled"
5+
LBAttributeDeletionProtection = "deletion_protection.enabled"
6+
LBAttributeAccessLogsS3Enabled = "access_logs.s3.enabled"
7+
LBAttributeAccessLogsS3Bucket = "access_logs.s3.bucket"
8+
LBAttributeAccessLogsS3Prefix = "access_logs.s3.prefix"
9+
LBAttributeLoadBalancingCrossZoneEnabled = "load_balancing.cross_zone.enabled"
10+
LBAttributeLoadBalancingDnsClientRoutingPolicy = "dns_record.client_routing_policy"
11+
LBAttributeAvailabilityZoneAffinity = "availability_zone_affinity"
12+
LBAttributePartialAvailabilityZoneAffinity = "partial_availability_zone_affinity"
13+
LBAttributeAnyAvailabilityZone = "any_availability_zone"
614
)
715

816
const (
File renamed without changes.

0 commit comments

Comments
 (0)