Skip to content

Commit

Permalink
Add comment for shortenHostname function
Browse files Browse the repository at this point in the history
Signed-off-by: Rizwana777 <rizwananaaz177@gmail.com>
  • Loading branch information
Rizwana777 committed Feb 8, 2024
1 parent adb9cfb commit 12b0d24
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions controllers/argocd/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ import (

var routeAPIFound = false

const (
maxLabelLength = 63
maxHostnameLength = 253
minFirstLabelSize1 = 20
)

// IsRouteAPIAvailable returns true if the Route API is present.
func IsRouteAPIAvailable() bool {
return routeAPIFound
Expand Down Expand Up @@ -382,6 +388,12 @@ func (r *ReconcileArgoCD) reconcileApplicationSetControllerWebhookRoute(cr *argo
return r.Client.Update(context.TODO(), route)
}

// The algorithm used by this function is:
// - If the FIRST label ("console-openshift-console" in the above case) is longer than 63 characters, shorten (truncate the end) it to 63.
// - If any other label is longer than 63 characters, return an error
// - After all the labels are 63 characters or less, check the length of the overall hostname:
// - If the overall hostname is > 255, then shorten the FIRST label until the host name is < 255
// - After the FIRST label has been shortened, if it is < 20, then return an error (this is a sanity test to ensure the label is likely to be unique)
func shortenHostname(hostname string) (string, error) {
if hostname == "" {
return "", nil
Expand All @@ -391,13 +403,13 @@ func shortenHostname(hostname string) (string, error) {
labels := strings.Split(hostname, ".")

// Check and truncate the FIRST label if longer than 63 characters
if len(labels[0]) > 63 {
labels[0] = labels[0][:63]
if len(labels[0]) > maxLabelLength {
labels[0] = labels[0][:maxLabelLength]
}

// Check other labels and return an error if any is longer than 63 characters
for _, label := range labels[1:] {
if len(label) > 63 {
if len(label) > maxLabelLength {
return "", fmt.Errorf("label length exceeds 63 characters")
}
}
Expand All @@ -406,15 +418,15 @@ func shortenHostname(hostname string) (string, error) {
resultHostname := strings.Join(labels, ".")

// Check and shorten the overall hostname
if len(resultHostname) > 253 {
// Shorten the first label until the length is less than 255
for len(resultHostname) > 253 && len(labels[0]) > 20 {
if len(resultHostname) > maxHostnameLength {
// Shorten the first label until the length is less than 253
for len(resultHostname) > maxHostnameLength && len(labels[0]) > 20 {
labels[0] = labels[0][:len(labels[0])-1]
resultHostname = strings.Join(labels, ".")
}

// Check if the first label is still less than 20 characters
if len(labels[0]) < 20 {
if len(labels[0]) < minFirstLabelSize1 {
return "", fmt.Errorf("shortened first label is less than 20 characters")
}
}
Expand Down

0 comments on commit 12b0d24

Please sign in to comment.