diff --git a/src/sources/docker.rs b/src/sources/docker.rs index d3e1d0e..4a4ceb1 100644 --- a/src/sources/docker.rs +++ b/src/sources/docker.rs @@ -36,7 +36,7 @@ const INSPECT_CONCURRENCY: usize = 8; /// Lines queued towards a follow consumer. const FOLLOW_CHANNEL: usize = 256; /// Log drivers the Docker API can read back. -const READABLE_DRIVERS: [&str; 3] = ["json-file", "local", "journald"]; +const READABLE_DRIVERS: [&str; 4] = ["json-file", "local", "journald", "k8s-file"]; pub struct BollardDocker { socket: PathBuf, @@ -129,7 +129,9 @@ impl DockerSource for BollardDocker { Err(BollardError::DockerResponseServerError { status_code: 404, .. }) => {} - Err(e) => return Err(map_error(&e, &socket)), + Err(e) => { + tracing::warn!(error = %e, "skipping container that failed to inspect"); + } } } Ok(out) @@ -158,6 +160,9 @@ impl DockerSource for BollardDocker { Ok(lines) } + /// The receiver closes on shutdown, consumer drop, daemon stream end or a + /// stream error. End and error are indistinguishable to the consumer + /// (errors are logged). A trailing unterminated line is flushed first. async fn follow_logs( &self, name: &str, @@ -180,16 +185,19 @@ impl DockerSource for BollardDocker { () = tx.closed() => break, next = s.next() => next, }; - let frame = match next { - Some(Ok(frame)) => frame, + let mut batch = Vec::new(); + let mut finished = false; + match next { + Some(Ok(frame)) => splitter.push(frame, &mut |l| batch.push(l)), Some(Err(e)) => { tracing::warn!(error = %e, container = %name, "docker log follow ended"); - break; + finished = true; } - None => break, - }; - let mut batch = Vec::new(); - splitter.push(frame, &mut |l| batch.push(l)); + None => finished = true, + } + if finished { + splitter.finish(&mut |l| batch.push(l)); + } for line in batch { tokio::select! { biased; @@ -201,6 +209,9 @@ impl DockerSource for BollardDocker { } } } + if finished { + break; + } } // Dropping `s` closes the HTTP connection and ends the daemon-side stream. }); @@ -213,8 +224,8 @@ impl DockerSource for BollardDocker { /// Strip the leading `/` and reject names that could alter the request path. fn normalize_name(name: &str) -> Result { let n = name.trim().trim_start_matches('/'); - let valid = !n.is_empty() - && n.len() <= 255 + let valid = n.len() <= 255 + && n.chars().next().is_some_and(|c| c.is_ascii_alphanumeric()) && n.chars() .all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '-')); if valid { @@ -264,6 +275,10 @@ fn map_error(e: &BollardError, socket: &str) -> AppError { 401 | 403 => AppError::Unavailable(format!("docker denied access: {message}")), 404 => AppError::NotFound(message.clone()), 400 => AppError::BadRequest(message.clone()), + 501 => AppError::BadRequest(message.clone()), + 500 if message.to_ascii_lowercase().contains("logging driver") => { + AppError::BadRequest(message.clone()) + } _ => AppError::Internal(format!("docker API error {status_code}: {message}")), }, BollardError::IOError { err } => match err.kind() { @@ -459,6 +474,8 @@ mod tests { assert_eq!(normalize_name("web.1_x").unwrap(), "web.1_x"); assert!(normalize_name("").is_err()); assert!(normalize_name("a/b").is_err()); + assert!(normalize_name("..").is_err()); + assert!(normalize_name("-x").is_err()); assert!(normalize_name("a?b").is_err()); } @@ -503,6 +520,33 @@ mod tests { assert!(matches!(map_error(&refused, "s"), AppError::Unavailable(_))); } + #[test] + fn summary_mapping() { + use bollard::models::{ContainerState, Health, HealthStatusEnum}; + let i = ContainerInspectResponse { + id: Some("abc".into()), + name: Some("/web".into()), + restart_count: Some(2), + state: Some(ContainerState { + status: Some(ContainerStateStatusEnum::RUNNING), + started_at: Some("2026-10-15T12:00:00Z".into()), + health: Some(Health { + status: Some(HealthStatusEnum::HEALTHY), + ..Default::default() + }), + ..Default::default() + }), + ..Default::default() + }; + let now: Timestamp = "2026-10-15T12:01:00Z".parse().unwrap(); + let s = summary_from_inspect(&i, now); + assert_eq!(s.name, "web"); + assert_eq!(s.state, "running"); + assert_eq!(s.health.as_deref(), Some("healthy")); + assert_eq!(s.uptime_seconds, Some(60)); + assert_eq!(s.restart_count, Some(2)); + } + #[test] fn log_driver_check() { let mut i = ContainerInspectResponse::default();