Skip to content

Commit 53a1465

Browse files
authored
feature: Checks whether an object is already deleted before attempting to delete it (#65)
BREAKING CHANGE: The return value of the method has changed
1 parent 61c9e0e commit 53a1465

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/client.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::error::OperatorResult;
2+
use crate::finalizer;
23
use crate::label_selector;
4+
use crate::podutils;
35

46
use either::Either;
57
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{Condition, LabelSelector};
@@ -229,17 +231,38 @@ impl Client {
229231
.await?)
230232
}
231233

232-
/// Which of the two results this returns depends on the API.
234+
/// This deletes a resource _if it is not deleted already_.
235+
///
236+
/// It checks whether the resource is already deleted by looking at the `deletion_timestamp`
237+
/// of the resource using the [`finalizer::has_deletion_stamp`] method.
238+
/// If that is the case it'll return a `Ok(None)`.
239+
///
240+
/// In case the object is actually deleted or marked for deletion there are two possible
241+
/// return types.
242+
/// Which of the two are returned depends on the API being called.
233243
/// Take a look at the Kubernetes API reference.
234244
/// Some `delete` endpoints return the object and others return a `Status` object.
235-
pub async fn delete<T>(&self, resource: &T) -> OperatorResult<Either<T, Status>>
245+
pub async fn delete<T>(&self, resource: &T) -> OperatorResult<Option<Either<T, Status>>>
236246
where
237247
T: Clone + DeserializeOwned + Meta,
238248
{
239-
let api: Api<T> = self.get_api(Meta::namespace(resource));
240-
Ok(api
241-
.delete(&Meta::name(resource), &self.delete_params)
242-
.await?)
249+
if finalizer::has_deletion_stamp(resource) {
250+
trace!(
251+
"Resource ([{}]) already has `deletion_timestamp`, not deleting",
252+
podutils::get_log_name(resource)
253+
);
254+
Ok(None)
255+
} else {
256+
trace!(
257+
"Resource ([{}]) does not have a `deletion_timestamp`, deleting now",
258+
podutils::get_log_name(resource)
259+
);
260+
let api: Api<T> = self.get_api(Meta::namespace(resource));
261+
Ok(Some(
262+
api.delete(&Meta::name(resource), &self.delete_params)
263+
.await?,
264+
))
265+
}
243266
}
244267

245268
/// Sets a condition on a status.

0 commit comments

Comments
 (0)