Skip to content

Commit

Permalink
Sort the httproute first
Browse files Browse the repository at this point in the history
Signed-off-by: lubronzhan <lubronzhan@gmail.com>
  • Loading branch information
lubronzhan committed Feb 8, 2024
1 parent 0809407 commit 91f1ebc
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion internal/dag/gatewayapi_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"net/http"
"regexp"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -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{})
}

Expand Down Expand Up @@ -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
}

0 comments on commit 91f1ebc

Please sign in to comment.