Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kubernetes_logs): delaying applied new pod #13886

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/kubernetes/reflector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub async fn custom_reflector<K, W>(
match event {
// Immediately reoncile `Applied` event
watcher::Event::Applied(_) => {
trace!(message = "Processing Applied event.", ?event);
store.apply_watcher_event(&event);
trace!(message = "Queuing Applied event.", ?event);
delay_queue.insert(event.to_owned(), Duration::from_secs(delay_deletion.as_secs() + 1));
}
// Delay reconciling any `Deleted` events
watcher::Event::Deleted(_) => {
Expand Down Expand Up @@ -60,7 +60,7 @@ pub async fn custom_reflector<K, W>(
result = delay_queue.next(), if !delay_queue.is_empty() => {
match result {
Some(event) => {
trace!(message = "Processing Deleted event.", ?event);
trace!(message = "Processing delayed event.", ?event);
store.apply_watcher_event(&event.into_inner());
},
// DelayQueue returns None if the queue is exhausted,
Expand Down Expand Up @@ -105,7 +105,8 @@ mod tests {
.await
.unwrap();
tokio::spawn(custom_reflector(store_w, rx, Duration::from_secs(1)));
tokio::time::sleep(Duration::from_secs(1)).await;
// Wait until the resource is added
tokio::time::sleep(Duration::from_secs(3)).await;
assert_eq!(store.get(&ObjectRef::from_obj(&cm)).as_deref(), Some(&cm));
}

Expand All @@ -121,18 +122,46 @@ mod tests {
..ConfigMap::default()
};
let (mut tx, rx) = mpsc::channel::<_>(5);
tokio::spawn(custom_reflector(store_w, rx, Duration::from_secs(2)));
tx.send(Ok(watcher::Event::Applied(cm.clone())))
.await
.unwrap();
tokio::time::sleep(Duration::from_secs(4)).await;
// Ensure the Resource is available
assert_eq!(store.get(&ObjectRef::from_obj(&cm)).as_deref(), Some(&cm));

tx.send(Ok(watcher::Event::Deleted(cm.clone())))
.await
.unwrap();
tokio::spawn(custom_reflector(store_w, rx, Duration::from_secs(2)));
// Ensure the Resource is still available after deletion
tokio::time::sleep(Duration::from_secs(1)).await;
assert_eq!(store.get(&ObjectRef::from_obj(&cm)).as_deref(), Some(&cm));
// Ensure the Resource is removed once the `delay_deletion` has elapsed
tokio::time::sleep(Duration::from_secs(5)).await;
assert_eq!(store.get(&ObjectRef::from_obj(&cm)), None);
}

#[tokio::test]
async fn applied_should_apply_with_delay() {
let store_w = store::Writer::default();
let store = store_w.as_reader();
let cm = ConfigMap {
metadata: ObjectMeta {
name: Some("a".to_string()),
..ObjectMeta::default()
},
..ConfigMap::default()
};
let (mut tx, rx) = mpsc::channel::<_>(5);
tokio::spawn(custom_reflector(store_w, rx, Duration::from_secs(2)));
tx.send(Ok(watcher::Event::Applied(cm.clone())))
.await
.unwrap();
// Ensure the Resource is not available instantly
assert_eq!(store.get(&ObjectRef::from_obj(&cm)), None);

tokio::time::sleep(Duration::from_secs(4)).await;
// Ensure the Resourse is available
assert_eq!(store.get(&ObjectRef::from_obj(&cm)).as_deref(), Some(&cm));
}
}