From 11f56fd125381164201dc5f881ffb5aaa9f71975 Mon Sep 17 00:00:00 2001 From: vixns Date: Fri, 5 Apr 2024 18:24:16 +0200 Subject: [PATCH] Change route sorting order to Exact > RegularExpression > PathPrefix (#2579) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Change route sorting order to Exact > RegularExpression > PathPrefix kubernetes-sigs/gateway-api#1770 kubernetes-sigs/gateway-api#1855 Signed-off-by: Stéphane Cottin --- internal/gatewayapi/sort.go | 20 +++++++++---------- .../grpcroute-with-service-match.out.yaml | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/internal/gatewayapi/sort.go b/internal/gatewayapi/sort.go index 00a5fc6389d..75d9ebc503a 100644 --- a/internal/gatewayapi/sort.go +++ b/internal/gatewayapi/sort.go @@ -18,33 +18,33 @@ func (x XdsIRRoutes) Swap(i, j int) { x[i], x[j] = x[j], x[i] } func (x XdsIRRoutes) Less(i, j int) bool { // 1. Sort based on path match type - // Exact > PathPrefix > RegularExpression + // Exact > RegularExpression > PathPrefix if x[i].PathMatch != nil && x[i].PathMatch.Exact != nil { if x[j].PathMatch != nil { - if x[j].PathMatch.Prefix != nil { + if x[j].PathMatch.SafeRegex != nil { return false } - if x[j].PathMatch.SafeRegex != nil { + if x[j].PathMatch.Prefix != nil { return false } } } - if x[i].PathMatch != nil && x[i].PathMatch.Prefix != nil { + if x[i].PathMatch != nil && x[i].PathMatch.SafeRegex != nil { if x[j].PathMatch != nil { if x[j].PathMatch.Exact != nil { return true } - if x[j].PathMatch.SafeRegex != nil { + if x[j].PathMatch.Prefix != nil { return false } } } - if x[i].PathMatch != nil && x[i].PathMatch.SafeRegex != nil { + if x[i].PathMatch != nil && x[i].PathMatch.Prefix != nil { if x[j].PathMatch != nil { if x[j].PathMatch.Exact != nil { return true } - if x[j].PathMatch.Prefix != nil { + if x[j].PathMatch.SafeRegex != nil { return true } } @@ -96,12 +96,12 @@ func pathMatchCount(pathMatch *ir.StringMatch) int { if pathMatch.Exact != nil { return len(*pathMatch.Exact) } - if pathMatch.Prefix != nil { - return len(*pathMatch.Prefix) - } if pathMatch.SafeRegex != nil { return len(*pathMatch.SafeRegex) } + if pathMatch.Prefix != nil { + return len(*pathMatch.Prefix) + } } return 0 } diff --git a/internal/gatewayapi/testdata/grpcroute-with-service-match.out.yaml b/internal/gatewayapi/testdata/grpcroute-with-service-match.out.yaml index 9624ffcaab1..ca598b9d046 100644 --- a/internal/gatewayapi/testdata/grpcroute-with-service-match.out.yaml +++ b/internal/gatewayapi/testdata/grpcroute-with-service-match.out.yaml @@ -126,11 +126,11 @@ xdsIR: weight: 1 hostname: '*' isHTTP2: true - name: grpcroute/default/grpcroute-1/rule/0/match/0/* + name: grpcroute/default/grpcroute-1/rule/0/match/1/* pathMatch: distinct: false name: "" - prefix: /com.ExampleExact + safeRegex: /com.[A-Z]+/[A-Za-z_][A-Za-z_0-9]* - backendWeights: invalid: 0 valid: 0 @@ -145,8 +145,8 @@ xdsIR: weight: 1 hostname: '*' isHTTP2: true - name: grpcroute/default/grpcroute-1/rule/0/match/1/* + name: grpcroute/default/grpcroute-1/rule/0/match/0/* pathMatch: distinct: false name: "" - safeRegex: /com.[A-Z]+/[A-Za-z_][A-Za-z_0-9]* + prefix: /com.ExampleExact