|
1 | | -use crate::error::OperatorResult; |
| 1 | +use crate::error::{Error, OperatorResult}; |
2 | 2 | use crate::label_selector; |
3 | 3 |
|
4 | | -use backoff::backoff::Backoff; |
5 | | -use backoff::ExponentialBackoff; |
6 | 4 | use either::Either; |
7 | 5 | use futures::StreamExt; |
8 | 6 | use k8s_openapi::apimachinery::pkg::apis::meta::v1::LabelSelector; |
9 | 7 | use kube::api::{DeleteParams, ListParams, Patch, PatchParams, PostParams, Resource, ResourceExt}; |
10 | 8 | use kube::client::Client as KubeClient; |
11 | 9 | use kube::core::Status; |
| 10 | +use kube::runtime::wait::delete::delete_and_finalize; |
12 | 11 | use kube::runtime::WatchStreamExt; |
13 | 12 | use kube::{Api, Config}; |
14 | 13 | use serde::de::DeserializeOwned; |
15 | 14 | use serde::Serialize; |
16 | 15 | use std::convert::TryFrom; |
17 | 16 | use std::fmt::{Debug, Display}; |
18 | | -use tracing::{error, info, trace}; |
| 17 | +use tracing::trace; |
19 | 18 |
|
20 | 19 | /// This `Client` can be used to access Kubernetes. |
21 | 20 | /// It wraps an underlying [kube::client::Client] and provides some common functionality. |
@@ -370,45 +369,21 @@ impl Client { |
370 | 369 | /// from Kubernetes |
371 | 370 | pub async fn ensure_deleted<T>(&self, resource: T) -> OperatorResult<()> |
372 | 371 | where |
373 | | - T: Clone + Debug + DeserializeOwned + Resource, |
| 372 | + T: Clone + Debug + DeserializeOwned + Resource + Send + 'static, |
374 | 373 | <T as Resource>::DynamicType: Default, |
375 | 374 | { |
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?) |
412 | 387 | } |
413 | 388 |
|
414 | 389 | /// Returns an [kube::Api] object which is either namespaced or not depending on whether |
|
0 commit comments