-
Notifications
You must be signed in to change notification settings - Fork 531
feat(sdk): inject Sandbox.create env_vars into the guest via envd /init #554
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
Closed
xiaojunxiang2023
wants to merge
1
commit into
TencentCloud:master
from
xiaojunxiang2023:fix/create-env-vars-injection
+231
−3
Closed
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,15 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import random | ||
| import time | ||
| from datetime import datetime, timezone | ||
| from typing import Any, Callable, Dict | ||
|
|
||
| import httpx | ||
| import requests | ||
|
|
||
| from ._commands import CommandResult, Commands | ||
| from ._commands import CommandResult, Commands, ENVD_PORT | ||
| from ._config import Config | ||
| from ._exceptions import ApiError, AuthenticationError, CubeSandboxError, SandboxNotFoundError, TemplateNotFoundError | ||
| from ._filesystem import Filesystem | ||
|
|
@@ -18,6 +21,10 @@ | |
| from ._transport import build_client | ||
|
|
||
| JUPYTER_PORT = 49999 | ||
| ENVD_INIT_MAX_ATTEMPTS = 5 | ||
| ENVD_INIT_RETRY_BASE_SECS = 0.8 | ||
| ENVD_INIT_RETRY_JITTER_SECS = 0.4 | ||
| ENVD_INIT_REQ_TIMEOUT_SECS = 10.0 | ||
|
|
||
|
|
||
| def _check_response(resp: requests.Response) -> None: | ||
|
|
@@ -156,7 +163,10 @@ def create( | |
| resp = s.post(f"{cfg.api_url}/sandboxes", json=payload, | ||
| headers={"Content-Type": "application/json"}) | ||
| _check_response(resp) | ||
| return cls(resp.json(), config=cfg) | ||
| sandbox = cls(resp.json(), config=cfg) | ||
| if env_vars: | ||
| sandbox._init_env_vars(env_vars) | ||
| return sandbox | ||
|
|
||
| @classmethod | ||
| def connect(cls, sandbox_id: str, *, config: Config | None = None) -> "Sandbox": | ||
|
|
@@ -667,3 +677,64 @@ def _build_session(self) -> requests.Session: | |
| def _build_data_client(self) -> httpx.Client: | ||
| """Build an HTTP client for CubeProxy-routed sandbox data-plane APIs.""" | ||
| return build_client(self._config) | ||
|
|
||
| def _init_env_vars(self, env_vars: Dict[str, str]) -> None: | ||
| """Make create-time env_vars visible to later commands.run / run_code. | ||
|
|
||
| The control plane only records env_vars as sandbox metadata; it never | ||
| loads them into the guest runtime. So once the sandbox is up we push | ||
| them into the guest via envd's native POST /init, reusing the exact | ||
| CubeProxy data-plane channel commands.run already uses. envd stores them | ||
| as global defaults and merges them into every later process execution, | ||
| giving the precedence ``template env < create env < per-command env``. | ||
|
|
||
| Routing through the configured data-plane client means no extra | ||
| deployment configuration is required: whatever address commands.run | ||
| reaches envd on, /init reaches it on too. | ||
| """ | ||
| if not env_vars: | ||
| return | ||
| if self._client is None: | ||
| self._client = self._build_data_client() | ||
|
xiaojunxiang2023 marked this conversation as resolved.
|
||
|
|
||
| headers = {} | ||
| access_token = self._data.get("envdAccessToken") | ||
| if access_token: | ||
| headers["X-Access-Token"] = access_token | ||
|
|
||
| url = f"http://{self.get_host(ENVD_PORT)}/init" | ||
| body = { | ||
| "envVars": env_vars, | ||
| "timestamp": datetime.now(timezone.utc).isoformat(), | ||
| } | ||
|
|
||
| # The proxy route to a freshly created sandbox may settle a moment after | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have any clues on this? |
||
| # create returns, so retry briefly before surfacing a hard failure. | ||
| last_error: Exception | None = None | ||
| for attempt in range(ENVD_INIT_MAX_ATTEMPTS): | ||
| if attempt: | ||
| delay = ENVD_INIT_RETRY_BASE_SECS + random.uniform( | ||
| 0, ENVD_INIT_RETRY_JITTER_SECS | ||
| ) | ||
| time.sleep(delay) | ||
| try: | ||
| resp = self._client.post( | ||
| url, | ||
| json=body, | ||
| headers=headers, | ||
| timeout=ENVD_INIT_REQ_TIMEOUT_SECS, | ||
| ) | ||
| try: | ||
| if resp.status_code < 400: | ||
| return | ||
| last_error = RuntimeError( | ||
| f"envd /init returned HTTP {resp.status_code}" | ||
| ) | ||
| finally: | ||
| resp.close() | ||
| except BaseException as exc: | ||
| if isinstance(exc, (KeyboardInterrupt, SystemExit)): | ||
| raise | ||
| last_error = exc | ||
|
|
||
| raise CubeSandboxError(f"failed to inject create env_vars into sandbox: {last_error}") | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.