Skip to content

feat(gateway): Add NetworkService session affinity - #1005

Merged
privateip merged 1 commit into
mainfrom
fix/issue-733
Oct 22, 2026
Merged

privateip merged 1 commit into
mainfrom
fix/issue-733

Conversation

@privateip

@privateip privateip commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The gateway balances each connection from a client on its own, so nothing can pin a client to one backend.

A service can now choose ClientIP or ClientIPAndProtocol affinity, which sends a client's connections to one backend across ports and across services over the same backends, on every gateway node alike.

The default keeps today's behaviour, and upgrading moves no existing flow.

Changing a service's affinity moves most of its open connections, and changing its backend set moves about one client in N, where N is the number of backends.

Test plan

  • Each mode pins clients as described, and two gateway nodes pick the same backend for the same client
  • The loaded datapath picks the backend the control plane predicts for every mode over TCP and UDP, IPv6 and IPv4
  • Editing a service's affinity reprograms the running gateway, and a map pinned by the previous release keeps its rows
  • Lint, unit, root datapath and Kind e2e suites pass

Fixes #733

🤖 Generated with Claude Code

The gateway always hashed a client's address and source port, so every connection from one client was balanced on its own and nothing could pin a client to one backend.

NetworkService gains spec.sessionAffinity with three values:

- None, the default, keeps hashing the address and source port.
- ClientIP hashes the client's address only, so every connection from one address reaches one backend.
- ClientIPAndProtocol hashes the address and the L4 protocol.

The setting reaches the datapath as one byte in each vip_table value. It sits in the alignment gap before generation, so the value stays 3216 bytes, a vip_table pinned by an older release stays compatible and keeps its rows, and those rows read as None. flow_hash and its Go mirror FlowHash take the affinity and the protocol, and VIPTable.Register refuses an unknown affinity.

Under either client-IP mode the Maglev table is keyed by backend address rather than address and port. The API makes a tenant serve a second VIP from another NetworkService with a different backendPort, and keying by port would give those services different tables over the same pods, so a client would reach different backends. None keeps the old keying, so upgrading moves no existing flow.

The engine compares the affinity when deciding whether a rule changed, so editing it reprograms every gateway node at the next reconcile without a restart. A change rehashes every connection of the service, and the docs say so, along with the bound that a backend-set change moves about 1 in N clients.

Tests cover each mode across two independently programmed gateway nodes and three services over one backend set, the loaded XDP program against FlowHash for every mode over TCP and UDP on IPv6 and IPv4, a live affinity change, and an upgrade over a vip_table pinned at the previous layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@privateip privateip changed the title feat(gateway): Add session affinity to NetworkService feat(gateway): Add NetworkService session affinity Oct 22, 2026
@privateip
privateip marked this pull request as ready for review October 22, 2026 17:13
@privateip
privateip requested a review from a team as a code owner October 22, 2026 17:13
@privateip
privateip enabled auto-merge October 22, 2026 17:13
@privateip

Copy link
Copy Markdown
Contributor Author

Two review passes ran before this was marked ready. CI is green at e29e48e, and auto-merge waits for an approving review.

First pass, at 9070dd0. The reviewers split, so the open questions went to a person, who chose to land #1003 first, squash the follow-up commit, and roll out to staging on merge without a soak.

pr-adversary:            VERDICT: merge. I found no correctness defect in the diff, but the hold-for-CI decision above still stands.
pr-conventions-reviewer: VERDICT: hold. #845 and #1003 both edit files this PR changes, and the order they land in is a human decision.

Second pass, at 15540b9, after the squash and the rebase onto #1003. The person chose to land this before #845, whose author rebases and regenerates the CRD afterwards.

pr-rereviewer:           VERDICT: merge. All four applied findings are settled and I found no regression, but I could not see CI at the new head, so confirm `gh pr checks 1005` is fully green, including Unit Tests (root) and E2E, before merging.
pr-conventions-reviewer: VERDICT: hold. The change collides with PR #845 on the same CRD, API type and controller files, so a human has to choose which lands first.

Since then the branch was rebased onto #1006, which touched the same API file. Both sides were kept, the CRD and API reference regenerated with no extra diff, and the subject was shortened to 50 characters.

The enum values ClientIP and ClientIPAndProtocol stay in the description because they are what users write. The commit trailer was left as the session's required attribution.

What each reviewer ran:

pr-adversary (9070dd0b):
  gh pr view 1005 --json title,body,commits; gh pr checks 1005; gh api repos/datum-cloud/galactic/issues/733
  git diff --stat / git diff 09b2d2ce...9070dd0b (api, kerneldatapath, types, diff, viptable, edgedsr.c, flowhash, controller)
  git ls-files internal/plumbing/ebpf/edgeprog; read of generated EdgedsrVipValue
  go build ./...; go vet ./internal/gateway/ ./internal/plumbing/ebpf/... ./internal/controller/
  go test ./internal/gateway/ ./internal/plumbing/ebpf/edgemap/ ./internal/plumbing/ebpf/edgeprog/ ./internal/controller/
  read of resolveFamilyBackends and selectRuleBackends

pr-conventions-reviewer (9070dd0b):
  gh pr view 1005 --json title,body,commits,files,baseRefName,isDraft
  git diff --stat 09b2d2ce...9070dd0b; git diff of edgedsr.c; git log 09b2d2ce..9070dd0b
  gh pr list --state open --json number,title,headRefName,files --limit 50
  grep of docs/agents/CONVENTIONS.md for commit rules

pr-rereviewer (15540b98):
  git log --show-signature a27bd65a..HEAD; git range-diff of old and new heads
  git diff 9070dd0b HEAD --stat; git diff a27bd65a HEAD --stat; git diff f046bb2c a27bd65a -- viptable.go
  gh pr checks 1005; gh pr view 1005 --json body
  grep for Register(, FlowHash( and vip_value over internal and cmd
  go build ./...; go vet ./internal/plumbing/ebpf/... ./internal/gateway/... ./cmd/galactic-gateway/...
  go test ./internal/plumbing/ebpf/edgemap/ ./internal/plumbing/ebpf/edgeprog/ ./internal/gateway/ ./internal/controller/

pr-conventions-reviewer (15540b98):
  gh pr view 1005 --json title,body,commits,files,baseRefName,isDraft
  git merge-base HEAD origin/main; git diff --stat a27bd65a HEAD
  git diff a27bd65a HEAD -- edgedsr.c viptable.go api config internal/gateway/types.go internal/controller/networkgateway_controller.go
  gh pr list --state open --json number,title,headRefName,files --limit 50
  git ls-files / git check-ignore on internal/plumbing/ebpf/edgeprog; grep of docs/agents/CONVENTIONS.md

Locally at e29e48e, lint, the unit suite and the root eBPF suites pass, and the Kind e2e passed before the final rebase. No review requirement was relaxed, and the code owners are requested.

@privateip
privateip merged commit c1ad914 into main Oct 22, 2026
17 checks passed
@privateip
privateip deleted the fix/issue-733 branch October 22, 2026 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Configurable session affinity for load balancer rules

2 participants