Skip to content

Commit ffe3fe5

Browse files
committed
feat(server): add sandbox k8s runtime
1 parent e173fce commit ffe3fe5

29 files changed

+4190
-32
lines changed

server/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ The server uses a TOML configuration file to select and configure the underlying
5858
```bash
5959
cp example.config.toml ~/.sandbox.toml
6060
```
61+
**[optional] Create K8S configuration file:
62+
The K8S version of the Sandbox Operator needs to be deployed in the cluster, refer to the Kubernetes directory.
63+
```bash
64+
cp example.config.k8s.toml ~/.sandbox.toml
65+
cp example.batchsandbox-template.yaml ~/batchsandbox-template.yaml
66+
```
6167

6268
**[optional] Edit `~/.sandbox.toml`** for your environment:
6369

server/README_zh.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ uv sync
6161
```bash
6262
cp example.config.zh.toml ~/.sandbox.toml
6363
```
64+
**[可选] 复制K8S版本配置文件:
65+
需要在集群中部署 K8S版本的Sandbox Operator,参考Kubernetes目录。
66+
```bash
67+
cp example.config.k8s.zh.toml ~/.sandbox.toml
68+
cp example.batchsandbox-template.yaml ~/batchsandbox-template.yaml
69+
```
6470

6571
**[可选] 编辑 `~/.sandbox.toml`** 适配您的环境:
6672

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Example BatchSandbox CR template for OpenSandbox Kubernetes runtime
2+
# This is a complete BatchSandbox CR template that will be merged with runtime values
3+
#
4+
# Usage in config.toml:
5+
# [kubernetes]
6+
# batchsandbox_template_file = "/path/to/this/file.yaml"
7+
8+
# Metadata template (will be merged with runtime-generated metadata)
9+
metadata:
10+
annotations:
11+
template-source: "batchsandbox-template.yaml"
12+
managed-by: "opensandbox"
13+
14+
# Spec template
15+
spec:
16+
replicas: 1
17+
# Pod template specification
18+
template:
19+
spec:
20+
restartPolicy: Never
21+
tolerations:
22+
- operator: "Exists"

server/example.config.k8s.toml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2025 Alibaba Group Holding Ltd.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Example Kubernetes Runtime Configuration for OpenSandbox Server
16+
#
17+
# This configuration file demonstrates how to configure the OpenSandbox server
18+
# to use Kubernetes as the sandbox runtime.
19+
#
20+
# Usage:
21+
# 1. Copy this file to ~/.sandbox.toml (or set SANDBOX_CONFIG_PATH environment variable)
22+
# 2. Update the configuration values according to your environment
23+
# 3. Start the server: uvicorn src.main:app --host 0.0.0.0 --port 8080
24+
25+
[server]
26+
host = "0.0.0.0"
27+
port = 8080
28+
log_level = "INFO"
29+
# api_key = "your-secret-api-key" # Optional: Uncomment to enable API key authentication
30+
31+
[runtime]
32+
type = "kubernetes"
33+
execd_image = "opensandbox/execd:latest"
34+
35+
[kubernetes]
36+
# Path to kubeconfig file. Leave as null to use in-cluster configuration
37+
# Replace with your path
38+
kubeconfig_path = "~/.kube/config"
39+
40+
# Namespace for sandbox workloads
41+
namespace = "opensandbox"
42+
43+
# Workload provider type: available providers are registered in the provider factory
44+
# If not specified, uses the first registered provider (typically "batchsandbox")
45+
workload_provider = "batchsandbox"
46+
47+
# Path to the BatchSandbox template file
48+
# Replace with your path
49+
batchsandbox_template_file = "~/batchsandbox-template.yaml"

server/example.config.k8s.zh.toml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2025 Alibaba Group Holding Ltd.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Example Kubernetes Runtime Configuration for OpenSandbox Server
16+
#
17+
# This configuration file demonstrates how to configure the OpenSandbox server
18+
# to use Kubernetes as the sandbox runtime.
19+
#
20+
# Usage:
21+
# 1. Copy this file to ~/.sandbox.toml (or set SANDBOX_CONFIG_PATH environment variable)
22+
# 2. Update the configuration values according to your environment
23+
# 3. Start the server: uvicorn src.main:app --host 0.0.0.0 --port 8080
24+
25+
[server]
26+
host = "0.0.0.0"
27+
port = 8080
28+
log_level = "INFO"
29+
# api_key = "your-secret-api-key" # Optional: Uncomment to enable API key authentication
30+
31+
[runtime]
32+
type = "kubernetes"
33+
execd_image = "sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/execd:latest"
34+
35+
[kubernetes]
36+
# Path to kubeconfig file. Leave as null to use in-cluster configuration
37+
# Replace with your path
38+
kubeconfig_path = "~/.kube/config"
39+
40+
# Namespace for sandbox workloads
41+
namespace = "opensandbox"
42+
43+
# Workload provider type: available providers are registered in the provider factory
44+
# If not specified, uses the first registered provider (typically "batchsandbox")
45+
workload_provider = "batchsandbox"
46+
47+
# Path to the BatchSandbox template file
48+
# Replace with your path
49+
batchsandbox_template_file = "~/batchsandbox-template.yaml"

