-
Notifications
You must be signed in to change notification settings - Fork 188
feat(python-sdk): add SandboxInClusterConnectionConfig to bypass router #489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vgunapati
wants to merge
2
commits into
kubernetes-sigs:main
Choose a base branch
from
vgunapati:skip-router-layer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
clients/python/agentic-sandbox-client/k8s_agent_sandbox/test/unit/test_connector.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| # Copyright 2026 The Kubernetes Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import unittest | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import requests | ||
|
|
||
| from k8s_agent_sandbox.connector import ( | ||
| DirectConnectionStrategy, | ||
| GatewayConnectionStrategy, | ||
| LocalTunnelConnectionStrategy, | ||
| InClusterConnectionStrategy, | ||
| SandboxConnector, | ||
| ) | ||
| from k8s_agent_sandbox.models import ( | ||
| SandboxDirectConnectionConfig, | ||
| SandboxGatewayConnectionConfig, | ||
| SandboxLocalTunnelConnectionConfig, | ||
| SandboxInClusterConnectionConfig, | ||
| ) | ||
|
|
||
|
|
||
| class TestInClusterConnectionStrategy(unittest.TestCase): | ||
| """Unit tests for InClusterConnectionStrategy.""" | ||
|
|
||
| def setUp(self): | ||
| self.config = SandboxInClusterConnectionConfig(server_port=8888) | ||
| self.strategy = InClusterConnectionStrategy( | ||
| sandbox_id="my-sandbox", | ||
| namespace="dev", | ||
| config=self.config, | ||
| ) | ||
|
|
||
| def test_connect_returns_correct_dns_url(self): | ||
| url = self.strategy.connect() | ||
| self.assertEqual(url, "http://my-sandbox.dev.svc.cluster.local:8888") | ||
|
|
||
| def test_connect_uses_custom_port(self): | ||
| config = SandboxInClusterConnectionConfig(server_port=9000) | ||
| strategy = InClusterConnectionStrategy("sb", "ns", config) | ||
| self.assertEqual(strategy.connect(), "http://sb.ns.svc.cluster.local:9000") | ||
|
|
||
| def test_connect_is_idempotent(self): | ||
| self.assertEqual(self.strategy.connect(), self.strategy.connect()) | ||
|
|
||
| def test_does_not_inject_router_headers(self): | ||
| self.assertFalse(self.strategy.should_inject_router_headers()) | ||
|
|
||
| def test_verify_connection_does_not_raise(self): | ||
| self.strategy.verify_connection() | ||
|
|
||
| def test_close_does_not_raise(self): | ||
| self.strategy.close() | ||
|
|
||
|
|
||
| class TestExistingStrategiesDefaultHeaderInjection(unittest.TestCase): | ||
| """Regression: existing strategies must still inject router headers by default.""" | ||
|
|
||
| def test_direct_injects_headers(self): | ||
| s = DirectConnectionStrategy(SandboxDirectConnectionConfig(api_url="http://x")) | ||
| self.assertTrue(s.should_inject_router_headers()) | ||
|
|
||
| def test_gateway_injects_headers(self): | ||
| s = GatewayConnectionStrategy( | ||
| SandboxGatewayConnectionConfig(gateway_name="gw"), | ||
| k8s_helper=MagicMock(), | ||
| ) | ||
| self.assertTrue(s.should_inject_router_headers()) | ||
|
|
||
| def test_local_tunnel_injects_headers(self): | ||
| s = LocalTunnelConnectionStrategy( | ||
| sandbox_id="s", namespace="ns", | ||
| config=SandboxLocalTunnelConnectionConfig(), | ||
| ) | ||
| self.assertTrue(s.should_inject_router_headers()) | ||
|
|
||
|
|
||
| class TestSandboxConnectorStrategySelection(unittest.TestCase): | ||
| def _make_connector(self, config): | ||
| return SandboxConnector( | ||
| sandbox_id="sb", | ||
| namespace="ns", | ||
| connection_config=config, | ||
| k8s_helper=MagicMock(), | ||
| ) | ||
|
|
||
| def test_selects_in_cluster_strategy(self): | ||
| config = SandboxInClusterConnectionConfig() | ||
| connector = self._make_connector(config) | ||
| self.assertIsInstance(connector.strategy, InClusterConnectionStrategy) | ||
|
|
||
| def test_selects_direct_strategy(self): | ||
| config = SandboxDirectConnectionConfig(api_url="http://x") | ||
| connector = self._make_connector(config) | ||
| self.assertIsInstance(connector.strategy, DirectConnectionStrategy) | ||
|
|
||
| def test_raises_on_unknown_config_type(self): | ||
| with self.assertRaises((ValueError, Exception)): | ||
| SandboxConnector( | ||
| sandbox_id="sb", | ||
| namespace="ns", | ||
| connection_config=object(), | ||
| k8s_helper=MagicMock(), | ||
| ) | ||
|
|
||
|
|
||
| class TestSandboxConnectorHeaderInjection(unittest.TestCase): | ||
| def _make_connector_with_strategy(self, strategy, config): | ||
| connector = SandboxConnector( | ||
| sandbox_id="my-sb", | ||
| namespace="my-ns", | ||
| connection_config=config, | ||
| k8s_helper=MagicMock(), | ||
| ) | ||
| connector.strategy = strategy | ||
| mock_session = MagicMock() | ||
| connector.session = mock_session | ||
| return connector, mock_session | ||
|
|
||
| def _mock_ok_response(self): | ||
| mock_resp = MagicMock(spec=requests.Response) | ||
| mock_resp.raise_for_status.return_value = None | ||
| return mock_resp | ||
|
|
||
| def test_router_headers_NOT_sent_for_in_cluster(self): | ||
| config = SandboxInClusterConnectionConfig(server_port=8888) | ||
| strategy = InClusterConnectionStrategy("my-sb", "my-ns", config) | ||
| connector, mock_session = self._make_connector_with_strategy(strategy, config) | ||
| mock_session.request.return_value = self._mock_ok_response() | ||
|
|
||
| connector.send_request("GET", "/execute") | ||
|
|
||
| call_args, call_kwargs = mock_session.request.call_args | ||
| sent_headers = call_kwargs.get("headers", {}) | ||
| self.assertNotIn("X-Sandbox-ID", sent_headers) | ||
| self.assertNotIn("X-Sandbox-Namespace", sent_headers) | ||
| self.assertNotIn("X-Sandbox-Port", sent_headers) | ||
|
|
||
| def test_router_headers_ARE_sent_for_direct(self): | ||
| config = SandboxDirectConnectionConfig(api_url="http://router") | ||
| strategy = DirectConnectionStrategy(config) | ||
| connector, mock_session = self._make_connector_with_strategy(strategy, config) | ||
| mock_session.request.return_value = self._mock_ok_response() | ||
|
|
||
| connector.send_request("GET", "/execute") | ||
|
|
||
| call_args, call_kwargs = mock_session.request.call_args | ||
| sent_headers = call_kwargs.get("headers", {}) | ||
| self.assertIn("X-Sandbox-ID", sent_headers) | ||
| self.assertIn("X-Sandbox-Namespace", sent_headers) | ||
| self.assertIn("X-Sandbox-Port", sent_headers) | ||
|
|
||
| def test_in_cluster_url_is_pod_dns(self): | ||
| config = SandboxInClusterConnectionConfig(server_port=8888) | ||
| strategy = InClusterConnectionStrategy("my-sb", "my-ns", config) | ||
| connector, mock_session = self._make_connector_with_strategy(strategy, config) | ||
| mock_session.request.return_value = self._mock_ok_response() | ||
|
|
||
| connector.send_request("POST", "execute") | ||
|
|
||
| call_args, call_kwargs = mock_session.request.call_args | ||
| url = call_args[1] | ||
| self.assertEqual(url, "http://my-sb.my-ns.svc.cluster.local:8888/execute") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vicentefb is this okay to do so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any update on this ?