Repository navigation
feat(sandbox): use the in-cluster sandbox URL when reachable - #110
BDev-Koyeb wants to merge 3 commits into
Conversation
| def select_sandbox_url(sandbox_metadata: Any) -> Optional[str]: | ||
| """Pick which sandbox gateway URL to call. | ||
|
|
||
| The API returns a public URL and, on self-hosted clusters, an in-cluster | ||
| one. The in-cluster URL is used when its host resolves (we run inside the | ||
| cluster); KOYEB_SANDBOX_URL=internal|public forces the choice. | ||
| """ | ||
| public_url = getattr(sandbox_metadata, "public_url", None) | ||
| internal_url = getattr(sandbox_metadata, "internal_url", None) | ||
| if not internal_url: | ||
| return public_url | ||
| mode = os.getenv(SANDBOX_URL_ENV, "").strip().lower() | ||
| if mode == "public": | ||
| return public_url | ||
| if mode == "internal" or not public_url: | ||
| return internal_url | ||
| host = urlparse(internal_url).hostname | ||
| if host and _resolves(host): | ||
| return internal_url | ||
| return public_url |
There was a problem hiding this comment.
Most important part, I know there are a lot of ifs but it is pretty explicit I would say, but let me know if you want it differently
| if metadata and metadata.sandbox: | ||
| return metadata.sandbox.public_url, metadata.sandbox.routing_key | ||
| return ( | ||
| select_sandbox_url(metadata.sandbox), |
There was a problem hiding this comment.
Can we somehow cache this in Sandbox object? Once we resolved once, we know the URL and don't need to pay the price anymore
There was a problem hiding this comment.
This is great comment btw 👍
On it
There was a problem hiding this comment.
I think it is actually already cached, the picked URL is stored on the Sandbox (self._sandbox_url) and only reset after a redeploy
There was a problem hiding this comment.
koyeb-python-sdk/koyeb/sandbox/sandbox.py
Lines 1118 to 1130 in 70ccd3d
this gets it onlly if empty, and it should be there at that time, cleared at redeploy
There was a problem hiding this comment.
Tho looking into all this made me push fix for get_domain() that I found
Context
On self-hosted clusters (MAIS), sandboxes are used both from inside the cluster and from outside. The API now returns
internal_urlnext topublic_urlin the sandbox metadata (https://github.com/mistralai/api.koyeb.com/pull/8797), so in-cluster clients don't have to leave the cluster and come back.Implementation
select_sandbox_url()incontrol_plane.py, used by both places that readpublic_url:internal_url(SaaS, older API, older sandboxes) →public_url, as todayKOYEB_SANDBOX_URL=internal|public→ forced choiceinternal_url(it's a*.svc.cluster.localname, so it only resolves in the cluster; the lookup is cached per host)public_urlSandboxMetadatamodels (sync + async), spec and docs are edited by hand to add the field: regenerating with the current generator rewrites ~1000 files.Checks / QA
pytest koyeb/sandbox: 379 passed, including newTestSelectSandboxUrlcases