From 91f1ebc847d5ee242e874bdc78f06c8e086f6986 Mon Sep 17 00:00:00 2001 From: lubronzhan Date: Thu, 8 Feb 2024 00:24:27 -0800 Subject: [PATCH] Sort the httproute first Signed-off-by: lubronzhan --- internal/dag/gatewayapi_processor.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/internal/dag/gatewayapi_processor.go b/internal/dag/gatewayapi_processor.go index 5c78b3993a3..efdb50b3d21 100644 --- a/internal/dag/gatewayapi_processor.go +++ b/internal/dag/gatewayapi_processor.go @@ -18,6 +18,7 @@ import ( "fmt" "net/http" "regexp" + "sort" "strings" "time" @@ -156,8 +157,10 @@ func (p *GatewayAPIProcessor) Run(dag *DAG, source *KubernetesCache) { // to each Listener so we can set status properly. listenerAttachedRoutes := map[string]int{} + // sort httproutes based on age/name first + sortedHTTPRoutes := sortRoutes(p.source.httproutes) // Process HTTPRoutes. - for _, httpRoute := range p.source.httproutes { + for _, httpRoute := range sortedHTTPRoutes { p.processRoute(KindHTTPRoute, httpRoute, httpRoute.Spec.ParentRefs, gatewayNotProgrammedCondition, listenerInfos, listenerAttachedRoutes, &gatewayapi_v1beta1.HTTPRoute{}) } @@ -2348,3 +2351,21 @@ func handlePathRewritePrefixRemoval(p *PathRewritePolicy, mc *matchConditions) * return p } + +// sort routes based on age in ascending order +// if creation times are the same, sort based on name in ascending order +func sortRoutes(m map[types.NamespacedName]*gatewayapi_v1beta1.HTTPRoute) []*gatewayapi_v1beta1.HTTPRoute { + routes := []*gatewayapi_v1beta1.HTTPRoute{} + for _, r := range m { + routes = append(routes, r) + } + sort.SliceStable(routes, func(i, j int) bool { + // compare creation time first, then the name + if routes[i].CreationTimestamp.Equal(&routes[j].CreationTimestamp) { + return routes[i].Name < routes[j].Name + } + return routes[i].CreationTimestamp.Before(&routes[j].CreationTimestamp) + }) + + return routes +}