Skip to content

Commit b49ab8c

Browse files
committed
Stream produced by watch_object will include an item when the object isn't in any initial list
- added a comment - resolved a nightly clippy - use a match guard instead of an nested if Signed-off-by: Mark Ingram <[email protected]>
1 parent 4cb7e31 commit b49ab8c

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

kube-runtime/src/watcher.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,13 @@ pub fn watch_object<K: Resource + Clone + DeserializeOwned + Debug + Send + 'sta
845845
// footgun: Api::all may generate events from namespaced objects with the same name in different namespaces
846846
let fields = format!("metadata.name={name}");
847847
watcher(api, Config::default().fields(&fields))
848-
// track whether the object was seen in each initial listing
848+
// The `obj_seen` state is used to track whether the object exists in each Init / InitApply / InitDone
849+
// sequence of events. If the object wasn't seen in any particular sequence it is treated as deleted and
850+
// `None` is emitted when the InitDone event is received.
851+
//
852+
// The first check ensures `None` is emitted if the object was already gone (or not found), subsequent
853+
// checks ensure `None` is emitted even if for some reason the Delete event wasn't received, which
854+
// could happen given K8S events aren't guaranteed delivery.
849855
.scan(false, |obj_seen, event| {
850856
if matches!(event, Ok(Event::Init)) {
851857
*obj_seen = false;
@@ -857,19 +863,13 @@ pub fn watch_object<K: Resource + Clone + DeserializeOwned + Debug + Send + 'sta
857863
.filter_map(|(obj_seen, event)| async move {
858864
match event {
859865
// Pass up `Some` for Found / Updated
860-
Ok(Event::Apply(obj)) | Ok(Event::InitApply(obj)) => Some(Ok(Some(obj))),
866+
Ok(Event::Apply(obj) | Event::InitApply(obj)) => Some(Ok(Some(obj))),
861867
// Pass up `None` for Deleted
862868
Ok(Event::Delete(_)) => Some(Ok(None)),
863-
// Ignore marker event
864-
Ok(Event::Init) => None,
865-
// Pass up `None` if the object wasn't seen in any initial list
866-
Ok(Event::InitDone) => {
867-
if obj_seen {
868-
None
869-
} else {
870-
Some(Ok(None))
871-
}
872-
}
869+
// Pass up `None` if the object wasn't seen in the initial list
870+
Ok(Event::InitDone) if !obj_seen => Some(Ok(None)),
871+
// Ignore marker events
872+
Ok(Event::Init | Event::InitDone) => None,
873873
// Bubble up errors
874874
Err(err) => Some(Err(err)),
875875
}

0 commit comments

Comments
 (0)