From 9f6bf68f780a387cd8d02ee2cad26349d6338193 Mon Sep 17 00:00:00 2001 From: vie-serendipity <2733147505@qq.com> Date: Mon, 25 Nov 2024 11:37:49 +0800 Subject: [PATCH] feat: add predicates to binding controller Signed-off-by: vie-serendipity <2733147505@qq.com> --- pkg/controllers/binding/binding_controller.go | 29 ++++++++++++++++++- .../cluster_resource_binding_controller.go | 29 ++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/pkg/controllers/binding/binding_controller.go b/pkg/controllers/binding/binding_controller.go index 52236646fbcc..766ed6fc5247 100644 --- a/pkg/controllers/binding/binding_controller.go +++ b/pkg/controllers/binding/binding_controller.go @@ -30,9 +30,11 @@ import ( "k8s.io/client-go/tools/record" "k8s.io/klog/v2" controllerruntime "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -167,7 +169,7 @@ func (c *ResourceBindingController) removeOrphanWorks(ctx context.Context, bindi func (c *ResourceBindingController) SetupWithManager(mgr controllerruntime.Manager) error { return controllerruntime.NewControllerManagedBy(mgr). Named(ControllerName). - For(&workv1alpha2.ResourceBinding{}). + For(&workv1alpha2.ResourceBinding{}, builder.WithPredicates(bindingPredicateFn)). WithEventFilter(predicate.GenerationChangedPredicate{}). Watches(&policyv1alpha1.OverridePolicy{}, handler.EnqueueRequestsFromMapFunc(c.newOverridePolicyFunc())). Watches(&policyv1alpha1.ClusterOverridePolicy{}, handler.EnqueueRequestsFromMapFunc(c.newOverridePolicyFunc())). @@ -228,3 +230,28 @@ func (c *ResourceBindingController) newOverridePolicyFunc() handler.MapFunc { return requests } } + +var bindingPredicateFn = predicate.Funcs{ + CreateFunc: func(tce event.TypedCreateEvent[client.Object]) bool { + binding, ok := tce.Object.(*workv1alpha2.ResourceBinding) + if !ok { + return false + } + return binding.Status.SchedulerObservedGeneration == binding.Generation + }, + UpdateFunc: func(tue event.TypedUpdateEvent[client.Object]) bool { + newBinding, ok := tue.ObjectNew.(*workv1alpha2.ResourceBinding) + if !ok { + return false + } + _, ok = tue.ObjectOld.(*workv1alpha2.ResourceBinding) + if !ok { + return false + } + + return newBinding.Status.SchedulerObservedGeneration == newBinding.Generation + }, + DeleteFunc: func(tde event.TypedDeleteEvent[client.Object]) bool { + return true + }, +} diff --git a/pkg/controllers/binding/cluster_resource_binding_controller.go b/pkg/controllers/binding/cluster_resource_binding_controller.go index 89cf6759e0bf..5cd6b212a85d 100644 --- a/pkg/controllers/binding/cluster_resource_binding_controller.go +++ b/pkg/controllers/binding/cluster_resource_binding_controller.go @@ -30,9 +30,11 @@ import ( "k8s.io/client-go/tools/record" "k8s.io/klog/v2" controllerruntime "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -163,7 +165,7 @@ func (c *ClusterResourceBindingController) removeOrphanWorks(ctx context.Context func (c *ClusterResourceBindingController) SetupWithManager(mgr controllerruntime.Manager) error { return controllerruntime.NewControllerManagedBy(mgr). Named(ClusterResourceBindingControllerName). - For(&workv1alpha2.ClusterResourceBinding{}). + For(&workv1alpha2.ClusterResourceBinding{}, builder.WithPredicates(clusterResourceBindingPredicateFn)). Watches(&policyv1alpha1.ClusterOverridePolicy{}, handler.EnqueueRequestsFromMapFunc(c.newOverridePolicyFunc())). WithEventFilter(predicate.GenerationChangedPredicate{}). WithOptions(controller.Options{RateLimiter: ratelimiterflag.DefaultControllerRateLimiter[controllerruntime.Request](c.RateLimiterOptions)}). @@ -214,3 +216,28 @@ func (c *ClusterResourceBindingController) newOverridePolicyFunc() handler.MapFu return requests } } + +var clusterResourceBindingPredicateFn = predicate.Funcs{ + CreateFunc: func(tce event.TypedCreateEvent[client.Object]) bool { + crBinding, ok := tce.Object.(*workv1alpha2.ClusterResourceBinding) + if !ok { + return false + } + return crBinding.Status.SchedulerObservedGeneration == crBinding.Generation + }, + UpdateFunc: func(tue event.TypedUpdateEvent[client.Object]) bool { + newBinding, ok := tue.ObjectNew.(*workv1alpha2.ClusterResourceBinding) + if !ok { + return false + } + _, ok = tue.ObjectOld.(*workv1alpha2.ClusterResourceBinding) + if !ok { + return false + } + + return newBinding.Status.SchedulerObservedGeneration == newBinding.Generation + }, + DeleteFunc: func(tde event.TypedDeleteEvent[client.Object]) bool { + return true + }, +}