Skip to content

Commit 63132db

Browse files
committed
feat(kubernetes): migrate legacy control flow on restart
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 3470624 commit 63132db

13 files changed

Lines changed: 107 additions & 72 deletions

File tree

‎.agents/skills/helm-dev-environment/SKILL.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ The Skaffold flow builds distinct `gateway`, `sandbox`, and `supervisor` images
8181
and deploys the OpenShell Helm chart. The Kubernetes driver creates a
8282
capability-free workload Pod and a directly managed capability-free supervisor
8383
Pod. One namespace-wide NetworkPolicy denies direct egress from every OpenShell
84-
workload Pod. New sandboxes default to `runtime-initiated` control: the sandbox
84+
workload Pod. New sandbox generations use `runtime-initiated` control: the sandbox
8585
runtime dials its supervisor Pod, and the driver adds a per-sandbox
86-
`os-control-<id>` NetworkPolicy for that path only. Set
87-
`gatewayConfig."openshell.drivers.kubernetes".sandbox_runtime.control_connection`
88-
to `supervisor-initiated` when testing with an older supervisor or sandbox
89-
runtime image. Existing sandboxes keep the mode recorded at creation. The
86+
`os-control-<id>` NetworkPolicy for that path only. Existing running generations
87+
keep their connection until replaced; stop/start and gateway recovery that
88+
recreates both Pods upgrade legacy generations automatically. Use compatible
89+
gateway, supervisor, and sandbox runtime images together; there is no legacy
90+
rollout flag. The
9091
`pkiInitJob` hook (a pre-install Job that runs `openshell-gateway generate-certs`)
9192
generates gateway and CLI TLS secrets on first install. Supervisor Pods project
9293
only `ca.crt` and authenticate gateway RPCs with sandbox bearer tokens. User

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ enforcement, so operators must verify CNI support before running sandboxes.
105105

106106
### Control connection direction
107107

108-
Each sandbox records which side opens its Sandbox Protocol TCP connection in
109-
the Sandbox annotation `openshell.ai/sandbox-runtime-control-connection`,
110-
written once at creation from `sandbox_runtime.control_connection` (default
111-
`runtime-initiated`). A sandbox without the annotation was created before the
112-
mode existed and is `supervisor-initiated`. Start, gateway-restart recovery,
113-
and image upgrades read the annotation and never apply the current default, so
114-
both modes run side by side in one namespace. An unknown value fails closed.
108+
Every new runtime generation uses `runtime-initiated` control. The Sandbox
109+
annotation `openshell.ai/sandbox-runtime-control-connection` records the current
110+
generation's mode. A missing annotation identifies a legacy,
111+
`supervisor-initiated` generation, which keeps working until replaced. Stop/start
112+
upgrades it automatically: with both old Pods removed, the driver changes the
113+
Service selector in place, preserves its ClusterIP, and records the new mode
114+
before releasing the replacement workload. Gateway recovery that recreates both
115+
Pods also upgrades the generation. Reconciliation never migrates a running
116+
generation. An unknown annotation value fails closed; there is no rollout flag.
115117

116118
Only the dial direction differs. In both modes the sandbox runtime presents the
117119
generation-pinned TLS server identity and serves gRPC, and the supervisor

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ pub const DEFAULT_WORKSPACE_STORAGE_SIZE: &str = "2Gi";
7676

