diff --git a/crates/openshell-driver-kubernetes/src/driver.rs b/crates/openshell-driver-kubernetes/src/driver.rs index f7eaf4bd6b..c2e1b52fe1 100644 --- a/crates/openshell-driver-kubernetes/src/driver.rs +++ b/crates/openshell-driver-kubernetes/src/driver.rs @@ -38,6 +38,7 @@ use k8s_openapi::api::networking::v1::{ NetworkPolicy, NetworkPolicyIngressRule, NetworkPolicyPeer, NetworkPolicyPort, NetworkPolicySpec, }; +use k8s_openapi::apimachinery::pkg::api::resource::Quantity; use k8s_openapi::apimachinery::pkg::apis::meta::v1::{LabelSelector, OwnerReference}; use k8s_openapi::apimachinery::pkg::util::intstr::IntOrString; use kube::api::{ @@ -612,6 +613,21 @@ fn admission_error(error: tonic::Status) -> KubernetesDriverError { } } const SANDBOX_RUNTIME_RECONCILE_INTERVAL: Duration = Duration::from_secs(30); +fn pod_requests_gpu(pod: &Pod) -> bool { + let nonzero = |resources: &Option>| { + resources + .as_ref() + .and_then(|resources| resources.get(GPU_RESOURCE_NAME)) + .is_some_and(|quantity| quantity.0.trim().parse::().is_ok_and(|n| n > 0)) + }; + pod.spec + .iter() + .flat_map(|spec| &spec.containers) + .filter(|container| container.name == "agent") + .filter_map(|container| container.resources.as_ref()) + .any(|resources| nonzero(&resources.limits) || nonzero(&resources.requests)) +} + fn random_sandbox_runtime_token() -> Result { use std::fmt::Write as _; @@ -3944,6 +3960,7 @@ impl KubernetesComputeDriver { host_gateway_ip: self.config.host_gateway_ip.parse().ok(), workload_identity, child_env, + gpu_requested: pod_requests_gpu(&workload_pod), } .provision() .map_err(|error| KubernetesDriverError::Message(error.to_string()))?; @@ -9135,6 +9152,36 @@ fn spawn_namespace_file_watcher( mod tests { use super::*; + #[test] + fn pod_requests_gpu_reads_the_agent_container_only() { + let pod = |containers: serde_json::Value| -> Pod { + serde_json::from_value(serde_json::json!({ + "apiVersion": "v1", + "kind": "Pod", + "metadata": {"name": "pod"}, + "spec": {"containers": containers}, + })) + .unwrap() + }; + let gpu = |name: &str, field: &str, quantity: &str| serde_json::json!({"name": name, "resources": {field: {GPU_RESOURCE_NAME: quantity}}}); + + assert!(pod_requests_gpu(&pod(serde_json::json!([gpu( + "agent", "limits", "1" + )])))); + assert!(pod_requests_gpu(&pod(serde_json::json!([gpu( + "agent", "requests", "2" + )])))); + assert!(!pod_requests_gpu(&pod(serde_json::json!([gpu( + "agent", "limits", "0" + )])))); + assert!(!pod_requests_gpu(&pod(serde_json::json!([gpu( + "sidecar", "limits", "1" + )])))); + assert!(!pod_requests_gpu(&pod( + serde_json::json!([{"name": "agent"}]) + ))); + } + /// Serve Pod GETs over one reusable HTTP/1 connection per client, /// delaying a request for Pod `slow-` by that many milliseconds. async fn serve_delayed_pods() -> std::net::SocketAddr { diff --git a/crates/openshell-driver-kubernetes/src/isolation.rs b/crates/openshell-driver-kubernetes/src/isolation.rs index d943bac705..f806db081e 100644 --- a/crates/openshell-driver-kubernetes/src/isolation.rs +++ b/crates/openshell-driver-kubernetes/src/isolation.rs @@ -23,6 +23,7 @@ use kube::core::ObjectMeta; use openshell_isolation_interface::contract::{ BackendError, ProvisioningEvidence, ResolvedWorkloadIdentity, }; +use openshell_sandbox_backend::GPU_RESOURCE_CLAIM; use openshell_sandbox_backend::boundary_protocol::{ BoundaryConfig, BoundaryListener, GatewayVerificationKey, SandboxRuntimeDescriptor, SandboxTlsClientConfig, SandboxTlsServerConfig, SandboxTransport, @@ -408,6 +409,7 @@ pub struct KubernetesSandboxRuntimeBoundarySpec { pub host_gateway_ip: Option, pub workload_identity: ResolvedWorkloadIdentity, pub child_env: HashMap, + pub gpu_requested: bool, } /// Protected workload-pod config and matching sandbox-runtime descriptor. @@ -448,6 +450,9 @@ impl KubernetesSandboxRuntimeBoundarySpec { self.egress_policy_resource_version, ), ]); + if self.gpu_requested { + resource_claims.insert(GPU_RESOURCE_CLAIM.to_string(), "true".to_string()); + } let mut supervisor_namespace = None; let mut shared_port = None; let (listener, transport) = match self.control_path { @@ -676,7 +681,33 @@ mod tests { ) .unwrap(), child_env: HashMap::new(), + gpu_requested: false, + } + } + + #[test] + fn gpu_claim_is_set_only_when_requested() { + let cpu = spec().provision().unwrap(); + assert!( + !cpu.boundary_config + .resource_claims + .contains_key(GPU_RESOURCE_CLAIM) + ); + + let gpu = KubernetesSandboxRuntimeBoundarySpec { + gpu_requested: true, + ..spec() } + .provision() + .unwrap(); + assert_eq!( + gpu.boundary_config.resource_claims[GPU_RESOURCE_CLAIM], + "true" + ); + assert_eq!( + gpu.boundary_config.resource_claims, + gpu.runtime_descriptor.resource_claims + ); } #[test]