Skip to content

Commit 95f9be8

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

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
@@ -612,10 +612,11 @@ impl VmDriver {
612612
)));
613613
}
614614

615-
let spec = sandbox.spec.as_ref();
616-
let is_gpu = spec.is_some_and(|s| s.gpu);
617-
let gpu_device = spec.map_or("", |s| s.gpu_device.as_str());
618-
let gpu_bdf = if is_gpu {
615+
let gpu_device = sandbox
616+
.spec
617+
.as_ref()
618+
.and_then(|spec| requested_gpu_device(spec.gpu, &spec.gpu_device));
619+
let gpu_bdf = if let Some(gpu_device) = gpu_device {
619620
Some(self.assign_gpu_to_record(&sandbox.id, gpu_device).await?)
620621
} else {
621622
None
@@ -2614,6 +2615,10 @@ fn validate_sandbox_id(sandbox_id: &str) -> Result<(), Status> {
26142615
Ok(())
26152616
}
26162617

2618+
fn requested_gpu_device(gpu: bool, gpu_device: &str) -> Option<&str> {
2619+
gpu.then_some(gpu_device)
2620+
}
2621+
26172622
#[allow(clippy::result_large_err)]
26182623
fn validate_gpu_request(gpu: bool, gpu_device: &str, gpu_enabled: bool) -> Result<(), Status> {
26192624
if gpu && !gpu_enabled {
@@ -4509,6 +4514,24 @@ mod tests {
45094514
assert!(err.message().contains("gpu_device requires gpu=true"));
45104515
}
45114516

4517+
#[test]
4518+
fn requested_gpu_device_returns_none_without_gpu_request() {
4519+
assert_eq!(requested_gpu_device(false, ""), None);
4520+
}
4521+
4522+
#[test]
4523+
fn requested_gpu_device_defaults_empty_request_to_inventory_choice() {
4524+
assert_eq!(requested_gpu_device(true, ""), Some(""));
4525+
}
4526+
4527+
#[test]
4528+
fn requested_gpu_device_returns_explicit_device_id() {
4529+
assert_eq!(
4530+
requested_gpu_device(true, "0000:2d:00.0"),
4531+
Some("0000:2d:00.0")
4532+
);
4533+
}
4534+
45124535
#[test]
45134536
fn validate_vm_sandbox_rejects_platform_config() {
45144537
let sandbox = Sandbox {

0 commit comments

Comments
 (0)