Merge pull request #513 from RainbowMango/pr_supprese_work

Suppress reported work from propagating
This commit is contained in:
karmada-bot 2021-07-09 15:11:27 +08:00 committed by GitHub
commit ac5c41e948
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 5 deletions

View File

@ -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()

View File

@ -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,
},
}

View File

@ -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"
)

View File

@ -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)