Skip to content
This repository was archived by the owner on Nov 12, 2026. It is now read-only.

fix: collision-free port assignment for proxy-sidecar mode - #296

Merged
huang195 merged 6 commits into
mainfrom
fix/proxy-sidecar-service-targetport
Apr 20, 2026
Merged

huang195 merged 6 commits into
mainfrom
fix/proxy-sidecar-service-targetport

Conversation

@huang195

@huang195 huang195 commented Apr 18, 2026 •

Copy link
Copy Markdown
Member

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:

  1. Collects all ports in use across all pod containers
  2. Steals the agent's first port for the reverse proxy (so the Service doesn't need patching)
  3. Finds the first free port for the agent (starting from original+1)
  4. Finds the first free port for the forward proxy (starting from 8081)
  5. Injects REVERSE_PROXY_ADDR, REVERSE_PROXY_BACKEND, FORWARD_PROXY_ADDR env vars into the authbridge-proxy container
  6. Injects HTTP_PROXY/HTTPS_PROXY with the actual forward proxy port into app containers
  7. Patches the agent's PORT env var to bind to the new port

If no free port is available (port exhaustion), the webhook rejects the pod with a clear admission error.

Test plan

  • go build ./... passes
  • go test ./internal/webhook/injector/... passes
  • E2E: deploy agent with proxy-sidecar mode, verify inbound auth logs show inbound authorized

Assisted-By: Claude (Anthropic AI) [email protected]

@huang195
huang195 requested a review from a team as a code owner April 18, 2026 20:40
@huang195
huang195 force-pushed the fix/proxy-sidecar-service-targetport branch 6 times, most recently from 6968fac to f8d293b Compare April 18, 2026 21:20
@huang195 huang195 changed the title fix: patch Service targetPort for proxy-sidecar mode fix: collision-free port assignment for proxy-sidecar mode Apr 18, 2026
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]>
@huang195
huang195 force-pushed the fix/proxy-sidecar-service-targetport branch from f8d293b to c1756a8 Compare April 18, 2026 21:21
- 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]>
@huang195
huang195 force-pushed the fix/proxy-sidecar-service-targetport branch from 8d3ba13 to fa15e56 Compare April 18, 2026 22:04
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 pdettori left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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]>
@huang195
huang195 merged commit e8b1b0d into main Apr 20, 2026
15 checks passed
@huang195
huang195 deleted the fix/proxy-sidecar-service-targetport branch April 20, 2026 02:57
huang195 added a commit to huang195/kagenti that referenced this pull request Apr 21, 2026
- 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]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants