Skip to content

Commit a2d6670

Browse files
bnuttTimothy-Dougherty
authored andcommitted
Set maximum length for custom load-balancer-name (kubernetes-sigs#2195)
* Set maximum length for custom load-balancer-name AWS only allows loadbalancer names with a length up to 32 characters, this change trims the length of the provided value when a custom load balancer name is provided. * Trim length for custom lb name for ALBs
1 parent f0e7626 commit a2d6670

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

docs/guide/ingress/annotations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ Traffic Listening can be controlled with following annotations:
157157
## Traffic Routing
158158
Traffic Routing can be controlled with following annotations:
159159

160-
- <a name="load-balancer-name">`alb.ingress.kubernetes.io/load-balancer-name`</a> specifies the custom name to use for the load balancer.
160+
- <a name="load-balancer-name">`alb.ingress.kubernetes.io/load-balancer-name`</a> specifies the custom name to use for the load balancer. Name longer than 32 characters will be trimmed.
161161

162162
!!!note "Merge Behavior"
163163
`name` is exclusive across all Ingresses in an IngressGroup.

docs/guide/service/annotations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
## Traffic Routing
4646
Traffic Routing can be controlled with following annotations:
4747

48-
- <a name="load-balancer-name">`service.beta.kubernetes.io/aws-load-balancer-name`</a> specifies the custom name to use for the load balancer.
48+
- <a name="load-balancer-name">`service.beta.kubernetes.io/aws-load-balancer-name`</a> specifies the custom name to use for the load balancer. Name longer than 32 characters will be trimmed.
4949

5050
!!!note "limitations"
5151
- If you modify this annotation after service creation, there is no effect.

pkg/ingress/model_build_load_balancer.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"encoding/hex"
77
"fmt"
88
"regexp"
9-
"sigs.k8s.io/aws-load-balancer-controller/pkg/deploy/tracking"
109
"strings"
1110

11+
"sigs.k8s.io/aws-load-balancer-controller/pkg/deploy/tracking"
12+
1213
awssdk "github.com/aws/aws-sdk-go/aws"
1314
ec2sdk "github.com/aws/aws-sdk-go/service/ec2"
1415
"github.com/google/go-cmp/cmp"
@@ -97,6 +98,10 @@ func (t *defaultModelBuildTask) buildLoadBalancerName(_ context.Context, scheme
9798
}
9899
if len(explicitNames) == 1 {
99100
name, _ := explicitNames.PopAny()
101+
// The name of the loadbalancer can only have up to 32 characters
102+
if len(name) > 32 {
103+
name = name[:32]
104+
}
100105
return name, nil
101106
}
102107
if len(explicitNames) > 1 {

pkg/ingress/model_build_load_balancer_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package ingress
33
import (
44
"context"
55
"errors"
6+
"testing"
7+
68
awssdk "github.com/aws/aws-sdk-go/aws"
79
"github.com/stretchr/testify/assert"
810
networking "k8s.io/api/networking/v1beta1"
911
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1012
"k8s.io/apimachinery/pkg/util/sets"
1113
"sigs.k8s.io/aws-load-balancer-controller/pkg/annotations"
1214
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
13-
"testing"
1415
)
1516

1617
func Test_defaultModelBuildTask_buildLoadBalancerCOIPv4Pool(t *testing.T) {
@@ -557,6 +558,29 @@ func Test_defaultModelBuildTask_buildLoadBalancerName(t *testing.T) {
557558
},
558559
want: "foo",
559560
},
561+
{
562+
name: "trim name annotation",
563+
fields: fields{
564+
ingGroup: Group{
565+
ID: GroupID{Namespace: "awesome-ns", Name: "ing-1"},
566+
Members: []ClassifiedIngress{
567+
{
568+
Ing: &networking.Ingress{
569+
ObjectMeta: metav1.ObjectMeta{
570+
Namespace: "awesome-ns",
571+
Name: "ing-1",
572+
Annotations: map[string]string{
573+
"alb.ingress.kubernetes.io/load-balancer-name": "bazbazfoofoobazbazfoofoobazbazfoo",
574+
},
575+
},
576+
},
577+
},
578+
},
579+
},
580+
scheme: elbv2.LoadBalancerSchemeInternetFacing,
581+
},
582+
want: "bazbazfoofoobazbazfoofoobazbazfo",
583+
},
560584
{
561585
name: "name annotation on single ingress only",
562586
fields: fields{

pkg/service/model_build_load_balancer.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77
"fmt"
88
"net"
99
"regexp"
10-
"sigs.k8s.io/aws-load-balancer-controller/pkg/algorithm"
11-
elbv2deploy "sigs.k8s.io/aws-load-balancer-controller/pkg/deploy/elbv2"
1210
"sort"
1311
"strconv"
1412

13+
"sigs.k8s.io/aws-load-balancer-controller/pkg/algorithm"
14+
elbv2deploy "sigs.k8s.io/aws-load-balancer-controller/pkg/deploy/elbv2"
15+
1516
"github.com/aws/aws-sdk-go/aws"
1617
"github.com/aws/aws-sdk-go/service/ec2"
1718
"github.com/pkg/errors"
@@ -356,6 +357,10 @@ var invalidLoadBalancerNamePattern = regexp.MustCompile("[[:^alnum:]]")
356357
func (t *defaultModelBuildTask) buildLoadBalancerName(_ context.Context, scheme elbv2model.LoadBalancerScheme) string {
357358
var name string
358359
if exists := t.annotationParser.ParseStringAnnotation(annotations.SvcLBSuffixLoadBalancerName, &name, t.service.Annotations); exists {
360+
// The name of the loadbalancer can only have up to 32 characters
361+
if len(name) > 32 {
362+
name = name[:32]
363+
}
359364
return name
360365
}
361366
uuidHash := sha256.New()

pkg/service/model_build_load_balancer_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package service
33
import (
44
"context"
55
"errors"
6+
"testing"
7+
68
elbv2sdk "github.com/aws/aws-sdk-go/service/elbv2"
79
"k8s.io/apimachinery/pkg/util/sets"
810
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/core"
911
"sigs.k8s.io/aws-load-balancer-controller/pkg/networking"
10-
"testing"
1112

1213
"github.com/aws/aws-sdk-go/aws"
1314
"github.com/aws/aws-sdk-go/service/ec2"
@@ -938,6 +939,19 @@ func Test_defaultModelBuildTask_buildLoadBalancerName(t *testing.T) {
938939
},
939940
want: "baz",
940941
},
942+
{
943+
name: "trim name annotation",
944+
service: &corev1.Service{
945+
ObjectMeta: metav1.ObjectMeta{
946+
Namespace: "foo",
947+
Name: "bar",
948+
Annotations: map[string]string{
949+
"service.beta.kubernetes.io/aws-load-balancer-name": "bazbazfoofoobazbazfoofoobazbazfoo",
950+
},
951+
},
952+
},
953+
want: "bazbazfoofoobazbazfoofoobazbazfo",
954+
},
941955
}
942956
for _, tt := range tests {
943957
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)