Skip to content

fix: route Hostname/ServiceEntry backendRefs to the requested port on multi-port hosts#14212

Merged
ymesika merged 4 commits into
kgateway-dev:mainfrom
ymesika:serviceentry-backendref-multiport-routing
Jun 9, 2026
Merged

fix: route Hostname/ServiceEntry backendRefs to the requested port on multi-port hosts#14212
ymesika merged 4 commits into
kgateway-dev:mainfrom
ymesika:serviceentry-backendref-multiport-routing

Conversation

@ymesika

@ymesika ymesika commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Description

An HTTPRoute backendRef of kind: Hostname or kind: ServiceEntry with an explicit port could route to the wrong port's cluster when the target ServiceEntry defines multiple ports for the same host.

Example: a ServiceEntry exposing both :80 and :443 on se.example.com, with an HTTPRoute backendRef of port: 80, routes to the ..._443 cluster instead of ..._80. The route shows Accepted/ResolvedRefs, so it only fails at runtime as wrong upstream routing (e.g. 400 Client sent an HTTP request to an HTTPS server, or 503 against an HTTP-only backend).

Root cause

ServiceEntry backends are aliased into the BackendIndex keyed by backendKey, which embeds ir.ObjectSource plus a port. The KRT index serializes keys via fmt.Stringer, but backendKey had no String() of its own — so the embedded ir.ObjectSource.String() was promoted, and that only encodes group/kind/namespace/name, dropping the port.

Every port of a multi-port host therefore hashed to the same bucket. A lookup for :80 returned all ports' backends, and the ResourceName() tiebreak picked :443 (...:443_... sorts before ...:80_... since "4" < "8") regardless of the requested port.

The Fix

Give backendKey a String() that includes the port, so each (source, port) gets its own bucket.
Applies to both kind: Hostname and kind: ServiceEntry backendRefs.

Change Type

/kind fix

Changelog

Fixed route Hostname/ServiceEntry backendRefs to the requested port on multi-port hosts

Additional Notes

Copilot AI review requested due to automatic review settings June 9, 2026 13:39
@gateway-bot gateway-bot added kind/fix Categorizes issue or PR as related to a bug. release-note labels Jun 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes incorrect backend resolution when an HTTPRoute backendRef of kind: Hostname or kind: ServiceEntry specifies a port against a multi-port ServiceEntry host. The root cause was backendKey key-string serialization dropping the port, collapsing all ports into the same KRT index bucket and allowing the wrong cluster to be chosen at runtime.

Changes:

  • Add a backendKey.String() implementation that includes the port to ensure per-port index bucketing.
  • Add new serviceentry/destination-rule golden test coverage for a multi-port ServiceEntry referenced via Hostname backendRef with an explicit port.
  • Add/record expected output demonstrating routing to the correct ..._80 cluster (instead of incorrectly selecting ..._443).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
pkg/krtcollections/policy.go Adds backendKey.String() that includes port to prevent index key collisions across ports.
pkg/kgateway/setup/testdata/serviceentry/dr/se-hostname-ref-multiport.yaml New test input: multi-port ServiceEntry with HTTPRoute referencing host via Hostname backendRef on port 80.
pkg/kgateway/setup/testdata/serviceentry/dr/se-hostname-ref-multiport-out.yaml New golden output verifying the route cluster resolves to the ..._80 ServiceEntry cluster.

@davidjumani
davidjumani enabled auto-merge June 9, 2026 13:55
@davidjumani
davidjumani added this pull request to the merge queue Jun 9, 2026
Comment thread pkg/krtcollections/policy.go Outdated
@ymesika
ymesika removed this pull request from the merge queue due to a manual request Jun 9, 2026
ymesika and others added 2 commits June 9, 2026 17:43
Co-authored-by: Omar Hammami <[email protected]>
Signed-off-by: Yossi Mesika <[email protected]>
Signed-off-by: Yossi Mesika <[email protected]>
@ymesika
ymesika added this pull request to the merge queue Jun 9, 2026
Merged via the queue into kgateway-dev:main with commit b74cebf Jun 9, 2026
32 checks passed
@ymesika
ymesika deleted the serviceentry-backendref-multiport-routing branch June 9, 2026 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/fix Categorizes issue or PR as related to a bug. release-note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants