Skip to content

Commit 122942e

Browse files
fix(server): preserve sandbox status on statusless driver updates
When a driver update arrives without a status payload (e.g. before Kubernetes populates the status subresource), preserve the stored phase, conditions, and current policy version instead of resetting them. Adds a regression test covering the edge case. Signed-off-by: Derek Carr <[email protected]>
1 parent 8334767 commit 122942e

1 file changed

Lines changed: 71 additions & 4 deletions

File tree

  • crates/openshell-server/src/compute

‎crates/openshell-server/src/compute/mod.rs‎

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,12 @@ impl ComputeRuntime {
924924
let sandbox = self
925925
.store
926926
.update_message_cas::<Sandbox, _>(&incoming.id, 0, |sandbox| {
927-
let mut phase = derive_phase(incoming.status.as_ref());
927+
let old_phase =
928+
SandboxPhase::try_from(sandbox.phase()).unwrap_or(SandboxPhase::Unknown);
929+
let mut phase = incoming
930+
.status
931+
.as_ref()
932+
.map_or(old_phase, |status| derive_phase(Some(status)));
928933
let supervisor_promoted = session_connected
929934
&& matches!(phase, SandboxPhase::Provisioning | SandboxPhase::Unknown);
930935
if supervisor_promoted {
@@ -935,14 +940,19 @@ impl ComputeRuntime {
935940
let mut status = incoming
936941
.status
937942
.as_ref()
938-
.map(|s| public_status_from_driver(s, phase, cpv));
943+
.map(|s| public_status_from_driver(s, phase, cpv))
944+
.or_else(|| sandbox.status.clone());
939945
rewrite_user_facing_conditions(&mut status, sandbox.spec.as_ref());
940946
if supervisor_promoted {
941947
ensure_supervisor_ready_status(&mut status, &sandbox_name);
942948
}
943949

944-
let old_phase =
945-
SandboxPhase::try_from(sandbox.phase()).unwrap_or(SandboxPhase::Unknown);
950+
if let Some(s) = status.as_mut()
951+
&& s.sandbox_name.is_empty()
952+
{
953+
s.sandbox_name.clone_from(&sandbox_name);
954+
}
955+
946956
if old_phase != phase {
947957
info!(
948958
sandbox_id = %incoming.id,
@@ -978,6 +988,7 @@ impl ComputeRuntime {
978988
}
979989
sandbox.status = status;
980990
sandbox.set_phase(phase as i32);
991+
sandbox.set_current_policy_version(cpv);
981992
})
982993
.await
983994
.map_err(|e| match e {
@@ -2328,6 +2339,62 @@ mod tests {
23282339
);
23292340
}
23302341

2342+
#[tokio::test]
2343+
async fn apply_sandbox_update_without_status_preserves_existing_status() {
2344+
let runtime = test_runtime(Arc::new(TestDriver::default())).await;
2345+
let mut sandbox = sandbox_record("sb-1", "sandbox-a", SandboxPhase::Ready);
2346+
sandbox.status = Some(SandboxStatus {
2347+
sandbox_name: "sandbox-a".to_string(),
2348+
conditions: vec![SandboxCondition {
2349+
r#type: "Ready".to_string(),
2350+
status: "True".to_string(),
2351+
reason: "DependenciesReady".to_string(),
2352+
message: "Pod is Ready".to_string(),
2353+
last_transition_time: String::new(),
2354+
}],
2355+
current_policy_version: 7,
2356+
..Default::default()
2357+
});
2358+
sandbox.set_phase(SandboxPhase::Ready as i32);
2359+
runtime.store.put_message(&sandbox).await.unwrap();
2360+
2361+
runtime
2362+
.apply_sandbox_update(DriverSandbox {
2363+
id: "sb-1".to_string(),
2364+
name: "sandbox-a".to_string(),
2365+
namespace: "default".to_string(),
2366+
spec: None,
2367+
status: None,
2368+
})
2369+
.await
2370+
.unwrap();
2371+
2372+
let stored = runtime
2373+
.store
2374+
.get_message::<Sandbox>("sb-1")
2375+
.await
2376+
.unwrap()
2377+
.unwrap();
2378+
assert_eq!(
2379+
SandboxPhase::try_from(stored.phase()).unwrap(),
2380+
SandboxPhase::Ready
2381+
);
2382+
assert_eq!(stored.current_policy_version(), 7);
2383+
let ready = stored
2384+
.status
2385+
.as_ref()
2386+
.and_then(|status| {
2387+
status
2388+
.conditions
2389+
.iter()
2390+
.find(|condition| condition.r#type == "Ready")
2391+
})
2392+
.unwrap();
2393+
assert_eq!(ready.status, "True");
2394+
assert_eq!(ready.reason, "DependenciesReady");
2395+
assert_eq!(ready.message, "Pod is Ready");
2396+
}
2397+
23312398
#[tokio::test]
23322399
async fn apply_sandbox_update_promotes_connected_supervisor_session_to_ready() {
23332400
let runtime = test_runtime(Arc::new(TestDriver::default())).await;

0 commit comments

Comments
 (0)