From d295ee14927f5f0877a1bcc9bbeef0c8ed2d20cf Mon Sep 17 00:00:00 2001 From: Angel Misevski Date: Fri, 19 Jan 2024 15:37:07 -0500 Subject: [PATCH] Update how too-long endpoint hostnames are handled Update the hostname format used for endpoints whose hostnames are too long from -. to -. This is necessary as the iteration order through endpoints is random (iterating through Go maps is random), resulting in inconsistent numbers used for . Using a combination of workspace ID and endpoint name should always be valid: * Workspace IDs are 25 characters long * Endpoint names are restricted to max 15 characters by the Devfile API * Endpoint names and workspace IDs are required to be alphanumeric with dashes, starting and ending with an alphanumeric character * Endpoint names are unique across all endpoints in the workspace Signed-off-by: Angel Misevski --- controllers/devworkspace/solver/che_routing_test.go | 12 ++++++------ controllers/devworkspace/solver/endpoint_strategy.go | 6 ++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/controllers/devworkspace/solver/che_routing_test.go b/controllers/devworkspace/solver/che_routing_test.go index fb0f45f0c..4ff017192 100644 --- a/controllers/devworkspace/solver/che_routing_test.go +++ b/controllers/devworkspace/solver/che_routing_test.go @@ -1569,24 +1569,24 @@ func TestReportSubdomainExposedEndpointsLongUsername(t *testing.T) { if e1.Name != "e1" { t.Errorf("The first endpoint should have been e1 but is %s", e1.Name) } - if e1.Url != "https://wsid-1.down.on.earth/1/" { - t.Errorf("The e1 endpoint should have the following URL: '%s' but has '%s'.", "https://wsid-1.down.on.earth/1/", e1.Url) + if e1.Url != "https://wsid-e1.down.on.earth/1/" { + t.Errorf("The e1 endpoint should have the following URL: '%s' but has '%s'.", "https://wsid-e1.down.on.earth/1/", e1.Url) } e2 := m1[1] if e2.Name != "e2" { t.Errorf("The second endpoint should have been e2 but is %s", e1.Name) } - if e2.Url != "https://wsid-2.down.on.earth/2.js" { - t.Errorf("The e2 endpoint should have the following URL: '%s' but has '%s'.", "https://wsid-2.down.on.earth/2.js", e2.Url) + if e2.Url != "https://wsid-e2.down.on.earth/2.js" { + t.Errorf("The e2 endpoint should have the following URL: '%s' but has '%s'.", "https://wsid-e2.down.on.earth/2.js", e2.Url) } e3 := m1[2] if e3.Name != "e3" { t.Errorf("The third endpoint should have been e3 but is %s", e1.Name) } - if e3.Url != "http://wsid-3.down.on.earth/" { - t.Errorf("The e3 endpoint should have the following URL: '%s' but has '%s'.", "https://wsid-3.down.on.earth/", e3.Url) + if e3.Url != "http://wsid-e3.down.on.earth/" { + t.Errorf("The e3 endpoint should have the following URL: '%s' but has '%s'.", "https://wsid-e3.down.on.earth/", e3.Url) } } diff --git a/controllers/devworkspace/solver/endpoint_strategy.go b/controllers/devworkspace/solver/endpoint_strategy.go index 3d87af2b9..78e66517e 100644 --- a/controllers/devworkspace/solver/endpoint_strategy.go +++ b/controllers/devworkspace/solver/endpoint_strategy.go @@ -72,8 +72,10 @@ func (u UsernameWkspName) getEndpointPathPrefix(endpointPath string) string { func (u UsernameWkspName) getHostname(endpointInfo *EndpointInfo, baseDomain string) string { subDomain := fmt.Sprintf("%s-%s-%s", u.username, u.workspaceName, endpointInfo.endpointName) if errs := validation.IsValidLabelValue(subDomain); len(errs) > 0 { - // if subdomain is not valid, use legacy paths - return fmt.Sprintf("%s-%d.%s", u.workspaceID, endpointInfo.order, baseDomain) + // If subdomain is not valid (e.g. too long), use alternate format + // The below should always be under 63 characters, as endpoint names are limited to 15 characters and workspace IDs are + // 25 characters. + return fmt.Sprintf("%s-%s.%s", u.workspaceID, endpointInfo.endpointName, baseDomain) } return fmt.Sprintf("%s.%s", subDomain, baseDomain)