From d94752ebc87395fd90bc38ad60f0ebe7b8d56ae7 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Sat, 26 Oct 2024 20:48:54 +0200 Subject: [PATCH] DRA resourceslice controller: use preconditions for Delete It's better to verify UID and ResourceVersion of the ResourceSlice that we want to delete. If anything changed, the decision to remove it might not apply anymore and we need to check again. --- .../resourceslice/resourceslicecontroller.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/dynamic-resource-allocation/resourceslice/resourceslicecontroller.go b/staging/src/k8s.io/dynamic-resource-allocation/resourceslice/resourceslicecontroller.go index 1fcd434cf1f..6e4c1852c77 100644 --- a/staging/src/k8s.io/dynamic-resource-allocation/resourceslice/resourceslicecontroller.go +++ b/staging/src/k8s.io/dynamic-resource-allocation/resourceslice/resourceslicecontroller.go @@ -605,8 +605,22 @@ func (c *Controller) syncPool(ctx context.Context, poolName string) error { // Remove stale slices. for _, slice := range obsoleteSlices { - logger.V(5).Info("Deleting obsolete resource slice", "slice", klog.KObj(slice)) - if err := c.kubeClient.ResourceV1alpha3().ResourceSlices().Delete(ctx, slice.Name, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) { + options := metav1.DeleteOptions{ + Preconditions: &metav1.Preconditions{ + UID: &slice.UID, + ResourceVersion: &slice.ResourceVersion, + }, + } + // It can happen that we sync again shortly after deleting a + // slice and before the slice gets removed from the informer + // cache. The MutationCache can't help here because it does not + // track pending deletes. + // + // If this happens, we get a "not found error" and nothing + // changes on the server. The only downside is the extra API + // call. This isn't as bad as extra creates. + logger.V(5).Info("Deleting obsolete resource slice", "slice", klog.KObj(slice), "deleteOptions", options) + if err := c.kubeClient.ResourceV1alpha3().ResourceSlices().Delete(ctx, slice.Name, options); err != nil && !apierrors.IsNotFound(err) { return fmt.Errorf("delete resource slice: %w", err) } }