From 7ee329887a96521fdacb5f14dbfef5cafd426b2c Mon Sep 17 00:00:00 2001 From: Eirik A Date: Wed, 5 Jun 2024 14:14:36 +0100 Subject: [PATCH] runtime: document scope footgun in `watch_object` (#1510) Signed-off-by: clux --- kube-runtime/src/watcher.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/kube-runtime/src/watcher.rs b/kube-runtime/src/watcher.rs index 25c939a23..b9833f1db 100644 --- a/kube-runtime/src/watcher.rs +++ b/kube-runtime/src/watcher.rs @@ -806,19 +806,29 @@ pub fn metadata_watcher( api: Api, name: &str, ) -> impl Stream>> + Send { - watcher(api, Config::default().fields(&format!("metadata.name={name}"))).filter_map(|event| async { + // filtering by object name in given scope, so there's at most one matching object + // footgun: Api::all may generate events from namespaced objects with the same name in different namespaces + let fields = format!("metadata.name={name}"); + watcher(api, Config::default().fields(&fields)).filter_map(|event| async { match event { - Ok(Event::Delete(_)) => Some(Ok(None)), - // We're filtering by object name, so we should only get one (provided the Api is scoped correctly) - // Take the first one: + // Pass up `Some` for Found / Updated Ok(Event::Apply(obj) | Event::InitApply(obj)) => Some(Ok(Some(obj))), + // Pass up `None` for Deleted + Ok(Event::Delete(_)) => Some(Ok(None)), + // Ignore marker events Ok(Event::Init | Event::InitDone) => None, + // Bubble up errors Err(err) => Some(Err(err)), } })