7777
/// Which side opens the Sandbox Protocol TCP connection.
7878
///
79-
/// Recorded on each Sandbox resource at creation. Existing sandboxes keep their
80-
/// recorded mode across stop/start, restarts, and upgrades; a sandbox created
81-
/// before this setting existed is `SupervisorInitiated`.
79+
/// Internal state for the current runtime generation. A missing Sandbox
80+
/// annotation identifies a legacy generation; every new generation uses
81+
/// `RuntimeInitiated`.
8282
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
8383
#[serde(rename_all = "kebab-case")]
8484
pub enum KubernetesControlConnection {
@@ -121,16 +121,12 @@ pub struct KubernetesSandboxRuntimeConfig {
121121
/// TCP port of the Sandbox Protocol listener: the workload boundary for
122122
/// supervisor-initiated sandboxes, the supervisor for runtime-initiated ones.
123123
pub boundary_port: u16,
124-
/// Connection mode recorded on sandboxes created from now on. Changing it
125-
/// never migrates an existing sandbox.
126-
pub control_connection: KubernetesControlConnection,
127124
}
128125

129126
impl Default for KubernetesSandboxRuntimeConfig {
130127
fn default() -> Self {
131128
Self {
132129
boundary_port: 5500,
133-
control_connection: KubernetesControlConnection::default(),
134130
}
135131
}
136132
}

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

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ const ANNOTATION_SANDBOX_RUNTIME_NETWORK_POLICY_UID: &str =
107107
"openshell.ai/sandbox-runtime-network-policy-uid";
108108
const ANNOTATION_SANDBOX_RUNTIME_NETWORK_POLICY_GENERATION: &str =
109109
"openshell.ai/sandbox-runtime-network-policy-generation";
110-
/// Durable, creation-time Sandbox Protocol connection mode. Absent on
111-
/// sandboxes created before the mode existed, which are supervisor-initiated.
110+
/// Connection mode of the current runtime generation. Absent on legacy
111+
/// generations, which remain supervisor-initiated until they are replaced.
112112
const ANNOTATION_SANDBOX_RUNTIME_CONTROL_CONNECTION: &str =
113113
"openshell.ai/sandbox-runtime-control-connection";
114114
const ANNOTATION_SANDBOX_RUNTIME_CONTROL_POLICY_UID: &str =
@@ -156,8 +156,7 @@ fn boundary_service_authority(
156156
)
157157
}
158158

