Skip to content

Commit 83e2d1d

Browse files
committed
fix(kubernetes): address PVC review follow-ups
1 parent a9cf61c commit 83e2d1d

2 files changed

Lines changed: 55 additions & 56 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub fn normalize_mount_target(target: &str) -> String {
115115
target.trim_end_matches('/').to_string()
116116
}
117117

118-
fn path_is_or_under(path: &Path, parent: &Path) -> bool {
118+
pub fn path_is_or_under(path: &Path, parent: &Path) -> bool {
119119
path == parent || path.starts_with(parent)
120120
}
121121

@@ -189,6 +189,10 @@ mod tests {
189189
validate_container_mount_target("/sandbox/work/./tmp").unwrap_err(),
190190
"mount target must be normalized and must not contain empty path segments or '.'"
191191
);
192+
assert_eq!(
193+
validate_container_mount_target("/sandbox/work/../../tmp").unwrap_err(),
194+
"mount target must not contain '..'"
195+
);
192196
validate_container_mount_target("/sandbox/work/").unwrap();
193197
}
194198
}

‎crates/openshell-driver-kubernetes/src/driver.rs‎

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use crate::config::{
88
KubernetesComputeConfig, SupervisorSideloadMethod,
99
};
1010
use futures::{Stream, StreamExt, TryStreamExt};
11-
use k8s_openapi::api::core::v1::{Event as KubeEventObj, Node};
11+
use k8s_openapi::api::core::v1::{
12+
Event as KubeEventObj, Node, PersistentVolumeClaimVolumeSource, Volume, VolumeMount,
13+
};
1214
use kube::api::{Api, ApiResource, DeleteParams, ListParams, PostParams};
1315
use kube::core::gvk::GroupVersionKind;
1416
use kube::core::{DynamicObject, ObjectMeta};
@@ -134,11 +136,12 @@ impl KubernetesSandboxDriverConfig {
134136
}
135137

136138
fn has_explicit_sandbox_data_mount(&self) -> bool {
137-
self.containers
138-
.agent
139-
.volume_mounts
140-
.iter()
141-
.any(|mount| path_is_or_under(&mount.mount_path, WORKSPACE_MOUNT_PATH))
139+
self.containers.agent.volume_mounts.iter().any(|mount| {
140+
driver_mounts::path_is_or_under(
141+
Path::new(&mount.mount_path),
142+
Path::new(WORKSPACE_MOUNT_PATH),
143+
)
144+
})
142145
}
143146
}
144147

@@ -214,6 +217,31 @@ impl Default for KubernetesDriverVolumeMountConfig {
214217
}
215218
}
216219

220+
impl From<&KubernetesDriverVolumeConfig> for Volume {
221+
fn from(volume: &KubernetesDriverVolumeConfig) -> Self {
222+
Self {
223+
name: volume.name.clone(),
224+
persistent_volume_claim: Some(PersistentVolumeClaimVolumeSource {
225+
claim_name: volume.persistent_volume_claim.claim_name.clone(),
226+
read_only: Some(volume.persistent_volume_claim.read_only),
227+
}),
228+
..Default::default()
229+
}
230+
}
231+
}
232+
233+
impl From<&KubernetesDriverVolumeMountConfig> for VolumeMount {
234+
fn from(mount: &KubernetesDriverVolumeMountConfig) -> Self {
235+
Self {
236+
name: mount.name.clone(),
237+
mount_path: mount.mount_path.clone(),
238+
read_only: Some(mount.read_only),
239+
sub_path: mount.sub_path.clone(),
240+
..Default::default()
241+
}
242+
}
243+
}
244+
217245
const CLIENT_TLS_VOLUME_NAME: &str = "openshell-client-tls";
218246
const SERVICE_ACCOUNT_TOKEN_VOLUME_NAME: &str = "openshell-sa-token";
219247
const SERVICE_ACCOUNT_TOKEN_MOUNT_PATH: &str = "/var/run/secrets/openshell";
@@ -295,34 +323,23 @@ fn validate_kubernetes_driver_volume_mounts(
295323
Ok(())
296324
}
297325

