Skip to content

Commit 3e3e763

Browse files
committed
Addresses review comments
1 parent 2aa102e commit 3e3e763

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

clients/python/agentic-sandbox-client/agentic_sandbox/sandbox_client.py

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
SANDBOX_API_GROUP = "agents.x-k8s.io"
3333
SANDBOX_API_VERSION = "v1alpha1"
3434
SANDBOX_PLURAL_NAME = "sandboxes"
35+
POD_NAME_ANNOTATION = "agents.x-k8s.io/pod-name"
3536

3637
logging.basicConfig(level=logging.INFO,
3738
format='%(asctime)s - %(levelname)s - %(message)s',
@@ -58,13 +59,15 @@ def __init__(
5859
namespace: str = "default",
5960
server_port: int = 8888,
6061
sandbox_ready_timeout: int = 180,
61-
port_forward_ready_timeout: int = 30
62+
port_forward_ready_timeout: int = 30,
63+
pod_name_ready_timeout: int = 1
6264
):
6365
self.template_name = template_name
6466
self.namespace = namespace
6567
self.server_port = server_port
6668
self.sandbox_ready_timeout = sandbox_ready_timeout
6769
self.port_forward_ready_timeout = port_forward_ready_timeout
70+
self.pod_name_ready_timeout = pod_name_ready_timeout
6871
self.claim_name: str | None = None
6972
self.sandbox_name: str | None = None
7073
self.pod_name: str | None = None
@@ -132,24 +135,60 @@ def _wait_for_sandbox_ready(self):
132135
break
133136

134137
if is_ready:
135-
self.sandbox_name = sandbox_object['metadata']['name']
136-
annotations = sandbox_object.get(
137-
'metadata', {}).get('annotations', {})
138-
pod_name_annotation = "agents.x-k8s.io/pod-name"
139-
if pod_name_annotation in annotations:
140-
self.pod_name = annotations[pod_name_annotation]
141-
logging.info(
142-
f"Found pod name from annotation: {self.pod_name}")
143-
else:
144-
self.pod_name = self.sandbox_name
145-
w.stop()
138+
metadata = sandbox_object.get(
139+
"metadata", {})
140+
self.sandbox_name = metadata.get(
141+
"name")
142+
if not self.sandbox_name:
143+
raise RuntimeError(
144+
"Could not determine sandbox name from sandbox object.")
145+
146146
logging.info(f"Sandbox {self.sandbox_name} is ready.")
147-
break
147+
self._wait_for_pod_name()
148+
w.stop()
149+
return
150+
151+
self.__exit__(None, None, None)
152+
raise TimeoutError(
153+
f"Sandbox did not become ready within {self.sandbox_ready_timeout} seconds.")
154+
155+
def _wait_for_pod_name(self, timeout: int = 30):
156+
"""
157+
Waits for the pod-name annotation to be present on the sandbox object.
158+
This wait is only necessary when using SandboxWarmPool.
159+
"""
160+
if self.pod_name_ready_timeout <= 0:
161+
logging.info(
162+
f"pod_name_ready_timeout {self.pod_name_ready_timeout} is <= 0. Defaulting pod to sandbox name {self.sandbox_name}.")
163+
self.pod_name = self.sandbox_name
164+
return
165+
w = watch.Watch()
166+
logging.info(
167+
f"Waiting for pod name annotation on sandbox {self.sandbox_name}...")
168+
for event in w.stream(
169+
func=self.custom_objects_api.list_namespaced_custom_object,
170+
namespace=self.namespace,
171+
group=SANDBOX_API_GROUP,
172+
version=SANDBOX_API_VERSION,
173+
plural=SANDBOX_PLURAL_NAME,
174+
field_selector=f"metadata.name={self.sandbox_name}",
175+
timeout_seconds=self.pod_name_ready_timeout
176+
):
177+
if event["type"] in ["ADDED", "MODIFIED"]:
178+
sandbox_object = event['object']
179+
annotations = sandbox_object.get(
180+
'metadata', {}).get('annotations', {})
181+
pod_name = annotations.get(POD_NAME_ANNOTATION)
182+
if pod_name:
183+
self.pod_name = pod_name
184+
logging.info(
185+
f"Found pod name from annotation: {self.pod_name}")
186+
w.stop()
187+
return
148188

149-
if not self.sandbox_name:
150-
self.__exit__(None, None, None)
151-
raise TimeoutError(
152-
f"Sandbox did not become ready or pod name could not be determined within {self.sandbox_ready_timeout} seconds.")
189+
logging.warning(
190+
f"Pod name annotation not found after {self.pod_name_ready_timeout} seconds. Defaulting to sandbox name {self.sandbox_name}.")
191+
self.pod_name = self.sandbox_name
153192

154193
def _start_and_wait_for_port_forward(self):
155194
"""
@@ -160,7 +199,7 @@ def _start_and_wait_for_port_forward(self):
160199
raise RuntimeError(
161200
"Cannot start port-forwarding, sandbox pod name is not known.")
162201
logging.info(
163-
f"Starting port-forwarding for sandbox {self.sandbox_name} with sandbox pod {self.pod_name}...")
202+
f"Starting port-forwarding for sandbox {self.sandbox_name} in namespace {self.namespace} with sandbox pod {self.pod_name}...")
164203
self.port_forward_process = subprocess.Popen(
165204
[
166205
"kubectl", "port-forward",

0 commit comments

Comments
 (0)