From e97bc205384c0e913a6e58708b5f5a37f9ebb3e8 Mon Sep 17 00:00:00 2001 From: Mark Ingram Date: Wed, 11 Sep 2024 12:35:11 +0100 Subject: [PATCH] Stream produced by `watch_object` will include an item when the object isn't in any initial list fixes #1576 --- kube-runtime/src/watcher.rs | 44 ++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/kube-runtime/src/watcher.rs b/kube-runtime/src/watcher.rs index 30aa932d5..0703baa10 100644 --- a/kube-runtime/src/watcher.rs +++ b/kube-runtime/src/watcher.rs @@ -14,7 +14,7 @@ use kube_client::{ Api, Error as ClientErr, }; use serde::de::DeserializeOwned; -use std::{clone::Clone, collections::VecDeque, fmt::Debug, time::Duration}; +use std::{clone::Clone, collections::VecDeque, fmt::Debug, future, time::Duration}; use thiserror::Error; use tracing::{debug, error, warn}; @@ -844,18 +844,36 @@ pub fn watch_object 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)), - } - }) + watcher(api, Config::default().fields(&fields)) + // track whether the object was seen in each initial listing + .scan(false, |obj_seen, event| { + if matches!(event, Ok(Event::Init)) { + *obj_seen = false; + } else if matches!(event, Ok(Event::InitApply(_))) { + *obj_seen = true; + } + future::ready(Some((*obj_seen, event))) + }) + .filter_map(|(obj_seen, event)| async move { + match event { + // Pass up `Some` for Found / Updated + Ok(Event::Apply(obj)) | Ok(Event::InitApply(obj)) => Some(Ok(Some(obj))), + // Pass up `None` for Deleted + Ok(Event::Delete(_)) => Some(Ok(None)), + // Ignore marker event + Ok(Event::Init) => None, + // Pass up `None` if the object wasn't seen in any initial list + Ok(Event::InitDone) => { + if obj_seen { + None + } else { + Some(Ok(None)) + } + } + // Bubble up errors + Err(err) => Some(Err(err)), + } + }) } /// Default watcher backoff inspired by Kubernetes' client-go.