Skip to content

Commit 148e5ad

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

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
@@ -615,10 +615,11 @@ impl VmDriver {
615615
)));
616616
}
617617

618-
let spec = sandbox.spec.as_ref();
619-
let is_gpu = spec.is_some_and(|s| s.gpu);
620-
let gpu_device = spec.map_or("", |s| s.gpu_device.as_str());
621-
let gpu_bdf = if is_gpu {
618+
let gpu_device = sandbox
619+
.spec
620+
.as_ref()
621+
.and_then(|spec| requested_gpu_device(spec.gpu, &spec.gpu_device));
622+
let gpu_bdf = if let Some(gpu_device) = gpu_device {
622623
Some(self.assign_gpu_to_record(&sandbox.id, gpu_device).await?)
623624
} else {
624625
None
@@ -2620,6 +2621,10 @@ fn validate_sandbox_id(sandbox_id: &str) -> Result<(), Status> {
26202621
Ok(())
26212622
}
26222623

2624+
fn requested_gpu_device(gpu: bool, gpu_device: &str) -> Option<&str> {
2625+
gpu.then_some(gpu_device)
2626+
}
2627+
26232628
#[allow(clippy::result_large_err)]
26242629
fn validate_gpu_request(gpu: bool, gpu_device: &str, gpu_enabled: bool) -> Result<(), Status> {
26252630
if gpu && !gpu_enabled {
@@ -4538,6 +4543,24 @@ mod tests {
45384543
assert!(err.message().contains("gpu_device requires gpu=true"));
45394544
}
45404545

4546+
#[test]
4547+
fn requested_gpu_device_returns_none_without_gpu_request() {
4548+
assert_eq!(requested_gpu_device(false, ""), None);
4549+
}
4550+
4551+
#[test]
4552+
fn requested_gpu_device_defaults_empty_request_to_inventory_choice() {
4553+
assert_eq!(requested_gpu_device(true, ""), Some(""));
4554+
}
4555+
4556+
#[test]
4557+
fn requested_gpu_device_returns_explicit_device_id() {
4558+
assert_eq!(
4559+
requested_gpu_device(true, "0000:2d:00.0"),
4560+
Some("0000:2d:00.0")
4561+
);
4562+
}
4563+
45414564
#[test]
45424565
fn validate_vm_sandbox_rejects_platform_config() {
45434566
let sandbox = Sandbox {

0 commit comments

Comments
 (0)