Skip to content

Commit 614c8c1

Browse files
authored
feat(kubernetes): support PVC subPath driver config (ai-openshell#2034)
* feat(kubernetes): support PVC subPath driver config Signed-off-by: mjamiv <michael.commack@gmail.com> * test(kubernetes): cover writable PVC driver config Signed-off-by: mjamiv <michael.commack@gmail.com> * fix(kubernetes): address PVC subPath review feedback Signed-off-by: mjamiv <michael.commack@gmail.com> * fix(kubernetes): address PVC config review follow-up Signed-off-by: mjamiv <michael.commack@gmail.com> * fix(kubernetes): address PVC review follow-ups --------- Signed-off-by: mjamiv <michael.commack@gmail.com>
1 parent 8eacb47 commit 614c8c1

6 files changed

Lines changed: 1152 additions & 52 deletions

File tree

‎crates/openshell-core/src/driver_mounts.rs‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ pub fn validate_container_mount_target(target: &str) -> Result<(), String> {
9191
if !target.starts_with('/') {
9292
return Err("mount target must be an absolute container path".to_string());
9393
}
94+
if target != "/" {
95+
let segments = target.split('/').skip(1).collect::<Vec<_>>();
96+
let has_internal_empty_segment = segments
97+
.iter()
98+
.take(segments.len().saturating_sub(1))
99+
.any(|segment| segment.is_empty());
100+
if has_internal_empty_segment || segments.contains(&".") {
101+
return Err(
102+
"mount target must be normalized and must not contain empty path segments or '.'"
103+
.to_string(),
104+
);
105+
}
106+
}
94107
let path = Path::new(target);
95108
if path == Path::new("/") {
96109
return Err("mount target must not be the container root".to_string());
@@ -122,7 +135,8 @@ pub fn normalize_mount_target(target: &str) -> String {
122135
target.trim_end_matches('/').to_string()
123136
}
124137

125-
fn path_is_or_under(path: &Path, parent: &Path) -> bool {
138+
/// Return true when `path` is exactly `parent` or is contained below it.
139+
pub fn path_is_or_under(path: &Path, parent: &Path) -> bool {
126140
path == parent || path.starts_with(parent)
127141
}
128142

@@ -185,4 +199,20 @@ mod tests {
185199
"mount target must not contain surrounding whitespace"
186200
);
187201
}
202+
#[test]
203+
fn mount_target_rejects_internal_empty_or_dot_segments() {
204+
assert_eq!(
205+
validate_container_mount_target("/sandbox/work//tmp").unwrap_err(),
206+
"mount target must be normalized and must not contain empty path segments or '.'"
207+
);
208+
assert_eq!(
209+
validate_container_mount_target("/sandbox/work/./tmp").unwrap_err(),
210+
"mount target must be normalized and must not contain empty path segments or '.'"
211+
);
212+
assert_eq!(
213+
validate_container_mount_target("/sandbox/work/../../tmp").unwrap_err(),
214+
"mount target must not contain '..'"
215+
);
216+
validate_container_mount_target("/sandbox/work/").unwrap();
217+
}
188218
}

‎crates/openshell-driver-docker/src/tests.rs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,39 @@ fn driver_config_allows_explicit_writable_volume_mounts() {
755755
assert_eq!(mounts[0].read_only, Some(false));
756756
}
757757

758+
#[test]
759+
fn driver_config_rejects_duplicate_mount_targets() {
760+
let mut sandbox = test_sandbox();
761+
sandbox
762+
.spec
763+
.as_mut()
764+
.unwrap()
765+
.template
766+
.as_mut()
767+
.unwrap()
768+
.driver_config = Some(json_struct(serde_json::json!({
769+
"mounts": [
770+
{
771+
"type": "volume",
772+
"source": "work-nfs",
773+
"target": "/sandbox/work"
774+
},
775+
{
776+
"type": "tmpfs",
777+
"target": "/sandbox/work"
778+
}
779+
]
780+
})));
781+
782+
let err = build_container_create_body(&sandbox, &runtime_config()).unwrap_err();
783+
784+
assert_eq!(err.code(), tonic::Code::FailedPrecondition);
785+
assert!(
786+
err.message()
787+
.contains("duplicate docker driver_config mount target")
788+
);
789+
}
790+
758791
#[test]
759792
fn driver_config_rejects_bind_mounts_unless_enabled() {
760793
let mut sandbox = test_sandbox();

‎crates/openshell-driver-kubernetes/README.md‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ nested schema and currently accepts:
117117
- `pod.priority_class_name`
118118
- `containers.agent.resources.requests`
119119
- `containers.agent.resources.limits`
120+
- `containers.agent.volume_mounts[].name`
121+
- `containers.agent.volume_mounts[].mount_path`
122+
- `containers.agent.volume_mounts[].sub_path`
123+
- `containers.agent.volume_mounts[].read_only`
124+
- `volumes[].name`
125+
- `volumes[].persistent_volume_claim.claim_name`
126+
- `volumes[].persistent_volume_claim.read_only`
120127

121128
Nested keys inside the `kubernetes` block use snake_case. The top-level
122129
`driver_config` envelope is keyed by driver names, so `kubernetes` is not part
@@ -139,3 +146,50 @@ driver's configured `default_runtime_class_name`; the typed public
139146
public `--gpu` flag for the default GPU request, pass a count to `--gpu` for
140147
counted GPU requests, and use `driver_config` only for additional driver-owned
141148
resource details.
149+
150+
Use PVC volumes to mount existing Kubernetes PersistentVolumeClaims into the
151+
agent container. PVC volumes and mounts default to read-only unless
152+
`read_only: false` is set explicitly. Read-write access requires
153+
`read_only: false` on both the PVC volume and each writable mount. The driver
154+
rejects duplicate volume names, invalid DNS-1123 volume labels or PVC claim
155+
subdomain names, mounts that reference unknown volumes, non-normalized or
156+
protected mount paths, and absolute or parent-traversing `sub_path` values.
157+
158+
Any explicit driver-config mount under `/sandbox` disables the driver's
159+
default `/sandbox` workspace PVC injection for that sandbox. Only the explicit
160+
mount paths persist through the external PVC; other `/sandbox` paths come from
161+
the current sandbox image.
162+
163+
```shell
164+
openshell sandbox create \
165+
--driver-config-json '{
166+
"kubernetes": {
167+
"volumes": [{
168+
"name": "user-data",
169+
"persistent_volume_claim": {
170+
"claim_name": "pvc-user-data-123",
171+
"read_only": false
172+
}
173+
}],
174+
"containers": {
175+
"agent": {
176+
"volume_mounts": [
177+
{
178+
"name": "user-data",
179+
"mount_path": "/sandbox/.openshell/workspace",
180+
"sub_path": "workspace",
181+
"read_only": false
182+
},
183+
{
184+
"name": "user-data",
185+
"mount_path": "/sandbox/.openshell/memory",
186+
"sub_path": "memory",
187+
"read_only": false
188+
}
189+
]
190+
}
191+
}
192+
}
193+
}' \
194+
-- claude
195+
```

0 commit comments

Comments
 (0)