diff --git a/pkg/controllers/execution/execution_controller.go b/pkg/controllers/execution/execution_controller.go index d768b73dc..9adc3ed3c 100644 --- a/pkg/controllers/execution/execution_controller.go +++ b/pkg/controllers/execution/execution_controller.go @@ -154,28 +154,6 @@ func (c *Controller) tryDeleteWorkload(clusterName string, work *workv1alpha1.Wo return err } - fedKey, err := keys.FederatedKeyFunc(clusterName, workload) - if err != nil { - klog.Errorf("Failed to get FederatedKey %s, error: %v", workload.GetName(), err) - return err - } - - clusterObj, err := helper.GetObjectFromCache(c.RESTMapper, c.InformerManager, fedKey) - if err != nil { - if apierrors.IsNotFound(err) { - return nil - } - klog.Errorf("Failed to get resource %v from member cluster, err is %v ", workload.GetName(), err) - return err - } - - // Avoid deleting resources that not managed by karmada. - if util.GetLabelValue(clusterObj.GetLabels(), util.ManagedByKarmadaLabel) != util.ManagedByKarmadaLabelValue { - klog.Infof("Abort deleting the resource(kind=%s, %s/%s) exists in cluster %v but not managed by karmada", clusterObj.GetKind(), clusterObj.GetNamespace(), clusterObj.GetName(), clusterName) - return nil - } - util.MergeLabel(workload, workv1alpha2.WorkPermanentIDLabel, util.GetLabelValue(work.Labels, workv1alpha2.WorkPermanentIDLabel)) - err = c.ObjectWatcher.Delete(clusterName, workload) if err != nil { klog.Errorf("Failed to delete resource in the given member cluster %v, err is %v", clusterName, err) diff --git a/pkg/util/objectwatcher/objectwatcher.go b/pkg/util/objectwatcher/objectwatcher.go index 548aa6a12..888ab4b2e 100644 --- a/pkg/util/objectwatcher/objectwatcher.go +++ b/pkg/util/objectwatcher/objectwatcher.go @@ -178,6 +178,27 @@ func (o *objectWatcherImpl) Update(clusterName string, desireObj, clusterObj *un } func (o *objectWatcherImpl) Delete(clusterName string, desireObj *unstructured.Unstructured) error { + fedKey, err := keys.FederatedKeyFunc(clusterName, desireObj) + if err != nil { + klog.Errorf("Failed to get FederatedKey %s, error: %v", desireObj.GetName(), err) + return err + } + + clusterObj, err := helper.GetObjectFromCache(o.RESTMapper, o.InformerManager, fedKey) + if err != nil { + if apierrors.IsNotFound(err) { + return nil + } + klog.Errorf("Failed to get the resource(kind=%s, %s/%s) from the member cluster %s, err is %v", desireObj.GetKind(), desireObj.GetNamespace(), desireObj.GetName(), clusterName, err) + return err + } + + // Avoid deleting resources that are not managed by Karmada. + if !o.isManagedResource(clusterObj) { + klog.Infof("Abort deleting the resource(kind=%s, %s/%s) which exists in the member cluster %s but is not managed by Karmada.", clusterObj.GetKind(), clusterObj.GetNamespace(), clusterObj.GetName(), clusterName) + return nil + } + dynamicClusterClient, err := o.ClusterClientSetFunc(clusterName, o.KubeClientSet) if err != nil { klog.Errorf("Failed to build dynamic cluster client for cluster %s, err: %v.", clusterName, err)