Skip to content

Commit 55977f7

Browse files
committed
Addresses review comments
1 parent 0897b96 commit 55977f7

File tree

1 file changed

+56
-21
lines changed

1 file changed

+56
-21
lines changed

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

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
SANDBOX_API_GROUP = "agents.x-k8s.io"
3838
SANDBOX_API_VERSION = "v1alpha1"
3939
SANDBOX_PLURAL_NAME = "sandboxes"
40+
POD_NAME_ANNOTATION = "agents.x-k8s.io/pod-name"
4041

4142
logging.basicConfig(level=logging.INFO,
4243
format='%(asctime)s - %(levelname)s - %(message)s',
@@ -62,8 +63,8 @@ def __init__(
6263
namespace: str = "default",
6364
server_port: int = 8888,
6465
sandbox_ready_timeout: int = 180,
65-
gateway_ready_timeout: int = 180,
6666
port_forward_ready_timeout: int = 30,
67+
pod_name_ready_timeout: int = 1
6768
):
6869
self.template_name = template_name
6970
self.namespace = namespace
@@ -74,9 +75,7 @@ def __init__(
7475
self.sandbox_ready_timeout = sandbox_ready_timeout
7576
self.gateway_ready_timeout = gateway_ready_timeout
7677
self.port_forward_ready_timeout = port_forward_ready_timeout
77-
78-
self.port_forward_process: subprocess.Popen | None = None
79-
78+
self.pod_name_ready_timeout = pod_name_ready_timeout
8079
self.claim_name: str | None = None
8180
self.sandbox_name: str | None = None
8281
self.pod_name: str | None = None
@@ -159,24 +158,60 @@ def _wait_for_sandbox_ready(self):
159158
break
160159

161160
if is_ready:
162-
self.sandbox_name = sandbox_object['metadata']['name']
163-
annotations = sandbox_object.get(
164-
'metadata', {}).get('annotations', {})
165-
pod_name_annotation = "agents.x-k8s.io/pod-name"
166-
if pod_name_annotation in annotations:
167-
self.pod_name = annotations[pod_name_annotation]
168-
logging.info(
169-
f"Found pod name from annotation: {self.pod_name}")
170-
else:
171-
self.pod_name = self.sandbox_name
172-
w.stop()
161+
metadata = sandbox_object.get(
162+
"metadata", {})
163+
self.sandbox_name = metadata.get(
164+
"name")
165+
if not self.sandbox_name:
166+
raise RuntimeError(
167+
"Could not determine sandbox name from sandbox object.")
168+
173169
logging.info(f"Sandbox {self.sandbox_name} is ready.")
174-
break
170+
self._wait_for_pod_name()
171+
w.stop()
172+
return
173+
174+
self.__exit__(None, None, None)
175+
raise TimeoutError(
176+
f"Sandbox did not become ready within {self.sandbox_ready_timeout} seconds.")
177+
178+
def _wait_for_pod_name(self, timeout: int = 30):
179+
"""
180+
Waits for the pod-name annotation to be present on the sandbox object.
181+
This wait is only necessary when using SandboxWarmPool.
182+
"""
183+
if self.pod_name_ready_timeout <= 0:
184+
logging.info(
185+
f"pod_name_ready_timeout {self.pod_name_ready_timeout} is <= 0. Defaulting pod to sandbox name {self.sandbox_name}.")
186+
self.pod_name = self.sandbox_name
187+
return
188+
w = watch.Watch()
189+
logging.info(
190+
f"Waiting for pod name annotation on sandbox {self.sandbox_name}...")
191+
for event in w.stream(
192+
func=self.custom_objects_api.list_namespaced_custom_object,
193+
namespace=self.namespace,
194+
group=SANDBOX_API_GROUP,
195+
version=SANDBOX_API_VERSION,
196+
plural=SANDBOX_PLURAL_NAME,
197+
field_selector=f"metadata.name={self.sandbox_name}",
198+
timeout_seconds=self.pod_name_ready_timeout
199+
):
200+
if event["type"] in ["ADDED", "MODIFIED"]:
201+
sandbox_object = event['object']
202+
annotations = sandbox_object.get(
203+
'metadata', {}).get('annotations', {})
204+
pod_name = annotations.get(POD_NAME_ANNOTATION)
205+
if pod_name:
206+
self.pod_name = pod_name
207+
logging.info(
208+
f"Found pod name from annotation: {self.pod_name}")
209+
w.stop()
210+
return
175211

176-
if not self.sandbox_name:
177-
self.__exit__(None, None, None)
178-
raise TimeoutError(
179-
f"Sandbox did not become ready or pod name could not be determined within {self.sandbox_ready_timeout} seconds.")
212+
logging.warning(
213+
f"Pod name annotation not found after {self.pod_name_ready_timeout} seconds. Defaulting to sandbox name {self.sandbox_name}.")
214+
self.pod_name = self.sandbox_name
180215

181216
def _start_and_wait_for_port_forward(self):
182217
"""
@@ -187,7 +222,7 @@ def _start_and_wait_for_port_forward(self):
187222
raise RuntimeError(
188223
"Cannot start port-forwarding, sandbox pod name is not known.")
189224
logging.info(
190-
f"Starting port-forwarding for sandbox {self.sandbox_name} with sandbox pod {self.pod_name}...")
225+
f"Starting port-forwarding for sandbox {self.sandbox_name} in namespace {self.namespace} with sandbox pod {self.pod_name}...")
191226
self.port_forward_process = subprocess.Popen(
192227
[
193228
"kubectl", "port-forward",

0 commit comments

Comments
 (0)