Skip to content

Commit 919f2be

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

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

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

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

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

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

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

2580-
if spec.gpu && !gpu_enabled {
2581-
return Err(Status::failed_precondition(
2582-
"GPU support is not enabled on this driver; start with --gpu",
2583-
));
2584-
}
2585-
2586-
if !spec.gpu && !spec.gpu_device.is_empty() {
2587-
return Err(Status::invalid_argument("gpu_device requires gpu=true"));
2588-
}
2580+
validate_gpu_request(spec.gpu, &spec.gpu_device, gpu_enabled)?;
25892581

25902582
if let Some(template) = spec.template.as_ref() {
25912583
if !template.agent_socket_path.is_empty() {
@@ -2628,6 +2620,20 @@ fn validate_sandbox_id(sandbox_id: &str) -> Result<(), Status> {
26282620
Ok(())
26292621
}
26302622

2623+
#[allow(clippy::result_large_err)]
2624+
fn validate_gpu_request(gpu: bool, gpu_device: &str, gpu_enabled: bool) -> Result<(), Status> {
2625+
if gpu && !gpu_enabled {
2626+
return Err(Status::failed_precondition(
2627+
"GPU support is not enabled on this driver; start with --gpu",
2628+
));
2629+
}
2630+
2631+
if !gpu && !gpu_device.is_empty() {
2632+
return Err(Status::invalid_argument("gpu_device requires gpu=true"));
2633+
}
2634+
Ok(())
2635+
}
2636+
26312637
#[allow(clippy::result_large_err)]
26322638
fn parse_registry_reference(image_ref: &str) -> Result<Reference, Status> {
26332639
Reference::try_from(image_ref).map_err(|err| {

0 commit comments

Comments
 (0)