Derive a `DriverCondition` from Podman container state.
(state: &ContainerState)
| 331 | |
| 332 | /// Derive a `DriverCondition` from Podman container state. |
| 333 | fn condition_from_state(state: &ContainerState) -> DriverCondition { |
| 334 | let (status_val, reason, message) = match state.status.as_str() { |
| 335 | "running" => match &state.health { |
| 336 | Some(HealthState { status }) if status == "healthy" => { |
| 337 | ("True", "HealthCheckPassed", String::new()) |
| 338 | } |
| 339 | Some(HealthState { status }) if status == "unhealthy" => { |
| 340 | ("False", "HealthCheckFailed", String::new()) |
| 341 | } |
| 342 | Some(HealthState { status }) if status == "starting" => { |
| 343 | ("False", "HealthCheckStarting", String::new()) |
| 344 | } |
| 345 | _ => ("False", CONDITION_STARTING, String::new()), |
| 346 | }, |
| 347 | "created" => ("False", "ContainerCreated", String::new()), |
| 348 | "exited" | "stopped" => { |
| 349 | let msg = if state.oom_killed { |
| 350 | "Container was killed by the OOM killer".to_string() |
| 351 | } else { |
| 352 | format!("Container exited with code {}", state.exit_code) |
| 353 | }; |
| 354 | let reason = if state.oom_killed { |
| 355 | "OOMKilled" |
| 356 | } else { |
| 357 | CONDITION_EXITED |
| 358 | }; |
| 359 | ("False", reason, msg) |
| 360 | } |
| 361 | other => ( |
| 362 | "Unknown", |
| 363 | "Unknown", |
| 364 | format!("Unknown container state: {other}"), |
| 365 | ), |
| 366 | }; |
| 367 | |
| 368 | // Use Podman's state timestamps for last_transition_time: |
| 369 | // - Running/healthy states use started_at |
| 370 | // - Stopped/exited states use finished_at |
| 371 | let last_transition_time = match state.status.as_str() { |
| 372 | "running" => state.started_at.clone().unwrap_or_default(), |
| 373 | "exited" | "stopped" => state.finished_at.clone().unwrap_or_default(), |
| 374 | _ => String::new(), |
| 375 | }; |
| 376 | |
| 377 | DriverCondition { |
| 378 | r#type: "Ready".to_string(), |
| 379 | status: status_val.to_string(), |
| 380 | reason: reason.to_string(), |
| 381 | message, |
| 382 | last_transition_time, |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | #[cfg(test)] |
| 387 | mod tests { |