Skip to content

Commit 3a88553

Browse files
authored
feat(sandbox): support custom volume settings (#2957)
1 parent f3efd71 commit 3a88553

5 files changed

Lines changed: 43 additions & 47 deletions

File tree

apiserver/paasng/conf.yaml.tpl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -909,20 +909,23 @@ DEV_SANDBOX_CLUSTER: ""
909909
# 存放 sandbox daemon 二进制的 key
910910
# AGENT_SANDBOX_DAEMON_KEY: "sandbox/daemon"
911911

912-
# ---- Agent Sandbox 共享存储(CFS + CSI inline 方式)----
912+
# ---- Agent Sandbox 共享存储(CSI inline 方式)----
913913

914-
# 是否启用 Agent Sandbox 共享卷功能,未配置关键字段(FSID/HOST)时保持关闭
914+
# 是否启用 Agent Sandbox 共享卷功能,未正确配置 CSI driver / volumeAttributes 时保持关闭
915915
# AGENT_SANDBOX_VOLUME_ENABLED: false
916-
# CSI driver 名称,默认腾讯云 TKE 的 CFS 驱动
917-
# AGENT_SANDBOX_CFS_DRIVER: "com.tencent.cloud.csi.cfs"
918-
# CFS 文件系统 ID
919-
# AGENT_SANDBOX_CFS_FSID: ""
920-
# CFS 挂载目标地址
921-
# AGENT_SANDBOX_CFS_HOST: ""
922-
# CFS 根路径
923-
# AGENT_SANDBOX_CFS_PATH: "/"
924-
# CFS 协议版本
925-
# AGENT_SANDBOX_CFS_VERS: "3"
916+
# CSI driver 名称,默认腾讯云 TKE 的 CFS 驱动,可替换为其他 RWX CSI driver(如 NFS、CephFS)
917+
# AGENT_SANDBOX_VOLUME_CSI_DRIVER: "com.tencent.cloud.csi.cfs"
918+
# CSI volumeAttributes,原样透传给 K8s,键值由具体 CSI driver 决定
919+
# CFS 示例:
920+
# AGENT_SANDBOX_VOLUME_CSI_ATTRIBUTES:
921+
# fsid: ""
922+
# host: ""
923+
# path: "/"
924+
# vers: "3"
925+
# NFS 示例:
926+
# AGENT_SANDBOX_VOLUME_CSI_ATTRIBUTES:
927+
# server: "10.0.0.1"
928+
# share: "/data"
926929

927930
# 沙箱容器内不允许用户挂载共享卷的路径前缀黑名单,防止覆盖 daemon 或基础工具链
928931
# AGENT_SANDBOX_MOUNT_PATH_DENY_PREFIXES:

apiserver/paasng/paas_wl/bk_app/agent_sandbox/kres_slzs.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,14 @@ def _build_resources(cpu: float, memory: float) -> Dict[str, Dict[str, str]]:
145145
def _build_csi_volume_source() -> Dict[str, Any]:
146146
"""Build the ``csi`` source block for the shared inline volume.
147147
148-
The driver and volumeAttributes are driven by settings so that future
149-
deployments can swap CFS for other RWX-capable CSI drivers (e.g. CephFS,
150-
NFS) without code changes. For drivers beyond Tencent Cloud CFS, additional
151-
branches can be added here once their volumeAttributes schema is known.
148+
The driver and volumeAttributes are fully driven by settings so that any
149+
RWX-capable CSI driver (Tencent Cloud CFS, NFS, CephFS, ...) can be used
150+
without code changes: ``AGENT_SANDBOX_VOLUME_CSI_ATTRIBUTES`` is passed
151+
through as-is, and its keys are defined by the target CSI driver.
152152
"""
153-
driver = settings.AGENT_SANDBOX_CFS_DRIVER
154153
return {
155-
"driver": driver,
156-
"volumeAttributes": {
157-
"fsid": settings.AGENT_SANDBOX_CFS_FSID,
158-
"host": settings.AGENT_SANDBOX_CFS_HOST,
159-
"path": settings.AGENT_SANDBOX_CFS_PATH,
160-
"vers": settings.AGENT_SANDBOX_CFS_VERS,
161-
},
154+
"driver": settings.AGENT_SANDBOX_VOLUME_CSI_DRIVER,
155+
"volumeAttributes": dict(settings.AGENT_SANDBOX_VOLUME_CSI_ATTRIBUTES),
162156
}
163157

164158

apiserver/paasng/paasng/platform/agent_sandbox/serializers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ class Meta:
177177
fields = ("uuid", "name", "display_name", "application_id", "storage_instance_id", "storage_path", "created")
178178

179179
def get_storage_instance_id(self, obj) -> str:
180-
return settings.AGENT_SANDBOX_CFS_FSID
180+
# 存储实例标识,键名由具体 CSI driver 决定(CFS 为 fsid,NFS 为 server 等)
181+
attrs = settings.AGENT_SANDBOX_VOLUME_CSI_ATTRIBUTES or {}
182+
return attrs.get("fsid") or attrs.get("server") or ""
181183

182184
def get_storage_path(self, obj) -> str:
183185
return obj.storage_path

apiserver/paasng/paasng/settings/workloads.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,15 @@
118118
# 示例:'agent-sandbox-router.example.com', 'example.com/agent-sandbox-router'
119119
AGENT_SANDBOX_ROUTER_ENDPOINT = settings.get("AGENT_SANDBOX_ROUTER_ENDPOINT")
120120

121-
# Agent Sandbox 共享存储(CFS + CSI inline 方式)
122-
# 未配置关键字段(FSID/HOST)时保持关闭,避免 Pod 因配置缺失而起不来
121+
# Agent Sandbox 共享存储(CSI inline 方式)
122+
# 未启用或未正确配置 CSI driver / volumeAttributes 时保持关闭,避免 Pod 因配置缺失而起不来
123123
AGENT_SANDBOX_VOLUME_ENABLED = settings.get("AGENT_SANDBOX_VOLUME_ENABLED", False)
124-
# CSI driver 名称,默认腾讯云 TKE 的 CFS 驱动
125-
AGENT_SANDBOX_CFS_DRIVER = settings.get("AGENT_SANDBOX_CFS_DRIVER", "com.tencent.cloud.csi.cfs")
126-
# CFS 文件系统 ID / 挂载目标地址 / 根路径 / 协议版本,来自 CFS 控制台
127-
AGENT_SANDBOX_CFS_FSID = settings.get("AGENT_SANDBOX_CFS_FSID", "")
128-
AGENT_SANDBOX_CFS_HOST = settings.get("AGENT_SANDBOX_CFS_HOST", "")
129-
AGENT_SANDBOX_CFS_PATH = settings.get("AGENT_SANDBOX_CFS_PATH", "/")
130-
AGENT_SANDBOX_CFS_VERS = settings.get("AGENT_SANDBOX_CFS_VERS", "3")
124+
# CSI driver 名称,默认腾讯云 TKE 的 CFS 驱动;可替换为其他 RWX CSI driver(如 NFS、CephFS)
125+
AGENT_SANDBOX_VOLUME_CSI_DRIVER = settings.get("AGENT_SANDBOX_VOLUME_CSI_DRIVER", "com.tencent.cloud.csi.cfs")
126+
# CSI volumeAttributes,原样透传给 K8s(键值由具体 CSI driver 决定)。
127+
# CFS 示例: {"fsid": "...", "host": "...", "path": "/", "vers": "3"}
128+
# NFS 示例: {"server": "10.0.0.1", "share": "/data"}
129+
AGENT_SANDBOX_VOLUME_CSI_ATTRIBUTES = settings.get("AGENT_SANDBOX_VOLUME_CSI_ATTRIBUTES", {})
131130

132131
# ---------------
133132
# 资源命名配置

apiserver/paasng/tests/paas_wl/bk_app/agent_sandbox/test_kres_slzs.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ def test_single_volume_mount_is_rw(self, sbx_app):
9797
assert len(spec["volumes"]) == 1
9898
vol = spec["volumes"][0]
9999
assert vol["name"] == SHARED_VOLUME_NAME_IN_POD
100-
assert vol["csi"]["driver"] == settings.AGENT_SANDBOX_CFS_DRIVER
101-
attrs = vol["csi"]["volumeAttributes"]
102-
assert set(attrs.keys()) == {"fsid", "host", "path", "vers"}
100+
assert vol["csi"]["driver"] == settings.AGENT_SANDBOX_VOLUME_CSI_DRIVER
101+
assert vol["csi"]["volumeAttributes"] == dict(settings.AGENT_SANDBOX_VOLUME_CSI_ATTRIBUTES)
103102

104103
mounts = spec["containers"][0]["volumeMounts"]
105104
assert mounts == [
@@ -145,11 +144,12 @@ def test_two_volumes_share_single_csi_volume(self, sbx_app):
145144
assert sub_paths == [f"app/{vid1.replace('-', '')}", f"app/{vid2.replace('-', '')}"]
146145

147146
def test_csi_attributes_follow_settings(self, sbx_app, settings):
148-
settings.AGENT_SANDBOX_CFS_DRIVER = "com.example.csi.other"
149-
settings.AGENT_SANDBOX_CFS_FSID = "fsid-x"
150-
settings.AGENT_SANDBOX_CFS_HOST = "10.0.0.1"
151-
settings.AGENT_SANDBOX_CFS_PATH = "/data"
152-
settings.AGENT_SANDBOX_CFS_VERS = "4"
147+
# 验证 driver 与 volumeAttributes 完全由 settings 透传,可适配任意 CSI 实现(如 NFS)
148+
settings.AGENT_SANDBOX_VOLUME_CSI_DRIVER = "nfs.csi.k8s.io"
149+
settings.AGENT_SANDBOX_VOLUME_CSI_ATTRIBUTES = {
150+
"server": "10.0.0.1",
151+
"share": "/data",
152+
}
153153

154154
vid = _vol_id()
155155
sbx = _make_sandbox(
@@ -166,12 +166,10 @@ def test_csi_attributes_follow_settings(self, sbx_app, settings):
166166
spec = AgentSandboxSerializer._construct_pod_spec(sbx)
167167
csi = spec["volumes"][0]["csi"]
168168

169-
assert csi["driver"] == "com.example.csi.other"
169+
assert csi["driver"] == "nfs.csi.k8s.io"
170170
assert csi["volumeAttributes"] == {
171-
"fsid": "fsid-x",
172-
"host": "10.0.0.1",
173-
"path": "/data",
174-
"vers": "4",
171+
"server": "10.0.0.1",
172+
"share": "/data",
175173
}
176174

177175

0 commit comments

Comments
 (0)