298-
fn validate_kubernetes_name_text(value: &str, field: &str) -> Result<(), String> {
299-
if value != value.trim() {
300-
return Err(format!(
301-
"{field} must not contain leading or trailing whitespace"
302-
));
326+
// TODO: replace with an openshell_core Kubernetes-name helper once available.
327+
fn is_dns_label(label: &str) -> bool {
328+
if label.is_empty() || label.len() > 63 || label.starts_with('-') || label.ends_with('-') {
329+
return false;
303330
}
304-
driver_mounts::validate_mount_source(value, field).map(|_| ())
331+
label
332+
.bytes()
333+
.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
305334
}
306335

307-
fn is_kubernetes_dns1123_label(value: &str) -> bool {
308-
!value.is_empty()
309-
&& value.len() <= 63
310-
&& value
311-
.bytes()
312-
.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
313-
&& value
314-
.as_bytes()
315-
.first()
316-
.is_some_and(u8::is_ascii_alphanumeric)
317-
&& value
318-
.as_bytes()
319-
.last()
320-
.is_some_and(u8::is_ascii_alphanumeric)
336+
// TODO: replace with an openshell_core Kubernetes-name helper once available.
337+
fn is_dns_subdomain(value: &str) -> bool {
338+
value.len() <= 253 && value.split('.').all(is_dns_label)
321339
}
322340

323341
fn validate_kubernetes_dns1123_label(value: &str, field: &str) -> Result<(), String> {
324-
validate_kubernetes_name_text(value, field)?;
325-
if !is_kubernetes_dns1123_label(value) {
342+
if !is_dns_label(value) {
326343
return Err(format!(
327344
"{field} must be a DNS-1123 label: use lowercase alphanumeric characters or '-', start and end with an alphanumeric character, and use at most 63 characters"
328345
));
@@ -331,10 +348,7 @@ fn validate_kubernetes_dns1123_label(value: &str, field: &str) -> Result<(), Str
331348
}
332349

333350
fn validate_kubernetes_dns1123_subdomain(value: &str, field: &str) -> Result<(), String> {
334-
validate_kubernetes_name_text(value, field)?;
335-
let is_dns1123_subdomain =
336-
value.len() <= 253 && value.split('.').all(is_kubernetes_dns1123_label);
337-
if !is_dns1123_subdomain {
351+
if !is_dns_subdomain(value) {
338352
return Err(format!(
339353
"{field} must be a DNS-1123 subdomain: use lowercase alphanumeric characters, '-' or '.', start and end with an alphanumeric character, and use at most 253 characters"
340354
));
@@ -343,13 +357,8 @@ fn validate_kubernetes_dns1123_subdomain(value: &str, field: &str) -> Result<(),
343357
}
344358

345359
fn mount_path_conflicts_with_protected_path(mount_path: &str, protected_path: &str) -> bool {
346-
path_is_or_under(mount_path, protected_path) || path_is_or_under(protected_path, mount_path)
347-
}
348-
349-
fn path_is_or_under(path: &str, parent: &str) -> bool {
350-
let path = Path::new(path);
351-
let parent = Path::new(parent);
352-
path == parent || path.starts_with(parent)
360+
driver_mounts::path_is_or_under(Path::new(mount_path), Path::new(protected_path))
361+
|| driver_mounts::path_is_or_under(Path::new(protected_path), Path::new(mount_path))
353362
}
354363

355364
fn validate_kubernetes_protected_path_conflicts(
@@ -370,27 +379,13 @@ fn validate_kubernetes_protected_path_conflicts(
370379
}
371380

372381
fn kubernetes_driver_volume_to_k8s(volume: &KubernetesDriverVolumeConfig) -> serde_json::Value {
373-
serde_json::json!({
374-
"name": volume.name.as_str(),
375-
"persistentVolumeClaim": {
376-
"claimName": volume.persistent_volume_claim.claim_name.as_str(),
377-
"readOnly": volume.persistent_volume_claim.read_only,
378-
}
379-
})
382+
serde_json::to_value(Volume::from(volume)).expect("Volume serializes to JSON")
380383
}
381384

382385
fn kubernetes_driver_volume_mount_to_k8s(
383386
mount: &KubernetesDriverVolumeMountConfig,
384387
) -> serde_json::Value {
385-
let mut value = serde_json::json!({
386-
"name": mount.name.as_str(),
387-
"mountPath": mount.mount_path.as_str(),
388-
"readOnly": mount.read_only,
389-
});
390-
if let Some(sub_path) = mount.sub_path.as_ref() {
391-
value["subPath"] = serde_json::json!(sub_path);
392-
}
393-
value
388+
serde_json::to_value(VolumeMount::from(mount)).expect("VolumeMount serializes to JSON")
394389
}
395390

396391
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)