Skip to content

Commit e0c5c2c

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

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

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

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

202202
pub async fn validate_sandbox_create(&self, sandbox: &Sandbox) -> Result<(), tonic::Status> {
203203
let gpu_requested = sandbox.spec.as_ref().is_some_and(|spec| spec.gpu);
204+
self.validate_gpu_request(gpu_requested).await
205+
}
206+
207+
async fn validate_gpu_request(&self, gpu_requested: bool) -> Result<(), tonic::Status> {
204208
if gpu_requested
205209
&& !self.has_gpu_capacity().await.map_err(|err| {
206210
tonic::Status::internal(format!("check GPU node capacity failed: {err}"))

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,15 +1456,7 @@ fn validate_vm_sandbox(sandbox: &Sandbox, gpu_enabled: bool) -> Result<(), Statu
14561456
.as_ref()
14571457
.ok_or_else(|| Status::invalid_argument("sandbox spec is required"))?;
14581458

1459-
if spec.gpu && !gpu_enabled {
1460-
return Err(Status::failed_precondition(
1461-
"GPU support is not enabled on this driver; start with --gpu",
1462-
));
1463-
}
1464-
1465-
if !spec.gpu && !spec.gpu_device.is_empty() {
1466-
return Err(Status::invalid_argument("gpu_device requires gpu=true"));
1467-
}
1459+
validate_gpu_request(spec.gpu, &spec.gpu_device, gpu_enabled)?;
14681460

14691461
if let Some(template) = spec.template.as_ref() {
14701462
if !template.agent_socket_path.is_empty() {
@@ -1481,7 +1473,6 @@ fn validate_vm_sandbox(sandbox: &Sandbox, gpu_enabled: bool) -> Result<(), Statu
14811473
Ok(())
14821474
}
14831475

1484-
#[allow(clippy::result_large_err)]
14851476
fn validate_sandbox_id(sandbox_id: &str) -> Result<(), Status> {
14861477
if sandbox_id.is_empty() {
14871478
return Err(Status::invalid_argument("sandbox id is required"));
@@ -1507,6 +1498,20 @@ fn validate_sandbox_id(sandbox_id: &str) -> Result<(), Status> {
15071498
Ok(())
15081499
}
15091500

1501+
#[allow(clippy::result_large_err)]
1502+
fn validate_gpu_request(gpu: bool, gpu_device: &str, gpu_enabled: bool) -> Result<(), Status> {
1503+
if gpu && !gpu_enabled {
1504+
return Err(Status::failed_precondition(
1505+
"GPU support is not enabled on this driver; start with --gpu",
1506+
));
1507+
}
1508+
1509+
if !gpu && !gpu_device.is_empty() {
1510+
return Err(Status::invalid_argument("gpu_device requires gpu=true"));
1511+
}
1512+
Ok(())
1513+
}
1514+
15101515
#[allow(clippy::result_large_err)]
15111516
fn parse_registry_reference(image_ref: &str) -> Result<Reference, Status> {
15121517
Reference::try_from(image_ref).map_err(|err| {

0 commit comments

Comments
 (0)