Skip to content

Commit bda8453

Browse files
authored
fix: calculateHTTPRoutePriority add hosts args (#137)
Signed-off-by: ashing <[email protected]>
1 parent 5fa6c01 commit bda8453

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

internal/provider/adc/translator/httproute.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,12 @@ func (t *Translator) translateBackendRef(tctx *provider.TranslateContext, ref ga
359359

360360
// calculateHTTPRoutePriority calculates the priority of the HTTP route.
361361
// ref: https://github.com/Kong/kubernetes-ingress-controller/blob/57472721319e2c63e56cb8540425257e8e02520f/internal/dataplane/translator/subtranslator/httproute_atc.go#L279-L296
362-
func calculateHTTPRoutePriority(match *gatewayv1.HTTPRouteMatch, ruleIndex int) uint64 {
362+
func calculateHTTPRoutePriority(match *gatewayv1.HTTPRouteMatch, ruleIndex int, hosts []string) uint64 {
363363
const (
364+
// PreciseHostnameShiftBits assigns bit 43-50 for the length of hostname(max length=253).
365+
PreciseHostnameShiftBits = 43
366+
// HostnameLengthShiftBits assigns bits 35-42 for the length of hostname(max length=253).
367+
HostnameLengthShiftBits = 35
364368
// ExactPathShiftBits assigns bit 34 to mark if the match is exact path match.
365369
ExactPathShiftBits = 34
366370
// PathLengthShiftBits assigns bits 23-32 to path length. (max length = 1024, but must start with /)
@@ -375,7 +379,35 @@ func calculateHTTPRoutePriority(match *gatewayv1.HTTPRouteMatch, ruleIndex int)
375379
RuleIndexShiftBits = 7
376380
)
377381

378-
var priority uint64 = 0
382+
var (
383+
priority uint64 = 0
384+
// Handle hostname priority
385+
// 1. Non-wildcard hostname priority
386+
// 2. Hostname length priority
387+
maxNonWildcardLength = 0
388+
maxHostnameLength = 0
389+
)
390+
391+
for _, host := range hosts {
392+
isNonWildcard := !strings.Contains(host, "*")
393+
394+
if isNonWildcard && len(host) > maxNonWildcardLength {
395+
maxNonWildcardLength = len(host)
396+
}
397+
398+
if len(host) > maxHostnameLength {
399+
maxHostnameLength = len(host)
400+
}
401+
}
402+
403+
// If there is a non-wildcard hostname, set the PreciseHostnameShiftBits bit
404+
if maxNonWildcardLength > 0 {
405+
priority |= (uint64(maxNonWildcardLength) << PreciseHostnameShiftBits)
406+
}
407+
408+
if maxHostnameLength > 0 {
409+
priority |= (uint64(maxHostnameLength) << HostnameLengthShiftBits)
410+
}
379411

380412
// ExactPathShiftBits
381413
if match.Path != nil && match.Path.Type != nil && *match.Path.Type == gatewayv1.PathMatchExact {
@@ -495,7 +527,7 @@ func (t *Translator) TranslateHTTPRoute(tctx *provider.TranslateContext, httpRou
495527
route.EnableWebsocket = ptr.To(true)
496528

497529
// Set the route priority
498-
priority := calculateHTTPRoutePriority(&match, ruleIndex)
530+
priority := calculateHTTPRoutePriority(&match, ruleIndex, hosts)
499531
route.Priority = ptr.To(int64(priority))
500532

501533
routes = append(routes, route)

0 commit comments

Comments
 (0)