server/example.config.toml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,3 @@ apparmor_profile = ""
4848
pids_limit = 512
4949
# Seccomp profile: empty string uses Docker default; set to an absolute path for a custom profile
5050
seccomp_profile = ""
51-
52-
53-
# -----------------------------------------------------------------
54-
# Kubernetes runtime example (uncomment when runtime.type = "kubernetes")
55-
# Exactly one runtime.* block should remain active at any time.
56-
#
57-
# [kubernetes]
58-
# # Path to kubeconfig for authenticating with the target cluster
59-
# kubeconfig_path = "~/.kube/config"
60-
# # Namespace that will host sandbox pods
61-
# namespace = "sandbox-system"
62-
# # Service account bound to sandbox workloads
63-
# service_account = "sandbox-runner"

server/example.config.zh.toml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,3 @@ apparmor_profile = ""
4848
pids_limit = 512
4949
# Seccomp profile: empty string uses Docker default; set to an absolute path for a custom profile
5050
seccomp_profile = ""
51-
52-
53-
# -----------------------------------------------------------------
54-
# Kubernetes runtime example (uncomment when runtime.type = "kubernetes")
55-
# Exactly one runtime.* block should remain active at any time.
56-
#
57-
# [kubernetes]
58-
# # Path to kubeconfig for authenticating with the target cluster
59-
# kubeconfig_path = "~/.kube/config"
60-
# # Namespace that will host sandbox pods
61-
# namespace = "sandbox-system"
62-
# # Service account bound to sandbox workloads
63-
# service_account = "sandbox-runner"

server/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies = [
2525
"docker",
2626
"fastapi",
2727
"httpx",
28+
"kubernetes",
2829
"pydantic",
2930
"pydantic-settings",
3031
"pyyaml",

server/src/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ class KubernetesRuntimeConfig(BaseModel):
104104
default=None,
105105
description="Service account bound to sandbox workloads.",
106106
)
107+
workload_provider: Optional[str] = Field(
108+
default=None,
109+
description="Workload provider type. If not specified, uses the first registered provider.",
110+
)
111+
batchsandbox_template_file: Optional[str] = Field(
112+
default=None,
113+
description="Path to BatchSandbox CR YAML template file. Used when workload_provider is 'batchsandbox'.",
114+
)
107115

108116

109117
class RuntimeConfig(BaseModel):

server/src/services/constants.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
SANDBOX_EMBEDDING_PROXY_PORT_LABEL = "opensandbox.io/embedding-proxy-port" # maps container 44772 -> host port
2121
SANDBOX_HTTP_PORT_LABEL = "opensandbox.io/http-port" # maps container 8080 -> host port
2222

23-
2423
class SandboxErrorCodes:
2524
"""Canonical error codes for sandbox service operations."""
2625

26+
# Docker runtime error codes
2727
DOCKER_INITIALIZATION_ERROR = "DOCKER::INITIALIZATION_ERROR"
2828
CONTAINER_QUERY_FAILED = "DOCKER::SANDBOX_QUERY_FAILED"
2929
SANDBOX_NOT_FOUND = "DOCKER::SANDBOX_NOT_FOUND"
@@ -41,8 +41,20 @@ class SandboxErrorCodes:
4141
BOOTSTRAP_INSTALL_FAILED = "DOCKER::SANDBOX_BOOTSTRAP_INSTALL_FAILED"
4242
INVALID_ENTRYPOINT = "DOCKER::INVALID_ENTRYPOINT"
4343
INVALID_PORT = "DOCKER::INVALID_PORT"
44-
INVALID_METADATA_LABEL = "SANDBOX::INVALID_METADATA_LABEL"
4544
NETWORK_MODE_ENDPOINT_UNAVAILABLE = "DOCKER::NETWORK_MODE_ENDPOINT_UNAVAILABLE"
45+
46+
# Kubernetes runtime error codes
47+
K8S_INITIALIZATION_ERROR = "KUBERNETES::INITIALIZATION_ERROR"
48+
K8S_SANDBOX_NOT_FOUND = "KUBERNETES::SANDBOX_NOT_FOUND"
49+
K8S_POD_FAILED = "KUBERNETES::POD_FAILED"
50+
K8S_POD_READY_TIMEOUT = "KUBERNETES::POD_READY_TIMEOUT"
51+
K8S_API_ERROR = "KUBERNETES::API_ERROR"
52+
K8S_POD_IP_NOT_AVAILABLE = "KUBERNETES::POD_IP_NOT_AVAILABLE"
53+
54+
# Common error codes
55+
UNKNOWN_ERROR = "SANDBOX::UNKNOWN_ERROR"
56+
API_NOT_SUPPORTED = "SANDBOX::API_NOT_SUPPORTED"
57+
INVALID_METADATA_LABEL = "SANDBOX::INVALID_METADATA_LABEL"
4658

4759

4860
__all__ = [

0 commit comments

Comments
 (0)