#4849·higress

Canary routes lose adjacency with their base when a Prefix / catch-all route coexists with other path types

Author: CH3CHOCreated Sep 29, 2026Updated Sep 29, 2026

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:

  1. The base route is a Prefix / route with no extra match conditions (i.e., it qualifies as isAllCatch in pkg/ingress/kube/common/tool.go:188).
  2. The canary route is the by-header / by-cookie / by-weight variant of that base, so it carries additional match conditions injected by ApplyByHeader and therefore does not qualify as isAllCatch.
  3. Other routes on the same host use a different pathType (typically Regex / 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:

yaml
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:

bash
curl http://localhost:8888/debug/configz

Expected 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 → base adjacent 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:

go
routes = append(routes[:pos+1], routes[pos:]...)
routes[pos] = canary

where 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:

  1. IsDefaultBackend routes are moved to the end of the list.
  2. isAllCatch routes (user-specified Prefix / with no extra match conditions) are moved to the end of the list.
  3. Within the same pathType, longer paths come first.
  4. Across different pathType values, the priority is Exact > Prefix > Regex.
  5. For the same path with the same match count, more specific method / header / query matchers 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:

  • frontend is Prefix / with no method/header/query in HTTPRoute.Match, so it qualifies as isAllCatch and is moved to the tail of the list.
  • frontend-canary has an additional cookie match injected by ApplyByHeader, so it does not qualify as isAllCatch and stays in the normal sort bucket.
  • backend and backend-canary are both Regex routes; neither qualifies as isAllCatch (the predicate only matches Prefix).
  • Cross-pathType comparison (Prefix > Regex) places frontend-canary ahead of all Regex routes.
  • Result: [frontend-canary, backend-canary, backend, frontend] — frontend-canary and its base frontend are separated by both Regex routes.

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

  1. 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.
  2. Unintuitive route table layout: canaries and bases are scattered, increasing the cost of reasoning about the overall canary strategy of a VirtualService.
  3. 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.

go
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 isAllCatch and similar decisions. O(N²) in the worst case.
  • Extend isAllCatch: make isAllCatch aware 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 — SortHTTPRoutes implementation
  • pkg/ingress/kube/common/tool.go:188-220 — isAllCatch predicate and its use
  • pkg/ingress/kube/ingress/controller.go:873-999 — ApplyCanaryIngress
  • pkg/ingress/kube/ingress/controller.go:982-995 — canary insertion position (immediately after base)
  • pkg/ingress/kube/ingress/controller.go:1178-1180 — isCanaryRoute association predicate
  • pkg/ingress/config/ingress_config.go:646-651 — SortHTTPRoutes invocation in convertVirtualService

Verification

  1. Apply the four ingresses above in a test cluster.
  2. Fetch the generated VirtualService via http://localhost:8888/debug/configz.
  3. For each canary route in the http list, verify that its base route is the immediate next entry.
  4. Unit tests: extend testApplyCanaryIngress in pkg/ingress/kube/ingress/controller_test.go with cases that combine Prefix / allCatch with Regex routes on the same host. Also add cases for pure Prefix and pure Regex configurations 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.