Skip to content

fix(backend): resolve agent port from Sandbox pod template, not a constant - #1558

Merged
huang195 merged 2 commits into
rossoctl:mainfrom
huang195:fix/resolve-agent-port-from-sandbox
May 12, 2026
Merged

huang195 merged 2 commits into
rossoctl:mainfrom
huang195:fix/resolve-agent-port-from-sandbox

Conversation

@huang195

Copy link
Copy Markdown
Member

Summary

Two previously-merged fixes disagree about which port Sandbox agents listen on:

With both in effect, the backend calls ...:8000 while 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:

  1. If the Service exposes ports (Deployment/StatefulSet/Job path) -> use the first port. Unchanged behavior.
  2. Else, if a Sandbox with the same name exists -> read spec.podTemplate.spec.containers[0] — containerPort first, then the PORT env var.
  3. Else -> fall back to 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
  • Replace the two :8000 assertions 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 test
  • Verified end-to-end on a local Kind cluster: built the patched backend image, rolled out, deployed the weather agent via UI, confirmed GET /.well-known/agent-card.json returns 200 and the agent card renders in the chat tab
  • CI green

Scope check

Path Behavior Changed?
Deployment / StatefulSet / Job Service has ports → first lookup wins No
Sandbox, default port (8080) Service empty → Sandbox lookup returns 8080 Fixes current bug
Sandbox, custom port Service empty → Sandbox lookup returns custom port New: improvement
Off-cluster, nothing found Final fallback :8080 with domain Same (was 8080)

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

…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
cwiklik self-requested a review May 12, 2026 17:56

@cwiklik cwiklik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@huang195
huang195 merged commit 529b4c1 into rossoctl:main May 12, 2026
21 of 22 checks passed
@huang195
huang195 deleted the fix/resolve-agent-port-from-sandbox branch May 12, 2026 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants