Skip to content

fix(kube-proxy): honor sessionAffinity=ClientIP for ClusterIP and NodePort#42

Open
indyjonesnl wants to merge 1 commit into
calfonso:mainfrom
indyjonesnl:upstream/kube-proxy-session-affinity
Open

fix(kube-proxy): honor sessionAffinity=ClientIP for ClusterIP and NodePort#42
indyjonesnl wants to merge 1 commit into
calfonso:mainfrom
indyjonesnl:upstream/kube-proxy-session-affinity

Conversation

@indyjonesnl
Copy link
Copy Markdown

Problem

`Service.spec.sessionAffinity = ClientIP` was ignored by our kube-proxy iptables rules. Traffic was always round-robin across backends, breaking conformance tests that assert client-affinity stickiness within the configured timeout window.

Fix

Translate `sessionAffinity: ClientIP` into iptables `-m recent` rules that record the client source IP on first match and re-route subsequent packets from the same source to the same backend within `sessionAffinityConfig.clientIP.timeoutSeconds` (default 10800s). Applies to both ClusterIP and NodePort services.

Verification

`cargo build --workspace --locked` clean. Depends on #32 for green CI.

…ePort

iptables-mode kube-proxy now produces sticky DNAT rules for any service
with sessionAffinity=ClientIP, regardless of endpoint count, and the
recent --set is collapsed into the same rule as DNAT so the match only
fires for packets that actually DNAT (correct protocol/port) — matching
upstream K8s pkg/proxy/iptables/proxier.go writeServiceToEndpointRules.

The api-server now defaults sessionAffinityConfig.clientIP.timeoutSeconds
to 10800s (3h) when sessionAffinity is ClientIP and no timeout was given,
and clears the config when affinity is switched to None on update, per
K8s pkg/apis/core/v1/defaults.go SetDefaults_Service.

Adds unit tests covering ClusterIP affinity (multi- and single-endpoint),
NodePort affinity, no-affinity service, and the xt_recent-unavailable
fallback path.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

1 participant