Skip to content

Commit 35d38ee

Browse files
committed
feat(kubernetes): claim pre-warmed pods from agent-sandbox warm pools
Reconcile operator-declared warm pools (one SandboxTemplate + SandboxWarmPool per pool), bind pre-warmed pods via a SandboxClaim instead of cold-creating a Sandbox, and watch claims to surface warm sandboxes. warm_eligible() exhaustively destructures the request spec/template so any newly added field must be classified before it can ride the warm path; custom-policy requests and per-request overrides fall back to cold. The pooled template uses an ephemeral workspace, an unmanaged network policy, and no injected identity. Signed-off-by: Roshni Malani <rmalani@nvidia.com>
1 parent 7e8ce1a commit 35d38ee

5 files changed

Lines changed: 1238 additions & 18 deletions

File tree

‎crates/openshell-driver-kubernetes/README.md‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,53 @@ This is a stopgap persistence model. It preserves user files across pod
3232
rescheduling but duplicates the base workspace and does not automatically apply
3333
image updates to existing PVCs. Future snapshotting should replace it.
3434

35+
## Warm Pools
36+
37+
When `warm_pool.enabled` is set, the driver pre-declares one operator-owned
38+
`SandboxTemplate` + `SandboxWarmPool` per configured pool (extension CRDs
39+
`extensions.agents.x-k8s.io/v1alpha1`) and satisfies a matching `CreateSandbox`
40+
by creating a `SandboxClaim` that binds a pre-warmed pod (~0.1s) instead of
41+
cold-creating a `Sandbox`. Only the trusted `default_image` with no per-request
42+
template or env overrides is pooled; everything else takes the cold path.
43+
44+
The pooled `SandboxTemplate` bakes the shared pod blueprint (image, mTLS mount,
45+
projected SA token, supervisor sideload, capabilities, runtimeClass, optional
46+
read-only shared data volume) but carries **no per-sandbox identity**. The
47+
template sets `networkPolicyManagement: Unmanaged` — OpenShell enforces egress
48+
itself (supervisor proxy + Landlock), and the controller's default `Managed`
49+
mode would otherwise impose a rule-less, default-deny `NetworkPolicy` that blocks
50+
the pod's own egress to the gateway. The writable `/sandbox` workspace is an
51+
ephemeral `emptyDir` seeded from the image: single-use and fail-safe (the kubelet
52+
reclaims it with the pod, so there is nothing to orphan). Claims set
53+
`shutdownPolicy: Delete` so teardown cascades to the bound `Sandbox`/Pod, and a
54+
claimed pod is never returned to the pool.
55+
56+
### Identity and policy on the warm path
57+
58+
A pooled pod boots **before** its claim assigns an identity, so the supervisor
59+
cannot fetch a per-sandbox policy or assert a sandbox-id at startup. Instead it:
60+
61+
- boots on the image's **baseline policy** (Landlock is applied once at process
62+
start and cannot be loosened later, so the baseline is what a pooled pod
63+
enforces);
64+
- runs with `OPENSHELL_SANDBOX_ID` unset, skipping identity-gated startup;
65+
- establishes its relay session with an empty `hello.sandbox_id`; the gateway
66+
derives the identity from the gateway-minted JWT obtained via
67+
`IssueSandboxToken` (resolved server-side from the claim-injected
68+
`openshell.io/sandbox-id` annotation + the durable claim mapping). The
69+
supervisor retries the bootstrap at a steady cadence until the claim binds.
70+
71+
Because a pooled pod can only enforce the baseline policy, a `CreateSandbox`
72+
request that carries a **custom policy** is never warm-pooled — the gateway sets
73+
`DriverSandboxSpec.disallow_warm_pool`, and such requests take the cold path so
74+
their policy is applied faithfully (no silent downgrade). Claim `env` injection
75+
is likewise disallowed.
76+
77+
The driver surfaces a warm sandbox to the gateway by watching `SandboxClaim`s
78+
(correlated to the gateway sandbox-id via the `openshell.ai/sandbox-id` label it
79+
stamps on each claim) and returns the bound claim's `(name, uid)` so the gateway
80+
can record the durable claim → sandbox-id mapping used by the auth re-anchor.
81+
3582
## Credentials, TLS, and Relay
3683

