@@ -2572,15 +2572,7 @@ fn validate_vm_sandbox(sandbox: &Sandbox, gpu_enabled: bool) -> Result<(), Statu
25722572 . as_ref ( )
25732573 . ok_or_else ( || Status :: invalid_argument ( "sandbox spec is required" ) ) ?;
25742574
2575- if spec. gpu && !gpu_enabled {
2576- return Err ( Status :: failed_precondition (
2577- "GPU support is not enabled on this driver; start with --gpu" ,
2578- ) ) ;
2579- }
2580-
2581- if !spec. gpu && !spec. gpu_device . is_empty ( ) {
2582- return Err ( Status :: invalid_argument ( "gpu_device requires gpu=true" ) ) ;
2583- }
2575+ validate_gpu_request ( spec. gpu , & spec. gpu_device , gpu_enabled) ?;
25842576
25852577 if let Some ( template) = spec. template . as_ref ( ) {
25862578 if !template. agent_socket_path . is_empty ( ) {
@@ -2597,7 +2589,6 @@ fn validate_vm_sandbox(sandbox: &Sandbox, gpu_enabled: bool) -> Result<(), Statu
25972589 Ok ( ( ) )
25982590}
25992591
2600- #[ allow( clippy:: result_large_err) ]
26012592fn validate_sandbox_id ( sandbox_id : & str ) -> Result < ( ) , Status > {
26022593 if sandbox_id. is_empty ( ) {
26032594 return Err ( Status :: invalid_argument ( "sandbox id is required" ) ) ;
@@ -2623,6 +2614,20 @@ fn validate_sandbox_id(sandbox_id: &str) -> Result<(), Status> {
26232614 Ok ( ( ) )
26242615}
26252616
2617+ #[ allow( clippy:: result_large_err) ]
2618+ fn validate_gpu_request ( gpu : bool , gpu_device : & str , gpu_enabled : bool ) -> Result < ( ) , Status > {
2619+ if gpu && !gpu_enabled {
2620+ return Err ( Status :: failed_precondition (
2621+ "GPU support is not enabled on this VM driver; start the VM compute driver with GPU support enabled" ,
2622+ ) ) ;
2623+ }
2624+
2625+ if !gpu && !gpu_device. is_empty ( ) {
2626+ return Err ( Status :: invalid_argument ( "gpu_device requires gpu=true" ) ) ;
2627+ }
2628+ Ok ( ( ) )
2629+ }
2630+
26262631#[ allow( clippy:: result_large_err) ]
26272632fn parse_registry_reference ( image_ref : & str ) -> Result < Reference , Status > {
26282633 Reference :: try_from ( image_ref) . map_err ( |err| {
@@ -4468,7 +4473,10 @@ mod tests {
44684473 let err = validate_vm_sandbox ( & sandbox, false )
44694474 . expect_err ( "gpu should be rejected when not enabled" ) ;
44704475 assert_eq ! ( err. code( ) , Code :: FailedPrecondition ) ;
4471- assert ! ( err. message( ) . contains( "GPU support is not enabled" ) ) ;
4476+ assert ! (
4477+ err. message( )
4478+ . contains( "GPU support is not enabled on this VM driver" )
4479+ ) ;
44724480 }
44734481
44744482 #[ test]
0 commit comments