@@ -107,8 +107,8 @@ const ANNOTATION_SANDBOX_RUNTIME_NETWORK_POLICY_UID: &str =
107107 "openshell.ai/sandbox-runtime-network-policy-uid" ;
108108const 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 .
112112const ANNOTATION_SANDBOX_RUNTIME_CONTROL_CONNECTION : & str =
113113 "openshell.ai/sandbox-runtime-control-connection" ;
114114const 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.
161160fn 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,
0 commit comments