159-
/// Read the connection mode recorded when the sandbox was created. Starting a
160-
/// sandbox never consults the current creation default.
159+
/// Read the current generation's mode without migrating a running workload.
161160
fn sandbox_runtime_control_connection(
162161
object: &DynamicObject,
163162
) -> Result<KubernetesControlConnection, KubernetesDriverError> {
@@ -2180,7 +2179,7 @@ impl KubernetesComputeDriver {
21802179
ANNOTATION_SANDBOX_RUNTIME_LOG_LEVEL.to_string(),
21812180
log_level.clone(),
21822181
);
2183-
let control_connection = self.config.sandbox_runtime.control_connection;
2182+
let control_connection = KubernetesControlConnection::RuntimeInitiated;
21842183
annotations.insert(
21852184
ANNOTATION_SANDBOX_RUNTIME_CONTROL_CONNECTION.to_string(),
21862185
control_connection.as_str().to_string(),
@@ -2333,7 +2332,7 @@ impl KubernetesComputeDriver {
23332332
}
23342333

23352334
/// Fail closed when a sandbox's boundary Service or control policy no
2336-
/// longer matches the mode recorded at creation. This is the reconcile
2335+
/// longer matches the mode recorded for its generation. This is the reconcile
23372336
/// pass's only Service read; a missing Service is drift once the sandbox
23382337
/// should be running. Returns `Precondition` for drift that must suspend.
23392338
async fn verify_sandbox_runtime_control_path(
@@ -2904,8 +2903,7 @@ impl KubernetesComputeDriver {
29042903
.get(&names.boundary_service)
29052904
.await
29062905
.map_err(KubernetesDriverError::from_kube)?;
2907-
// The Service and its selected role were fixed when the sandbox was
2908-
// created; restart must not reinterpret them under another mode.
2906+
// Running generations must retain the Service role prepared for them.
29092907
if !boundary_service_matches_mode(&service, sandbox_id, control_connection) {
29102908
return Err(KubernetesDriverError::Precondition(format!(
29112909
"boundary Service {} does not select the {} listener recorded for this sandbox",
@@ -3360,16 +3358,17 @@ impl KubernetesComputeDriver {
33603358
&namespace,
33613359
);
33623360
let pods = Api::<Pod>::namespaced(self.client.clone(), &namespace);
3363-
if pods
3361+
let workload_exists = pods
33643362
.get_opt(cr_name)
33653363
.await
33663364
.map_err(KubernetesDriverError::from_kube)?
3367-
.is_some()
3368-
{
3365+
.is_some();
3366+
if workload_exists || sandbox_runtime_should_run(&object) {
33693367
if !encoded_authentication.is_empty() {
33703368
// Gateway restart creates a fresh in-memory launch session.
3371-
// Recreate both Pods so neither side retains credentials for
3372-
// the gateway instance that was replaced.
3369+
// Suspend the controller and recreate both Pods so neither
3370+
// side retains credentials for the replaced gateway. Suspend
3371+
// even if the old workload Pod disappeared before this call.
33733372
self.stop_sandbox_inner(sandbox_id).await?;
33743373
return Box::pin(self.start_sandbox_runtime_generation(
33753374
sandbox_id,
@@ -3384,14 +3383,57 @@ impl KubernetesComputeDriver {
33843383
));
33853384
}
33863385

3387-
let control_connection = sandbox_runtime_control_connection(&object)?;
3386+
let previous_control_connection = sandbox_runtime_control_connection(&object)?;
3387+
let control_connection = KubernetesControlConnection::RuntimeInitiated;
33883388
let names = SandboxRuntimeNames::for_generation(sandbox_id, generation.as_str());
33893389
self.create_sandbox_runtime_fence(&namespace, &names)
33903390
.await?;
33913391
let mut stale_generations = sandbox_runtime_generation(&object).as_slice().to_vec();
33923392
stale_generations.push(generation.as_str());
33933393
self.delete_sandbox_runtime_supervisor(sandbox_id, &namespace, &stale_generations)
33943394
.await?;
3395+
// Both old Pods are gone and the lifecycle gate excludes reconcile.
3396+
// Upgrade the endpoint in place, preserving its stable ClusterIP. A
3397+
// retry may find this update completed before the new mode was saved.
3398+
let services = Api::<Service>::namespaced(self.client.clone(), &namespace);
3399+
let service = services
3400+
.get(&names.boundary_service)
3401+
.await
3402+
.map_err(KubernetesDriverError::from_kube)?;
3403+
if !boundary_service_matches_mode(&service, sandbox_id, control_connection) {
3404+
if !boundary_service_matches_mode(&service, sandbox_id, previous_control_connection) {
3405+
return Err(KubernetesDriverError::Precondition(format!(
3406+
"boundary Service {} no longer matches the recorded control connection",
3407+
names.boundary_service
3408+
)));
3409+
}
3410+
let version = service
3411+
.metadata
3412+
.resource_version
3413+
.as_deref()
3414+
.ok_or_else(|| {
3415+
KubernetesDriverError::Message(
3416+
"boundary Service has no resourceVersion".to_string(),
3417+
)
3418+
})?;
3419+
services
3420+
.patch(
3421+
&names.boundary_service,
3422+
&PatchParams::default(),
3423+
&Patch::Merge(serde_json::json!({
3424+
"metadata": {"resourceVersion": version},
3425+
"spec": {
3426+
"selector": crate::isolation::pair_role_labels(
3427+
&crate::sandbox_runtime::pair_label_value(sandbox_id),
3428+
crate::isolation::boundary_service_role(control_connection),
3429+
),
3430+
"publishNotReadyAddresses": true,
3431+
},
3432+
})),
3433+
)
3434+
.await
3435+
.map_err(KubernetesDriverError::from_kube)?;
3436+
}
33953437
let main_process_spec =
33963438
required_sandbox_annotation(&object, ANNOTATION_SANDBOX_RUNTIME_MAIN_PROCESS_SPEC)?;
33973439
let log_level = required_sandbox_annotation(&object, ANNOTATION_SANDBOX_RUNTIME_LOG_LEVEL)?;
@@ -3476,6 +3518,7 @@ impl KubernetesComputeDriver {
34763518
ANNOTATION_SANDBOX_RUNTIME_BOOTSTRAP_SESSION: launch_authentication.supervisor.session_id.to_string(),
34773519
ANNOTATION_SANDBOX_RUNTIME_GENERATION: generation.as_str(),
34783520
ANNOTATION_SANDBOX_RUNTIME_SUPERVISOR_UID: supervisor_uid.clone(),
3521+
ANNOTATION_SANDBOX_RUNTIME_CONTROL_CONNECTION: control_connection.as_str(),
34793522
});
34803523
running_patch["spec"]["podTemplate"]["spec"] = restart_pod_template_spec(
34813524
&volumes,

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ use openshell_core::VERSION;
1212
use openshell_core::proto::compute::v1::compute_driver_server::ComputeDriverServer;
1313
use openshell_driver_kubernetes::{
1414
ComputeDriverService, DEFAULT_GATEWAY_ID, DEFAULT_SANDBOX_SERVICE_ACCOUNT_NAME,
15-
KubernetesComputeConfig, KubernetesComputeDriver, KubernetesControlConnection,
16-
KubernetesImagePullPolicy, KubernetesSandboxRuntimeConfig, ManagedSshIngressConfig,
17-
WorkspaceMode,
15+
KubernetesComputeConfig, KubernetesComputeDriver, KubernetesImagePullPolicy,
16+
KubernetesSandboxRuntimeConfig, ManagedSshIngressConfig, WorkspaceMode,
1817
};
1918

2019
#[derive(Parser, Debug)]
@@ -158,14 +157,6 @@ struct Args {
158157
)]
159158
sandbox_runtime_boundary_port: u16,
160159

161-
/// Sandbox Protocol connection mode recorded on newly created sandboxes.
162-
#[arg(
163-
long,
164-
env = "OPENSHELL_K8S_SANDBOX_RUNTIME_CONTROL_CONNECTION",
165-
default_value = "runtime-initiated"
166-
)]
167-
sandbox_runtime_control_connection: KubernetesControlConnection,
168-
169160
/// Corporate HTTP forward proxy for policy-approved TLS CONNECT egress.
170161
#[arg(long, env = "OPENSHELL_UPSTREAM_PROXY")]
171162
https_proxy: Option<String>,
@@ -295,7 +286,6 @@ async fn main() -> Result<()> {
295286
supervisor_image_pull_policy: args.supervisor_image_pull_policy,
296287
sandbox_runtime: KubernetesSandboxRuntimeConfig {
297288
boundary_port: args.sandbox_runtime_boundary_port,
298-
control_connection: args.sandbox_runtime_control_connection,
299289
},
300290
https_proxy: args.https_proxy,
301291
no_proxy: args.no_proxy,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ pub fn workload_fence(
185185
}
186186

187187
/// Per-sandbox Sandbox Protocol endpoint. Its stable `ClusterIP` is allocated
188-
/// before either Pod runs; the selected role follows the sandbox's recorded
189-
/// connection mode and never changes for the sandbox's lifetime.
188+
/// before either Pod runs; the selected role follows the current generation's
189+
/// connection mode. A legacy endpoint is upgraded when both Pods are replaced.
190190
#[must_use]
191191
pub fn boundary_service(
192192
namespace: &str,

‎deploy/helm/openshell/templates/clusterrole.yaml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ rules:
101101
{{- if ne $workspaceMode "shared" }}
102102
- apiGroups: [""]
103103
resources: ["services"]
104-
verbs: ["create", "get"]
104+
# Restart upgrades a legacy control endpoint without replacing its ClusterIP.
105+
verbs: ["create", "get", "patch"]
105106
- apiGroups: ["networking.k8s.io"]
106107
resources: ["networkpolicies"]
107108
verbs: ["create", "get"]

‎deploy/helm/openshell/templates/role.yaml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ rules:
5858
- watch
5959
- apiGroups: [""]
6060
resources: ["services"]
61-
verbs: ["create", "get"]
61+
# Restart upgrades a legacy control endpoint without replacing its ClusterIP.
62+
verbs: ["create", "get", "patch"]
6263
- apiGroups: [""]
6364
resources: ["secrets"]
6465
# Bootstrap Secrets are generation-scoped. Recovery deletes them by exact

‎deploy/helm/openshell/tests/clusterrole_test.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ tests:
156156
content:
157157
apiGroups: [""]
158158
resources: ["services"]
159-
verbs: ["create", "get"]
159+
verbs: ["create", "get", "patch"]
160160
- contains:
161161
path: rules
162162
content:
@@ -181,7 +181,7 @@ tests:
181181
content:
182182
apiGroups: [""]
183183
resources: ["services"]
184-
verbs: ["create", "get"]
184+
verbs: ["create", "get", "patch"]
185185
- it: omits the ClusterRole when cluster-scoped RBAC is disabled
186186
set:
187187
rbac.clusterScoped.create: false

‎deploy/helm/openshell/tests/sandbox_namespace_test.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ tests:
6767
content:
6868
apiGroups: [""]
6969
resources: ["services"]
70-
verbs: ["create", "get"]
70+
verbs: ["create", "get", "patch"]
7171
- contains:
7272
path: rules
7373
content:

0 commit comments

Comments
 (0)