From fcd783fa60115e39632493bfe9b151069bb61908 Mon Sep 17 00:00:00 2001 From: BDev-Koyeb Date: Tue, 10 Nov 2026 18:21:12 +0100 Subject: [PATCH] feat(sandbox): KOYEB_SANDBOX_GATEWAY_URL overrides the sandbox gateway URL --- README.md | 6 ++++ koyeb/sandbox/sandbox.py | 6 ++++ .../sandbox/test_sandbox_gateway_override.py | 33 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 koyeb/sandbox/test_sandbox_gateway_override.py diff --git a/README.md b/README.md index 78afd9ca..6e6924a5 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,12 @@ Optionally, set a default region for all sandbox deployments (defaults to `"na"` export KOYEB_REGION="fra" ``` +Optionally, send all sandbox commands and file operations to a specific sandbox gateway instead of the one the API returns (for example an in-cluster Service on a self-hosted cluster): + +```bash copy +export KOYEB_SANDBOX_GATEWAY_URL="http://sandbox-gateway..svc" +``` + ### Example sandbox code See the [examples](/examples) directory for more basic operations. diff --git a/koyeb/sandbox/sandbox.py b/koyeb/sandbox/sandbox.py index 513b0011..c3a945f2 100644 --- a/koyeb/sandbox/sandbox.py +++ b/koyeb/sandbox/sandbox.py @@ -1138,6 +1138,7 @@ def _get_sandbox_url(self) -> Optional[Tuple[str, Optional[str]]]: def _get_conn_info(self) -> ConnectionInfo: """ Internal method to get the parameters needed to connect to the sandbox. + KOYEB_SANDBOX_GATEWAY_URL, when set, replaces the gateway URL. Returns: ConnectionInfo: the information needed to connect to the sandbox @@ -1151,6 +1152,11 @@ def _get_conn_info(self) -> ConnectionInfo: "Sandbox URL is not available (the sandbox may no longer exist)" ) sandbox_url, routing_key = url + gateway_url = os.getenv("KOYEB_SANDBOX_GATEWAY_URL") + if gateway_url: + # Full override of the gateway the API advertises, like + # KOYEB_API_HOST for the API (e.g. an in-cluster Service). + sandbox_url = f"{gateway_url.rstrip('/')}/koyeb-sandbox" return ConnectionInfo(sandbox_url, routing_key, self.sandbox_secret) def _get_client(self) -> "SandboxClient": # type: ignore[name-defined] diff --git a/koyeb/sandbox/test_sandbox_gateway_override.py b/koyeb/sandbox/test_sandbox_gateway_override.py new file mode 100644 index 00000000..0efd38d2 --- /dev/null +++ b/koyeb/sandbox/test_sandbox_gateway_override.py @@ -0,0 +1,33 @@ +"""KOYEB_SANDBOX_GATEWAY_URL replaces the gateway URL for exec/files.""" + +import unittest +from unittest import mock + +from koyeb.sandbox.sandbox import Sandbox + + +class TestSandboxGatewayOverride(unittest.TestCase): + def _sandbox(self): + sandbox = Sandbox( + sandbox_id="svc-1", app_id="app-1", service_id="svc-1", sandbox_secret="s" + ) + sandbox._sandbox_url = ("https://sandboxes.example.com/koyeb-sandbox", "rk") + return sandbox + + def test_unset_uses_url_from_api(self): + with mock.patch.dict("os.environ", {"KOYEB_SANDBOX_GATEWAY_URL": ""}): + conn = self._sandbox()._get_conn_info() + self.assertEqual(conn.public_url, "https://sandboxes.example.com/koyeb-sandbox") + self.assertEqual(conn.routing_key, "rk") + + def test_set_overrides_gateway(self): + env = {"KOYEB_SANDBOX_GATEWAY_URL": "http://sandbox-gateway.ns.svc/"} + with mock.patch.dict("os.environ", env): + conn = self._sandbox()._get_conn_info() + self.assertEqual(conn.public_url, "http://sandbox-gateway.ns.svc/koyeb-sandbox") + self.assertEqual(conn.routing_key, "rk") + self.assertEqual(conn.secret, "s") + + +if __name__ == "__main__": + unittest.main()