Skip to content

Commit 5f5b7bb

Browse files
committed
refactor(vm): derive GPU device request once
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent e0c5c2c commit 5f5b7bb

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,10 @@ impl VmDriver {
354354
return Err(Status::already_exists("sandbox already exists"));
355355
}
356356

357-
let spec = sandbox.spec.as_ref();
358-
let is_gpu = spec.is_some_and(|s| s.gpu);
359-
let gpu_device = spec.map_or("", |s| s.gpu_device.as_str());
357+
let gpu_device = sandbox
358+
.spec
359+
.as_ref()
360+
.and_then(|spec| requested_gpu_device(spec.gpu, &spec.gpu_device));
360361

361362
let state_dir = sandbox_state_dir(&self.config.state_dir, &sandbox.id)?;
362363
let rootfs = state_dir.join("rootfs");
@@ -433,7 +434,7 @@ impl VmDriver {
433434
)));
434435
}
435436

436-
let gpu_bdf = if is_gpu {
437+
let gpu_bdf = if let Some(gpu_device) = gpu_device {
437438
let inventory = self
438439
.gpu_inventory
439440
.as_ref()
@@ -1498,6 +1499,10 @@ fn validate_sandbox_id(sandbox_id: &str) -> Result<(), Status> {
14981499
Ok(())
14991500
}
15001501

1502+
fn requested_gpu_device(gpu: bool, gpu_device: &str) -> Option<&str> {
1503+
gpu.then_some(gpu_device)
1504+
}
1505+
15011506
#[allow(clippy::result_large_err)]
15021507
fn validate_gpu_request(gpu: bool, gpu_device: &str, gpu_enabled: bool) -> Result<(), Status> {
15031508
if gpu && !gpu_enabled {
@@ -2605,6 +2610,24 @@ mod tests {
26052610
assert!(err.message().contains("gpu_device requires gpu=true"));
26062611
}
26072612

2613+
#[test]
2614+
fn requested_gpu_device_returns_none_without_gpu_request() {
2615+
assert_eq!(requested_gpu_device(false, ""), None);
2616+
}
2617+
2618+
#[test]
2619+
fn requested_gpu_device_defaults_empty_request_to_inventory_choice() {
2620+
assert_eq!(requested_gpu_device(true, ""), Some(""));
2621+
}
2622+
2623+
#[test]
2624+
fn requested_gpu_device_returns_explicit_device_id() {
2625+
assert_eq!(
2626+
requested_gpu_device(true, "0000:2d:00.0"),
2627+
Some("0000:2d:00.0")
2628+
);
2629+
}
2630+
26082631
#[test]
26092632
fn validate_vm_sandbox_rejects_platform_config() {
26102633
let sandbox = Sandbox {

0 commit comments

Comments
 (0)