Skip to content

Commit c8180b3

Browse files
committed
Use a watch for waiting for deleted objects rather than polling (#452)
## Description *Please add a description here. This will become the commit message of the merge request later.* Co-authored-by: Teo Klestrup Röijezon <teo.roijezon@stackable.de>
1 parent 8667365 commit c8180b3

File tree

4 files changed

+28
-42
lines changed

4 files changed

+28
-42
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Changed
8+
9+
- Objects are now streamed rather than polled when waiting for them to be deleted ([#452]).
10+
11+
[#452]: https://github.com/stackabletech/operator-rs/pull/452
12+
713
## [0.24.0] - 2022-08-04
814

915
### Added

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ thiserror = "1.0.31"
2929
tokio = { version = "1.20.1", features = ["macros", "rt-multi-thread"] }
3030
tracing = "0.1.35"
3131
tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
32-
backoff = "0.4.0"
3332
derivative = "2.2.0"
3433
tracing-opentelemetry = "0.17.4"
3534
opentelemetry = { version = "0.17.0", features = ["rt-tokio"] }

src/client.rs

+16-41
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
use crate::error::OperatorResult;
1+
use crate::error::{Error, OperatorResult};
22
use crate::label_selector;
33

4-
use backoff::backoff::Backoff;
5-
use backoff::ExponentialBackoff;
64
use either::Either;
75
use futures::StreamExt;
86
use k8s_openapi::apimachinery::pkg::apis::meta::v1::LabelSelector;
97
use kube::api::{DeleteParams, ListParams, Patch, PatchParams, PostParams, Resource, ResourceExt};
108
use kube::client::Client as KubeClient;
119
use kube::core::Status;
10+
use kube::runtime::wait::delete::delete_and_finalize;
1211
use kube::runtime::WatchStreamExt;
1312
use kube::{Api, Config};
1413
use serde::de::DeserializeOwned;
1514
use serde::Serialize;
1615
use std::convert::TryFrom;
1716
use std::fmt::{Debug, Display};
18-
use tracing::{error, info, trace};
17+
use tracing::trace;
1918

2019
/// This `Client` can be used to access Kubernetes.
2120
/// It wraps an underlying [kube::client::Client] and provides some common functionality.
@@ -370,45 +369,21 @@ impl Client {
370369
/// from Kubernetes
371370
pub async fn ensure_deleted<T>(&self, resource: T) -> OperatorResult<()>
372371
where
373-
T: Clone + Debug + DeserializeOwned + Resource,
372+
T: Clone + Debug + DeserializeOwned + Resource + Send + 'static,
374373
<T as Resource>::DynamicType: Default,
375374
{
376-
let mut backoff_strategy = ExponentialBackoff {
377-
max_elapsed_time: None,
378-
..ExponentialBackoff::default()
379-
};
380-
381-
self.delete(&resource).await?;
382-
383-
loop {
384-
if self
385-
.get_opt::<T>(&resource.name_any(), resource.namespace().as_deref())
386-
.await?
387-
.is_none()
388-
{
389-
return Ok(());
390-
}
391-
392-
// When backoff returns `None` the timeout has expired
393-
match backoff_strategy.next_backoff() {
394-
Some(backoff) => {
395-
info!(
396-
"Waiting [{}] seconds before trying again..",
397-
backoff.as_secs()
398-
);
399-
tokio::time::sleep(backoff).await;
400-
}
401-
None => {
402-
// We offer no way of specifying a timeout, so this shouldn't happen,
403-
// if it does we'll log an error for now and continue iterating and wait for
404-
// the last interval we saw
405-
error!(
406-
"Waiting for deletion timed out, but no timeout was specified, this is an error and should not happen!"
407-
);
408-
tokio::time::sleep(backoff_strategy.current_interval).await;
409-
}
410-
}
411-
}
375+
Ok(delete_and_finalize(
376+
self.get_api::<T>(resource.namespace().as_deref()),
377+
resource
378+
.meta()
379+
.name
380+
.as_deref()
381+
.ok_or(Error::MissingObjectKey {
382+
key: "metadata.name",
383+
})?,
384+
&self.delete_params,
385+
)
386+
.await?)
412387
}
413388

414389
/// Returns an [kube::Api] object which is either namespaced or not depending on whether

src/error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ pub enum Error {
1515
source: kube::Error,
1616
},
1717

18+
#[error("Kubernetes failed to delete object: {source}")]
19+
KubeDeleteError {
20+
#[from]
21+
source: kube::runtime::wait::delete::Error,
22+
},
23+
1824
#[error("Object is missing key: {key}")]
1925
MissingObjectKey { key: &'static str },
2026

0 commit comments

Comments
 (0)