Skip to content

Commit 80100c0

Browse files
committed
fix(docker): preserve provisioning failure status
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 877ddba commit 80100c0

2 files changed

Lines changed: 94 additions & 5 deletions

File tree

‎crates/openshell-driver-docker/src/lib.rs‎

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,14 @@ impl DockerComputeDriver {
674674
sandbox_id: &str,
675675
sandbox_name: &str,
676676
) -> Result<Option<DriverSandbox>, Status> {
677+
let pending = self.pending_snapshot(sandbox_id, sandbox_name).await;
678+
if pending
679+
.as_ref()
680+
.is_some_and(pending_sandbox_has_provisioning_failure)
681+
{
682+
return Ok(pending);
683+
}
684+
677685
let container = self
678686
.find_managed_container_summary(sandbox_id, sandbox_name)
679687
.await?;
@@ -683,7 +691,7 @@ impl DockerComputeDriver {
683691
return Ok(Some(sandbox));
684692
}
685693

686-
Ok(self.pending_snapshot(sandbox_id, sandbox_name).await)
694+
Ok(pending)
687695
}
688696

689697
async fn current_snapshots(&self) -> Result<Vec<DriverSandbox>, Status> {
@@ -692,10 +700,11 @@ impl DockerComputeDriver {
692700
.iter()
693701
.filter_map(sandbox_from_container_summary)
694702
.collect::<Vec<_>>();
695-
let mut by_id = self.pending_snapshot_map().await;
696-
for sandbox in container_sandboxes {
697-
by_id.insert(sandbox.id.clone(), sandbox);
698-
}
703+
let mut by_id = container_sandboxes
704+
.into_iter()
705+
.map(|sandbox| (sandbox.id.clone(), sandbox))
706+
.collect::<HashMap<_, _>>();
707+
merge_pending_sandbox_snapshots(&mut by_id, self.pending_snapshot_map().await);
699708
let mut sandboxes = by_id.into_values().collect::<Vec<_>>();
700709
sandboxes.sort_by(|left, right| left.id.cmp(&right.id));
701710
Ok(sandboxes)
@@ -1828,6 +1837,33 @@ fn pending_sandbox_matches(sandbox: &DriverSandbox, sandbox_id: &str, sandbox_na
18281837
|| (!sandbox_name.is_empty() && sandbox.name == sandbox_name)
18291838
}
18301839

1840+
/// A pending sandbox holds either the ordinary in-progress snapshot or the
1841+
/// explicit error published by its provisioning task. The latter is more
1842+
/// informative than a transient Docker state observed while that task cleans
1843+
/// up a failed start, so it must win during snapshot reconciliation.
1844+
fn pending_sandbox_has_provisioning_failure(sandbox: &DriverSandbox) -> bool {
1845+
sandbox.status.as_ref().is_some_and(|status| {
1846+
status.conditions.iter().any(|condition| {
1847+
condition.r#type == "Ready"
1848+
&& condition.status.eq_ignore_ascii_case("false")
1849+
&& condition.reason != "Starting"
1850+
})
1851+
})
1852+
}
1853+
1854+
fn merge_pending_sandbox_snapshots(
1855+
snapshots: &mut HashMap<String, DriverSandbox>,
1856+
pending: HashMap<String, DriverSandbox>,
1857+
) {
1858+
for (sandbox_id, sandbox) in pending {
1859+
if pending_sandbox_has_provisioning_failure(&sandbox) {
1860+
snapshots.insert(sandbox_id, sandbox);
1861+
} else {
1862+
snapshots.entry(sandbox_id).or_insert(sandbox);
1863+
}
1864+
}
1865+
}
1866+
18311867
fn provisioning_condition() -> DriverCondition {
18321868
DriverCondition {
18331869
r#type: "Ready".to_string(),

‎crates/openshell-driver-docker/src/tests.rs‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,6 +2238,59 @@ fn pending_sandbox_snapshot_uses_docker_namespace_and_starting_condition() {
22382238
assert_eq!(status.conditions[0].message, "Docker container is starting");
22392239
}
22402240

2241+
#[test]
2242+
fn pending_provisioning_failure_overrides_transient_container_state() {
2243+
let sandbox = test_sandbox();
2244+
let dead_container = pending_sandbox_snapshot(
2245+
&sandbox,
2246+
"default",
2247+
error_condition("ContainerDead", "Container is dead"),
2248+
false,
2249+
);
2250+
let start_failure = pending_sandbox_snapshot(
2251+
&sandbox,
2252+
"default",
2253+
error_condition(
2254+
"ContainerStartFailed",
2255+
"Docker responded with status code 500: CDI device injection failed",
2256+
),
2257+
false,
2258+
);
2259+
2260+
let mut snapshots = HashMap::from([(sandbox.id.clone(), dead_container)]);
2261+
merge_pending_sandbox_snapshots(
2262+
&mut snapshots,
2263+
HashMap::from([(sandbox.id.clone(), start_failure)]),
2264+
);
2265+
2266+
let status = snapshots[&sandbox.id].status.as_ref().expect("status");
2267+
assert_eq!(status.conditions[0].reason, "ContainerStartFailed");
2268+
assert!(status.conditions[0]
2269+
.message
2270+
.contains("CDI device injection failed"));
2271+
}
2272+
2273+
#[test]
2274+
fn pending_starting_snapshot_does_not_override_container_state() {
2275+
let sandbox = test_sandbox();
2276+
let dead_container = pending_sandbox_snapshot(
2277+
&sandbox,
2278+
"default",
2279+
error_condition("ContainerDead", "Container is dead"),
2280+
false,
2281+
);
2282+
let starting = pending_sandbox_snapshot(&sandbox, "default", provisioning_condition(), false);
2283+
2284+
let mut snapshots = HashMap::from([(sandbox.id.clone(), dead_container)]);
2285+
merge_pending_sandbox_snapshots(
2286+
&mut snapshots,
2287+
HashMap::from([(sandbox.id.clone(), starting)]),
2288+
);
2289+
2290+
let status = snapshots[&sandbox.id].status.as_ref().expect("status");
2291+
assert_eq!(status.conditions[0].reason, "ContainerDead");
2292+
}
2293+
22412294
#[test]
22422295
fn validate_linux_elf_binary_rejects_non_elf_files() {
22432296
let tempdir = TempDir::new().unwrap();

0 commit comments

Comments
 (0)