Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions crates/openshell-driver-kubernetes/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<BTreeMap<String, Quantity>>| {
resources
.as_ref()
.and_then(|resources| resources.get(GPU_RESOURCE_NAME))
.is_some_and(|quantity| quantity.0.trim().parse::<u64>().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<String, KubernetesDriverError> {
use std::fmt::Write as _;

Expand Down Expand Up @@ -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()))?;
Expand Down Expand Up @@ -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-<millis>` by that many milliseconds.
async fn serve_delayed_pods() -> std::net::SocketAddr {
Expand Down
31 changes: 31 additions & 0 deletions crates/openshell-driver-kubernetes/src/isolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -408,6 +409,7 @@ pub struct KubernetesSandboxRuntimeBoundarySpec {
pub host_gateway_ip: Option<IpAddr>,
pub workload_identity: ResolvedWorkloadIdentity,
pub child_env: HashMap<String, String>,
pub gpu_requested: bool,
}

/// Protected workload-pod config and matching sandbox-runtime descriptor.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down
Loading