From a2fe71ee8eb5098b4a0f254d1588bc2f795ac8b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natalie=20Klestrup=20R=C3=B6ijezon?= Date: Fri, 25 Oct 2024 00:07:10 +0200 Subject: [PATCH 1/3] Change event watcher example to retry the connection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #1615 Signed-off-by: Natalie Klestrup Röijezon --- examples/event_watcher.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/examples/event_watcher.rs b/examples/event_watcher.rs index e6df87c2e..6f55b84a4 100644 --- a/examples/event_watcher.rs +++ b/examples/event_watcher.rs @@ -1,6 +1,6 @@ use std::pin::pin; -use futures::TryStreamExt; +use futures::StreamExt; use k8s_openapi::{ api::{core::v1::ObjectReference, events::v1::Event}, apimachinery::pkg::apis::meta::v1::Time, @@ -45,12 +45,17 @@ async fn main() -> anyhow::Result<()> { { println!("{0:<6} {1:<15} {2:<55} {3}", "AGE", "REASON", "OBJECT", "MESSAGE"); } - while let Some(ev) = event_stream.try_next().await? { - let age = ev.creation_timestamp().map(format_creation).unwrap_or_default(); - let reason = ev.reason.unwrap_or_default(); - let obj = ev.regarding.and_then(format_objref).unwrap_or_default(); - let note = ev.note.unwrap_or_default(); - println!("{0:<6} {1:<15} {2:<55} {3}", age, reason, obj, note); + while let Some(ev) = event_stream.next().await { + match ev { + Ok(ev) => { + let age = ev.creation_timestamp().map(format_creation).unwrap_or_default(); + let reason = ev.reason.unwrap_or_default(); + let obj = ev.regarding.and_then(format_objref).unwrap_or_default(); + let note = ev.note.unwrap_or_default(); + println!("{0:<6} {1:<15} {2:<55} {3}", age, reason, obj, note); + } + Err(err) => eprintln!("{:?}", anyhow::Error::new(err)), + } } Ok(()) } From 3eca71f9064cb2bd605665beb305bcd7e79acc6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natalie=20Klestrup=20R=C3=B6ijezon?= Date: Fri, 25 Oct 2024 00:09:57 +0200 Subject: [PATCH 2/3] Factor out printing to ensure consistency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Natalie Klestrup Röijezon --- examples/event_watcher.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/event_watcher.rs b/examples/event_watcher.rs index 6f55b84a4..62b03c29e 100644 --- a/examples/event_watcher.rs +++ b/examples/event_watcher.rs @@ -41,10 +41,10 @@ async fn main() -> anyhow::Result<()> { let event_stream = watcher(events, conf).default_backoff().applied_objects(); let mut event_stream = pin!(event_stream); - #[allow(clippy::print_literal)] // for consistency - { - println!("{0:<6} {1:<15} {2:<55} {3}", "AGE", "REASON", "OBJECT", "MESSAGE"); + fn print_event(age: &str, reason: &str, obj: &str, note: &str) { + println!("{age:<6} {reason:<15} {obj:<55} {note}"); } + print_event("AGE", "REASON", "OBJECT", "MESSAGE"); while let Some(ev) = event_stream.next().await { match ev { Ok(ev) => { @@ -52,7 +52,7 @@ async fn main() -> anyhow::Result<()> { let reason = ev.reason.unwrap_or_default(); let obj = ev.regarding.and_then(format_objref).unwrap_or_default(); let note = ev.note.unwrap_or_default(); - println!("{0:<6} {1:<15} {2:<55} {3}", age, reason, obj, note); + print_event(&age, &reason, &obj, ¬e); } Err(err) => eprintln!("{:?}", anyhow::Error::new(err)), } From 5c411fc986bd1a92e9ca81c851a44de8ab167188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natalie=20Klestrup=20R=C3=B6ijezon?= Date: Fri, 25 Oct 2024 00:39:26 +0200 Subject: [PATCH 3/3] Report an error if an invalid --for= argument is supplied, instead of ignoring it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Natalie Klestrup Röijezon --- examples/event_watcher.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/event_watcher.rs b/examples/event_watcher.rs index 62b03c29e..dd33229f0 100644 --- a/examples/event_watcher.rs +++ b/examples/event_watcher.rs @@ -36,6 +36,8 @@ async fn main() -> anyhow::Result<()> { if let Some(forval) = app.r#for { if let Some((kind, name)) = forval.split_once('/') { conf = conf.fields(&format!("regarding.kind={kind},regarding.name={name}")); + } else { + return Err(anyhow::Error::msg("Usage: --for=/")); } } let event_stream = watcher(events, conf).default_backoff().applied_objects();