diff --git a/pkg/controllers/multiclusterservice/endpointslice_collect_controller.go b/pkg/controllers/multiclusterservice/endpointslice_collect_controller.go index 95c4b0b6d630..b19d421e5cd0 100644 --- a/pkg/controllers/multiclusterservice/endpointslice_collect_controller.go +++ b/pkg/controllers/multiclusterservice/endpointslice_collect_controller.go @@ -422,8 +422,12 @@ func getEndpointSliceWorkMeta(c client.Client, ns string, workName string, endpo controllerSet.Insert(util.MultiClusterServiceKind) labels[util.EndpointSliceWorkManagedByLabel] = strings.Join(controllerSet.UnsortedList(), ".") } - workMeta := metav1.ObjectMeta{Name: workName, Namespace: ns, Labels: labels} - return workMeta, nil + return metav1.ObjectMeta{ + Name: workName, + Namespace: ns, + Labels: labels, + Finalizers: []string{util.MCSEndpointSliceDispatchControllerFinalizer}, + }, nil } func cleanupWorkWithEndpointSliceDelete(c client.Client, endpointSliceKey keys.FederatedKey) error { diff --git a/pkg/controllers/multiclusterservice/endpointslice_dispatch_controller.go b/pkg/controllers/multiclusterservice/endpointslice_dispatch_controller.go index adf7660ebfe2..cbdd60ff0d7d 100644 --- a/pkg/controllers/multiclusterservice/endpointslice_dispatch_controller.go +++ b/pkg/controllers/multiclusterservice/endpointslice_dispatch_controller.go @@ -308,7 +308,7 @@ func (c *EndpointsliceDispatchController) cleanOrphanDispatchedEndpointSlice(ctx return nil } -func (c *EndpointsliceDispatchController) dispatchEndpointSlice(ctx context.Context, work *workv1alpha1.Work, mcs *networkingv1alpha1.MultiClusterService) error { +func (c *EndpointsliceDispatchController) dispatchEndpointSlice(_ context.Context, work *workv1alpha1.Work, mcs *networkingv1alpha1.MultiClusterService) error { epsSourceCluster, err := names.GetClusterName(work.Namespace) if err != nil { klog.Errorf("Failed to get EndpointSlice source cluster name for work %s/%s", work.Namespace, work.Name) @@ -347,14 +347,6 @@ func (c *EndpointsliceDispatchController) dispatchEndpointSlice(ctx context.Cont return err } } - - if controllerutil.AddFinalizer(work, util.MCSEndpointSliceDispatchControllerFinalizer) { - if err := c.Client.Update(ctx, work); err != nil { - klog.Errorf("Failed to add finalizer %s for work %s/%s:%v", util.MCSEndpointSliceDispatchControllerFinalizer, work.Namespace, work.Name, err) - return err - } - } - return nil } diff --git a/pkg/util/helper/work.go b/pkg/util/helper/work.go index 9af79a4ceaf8..2cb59634ce97 100644 --- a/pkg/util/helper/work.go +++ b/pkg/util/helper/work.go @@ -85,6 +85,7 @@ func CreateOrUpdateWork(client client.Client, workMeta metav1.ObjectMeta, resour if util.GetLabelValue(runtimeObject.Labels, workv1alpha2.WorkPermanentIDLabel) == "" { runtimeObject.Labels = util.DedupeAndMergeLabels(runtimeObject.Labels, map[string]string{workv1alpha2.WorkPermanentIDLabel: uuid.New().String()}) } + runtimeObject.Finalizers = work.Finalizers return nil }) if err != nil {