Skip to content

Commit 8ad07a4

Browse files
committed
simplify: cache in-cluster URL at construction, trim docstring, extract test helper
1 parent cac8303 commit 8ad07a4

2 files changed

Lines changed: 24 additions & 45 deletions

File tree

clients/python/agentic-sandbox-client/k8s_agent_sandbox/connector.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ def verify_connection(self):
5454
pass
5555

5656
def should_inject_router_headers(self) -> bool:
57-
"""Returns True if X-Sandbox-* router headers should be attached to requests.
58-
59-
Defaults to True. InClusterConnectionStrategy overrides to False because
60-
requests go directly to the sandbox pod, which does not use those headers.
61-
"""
57+
"""Returns True if X-Sandbox-* router headers should be injected into requests."""
6258
return True
6359

6460
class DirectConnectionStrategy(ConnectionStrategy):
@@ -190,15 +186,13 @@ def __init__(
190186
namespace: str,
191187
config: SandboxInClusterConnectionConfig,
192188
):
193-
self.sandbox_id = sandbox_id
194-
self.namespace = namespace
195-
self.config = config
189+
self._base_url = (
190+
f"http://{sandbox_id}.{namespace}"
191+
f".svc.cluster.local:{config.server_port}"
192+
)
196193

197194
def connect(self) -> str:
198-
return (
199-
f"http://{self.sandbox_id}.{self.namespace}"
200-
f".svc.cluster.local:{self.config.server_port}"
201-
)
195+
return self._base_url
202196

203197
def verify_connection(self):
204198
pass

clients/python/agentic-sandbox-client/k8s_agent_sandbox/test/unit/test_sandboxclient.py

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -516,41 +516,26 @@ def test_in_cluster_config_default_port(self, _):
516516
sc = SandboxClient(connection_config=config)
517517
self.assertEqual(sc.connection_config.server_port, 8888)
518518

519-
@patch('uuid.uuid4')
520-
@patch('k8s_agent_sandbox.sandbox_client.K8sHelper')
521-
def test_sandbox_created_with_in_cluster_config(self, MockK8sHelper, mock_uuid):
522-
mock_uuid.return_value.hex = 'aabbccdd'
523-
in_cluster_config = SandboxInClusterConnectionConfig()
524-
client = SandboxClient(connection_config=in_cluster_config)
525-
client.k8s_helper.resolve_sandbox_name.return_value = 'my-sandbox'
526-
527-
mock_sandbox_class = MagicMock()
528-
mock_sandbox_class.return_value = MagicMock()
529-
client.sandbox_class = mock_sandbox_class
530-
531-
with patch.object(client, '_create_claim'), \
532-
patch.object(client, '_wait_for_sandbox_ready'):
533-
client.create_sandbox('my-template')
534-
535-
call_kwargs = mock_sandbox_class.call_args.kwargs
519+
def _create_sandbox_with_in_cluster_config(self, namespace='default'):
520+
with patch('k8s_agent_sandbox.sandbox_client.K8sHelper'), \
521+
patch('uuid.uuid4') as mock_uuid:
522+
mock_uuid.return_value.hex = 'aabbccdd'
523+
client = SandboxClient(connection_config=SandboxInClusterConnectionConfig())
524+
client.k8s_helper.resolve_sandbox_name.return_value = 'my-sandbox'
525+
mock_sandbox_class = MagicMock()
526+
mock_sandbox_class.return_value = MagicMock()
527+
client.sandbox_class = mock_sandbox_class
528+
with patch.object(client, '_create_claim'), \
529+
patch.object(client, '_wait_for_sandbox_ready'):
530+
client.create_sandbox('my-template', namespace=namespace)
531+
return mock_sandbox_class.call_args.kwargs
532+
533+
def test_sandbox_created_with_in_cluster_config(self):
534+
call_kwargs = self._create_sandbox_with_in_cluster_config()
536535
self.assertIsInstance(call_kwargs['connection_config'], SandboxInClusterConnectionConfig)
537536

538-
@patch('uuid.uuid4')
539-
@patch('k8s_agent_sandbox.sandbox_client.K8sHelper')
540-
def test_sandbox_namespace_passed_correctly(self, MockK8sHelper, mock_uuid):
541-
mock_uuid.return_value.hex = 'aabbccdd'
542-
client = SandboxClient(connection_config=SandboxInClusterConnectionConfig())
543-
client.k8s_helper.resolve_sandbox_name.return_value = 'my-sandbox'
544-
545-
mock_sandbox_class = MagicMock()
546-
mock_sandbox_class.return_value = MagicMock()
547-
client.sandbox_class = mock_sandbox_class
548-
549-
with patch.object(client, '_create_claim'), \
550-
patch.object(client, '_wait_for_sandbox_ready'):
551-
client.create_sandbox('my-template', namespace='prod')
552-
553-
call_kwargs = mock_sandbox_class.call_args.kwargs
537+
def test_sandbox_namespace_passed_correctly(self):
538+
call_kwargs = self._create_sandbox_with_in_cluster_config(namespace='prod')
554539
self.assertEqual(call_kwargs['namespace'], 'prod')
555540

556541

0 commit comments

Comments
 (0)