Skip to content

Commit c61b20f

Browse files
committed
refactor(service): move attribute slice func to shared utils
1 parent aa19e8c commit c61b20f

File tree

5 files changed

+108
-24
lines changed

5 files changed

+108
-24
lines changed

pkg/ingress/model_build_frontend_nlb.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,20 @@ func (t *defaultModelBuildTask) buildFrontendNlbSpec(ctx context.Context, scheme
238238
return elbv2model.LoadBalancerSpec{}, err
239239
}
240240

241+
lbAttributes, err := t.buildFrontendNlbAttributes(ctx)
242+
if err != nil {
243+
return elbv2model.LoadBalancerSpec{}, err
244+
}
245+
241246
spec := elbv2model.LoadBalancerSpec{
242-
Name: name,
243-
Type: elbv2model.LoadBalancerTypeNetwork,
244-
Scheme: scheme,
245-
IPAddressType: alb.Spec.IPAddressType,
246-
SecurityGroups: securityGroups,
247-
SubnetMappings: subnetMappings,
248-
Tags: tags,
247+
Name: name,
248+
Type: elbv2model.LoadBalancerTypeNetwork,
249+
Scheme: scheme,
250+
IPAddressType: alb.Spec.IPAddressType,
251+
LoadBalancerAttributes: lbAttributes,
252+
SecurityGroups: securityGroups,
253+
SubnetMappings: subnetMappings,
254+
Tags: tags,
249255
}
250256

251257
return spec, nil

pkg/service/model_build_load_balancer.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"fmt"
88
"net/netip"
99
"regexp"
10-
"sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants"
11-
"sort"
1210
"strconv"
1311

1412
awssdk "github.com/aws/aws-sdk-go-v2/aws"
@@ -24,6 +22,8 @@ import (
2422
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/core"
2523
elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
2624
"sigs.k8s.io/aws-load-balancer-controller/pkg/networking"
25+
"sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants"
26+
"sigs.k8s.io/aws-load-balancer-controller/pkg/shared_utils"
2727
)
2828

2929
const (
@@ -497,7 +497,7 @@ func (t *defaultModelBuildTask) buildLoadBalancerAttributes(_ context.Context) (
497497
return []elbv2model.LoadBalancerAttribute{}, err
498498
}
499499
mergedAttributes := algorithm.MergeStringMap(specificAttributes, loadBalancerAttributes)
500-
return makeAttributesSliceFromMap(mergedAttributes), nil
500+
return shared_utils.MakeAttributesSliceFromMap(mergedAttributes), nil
501501
}
502502

503503
func (t *defaultModelBuildTask) buildLoadBalancerMinimumCapacity(_ context.Context) (*elbv2model.MinimumLoadBalancerCapacity, error) {
@@ -527,20 +527,6 @@ func (t *defaultModelBuildTask) buildLoadBalancerMinimumCapacity(_ context.Conte
527527
return minimumLoadBalancerCapacity, nil
528528
}
529529

530-
func makeAttributesSliceFromMap(loadBalancerAttributesMap map[string]string) []elbv2model.LoadBalancerAttribute {
531-
attributes := make([]elbv2model.LoadBalancerAttribute, 0, len(loadBalancerAttributesMap))
532-
for attrKey, attrValue := range loadBalancerAttributesMap {
533-
attributes = append(attributes, elbv2model.LoadBalancerAttribute{
534-
Key: attrKey,
535-
Value: attrValue,
536-
})
537-
}
538-
sort.Slice(attributes, func(i, j int) bool {
539-
return attributes[i].Key < attributes[j].Key
540-
})
541-
return attributes
542-
}
543-
544530
func (t *defaultModelBuildTask) getLoadBalancerAttributes() (map[string]string, error) {
545531
var attributes map[string]string
546532
if _, err := t.annotationParser.ParseStringMapAnnotation(annotations.SvcLBSuffixLoadBalancerAttributes, &attributes, t.service.Annotations); err != nil {

pkg/service/model_build_target_group.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strconv"
1111
"strings"
1212

13+
"sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants"
14+
1315
awssdk "github.com/aws/aws-sdk-go-v2/aws"
1416
"github.com/pkg/errors"
1517
corev1 "k8s.io/api/core/v1"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package shared_utils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
8+
)
9+
10+
func TestMakeLoadBalancerAttributeSliceFromMap(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
attributeMap map[string]string
14+
expected []elbv2model.LoadBalancerAttribute
15+
}{
16+
{
17+
name: "empty map",
18+
attributeMap: map[string]string{},
19+
expected: []elbv2model.LoadBalancerAttribute{},
20+
},
21+
{
22+
name: "single attribute",
23+
attributeMap: map[string]string{"key1": "value1"},
24+
expected: []elbv2model.LoadBalancerAttribute{{Key: "key1", Value: "value1"}},
25+
},
26+
{
27+
name: "multiple attributes",
28+
attributeMap: map[string]string{"key2": "value2", "key1": "value1", "key3": "value3"},
29+
expected: []elbv2model.LoadBalancerAttribute{
30+
{Key: "key1", Value: "value1"},
31+
{Key: "key2", Value: "value2"},
32+
{Key: "key3", Value: "value3"},
33+
},
34+
},
35+
{
36+
name: "attributes with special characters",
37+
attributeMap: map[string]string{"key-with-dash": "value with spaces", "key_with_underscore": "value=with=equals"},
38+
expected: []elbv2model.LoadBalancerAttribute{
39+
{Key: "key-with-dash", Value: "value with spaces"},
40+
{Key: "key_with_underscore", Value: "value=with=equals"},
41+
},
42+
},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
result := MakeLoadBalancerAttributeSliceFromMap(tt.attributeMap)
48+
assert.Equal(t, tt.expected, result)
49+
})
50+
}
51+
}
52+
53+
func TestMakeLoadBalancerAttributeSliceFromMap_Sorting(t *testing.T) {
54+
// Test that attributes are properly sorted by key
55+
attributeMap := map[string]string{
56+
"zebra": "last",
57+
"apple": "first",
58+
"banana": "middle",
59+
}
60+
61+
result := MakeLoadBalancerAttributeSliceFromMap(attributeMap)
62+
63+
expected := []elbv2model.LoadBalancerAttribute{
64+
{Key: "apple", Value: "first"},
65+
{Key: "banana", Value: "middle"},
66+
{Key: "zebra", Value: "last"},
67+
}
68+
69+
assert.Equal(t, expected, result)
70+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package shared_utils
2+
3+
import (
4+
"sort"
5+
elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
6+
)
7+
8+
func MakeAttributesSliceFromMap(loadBalancerAttributesMap map[string]string) []elbv2model.LoadBalancerAttribute {
9+
attributes := make([]elbv2model.LoadBalancerAttribute, 0, len(loadBalancerAttributesMap))
10+
for attrKey, attrValue := range loadBalancerAttributesMap {
11+
attributes = append(attributes, elbv2model.LoadBalancerAttribute{
12+
Key: attrKey,
13+
Value: attrValue,
14+
})
15+
}
16+
sort.Slice(attributes, func(i, j int) bool {
17+
return attributes[i].Key < attributes[j].Key
18+
})
19+
return attributes
20+
}

0 commit comments

Comments
 (0)