Canary routes lose adjacency with their base when a Prefix / catch-all route coexists with other path types
Background
In Higress, when a canary ingress (e.g., one annotated with higress.io/canary: "true") is processed, ApplyCanaryIngress in pkg/ingress/kube/ingress/controller.go injects the canary route into the host's HTTPRoutes slice immediately after its base route. This preserves the nginx-ingress semantic that a canary is a conditional replacement of its base.
However, once all ingresses for a host are converted, convertVirtualService in pkg/ingress/config/ingress_config.go calls common.SortHTTPRoutes(routes) to apply global ordering. Under a specific set of conditions, the canary route ends up reordered away from its base, breaking the canary-base adjacency that operators rely on for readability.
When Does the Bug Occur?
The bug only manifests when all three of the following conditions hold on the same host:
- The base route is a
Prefix /route with no extra match conditions (i.e., it qualifies asisAllCatchinpkg/ingress/kube/common/tool.go:188). - The canary route is the by-header / by-cookie / by-weight variant of that base, so it carries additional match conditions injected by
ApplyByHeaderand therefore does not qualify asisAllCatch. - Other routes on the same host use a different
pathType(typicallyRegex/ImplementationSpecific).
When these conditions are met, the isAllCatch rule moves the base to the tail of the list, while the canary stays in the normal sort bucket. Cross-pathType comparison (Prefix > Regex) then places the canary ahead of all unrelated Regex routes, and the unrelated routes are inserted between the canary and its base.
Pure single-pathType configurations (only Prefix routes, or only Regex routes) do not exhibit this issue, because canary and base end up in the same sort bucket and the stable sort keeps them adjacent.
Steps to Reproduce
Apply the following four ingresses in the higress-system namespace:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: frontend
namespace: higress-system
annotations:
higress.io/destination: home-service.default.svc.cluster.local:8080
spec:
ingressClassName: higress
rules:
- host: www.foo.com
http:
paths:
- path: /
pathType: Prefix
backend:
resource: { apiGroup: networking.higress.io, kind: McpBridge, name: default }
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: frontend-canary
namespace: higress-system
annotations:
higress.io/canary: "true"
higress.io/canary-by-cookie: x-gray-frontend
higress.io/destination: foo-service.default.svc.cluster.local:8080
spec:
ingressClassName: higress
rules:
- host: www.foo.com
http:
paths:
- path: /
pathType: Prefix
backend:
resource: { apiGroup: networking.higress.io, kind: McpBridge, name: default }
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: backend
namespace: higress-system
annotations:
higress.io/destination: home-service.default.svc.cluster.local:8080
higress.io/use-regex: "true"
spec:
ingressClassName: higress
rules:
- host: www.foo.com
http:
paths:
- path: /api/(.*)
pathType: Prefix
backend:
resource: { apiGroup: networking.higress.io, kind: McpBridge, name: default }
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: backend-canary
namespace: higress-system
annotations:
higress.io/canary: "true"
higress.io/canary-by-header: x-gray-user
higress.io/canary-by-header-value: canary
higress.io/destination: bar-service.default.svc.cluster.local:8080
higress.io/use-regex: "true"
spec:
ingressClassName: higress
rules:
- host: www.foo.com
http:
paths:
- path: /api/(.*)
pathType: Prefix
backend:
resource: { apiGroup: networking.higress.io, kind: McpBridge, name: default }Fetch the generated VirtualService from the controller's debug endpoint:
curl http://localhost:8888/debug/configzExpected Behavior
Each canary route should be adjacent to its base route, with the pair participating in global sorting as a single logical unit. The canary (which carries the more specific match) comes first, the base follows:
[backend-canary, backend, frontend-canary, frontend]Rationale:
- In nginx-ingress semantics, a canary is a conditional replacement of its base route and should be bound together.
- Operators expect to see
canary → baseadjacent in the route list, not scattered across the virtual host. - A
(canary, base)pair shares the same path; their relative position should not be perturbed by unrelated routes.
Actual Behavior
[frontend-canary, backend-canary, backend, frontend]frontend-canary (Prefix, cookie canary) and frontend (Prefix allCatch) end up at opposite ends of the list, with backend-canary and backend inserted between them.
Root Cause Analysis
1. Insertion phase: canary is correctly placed adjacent to base
In pkg/ingress/kube/ingress/controller.go:982-995, the canary route is inserted via:
routes = append(routes[:pos+1], routes[pos:]...)
routes[pos] = canarywhere pos is the index of the base route in the slice. After this step, the canary sits right next to its base.
2. Sort phase: SortHTTPRoutes reorders canary away from base
SortHTTPRoutes (pkg/ingress/kube/common/tool.go:183-267) applies the following ordering:
IsDefaultBackendroutes are moved to the end of the list.isAllCatchroutes (user-specifiedPrefix /with no extra match conditions) are moved to the end of the list.- Within the same
pathType, longer paths come first. - Across different
pathTypevalues, the priority isExact > Prefix > Regex. - For the same path with the same match count, more specific
method/header/querymatchers come first.
isAllCatch is a Higress-internal sort step. The accompanying comment (// refer https://gateway-api.sigs.k8s.io/v1alpha2/references/spec/#httprouterule) is misleading — the Gateway API spec does not require that "user-specified root paths go last"; it only states that when matches is unspecified the default is Prefix /. See the Gateway API HTTPRouteRule reference for the actual precedence rules:
Across all rules specified on applicable Routes, precedence must be given to the match having:
- "Exact" path match.
- "Prefix" path match with largest number of characters.
- Method match.
- Largest number of header matches.
- Largest number of query param matches.
There is no "user-specified root path goes last" rule in the spec.
3. Why the three conditions trigger the bug
Walking through the comparator with the reproduction's routes:
frontendisPrefix /with no method/header/query inHTTPRoute.Match, so it qualifies asisAllCatchand is moved to the tail of the list.frontend-canaryhas an additional cookie match injected byApplyByHeader, so it does not qualify asisAllCatchand stays in the normal sort bucket.backendandbackend-canaryare bothRegexroutes; neither qualifies asisAllCatch(the predicate only matchesPrefix).- Cross-
pathTypecomparison (Prefix > Regex) placesfrontend-canaryahead of allRegexroutes. - Result:
[frontend-canary, backend-canary, backend, frontend]—frontend-canaryand its basefrontendare separated by bothRegexroutes.
4. Why pure single-pathType configs do not trigger this
If all routes are Prefix (or all are Regex), isAllCatch cannot split canary and base into different sort buckets, because the canary inherits the same path and pathType as its base, and the stable sort keeps them adjacent whenever their sort keys are equal. The bug only manifests when isAllCatch moves the base out of the canary's bucket and other pathType routes can be inserted into the gap.
Impact
- Poor operational/debugging experience: when reading the controller configz output, operators cannot intuitively locate the base of a given canary and must reverse-lookup via
RuleKey. - Unintuitive route table layout: canaries and bases are scattered, increasing the cost of reasoning about the overall canary strategy of a VirtualService.
- Semantic drift: while Envoy's actual matching behavior is unchanged (canary routes generally have more specific match conditions than their bases and therefore win), the semantic grouping of the route table is broken, contradicting the nginx-ingress semantic model that Higress inherits.
Proposed Solutions
Recommended: Group-based sorting
Treat (canary, base) as a single logical unit. Sort units against each other using Gateway API specificity keyed off the base's attributes, and within each unit place canary first, base second.
type routePair struct {
canary *WrapperHTTPRoute
base *WrapperHTTPRoute
}
func SortHTTPRoutes(routes []*WrapperHTTPRoute) {
pairs := groupByBaseKey(routes) // group by RuleKey
sort.SliceStable(pairs, func(i, j int) bool {
return routeLess(pairs[i].base, pairs[j].base) // base attributes represent the group
})
// Emit: within each group, canary first, then base
routes = routes[:0]
for _, p := range pairs {
if p.canary != nil {
routes = append(routes, p.canary)
}
if p.base != nil {
routes = append(routes, p.base)
}
}
}Alternatives
- Post-process: keep the current sort logic, then scan the sorted slice and move any canary back next to its base. Smallest diff but mixes concerns.
- Comparator lookup: in the comparator, look up the base route and use its attributes for
isAllCatchand similar decisions. O(N²) in the worst case. - Extend
isAllCatch: makeisAllCatchaware of canary variants so that the canary of an allCatch base is also treated as allCatch. Smallest change but tightest coupling.
Related Code
pkg/ingress/kube/common/tool.go:183-267—SortHTTPRoutesimplementationpkg/ingress/kube/common/tool.go:188-220—isAllCatchpredicate and its usepkg/ingress/kube/ingress/controller.go:873-999—ApplyCanaryIngresspkg/ingress/kube/ingress/controller.go:982-995— canary insertion position (immediately after base)pkg/ingress/kube/ingress/controller.go:1178-1180—isCanaryRouteassociation predicatepkg/ingress/config/ingress_config.go:646-651—SortHTTPRoutesinvocation inconvertVirtualService
Verification
- Apply the four ingresses above in a test cluster.
- Fetch the generated VirtualService via
http://localhost:8888/debug/configz. - For each canary route in the
httplist, verify that its base route is the immediate next entry. - Unit tests: extend
testApplyCanaryIngressinpkg/ingress/kube/ingress/controller_test.gowith cases that combinePrefix /allCatch withRegexroutes on the same host. Also add cases for purePrefixand pureRegexconfigurations to confirm the bug does not regress there.
Suggested Priority
Low to medium. No functional regression: Envoy still matches based on list order, and canary routes usually have more specific match conditions than their bases. However, readability and maintainability suffer, and the resulting layout contradicts the nginx-ingress semantic model that Higress inherits. Recommend handling as an improvement in a future release.
Source: higress-group/higress