Skip to content

Commit 9a0324c

Browse files
committed
refactor(gpu): centralize driver request validation
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 09bd8a9 commit 9a0324c

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ impl KubernetesComputeDriver {
205205

206206
pub async fn validate_sandbox_create(&self, sandbox: &Sandbox) -> Result<(), tonic::Status> {
207207
let gpu_requested = sandbox.spec.as_ref().is_some_and(|spec| spec.gpu);
208+
self.validate_gpu_request(gpu_requested).await
209+
}
210+
211+
async fn validate_gpu_request(&self, gpu_requested: bool) -> Result<(), tonic::Status> {
208212
if gpu_requested
209213
&& !self.has_gpu_capacity().await.map_err(|err| {
210214
tonic::Status::internal(format!("check GPU node capacity failed: {err}"))

crates/openshell-driver-vm/src/driver.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
26012592
fn 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)]
26272632
fn 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

Comments
 (0)