3784
The driver injects gateway callback configuration, sandbox identity, TLS client

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

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,68 @@ pub struct KubernetesComputeConfig {
211211
deserialize_with = "deserialize_provider_spiffe_workload_api_socket_path"
212212
)]
213213
pub provider_spiffe_workload_api_socket_path: String,
214+
/// Warm-pool configuration. When enabled, the gateway pre-declares one or
215+
/// more operator-owned `SandboxWarmPool`s and satisfies matching
216+
/// `CreateSandbox` requests by binding a pre-warmed pod via a `SandboxClaim`
217+
/// instead of cold-creating a `Sandbox`. Off by default.
218+
#[serde(default)]
219+
pub warm_pool: KubernetesWarmPoolConfig,
220+
}
221+
222+
/// Warm-pool configuration for the Kubernetes driver.
223+
///
224+
/// Only **operator-declared** pools using the gateway's trusted default image
225+
/// are warm-pooled. Requests for a user-supplied image or that carry a custom
226+
/// template fall back to the cold path — per-tenant pool isolation for
227+
/// untrusted images is out of scope (see RFC 0005 / issue #1879).
228+
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
229+
#[serde(default, deny_unknown_fields)]
230+
pub struct KubernetesWarmPoolConfig {
231+
/// Master switch. When false the driver never creates extension CRDs and
232+
/// every request takes the cold path.
233+
pub enabled: bool,
234+
/// Declared pools. Each maps to exactly one (image, runtimeClass, gpu)
235+
/// shape; the image is always the driver `default_image`.
236+
pub pools: Vec<WarmPoolSpec>,
237+
}
238+
239+
/// One operator-declared warm pool.
240+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
241+
#[serde(deny_unknown_fields)]
242+
pub struct WarmPoolSpec {
243+
/// Pool name. Also names the backing `SandboxWarmPool` and `SandboxTemplate`
244+
/// (suffixed). Must be a DNS-1123 label.
245+
pub name: String,
246+
/// Number of pods to keep warm.
247+
pub replicas: i32,
248+
/// `runtimeClassName` baked into the pool's `SandboxTemplate`. Empty uses
249+
/// the driver's `default_runtime_class_name`.
250+
#[serde(default)]
251+
pub runtime_class_name: String,
252+
/// Whether pooled pods request a GPU. GPU pools hold accelerators idle, so
253+
/// they are opt-in per pool.
254+
#[serde(default)]
255+
pub gpu: bool,
256+
/// Optional shared, read-only data volume mounted into every pooled pod
257+
/// (datasets / models / caches). Safe to share precisely because it is
258+
/// read-only and holds no per-agent state.
259+
#[serde(default, skip_serializing_if = "Option::is_none")]
260+
pub shared_volume: Option<SharedVolumeSpec>,
261+
}
262+
263+
/// A pre-existing PVC mounted read-only into pooled pods.
264+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
265+
#[serde(deny_unknown_fields)]
266+
pub struct SharedVolumeSpec {
267+
/// Name of an existing `PersistentVolumeClaim` in the sandbox namespace.
268+
/// Must support a read-many access mode (`ReadOnlyMany`/`ReadWriteMany`)
269+
/// when pods land on multiple nodes.
270+
pub claim_name: String,
271+
/// Absolute path the volume is mounted at inside the sandbox.
272+
pub mount_path: String,
273+
/// Optional sub-path within the volume to mount.
274+
#[serde(default, skip_serializing_if = "String::is_empty")]
275+
pub sub_path: String,
214276
}
215277

216278
/// Lower bound enforced by kubelet for projected SA tokens.
@@ -246,6 +308,7 @@ impl Default for KubernetesComputeConfig {
246308
default_runtime_class_name: String::new(),
247309
sa_token_ttl_secs: 3600,
248310
provider_spiffe_workload_api_socket_path: String::new(),
311+
warm_pool: KubernetesWarmPoolConfig::default(),
249312
}
250313
}
251314
}
@@ -459,4 +522,56 @@ mod tests {
459522
let cfg: KubernetesComputeConfig = serde_json::from_value(json).unwrap();
460523
assert_eq!(cfg.image_pull_secrets, ["regcred", "backup-regcred"]);
461524
}
525+
526+
#[test]
527+
fn default_warm_pool_is_disabled() {
528+
let cfg = KubernetesComputeConfig::default();
529+
assert!(!cfg.warm_pool.enabled);
530+
assert!(cfg.warm_pool.pools.is_empty());
531+
}
532+
533+
#[test]
534+
fn serde_override_warm_pool() {
535+
let json = serde_json::json!({
536+
"warm_pool": {
537+
"enabled": true,
538+
"pools": [
539+
{ "name": "default", "replicas": 3 },
540+
{
541+
"name": "gpu",
542+
"replicas": 1,
543+
"gpu": true,
544+
"runtime_class_name": "nvidia",
545+
"shared_volume": {
546+
"claim_name": "models",
547+
"mount_path": "/models",
548+
"sub_path": "llama"
549+
}
550+
}
551+
]
552+
}
553+
});
554+
let cfg: KubernetesComputeConfig = serde_json::from_value(json).unwrap();
555+
assert!(cfg.warm_pool.enabled);
556+
assert_eq!(cfg.warm_pool.pools.len(), 2);
557+
assert_eq!(cfg.warm_pool.pools[0].name, "default");
558+
assert_eq!(cfg.warm_pool.pools[0].replicas, 3);
559+
assert!(!cfg.warm_pool.pools[0].gpu);
560+
assert!(cfg.warm_pool.pools[0].shared_volume.is_none());
561+
let gpu = &cfg.warm_pool.pools[1];
562+
assert!(gpu.gpu);
563+
assert_eq!(gpu.runtime_class_name, "nvidia");
564+
let shared = gpu.shared_volume.as_ref().unwrap();
565+
assert_eq!(shared.claim_name, "models");
566+
assert_eq!(shared.mount_path, "/models");
567+
assert_eq!(shared.sub_path, "llama");
568+
}
569+
570+
#[test]
571+
fn serde_rejects_unknown_warm_pool_field() {
572+
let json = serde_json::json!({
573+
"warm_pool": { "enabled": true, "bogus": 1 }
574+
});
575+
assert!(serde_json::from_value::<KubernetesComputeConfig>(json).is_err());
576+
}
462577
}

0 commit comments

Comments
 (0)