Skip to content

Commit 7423f0f

Browse files
authored
reject custom load balancer name longer than 32 characters (#2295)
1 parent 314a9fd commit 7423f0f

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

docs/guide/ingress/annotations.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +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. Name longer than 32 characters will be trimmed.
161-
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 treated as an error.
162161
!!!note "Merge Behavior"
163162
`name` is exclusive across all Ingresses in an IngressGroup.
164163

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. Name longer than 32 characters will be trimmed.
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 treated as an error.
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (t *defaultModelBuildTask) buildLoadBalancerName(_ context.Context, scheme
100100
name, _ := explicitNames.PopAny()
101101
// The name of the loadbalancer can only have up to 32 characters
102102
if len(name) > 32 {
103-
name = name[:32]
103+
return "", errors.New("load balancer name cannot be longer than 32 characters")
104104
}
105105
return name, nil
106106
}

pkg/ingress/model_build_load_balancer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ func Test_defaultModelBuildTask_buildLoadBalancerName(t *testing.T) {
579579
},
580580
scheme: elbv2.LoadBalancerSchemeInternetFacing,
581581
},
582-
want: "bazbazfoofoobazbazfoofoobazbazfo",
582+
wantErr: errors.New("load balancer name cannot be longer than 32 characters"),
583583
},
584584
{
585585
name: "name annotation on single ingress only",

pkg/service/model_build_load_balancer.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ func (t *defaultModelBuildTask) buildLoadBalancerSpec(ctx context.Context, schem
5757
if err != nil {
5858
return elbv2model.LoadBalancerSpec{}, err
5959
}
60-
name := t.buildLoadBalancerName(ctx, scheme)
60+
name, err := t.buildLoadBalancerName(ctx, scheme)
61+
if err != nil {
62+
return elbv2model.LoadBalancerSpec{}, err
63+
}
6164
spec := elbv2model.LoadBalancerSpec{
6265
Name: name,
6366
Type: elbv2model.LoadBalancerTypeNetwork,
@@ -354,14 +357,14 @@ func (t *defaultModelBuildTask) getAnnotationSpecificLbAttributes() (map[string]
354357

355358
var invalidLoadBalancerNamePattern = regexp.MustCompile("[[:^alnum:]]")
356359

357-
func (t *defaultModelBuildTask) buildLoadBalancerName(_ context.Context, scheme elbv2model.LoadBalancerScheme) string {
360+
func (t *defaultModelBuildTask) buildLoadBalancerName(_ context.Context, scheme elbv2model.LoadBalancerScheme) (string, error) {
358361
var name string
359362
if exists := t.annotationParser.ParseStringAnnotation(annotations.SvcLBSuffixLoadBalancerName, &name, t.service.Annotations); exists {
360363
// The name of the loadbalancer can only have up to 32 characters
361364
if len(name) > 32 {
362-
name = name[:32]
365+
return "", errors.New("load balancer name cannot be longer than 32 characters")
363366
}
364-
return name
367+
return name, nil
365368
}
366369
uuidHash := sha256.New()
367370
_, _ = uuidHash.Write([]byte(t.clusterName))
@@ -371,5 +374,5 @@ func (t *defaultModelBuildTask) buildLoadBalancerName(_ context.Context, scheme
371374

372375
sanitizedNamespace := invalidLoadBalancerNamePattern.ReplaceAllString(t.service.Namespace, "")
373376
sanitizedName := invalidLoadBalancerNamePattern.ReplaceAllString(t.service.Name, "")
374-
return fmt.Sprintf("k8s-%.8s-%.8s-%.10s", sanitizedNamespace, sanitizedName, uuid)
377+
return fmt.Sprintf("k8s-%.8s-%.8s-%.10s", sanitizedNamespace, sanitizedName, uuid), nil
375378
}

pkg/service/model_build_load_balancer_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ func Test_defaultModelBuildTask_buildLoadBalancerName(t *testing.T) {
912912
clusterName string
913913
scheme elbv2.LoadBalancerScheme
914914
want string
915+
wantErrerror
915916
}{
916917
{
917918
name: "no name annotation",
@@ -940,7 +941,7 @@ func Test_defaultModelBuildTask_buildLoadBalancerName(t *testing.T) {
940941
want: "baz",
941942
},
942943
{
943-
name: "trim name annotation",
944+
name: "reject name longer than 32 characters",
944945
service: &corev1.Service{
945946
ObjectMeta: metav1.ObjectMeta{
946947
Namespace: "foo",
@@ -950,7 +951,7 @@ func Test_defaultModelBuildTask_buildLoadBalancerName(t *testing.T) {
950951
},
951952
},
952953
},
953-
want: "bazbazfoofoobazbazfoofoobazbazfo",
954+
wantErr: errors.New("load balancer name cannot be longer than 32 characters"),
954955
},
955956
}
956957
for _, tt := range tests {
@@ -960,8 +961,12 @@ func Test_defaultModelBuildTask_buildLoadBalancerName(t *testing.T) {
960961
clusterName: tt.clusterName,
961962
annotationParser: annotations.NewSuffixAnnotationParser("service.beta.kubernetes.io"),
962963
}
963-
got := task.buildLoadBalancerName(context.Background(), tt.scheme)
964-
assert.Equal(t, tt.want, got)
964+
got, err := task.buildLoadBalancerName(context.Background(), tt.scheme)
965+
if err != nil {
966+
assert.EqualError(t, err, tt.wantErr.Error())
967+
} else {
968+
assert.Equal(t, tt.want, got)
969+
}
965970
})
966971
}
967972
}

0 commit comments

Comments
 (0)