From d8e2053b35d5dc55857c8e1eff0eea0faeb0488a Mon Sep 17 00:00:00 2001 From: RainbowMango Date: Fri, 9 Jul 2021 11:55:30 +0800 Subject: [PATCH] Suppress reported work from propagating Signed-off-by: RainbowMango --- .../app/controllermanager.go | 4 +-- .../mcs/service_export_controller.go | 3 +- pkg/util/constants.go | 13 ++++++++ pkg/util/helper/predicate.go | 30 +++++++++++++++++-- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/cmd/controller-manager/app/controllermanager.go b/cmd/controller-manager/app/controllermanager.go index a20bf9c51..5cfd35800 100644 --- a/cmd/controller-manager/app/controllermanager.go +++ b/cmd/controller-manager/app/controllermanager.go @@ -210,7 +210,7 @@ func setupControllers(mgr controllerruntime.Manager, opts *options.Options, stop EventRecorder: mgr.GetEventRecorderFor(execution.ControllerName), RESTMapper: mgr.GetRESTMapper(), ObjectWatcher: objectWatcher, - PredicateFunc: helper.NewWorkPredicate(mgr), + PredicateFunc: helper.NewExecutionPredicate(mgr), ClusterClientSetFunc: util.NewClusterDynamicClientSet, } if err := executionController.SetupWithManager(mgr); err != nil { @@ -225,7 +225,7 @@ func setupControllers(mgr controllerruntime.Manager, opts *options.Options, stop StopChan: stopChan, WorkerNumber: 1, ObjectWatcher: objectWatcher, - PredicateFunc: helper.NewWorkPredicate(mgr), + PredicateFunc: helper.NewExecutionPredicate(mgr), ClusterClientSetFunc: util.NewClusterDynamicClientSet, } workStatusController.RunWorkQueue() diff --git a/pkg/controllers/mcs/service_export_controller.go b/pkg/controllers/mcs/service_export_controller.go index 0ebae260c..bd69fd456 100644 --- a/pkg/controllers/mcs/service_export_controller.go +++ b/pkg/controllers/mcs/service_export_controller.go @@ -374,7 +374,8 @@ func reportEndpointSlice(c client.Client, endpointSlice *unstructured.Unstructur Labels: map[string]string{ util.ServiceNamespaceLabel: endpointSlice.GetNamespace(), util.ServiceNameLabel: endpointSlice.GetLabels()[discoveryv1beta1.LabelServiceName], - // todo: add label to indicate work do not need execute + // indicate the Work should be not propagated since it's collected resource. + util.PropagationInstruction: util.PropagationInstructionSuppressed, }, } diff --git a/pkg/util/constants.go b/pkg/util/constants.go index bce22b49f..aa922e267 100644 --- a/pkg/util/constants.go +++ b/pkg/util/constants.go @@ -30,6 +30,14 @@ const ( // ServiceNameLabel is added to work object, which is report by member cluster, to specify service name associated with EndpointSlice. ServiceNameLabel = "endpointslice.karmada.io/name" + + // PropagationInstruction is used to mark a resource(like Work) propagation instruction. + // Valid values includes: + // - suppressed: indicates that the resource should not be propagated. + // + // Note: This instruction is intended to set on Work objects to indicate the Work should be ignored by + // execution controller. The instruction maybe deprecated once we extend the Work API and no other scenario want this. + PropagationInstruction = "propagation.karmada.io/instruction" ) // Define annotations used by karmada system. @@ -99,3 +107,8 @@ const ( // TemplateField indicates the 'template' field of a resource TemplateField = "template" ) + +const ( + // PropagationInstructionSuppressed indicates that the resource should not be propagated. + PropagationInstructionSuppressed = "suppressed" +) diff --git a/pkg/util/helper/predicate.go b/pkg/util/helper/predicate.go index f1008cbb9..5818baeca 100644 --- a/pkg/util/helper/predicate.go +++ b/pkg/util/helper/predicate.go @@ -12,11 +12,21 @@ import ( "github.com/karmada-io/karmada/pkg/util/names" ) -// NewWorkPredicate generates an event filter function with Work for karmada-controller-manager. -func NewWorkPredicate(mgr controllerruntime.Manager) predicate.Funcs { +// NewExecutionPredicate generates the event filter function to skip events that the controllers are uninterested. +// Used by controllers: +// - execution controller working in karmada-controller-manager +// - work status controller working in karmada-controller-manager +func NewExecutionPredicate(mgr controllerruntime.Manager) predicate.Funcs { return predicate.Funcs{ CreateFunc: func(createEvent event.CreateEvent) bool { obj := createEvent.Object.(*workv1alpha1.Work) + + // Ignore the object that has been suppressed. + if util.GetLabelValue(obj.Labels, util.PropagationInstruction) == util.PropagationInstructionSuppressed { + klog.V(5).Infof("Ignored Work(%s/%s) create event as propagation instruction is suppressed.", obj.Namespace, obj.Name) + return false + } + clusterName, err := names.GetClusterName(obj.Namespace) if err != nil { klog.Errorf("Failed to get member cluster name for work %s/%s", obj.Namespace, obj.Name) @@ -28,10 +38,18 @@ func NewWorkPredicate(mgr controllerruntime.Manager) predicate.Funcs { klog.Errorf("Failed to get the given member cluster %s", clusterName) return false } + return clusterObj.Spec.SyncMode == clusterv1alpha1.Push }, UpdateFunc: func(updateEvent event.UpdateEvent) bool { obj := updateEvent.ObjectNew.(*workv1alpha1.Work) + + // Ignore the object that has been suppressed. + if util.GetLabelValue(obj.Labels, util.PropagationInstruction) == util.PropagationInstructionSuppressed { + klog.V(5).Infof("Ignored Work(%s/%s) update event as propagation instruction is suppressed.", obj.Namespace, obj.Name) + return false + } + clusterName, err := names.GetClusterName(obj.Namespace) if err != nil { klog.Errorf("Failed to get member cluster name for work %s/%s", obj.Namespace, obj.Name) @@ -43,10 +61,18 @@ func NewWorkPredicate(mgr controllerruntime.Manager) predicate.Funcs { klog.Errorf("Failed to get the given member cluster %s", clusterName) return false } + return clusterObj.Spec.SyncMode == clusterv1alpha1.Push }, DeleteFunc: func(deleteEvent event.DeleteEvent) bool { obj := deleteEvent.Object.(*workv1alpha1.Work) + + // Ignore the object that has been suppressed. + if util.GetLabelValue(obj.Labels, util.PropagationInstruction) == util.PropagationInstructionSuppressed { + klog.V(5).Infof("Ignored Work(%s/%s) delete event as propagation instruction is suppressed.", obj.Namespace, obj.Name) + return false + } + clusterName, err := names.GetClusterName(obj.Namespace) if err != nil { klog.Errorf("Failed to get member cluster name for work %s/%s", obj.Namespace, obj.Name)