Skip to content

Commit 385f830

Browse files
fix: limit binding name length to 63 (#575)
1 parent d0d5688 commit 385f830

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

pkg/scheduler/framework/uniquename/uniquename.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
const (
18-
uuidLength = 6
18+
uuidLength = 8
1919
)
2020

2121
// minInt returns the smaller one of two integers.
@@ -27,7 +27,7 @@ func minInt(a, b int) int {
2727
}
2828

2929
// NewClusterResourceBindingName returns a unique name for a cluster resource binding in the
30-
// format of DNS subdomain names (RFC 1123).
30+
// format of DNS label names (RFC 1123). It will be used as a label on the work resource.
3131
//
3232
// The name is generated using the following format:
3333
// * [CRP-NAME] - [TARGET-CLUSTER-NAME] - [RANDOM-SUFFIX]
@@ -38,20 +38,20 @@ func minInt(a, b int) int {
3838
// of name collisions are extremely low.
3939
//
4040
// In addition, note that this function assumes that both the CRP name and the cluster name
41-
// are valid DNS subdomain names (RFC 1123).
41+
// are valid DNS label names (RFC 1123).
4242
func NewClusterResourceBindingName(CRPName string, clusterName string) (string, error) {
43-
reservedSlots := 2 + uuidLength // 2 dashs + 6 character UUID string
43+
reservedSlots := 2 + uuidLength // 2 dashs + 8 character UUID string
4444

45-
slotsPerSeg := (validation.DNS1123SubdomainMaxLength - reservedSlots) / 2
45+
slotsPerSeg := (validation.DNS1123LabelMaxLength - reservedSlots) / 2
4646
uniqueName := fmt.Sprintf("%s-%s-%s",
4747
CRPName[:minInt(slotsPerSeg, len(CRPName))],
48-
clusterName[:minInt(slotsPerSeg, len(clusterName))],
48+
clusterName[:minInt(slotsPerSeg+1, len(clusterName))],
4949
uuid.NewUUID()[:uuidLength],
5050
)
5151

52-
if errs := validation.IsDNS1123Subdomain(uniqueName); len(errs) != 0 {
52+
if errs := validation.IsDNS1123Label(uniqueName); len(errs) != 0 {
5353
// Do a sanity check here; normally this would not occur.
54-
return "", fmt.Errorf("failed to format a unique RFC 1123 DNS subdomain name with namespace %s, name %s: %v", CRPName, clusterName, errs)
54+
return "", fmt.Errorf("failed to format a unique RFC 1123 label name with CRP name %s, cluster name %s: %v", CRPName, clusterName, errs)
5555
}
5656
return uniqueName, nil
5757
}

pkg/scheduler/framework/uniquename/uniquename_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ const (
1515
crpName = "app"
1616
clusterName = "bravelion"
1717

18-
longName = "c7t2c6oppjnryqcihwweexeobs7tlmf08ha4qb5htc4cifzpalhb5ec2lbh3" +
19-
"j73reciaz2f0jfd2rl5qba6rzuuwgyw6d9e6la19bo89k41lphln4s4dy1gr" +
20-
"h1dvua17iu4ro61dxo91ayovns8cgnmshlsflmi68e3najm7dw5dqe17pih7" +
21-
"up0dtyvrqxyp90sxedbf"
18+
longName = "c7t2c6oppjnryqcihwweexeobs7tlmf08ha4qb5htc4cifzpalhb5ec2lbh3"
2219
)
2320

2421
// TO-DO (chenyu1): Expand the test cases as development proceeds.
@@ -44,8 +41,8 @@ func TestClusterResourceBindingUniqueName(t *testing.T) {
4441
name: "valid name (truncated)",
4542
crpName: longName,
4643
clusterName: longName,
47-
wantPrefix: fmt.Sprintf("%s-%s", longName[:122], longName[:122]),
48-
wantLength: 252,
44+
wantPrefix: fmt.Sprintf("%s-%s", longName[:26], longName[:27]),
45+
wantLength: 63,
4946
},
5047
{
5148
name: "invalid name",

pkg/webhook/fleetresourcehandler/fleetresourcehandler_webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"regexp"
88
"strings"
99

10-
fleetnetworkingv1alpha1 "go.goms.io/fleet-networking/api/v1alpha1"
1110
admissionv1 "k8s.io/api/admission/v1"
1211
"k8s.io/apimachinery/pkg/runtime"
1312
"k8s.io/apimachinery/pkg/types"
@@ -17,6 +16,7 @@ import (
1716
"sigs.k8s.io/controller-runtime/pkg/webhook"
1817
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
1918

19+
fleetnetworkingv1alpha1 "go.goms.io/fleet-networking/api/v1alpha1"
2020
clusterv1beta1 "go.goms.io/fleet/apis/cluster/v1beta1"
2121
fleetv1alpha1 "go.goms.io/fleet/apis/v1alpha1"
2222
"go.goms.io/fleet/pkg/utils"

0 commit comments

Comments
 (0)