Repository navigation
fix: collision-free port assignment for proxy-sidecar mode - #296
Conversation
6968fac to
f8d293b
Compare
Port-stealing with collision detection for all three proxy ports:
1. Collect all ports in use across all pod containers
2. Steal the agent's first port for the reverse proxy
3. Find first free port for the agent (starting from original+1)
4. Find first free port for the forward proxy (starting from 8081)
5. Inject REVERSE_PROXY_ADDR, REVERSE_PROXY_BACKEND, FORWARD_PROXY_ADDR
env vars into authbridge-proxy (expanded via ${} in ConfigMap YAML)
6. Inject HTTP_PROXY/HTTPS_PROXY with the actual forward proxy port
7. Patch agent PORT env var to bind to the new port
Port scanning respects the valid range (1-65535).
PORT env var assumption documented (standard for Python/Node agents).
Tests added:
- Port collision: agent on 8000+8001 -> moves to 8002
- Forward proxy collision: agent uses 8081 -> forward proxy on 8082
- setOrAddEnv: overwrites existing and adds new env vars
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Hai Huang <[email protected]>
f8d293b to
c1756a8
Compare
- authbridge-webhook.md: add deployment modes section (envoy-sidecar, proxy-sidecar, waypoint) with port-stealing behavior documentation - E2E README: authbridge-unified -> authbridge-envoy - E2E suite: add authbridge-light to sidecar image list - Helm values: envoy-with-processor -> authbridge-envoy, add authbridgeLight Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
findFreePort now returns an error instead of silently returning an occupied port when all ports up to 65535 are in use. The webhook rejects the pod with a clear admission error rather than creating a bind collision at runtime. Also adds a warning log when no agent container is found to relocate. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
Rename the YAML config ConfigMap and volume from authbridge-unified-config to authbridge-runtime-config. The name authbridge-config is already used by a separate flat key-value ConfigMap (KEYCLOAK_URL, KEYCLOAK_REALM, etc.) read by the operator webhook and sidecars. - authbridge-config: flat key-value pairs (operator + sidecars) — unchanged - authbridge-runtime-config: YAML config.yaml (authbridge binary) — renamed Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
8d3ba13 to
fa15e56
Compare
The rename incorrectly changed the flat key-value ConfigMap (authbridge-config) to authbridge-runtime-config. Only the YAML config ConfigMap should be renamed. The E2E test needs both: - authbridge-config: flat key-value (KEYCLOAK_URL, etc.) - authbridge-runtime-config: YAML config.yaml Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
pdettori
left a comment
There was a problem hiding this comment.
Clean port-stealing implementation with collision detection. The core findFreePort closure correctly scans and reserves ports, preventing cascading collisions. The ConfigMap rename from authbridge-unified-config to authbridge-runtime-config is consistent across all 12 files. All 15 CI checks passing including E2E.
Areas reviewed: Go, Helm, Tests, Docs, E2E fixtures
Commits: 5 commits, all signed-off with DCO
CI status: All 15 checks passing
| @@ -808,7 +808,7 @@ spec: | |||
| // --- AuthBridge Injection E2E fixtures --- | |||
There was a problem hiding this comment.
nit: Comment says "3 ConfigMaps" but the list now has 4 (authbridge-config, authbridge-runtime-config, spiffe-helper-config, envoy-config).
| var originalAgentPort int32 | ||
| var agentContainer *corev1.Container | ||
| for i := range podSpec.Containers { | ||
| c := &podSpec.Containers[i] |
There was a problem hiding this comment.
suggestion: Consider adding a test for the port exhaustion path (where findFreePort returns an error). The error handling is correct, but having test coverage would validate the admission rejection behavior.
| for _, c := range podSpec.Containers { | ||
| for _, p := range c.Ports { | ||
| usedPorts[p.ContainerPort] = true | ||
| } |
There was a problem hiding this comment.
suggestion: Consider a test for the fallback path: pod with no container ports (uses default 8000, agentContainer == nil). The warning log is good, but a test assertion would lock down the behavior.
| break | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
praise: Clean port reservation pattern — findFreePort marks allocated ports as used, preventing subsequent calls from reusing them. Handles the tricky case where agent port and forward proxy port could collide.
| if agentContainer != nil { | ||
| agentContainer.Ports[0].ContainerPort = newAgentPort | ||
| setOrAddEnv(agentContainer, "PORT", fmt.Sprintf("%d", newAgentPort)) | ||
| mutatorLog.Info("proxy-sidecar port stealing", |
There was a problem hiding this comment.
praise: Good fix — now skips HTTP_PROXY injection for spiffe-helper and client-registration sidecars, not just authbridge-proxy.
- Fix fixtures.go comment: '3 ConfigMaps' -> '4 ConfigMaps' - Add TestInjectAuthBridge_ProxySidecarMode_NoPorts_UsesDefault: agent with no container ports uses default 8000, no PORT env var patched, HTTP_PROXY still injected Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
- Chart.yaml: subchart 0.2.0-alpha.27 -> 0.2.0-alpha.28 - values.yaml: operator image 0.2.0-alpha.27 -> 0.2.0-alpha.28 - Rename authbridge-unified-config ConfigMap to authbridge-runtime-config (authbridge-config is already used by the flat key-value ConfigMap) - Add listener section with env var placeholders for proxy-sidecar mode Depends on: - rossoctl/operator#295 (mode-aware images) - rossoctl/operator#296 (port-stealing + rename) Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
Summary
In proxy-sidecar mode, inbound traffic must go through the authbridge reverse proxy for JWT validation. Without iptables interception, the Service targetPort determines whether traffic hits the reverse proxy or goes directly to the agent.
The webhook now uses port-stealing: the reverse proxy takes over the agent's original port (e.g.,
:8000), and the agent is moved to a free port. This way the Service targetPort stays correct without any Service patching.Problem
When deploying an agent with
kagenti.io/authbridge-mode: proxy-sidecar, the hardcoded port assignments (8080 reverse proxy, 8081 forward proxy) could collide with ports already in use by the agent, causing bind failures at runtime.Fix
The webhook now performs collision-free port assignment:
If no free port is available (port exhaustion), the webhook rejects the pod with a clear admission error.
Test plan
go build ./...passesgo test ./internal/webhook/injector/...passesinbound authorizedAssisted-By: Claude (Anthropic AI) [email protected]