Skip to content

Commit 08277aa

Browse files
committed
Trim length for custom lb name for ALBs
1 parent 11f8903 commit 08277aa

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
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.

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{

0 commit comments

Comments
 (0)