@@ -8,7 +8,9 @@ use crate::config::{
88 KubernetesComputeConfig , SupervisorSideloadMethod ,
99} ;
1010use 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+ } ;
1214use kube:: api:: { Api , ApiResource , DeleteParams , ListParams , PostParams } ;
1315use kube:: core:: gvk:: GroupVersionKind ;
1416use 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+
217245const CLIENT_TLS_VOLUME_NAME : & str = "openshell-client-tls" ;
218246const SERVICE_ACCOUNT_TOKEN_VOLUME_NAME : & str = "openshell-sa-token" ;
219247const 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
323341fn 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
333350fn 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
345359fn 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
355364fn validate_kubernetes_protected_path_conflicts (
@@ -370,27 +379,13 @@ fn validate_kubernetes_protected_path_conflicts(
370379}
371380
372381fn 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
382385fn 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