Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<namespace>.svc"
```

### Example sandbox code

See the [examples](/examples) directory for more basic operations.
Expand Down
6 changes: 6 additions & 0 deletions koyeb/sandbox/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
33 changes: 33 additions & 0 deletions koyeb/sandbox/test_sandbox_gateway_override.py
Original file line number Diff line number Diff line change
@@ -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()
Loading