Repository navigation
fix(backend): resolve agent port from Sandbox pod template, not a constant - #1558
Conversation
…stant PR rossoctl#1525 made `resolve_agent_url` fall back to DEFAULT_IN_CLUSTER_PORT (8000) for in-cluster callers when the Service has no ports (the Sandbox case). That contradicts the sandbox-builder change in `_build_sandbox_manifest`, which aligns the Sandbox pod's PORT and containerPort with service_port (default DEFAULT_OFF_CLUSTER_PORT = 8080) so that AGENT_ENDPOINT, the advertised agent card URL, and the actual listener all agree. With both changes in effect, the backend knocks on :8000 while the agent listens on :8080; envoy returns 503 and the UI shows "Agent card not available." Replace the hardcoded in-cluster fallback with a read of the owning Sandbox's pod template (`spec.podTemplate.spec.containers[0]` — containerPort first, then the PORT env var). Deployments are unaffected: their Service exposes ports, so the first lookup still wins and the new Sandbox lookup is skipped. A single DEFAULT_OFF_CLUSTER_PORT final fallback covers the case where both lookups fail (matches the sandbox-builder default and the external port used by Gateways/Ingresses). As a side benefit, this also handles Sandboxes created with a custom servicePorts[0].port — neither prior fix covered that case. Tests: - Replace the two :8000 assertions from rossoctl#1525 with Sandbox-aware tests (containerPort path and PORT-env path) and a final-fallback test that asserts :8080 when neither Service nor Sandbox is found. The long-term fix is upstream kubernetes-sigs/agent-sandbox#154 (Sandbox controller populating its Service with ports); this is the in-tree workaround until then. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Hai Huang <[email protected]>
cwiklik
left a comment
There was a problem hiding this comment.
Clean fix for the port-mismatch bug between PR #1507 and #1525. The 3-tier resolution (Service → Sandbox pod template → default) is well-structured and test coverage is adequate.
Areas reviewed: Python (logic, imports, security), Tests
Commits: 2 commits, all signed-off
CI: lint/tests/security pass; Deploy & Test pending
| if env.get("name") == "PORT": | ||
| try: | ||
| return int(env.get("value")) | ||
| except (TypeError, ValueError): |
There was a problem hiding this comment.
suggestion: The default_port=0 sentinel relies on if port: treating 0 as falsy to fall through to the Sandbox lookup. Consider adding a brief comment (e.g. # 0 = sentinel; Service had no ports) since this pattern is non-obvious and fragile if someone later adds explicit port-0 handling.
| ) | ||
| port = lookup_service_port(name, namespace, kube, fallback) | ||
| return get_agent_url(name, namespace, port) | ||
| if not containers: |
There was a problem hiding this comment.
nit: containers[0].get("env", []) or [] — the or [] is redundant since .get(key, []) already returns [] when the key is absent. It only guards against an explicit None value in the dict, which is unlikely from the K8s API.
Summary
Two previously-merged fixes disagree about which port Sandbox agents listen on:
_build_sandbox_manifestaligns the Sandbox pod'sPORTenv andcontainerPortwithservice_port(defaultDEFAULT_OFF_CLUSTER_PORT= 8080) soAGENT_ENDPOINT, the agent card's advertisedurl, and the actual listener all agree.resolve_agent_urlfalls back toDEFAULT_IN_CLUSTER_PORT(8000) when the Service has no ports and the backend is running in-cluster.With both in effect, the backend calls
...:8000while the Sandbox agent listens on...:8080. Envoy returns 503 and the UI surfaces "Agent card not available."What this PR does
Replaces the hardcoded in-cluster fallback with a read of the owning Sandbox's pod template:
spec.podTemplate.spec.containers[0]—containerPortfirst, then thePORTenv var.DEFAULT_OFF_CLUSTER_PORT(8080) — matches both the sandbox-builder default and the external port used by Gateways/Ingresses.This also handles Sandboxes created with a custom
servicePorts[0].port— neither prior fix covered that case.The long-term fix is upstream kubernetes-sigs/agent-sandbox#154 (Sandbox controller populating its Service with ports); this is the in-tree workaround until then.
Test plan
uv run pytest kagenti/backend/tests/test_routes.py -v— 15/15 pass locally:8000assertions from fix(backend): use correct port fallback for in-cluster agent resolution #1525 with Sandbox-aware tests (containerPort path + PORT-env path) and a final-fallback testGET /.well-known/agent-card.jsonreturns 200 and the agent card renders in the chat tabScope check
:8080with domainAssisted-By: Claude (Anthropic AI) <[email protected]>