Skip to content

Commit

Permalink
Stream produced by watch_object will include an item when the objec…
Browse files Browse the repository at this point in the history
…t isn't in any initial list

- added a comment
- resolved a nightly clippy
- use a match guard instead of an nested if
  • Loading branch information
markdingram committed Sep 12, 2024
1 parent 4cb7e31 commit 22abc45
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions kube-runtime/src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,13 @@ pub fn watch_object<K: Resource + Clone + DeserializeOwned + Debug + Send + 'sta
// 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))
// track whether the object was seen in each initial listing
// The `obj_seen` state is used to track whether the object exists in each Init / InitApply / InitDone
// sequence of events. If the object wasn't seen in any particular sequence it is treated as deleted and
// `None` is emitted when the InitDone event is received.
//
// The first check ensures `None` is emitted if the object was already gone (or not found), subsequent
// checks ensure `None` is emitted even if for some reason the Delete event wasn't received, which
// could happen given K8S events aren't guaranteed delivery.
.scan(false, |obj_seen, event| {
if matches!(event, Ok(Event::Init)) {
*obj_seen = false;
Expand All @@ -857,19 +863,13 @@ pub fn watch_object<K: Resource + Clone + DeserializeOwned + Debug + Send + 'sta
.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))),
Ok(Event::Apply(obj) | 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))
}
}
// Pass up `None` if the object wasn't seen in the initial list
Ok(Event::InitDone) if !obj_seen => Some(Ok(None)),
// Ignore marker events
Ok(Event::Init | Event::InitDone) => None,
// Bubble up errors
Err(err) => Some(Err(err)),

Check warning on line 874 in kube-runtime/src/watcher.rs

View check run for this annotation

Codecov / codecov/patch

kube-runtime/src/watcher.rs#L874

Added line #L874 was not covered by tests
}
Expand Down

0 comments on commit 22abc45

Please sign in to comment.