Skip to content

Commit 8a3f3e5

Browse files
authored
feat(server): add sandbox templates (ai-openshell/openshell#2833)
* feat(server): add sandbox workload templates Signed-off-by: Gordon Sim <gsim@redhat.com> * feat(go-sdk): add sandbox workload template support Signed-off-by: Gordon Sim <gsim@redhat.com> * feat(rust-sdk): add sandbox workload template support Signed-off-by: Gordon Sim <gsim@redhat.com> * feat(python-sdk): add sandbox workload template support Signed-off-by: Gordon Sim <gsim@redhat.com> * feat(typescript-sdk): add sandbox workload template support Signed-off-by: Gordon Sim <gsim@redhat.com> * docs(agents): document sandbox workload templates Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(cli): support default GPU requests in sandbox templates Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(server): cap sandbox templates per workspace Signed-off-by: Gordon Sim <gsim@redhat.com> * docs(architecture): document sandbox workload template boundaries Signed-off-by: Gordon Sim <gsim@redhat.com> * feat(cli+sdk): expose sandbox workload template provenance Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(cli): include sandbox template annotations in output Signed-off-by: Gordon Sim <gsim@redhat.com> * feat(sandbox): add label selectors to template listing Signed-off-by: Gordon Sim <gsim@redhat.com> * test(e2e): cover sandbox template failure paths Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(server): preserve command and ttl when creating sandbox from template Signed-off-by: Gordon Sim <gsim@redhat.com> * test(server): add field coverage test for template merge Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(go-sdk): add pagination support to fake client Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(cli): warn on env vars that looks like secrets Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(sdk-ts): propagate sandbox workspace through lifecycle calls Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(docs): update workspace management docs Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(ts-sdk): support command and tty when creating from template Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(go-sdk): support command and tty when creating from template Signed-off-by: Gordon Sim <gsim@redhat.com> * test(python-sdk): verify command and tty handling when creating from template Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(go-sdk): update docs and ClientInterface Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(server): validate sandbox create specs before I/O Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(go-sdk): guard empty DNS-1123 label validation Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(python-sdk): allow empty template builder mappings Signed-off-by: Gordon Sim <gsim@redhat.com> * fix(cli): align template GPU JSON default output Signed-off-by: Gordon Sim <gsim@redhat.com> --------- Signed-off-by: Gordon Sim <gsim@redhat.com>
1 parent fd4a064 commit 8a3f3e5

4 files changed

Lines changed: 655 additions & 3 deletions

File tree

‎src/openshell/__init__.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
SandboxRef,
1818
SandboxSession,
1919
SandboxStatusRef,
20+
SandboxTemplateClient,
21+
SandboxWorkloadTemplateProvenanceRef,
2022
TlsConfig,
2123
WorkspaceClient,
2224
WorkspaceRef,
@@ -41,6 +43,8 @@
4143
"SandboxRef",
4244
"SandboxSession",
4345
"SandboxStatusRef",
46+
"SandboxTemplateClient",
47+
"SandboxWorkloadTemplateProvenanceRef",
4448
"TlsConfig",
4549
"WorkspaceClient",
4650
"WorkspaceRef",

‎src/openshell/sandbox.py‎

Lines changed: 245 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,12 @@ def __reduce_ex__(self, protocol: SupportsIndex, /) -> str | tuple[Any, ...]:
394394
return type(self), (dict(self),)
395395

396396

397+
@dataclass(frozen=True)
398+
class SandboxWorkloadTemplateProvenanceRef:
399+
name: str
400+
resource_version: str
401+
402+
397403
@dataclass(frozen=True)
398404
class SandboxRef:
399405
id: str
@@ -403,6 +409,7 @@ class SandboxRef:
403409
# Excluded from equality/hash to preserve the original identity while the
404410
# immutable mapping remains safe for deepcopy, pickle, and asdict.
405411
labels: Mapping[str, str] = field(default_factory=_ImmutableLabels, compare=False)
412+
created_from_workload_template: SandboxWorkloadTemplateProvenanceRef | None = None
406413

407414
def __post_init__(self) -> None:
408415
object.__setattr__(self, "labels", _ImmutableLabels(self.labels))
@@ -726,6 +733,33 @@ def create(
726733
raise SandboxError("CreateSandbox returned empty sandbox id")
727734
return sandbox_ref
728735

736+
def create_from_template(
737+
self,
738+
*,
739+
workspace: str,
740+
template_name: str,
741+
spec: openshell_pb2.SandboxSpec | None = None,
742+
name: str | None = None,
743+
labels: Mapping[str, str] | None = None,
744+
) -> SandboxRef:
745+
if not template_name.strip():
746+
raise SandboxError("template_name is required")
747+
request_spec = spec if spec is not None else openshell_pb2.SandboxSpec()
748+
response = self._stub.CreateSandbox(
749+
openshell_pb2.CreateSandboxRequest(
750+
spec=request_spec,
751+
name=name or "",
752+
labels=dict(labels) if labels else {},
753+
workspace=workspace,
754+
workload_template_name=template_name,
755+
),
756+
timeout=self._timeout,
757+
)
758+
sandbox_ref = _sandbox_ref(response.sandbox)
759+
if sandbox_ref.id == "":
760+
raise SandboxError("CreateSandbox returned empty sandbox id")
761+
return sandbox_ref
762+
729763
def create_session(
730764
self,
731765
*,
@@ -738,6 +772,29 @@ def create_session(
738772
self, self.create(workspace=workspace, spec=spec, name=name, labels=labels)
739773
)
740774

775+
def create_session_from_template(
776+
self,
777+
*,
778+
workspace: str,
779+
template_name: str,
780+
spec: openshell_pb2.SandboxSpec | None = None,
781+
name: str | None = None,
782+
labels: Mapping[str, str] | None = None,
783+
) -> SandboxSession:
784+
return SandboxSession(
785+
self,
786+
self.create_from_template(
787+
workspace=workspace,
788+
template_name=template_name,
789+
spec=spec,
790+
name=name,
791+
labels=labels,
792+
),
793+
)
794+
795+
def sandbox_templates(self) -> SandboxTemplateClient:
796+
return SandboxTemplateClient(self._channel, timeout=self._timeout)
797+
741798
def get(self, sandbox_name: str, *, workspace: str) -> SandboxRef:
742799
response = self._stub.GetSandbox(
743800
openshell_pb2.GetSandboxRequest(name=sandbox_name, workspace=workspace),
@@ -1027,6 +1084,126 @@ def exec_python(
10271084
)
10281085

10291086

1087+
class SandboxTemplateClient:
1088+
"""gRPC client for reusable sandbox template lifecycle operations."""
1089+
1090+
def __init__(self, channel: grpc.Channel, *, timeout: float = 30.0) -> None:
1091+
self._stub = openshell_pb2_grpc.OpenShellStub(channel)
1092+
self._timeout = timeout
1093+
1094+
@classmethod
1095+
def from_sandbox_client(cls, client: SandboxClient) -> SandboxTemplateClient:
1096+
return client.sandbox_templates()
1097+
1098+
def create(
1099+
self,
1100+
*,
1101+
workspace: str,
1102+
template: openshell_pb2.SandboxWorkloadTemplate | None = None,
1103+
name: str | None = None,
1104+
image: str | None = None,
1105+
labels: Mapping[str, str] | None = None,
1106+
annotations: Mapping[str, str] | None = None,
1107+
environment: Mapping[str, str] | None = None,
1108+
cpu: str | None = None,
1109+
memory: str | None = None,
1110+
gpu_count: int | None = None,
1111+
gpu: bool = False,
1112+
driver_config: Mapping[str, Any] | None = None,
1113+
) -> openshell_pb2.SandboxWorkloadTemplate:
1114+
if template is None:
1115+
if name is None or not name.strip():
1116+
raise SandboxError("name is required when template is omitted")
1117+
template = _sandbox_workload_template(
1118+
name=name,
1119+
image=image,
1120+
labels=labels,
1121+
annotations=annotations,
1122+
environment=environment,
1123+
cpu=cpu,
1124+
memory=memory,
1125+
gpu_count=gpu_count,
1126+
gpu=gpu,
1127+
driver_config=driver_config,
1128+
)
1129+
elif (
1130+
any(value is not None for value in (name, image, cpu, memory, gpu_count))
1131+
or any(
1132+
bool(value)
1133+
for value in (labels, annotations, environment, driver_config)
1134+
)
1135+
or gpu
1136+
):
1137+
raise SandboxError(
1138+
"template cannot be combined with template builder fields"
1139+
)
1140+
1141+
response = self._stub.CreateSandboxTemplate(
1142+
openshell_pb2.CreateSandboxTemplateRequest(
1143+
workspace=workspace,
1144+
template=template,
1145+
),
1146+
timeout=self._timeout,
1147+
)
1148+
return response.template
1149+
1150+
def get(
1151+
self,
1152+
name: str,
1153+
*,
1154+
workspace: str,
1155+
) -> openshell_pb2.SandboxWorkloadTemplate:
1156+
response = self._stub.GetSandboxTemplate(
1157+
openshell_pb2.GetSandboxTemplateRequest(name=name, workspace=workspace),
1158+
timeout=self._timeout,
1159+
)
1160+
return response.template
1161+
1162+
def list(
1163+
self,
1164+
*,
1165+
workspace: str,
1166+
limit: int = 100,
1167+
offset: int = 0,
1168+
label_selector: str = "",
1169+
) -> builtins.list[openshell_pb2.SandboxWorkloadTemplate]:
1170+
response = self._stub.ListSandboxTemplates(
1171+
openshell_pb2.ListSandboxTemplatesRequest(
1172+
workspace=workspace,
1173+
limit=limit,
1174+
offset=offset,
1175+
label_selector=label_selector,
1176+
),
1177+
timeout=self._timeout,
1178+
)
1179+
return list(response.templates)
1180+
1181+
def list_for_all_workspaces(
1182+
self,
1183+
*,
1184+
limit: int = 100,
1185+
offset: int = 0,
1186+
label_selector: str = "",
1187+
) -> builtins.list[openshell_pb2.SandboxWorkloadTemplate]:
1188+
response = self._stub.ListSandboxTemplates(
1189+
openshell_pb2.ListSandboxTemplatesRequest(
1190+
all_workspaces=True,
1191+
limit=limit,
1192+
offset=offset,
1193+
label_selector=label_selector,
1194+
),
1195+
timeout=self._timeout,
1196+
)
1197+
return list(response.templates)
1198+
1199+
def delete(self, name: str, *, workspace: str) -> bool:
1200+
response = self._stub.DeleteSandboxTemplate(
1201+
openshell_pb2.DeleteSandboxTemplateRequest(name=name, workspace=workspace),
1202+
timeout=self._timeout,
1203+
)
1204+
return bool(response.deleted)
1205+
1206+
10301207
@dataclass(frozen=True)
10311208
class InferenceRouteConfig:
10321209
provider_name: str
@@ -1182,6 +1359,7 @@ def __init__(
11821359
spec: openshell_pb2.SandboxSpec | None = None,
11831360
name: str | None = None,
11841361
labels: Mapping[str, str] | None = None,
1362+
template_name: str | None = None,
11851363
timeout: float = 30.0,
11861364
ready_timeout_seconds: float = 120.0,
11871365
auto_refresh: bool = True,
@@ -1207,6 +1385,7 @@ def __init__(
12071385
self._name = name
12081386
# Copy so later caller mutation cannot change what gets sent on enter.
12091387
self._labels = dict(labels) if labels is not None else None
1388+
self._template_name = template_name
12101389
self._timeout = timeout
12111390
self._ready_timeout_seconds = ready_timeout_seconds
12121391
self._auto_refresh = auto_refresh
@@ -1232,10 +1411,12 @@ def __enter__(self) -> Sandbox:
12321411
# Creation metadata cannot be applied when attaching to an existing
12331412
# sandbox; reject it before opening a connection.
12341413
if self._sandbox_input is not None and (
1235-
self._name is not None or self._labels is not None
1414+
self._name is not None
1415+
or self._labels is not None
1416+
or self._template_name is not None
12361417
):
12371418
raise SandboxError(
1238-
"name and labels cannot be set when attaching to an existing sandbox"
1419+
"name, labels, and template_name cannot be set when attaching to an existing sandbox"
12391420
)
12401421

12411422
client = SandboxClient.from_active_cluster(
@@ -1248,7 +1429,15 @@ def __enter__(self) -> Sandbox:
12481429
)
12491430
self._client = client
12501431

1251-
if self._sandbox_input is None:
1432+
if self._sandbox_input is None and self._template_name is not None:
1433+
self._session = client.create_session_from_template(
1434+
workspace=self._workspace,
1435+
template_name=self._template_name,
1436+
spec=self._spec,
1437+
name=self._name,
1438+
labels=self._labels,
1439+
)
1440+
elif self._sandbox_input is None:
12521441
self._session = client.create_session(
12531442
workspace=self._workspace,
12541443
spec=self._spec,
@@ -1374,6 +1563,14 @@ def _serialize_python_callable(
13741563

13751564
def _sandbox_ref(sandbox: openshell_pb2.Sandbox) -> SandboxRef:
13761565
status = sandbox.status if sandbox.HasField("status") else None
1566+
provenance = (
1567+
SandboxWorkloadTemplateProvenanceRef(
1568+
name=sandbox.created_from_workload_template.name,
1569+
resource_version=sandbox.created_from_workload_template.resource_version,
1570+
)
1571+
if sandbox.HasField("created_from_workload_template")
1572+
else None
1573+
)
13771574
return SandboxRef(
13781575
id=sandbox.metadata.id if sandbox.metadata else "",
13791576
name=sandbox.metadata.name if sandbox.metadata else "",
@@ -1386,6 +1583,7 @@ def _sandbox_ref(sandbox: openshell_pb2.Sandbox) -> SandboxRef:
13861583
else None,
13871584
),
13881585
labels=sandbox.metadata.labels if sandbox.metadata else {},
1586+
created_from_workload_template=provenance,
13891587
)
13901588

13911589

@@ -1398,6 +1596,50 @@ def _default_spec() -> openshell_pb2.SandboxSpec:
13981596
return openshell_pb2.SandboxSpec()
13991597

14001598

1599+
def _sandbox_workload_template(
1600+
*,
1601+
name: str,
1602+
image: str | None = None,
1603+
labels: Mapping[str, str] | None = None,
1604+
annotations: Mapping[str, str] | None = None,
1605+
environment: Mapping[str, str] | None = None,
1606+
cpu: str | None = None,
1607+
memory: str | None = None,
1608+
gpu_count: int | None = None,
1609+
gpu: bool = False,
1610+
driver_config: Mapping[str, Any] | None = None,
1611+
) -> openshell_pb2.SandboxWorkloadTemplate:
1612+
if gpu_count is not None and gpu_count <= 0:
1613+
raise SandboxError("gpu_count must be greater than zero")
1614+
1615+
template = openshell_pb2.SandboxWorkloadTemplate()
1616+
template.metadata.name = name
1617+
# The gateway permits an empty workload so it can apply the default image
1618+
# at sandbox create time, but the workload message itself is required.
1619+
template.spec.workload.SetInParent()
1620+
if labels:
1621+
template.metadata.labels.update(dict(labels))
1622+
if annotations:
1623+
template.metadata.annotations.update(dict(annotations))
1624+
1625+
if image is not None:
1626+
template.spec.workload.image = image
1627+
if environment:
1628+
template.spec.workload.environment.update(dict(environment))
1629+
if cpu is not None:
1630+
template.spec.workload.resources.cpu = cpu
1631+
if memory is not None:
1632+
template.spec.workload.resources.memory = memory
1633+
if gpu or gpu_count is not None:
1634+
template.spec.workload.resources.gpu.SetInParent()
1635+
if gpu_count is not None:
1636+
template.spec.workload.resources.gpu.count = gpu_count
1637+
if driver_config:
1638+
template.spec.driver_config.update(dict(driver_config))
1639+
1640+
return template
1641+
1642+
14011643
def _xdg_config_home() -> pathlib.Path:
14021644
configured = os.environ.get("XDG_CONFIG_HOME")
14031645
if configured:

‎tests/openshell_test.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99
def test_version() -> None:
1010
"""Test that version is defined."""
1111
assert openshell.__version__
12+
13+
14+
def test_sandbox_template_client_exported() -> None:
15+
assert openshell.SandboxTemplateClient

0 commit comments

Comments
 (0)