|
1 | 1 | use crate::error::OperatorResult;
|
| 2 | +use crate::finalizer; |
2 | 3 | use crate::label_selector;
|
| 4 | +use crate::podutils; |
3 | 5 |
|
4 | 6 | use either::Either;
|
5 | 7 | use k8s_openapi::apimachinery::pkg::apis::meta::v1::{Condition, LabelSelector};
|
@@ -229,17 +231,38 @@ impl Client {
|
229 | 231 | .await?)
|
230 | 232 | }
|
231 | 233 |
|
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. |
233 | 243 | /// Take a look at the Kubernetes API reference.
|
234 | 244 | /// 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>>> |
236 | 246 | where
|
237 | 247 | T: Clone + DeserializeOwned + Meta,
|
238 | 248 | {
|
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 | + } |
243 | 266 | }
|
244 | 267 |
|
245 | 268 | /// Sets a condition on a status.
|
|
0 commit comments