From 59c2db992c7c5373c5125ec52a2eb8418cc119bc Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Wed, 27 Nov 2024 00:06:24 +0100 Subject: [PATCH] fix(controller): add `IgnoreDelete` predicate (#3003) Signed-off-by: Hidde Beydals --- internal/controller/promotions/promotions.go | 2 ++ .../controller/stages/control_flow_stages.go | 2 ++ internal/controller/stages/regular_stages.go | 2 ++ internal/controller/warehouses/warehouses.go | 11 ++-------- internal/predicate/ignore_delete.go | 20 +++++++++++++++++++ 5 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 internal/predicate/ignore_delete.go diff --git a/internal/controller/promotions/promotions.go b/internal/controller/promotions/promotions.go index 4b89815c0..138cc5beb 100644 --- a/internal/controller/promotions/promotions.go +++ b/internal/controller/promotions/promotions.go @@ -31,6 +31,7 @@ import ( "github.com/akuity/kargo/internal/kubeclient" libEvent "github.com/akuity/kargo/internal/kubernetes/event" "github.com/akuity/kargo/internal/logging" + intpredicate "github.com/akuity/kargo/internal/predicate" ) // ReconcilerConfig represents configuration for the promotion reconciler. @@ -114,6 +115,7 @@ func SetupReconcilerWithManager( c, err := ctrl.NewControllerManagedBy(kargoMgr). For(&kargoapi.Promotion{}). + WithEventFilter(intpredicate.IgnoreDelete[client.Object]{}). WithEventFilter(predicate.Or( predicate.GenerationChangedPredicate{}, kargo.RefreshRequested{}, diff --git a/internal/controller/stages/control_flow_stages.go b/internal/controller/stages/control_flow_stages.go index 2a6a11ecd..4445be621 100644 --- a/internal/controller/stages/control_flow_stages.go +++ b/internal/controller/stages/control_flow_stages.go @@ -26,6 +26,7 @@ import ( "github.com/akuity/kargo/internal/kubeclient" libEvent "github.com/akuity/kargo/internal/kubernetes/event" "github.com/akuity/kargo/internal/logging" + intpredicate "github.com/akuity/kargo/internal/predicate" ) type ControlFlowStageReconciler struct { @@ -121,6 +122,7 @@ func (r *ControlFlowStageReconciler) SetupWithManager( For(&kargoapi.Stage{}). Named("control_flow_stage"). WithOptions(controller.CommonOptions(r.cfg.MaxConcurrentControlFlowReconciles)). + WithEventFilter(intpredicate.IgnoreDelete[client.Object]{}). WithEventFilter( predicate.And( IsControlFlowStage(true), diff --git a/internal/controller/stages/regular_stages.go b/internal/controller/stages/regular_stages.go index 5856a9566..a60472613 100644 --- a/internal/controller/stages/regular_stages.go +++ b/internal/controller/stages/regular_stages.go @@ -36,6 +36,7 @@ import ( "github.com/akuity/kargo/internal/kubeclient" libEvent "github.com/akuity/kargo/internal/kubernetes/event" "github.com/akuity/kargo/internal/logging" + intpredicate "github.com/akuity/kargo/internal/predicate" "github.com/akuity/kargo/internal/rollouts" ) @@ -151,6 +152,7 @@ func (r *RegularStageReconciler) SetupWithManager( c, err := ctrl.NewControllerManagedBy(kargoMgr). For(&kargoapi.Stage{}). WithOptions(controller.CommonOptions(r.cfg.MaxConcurrentReconciles)). + WithEventFilter(intpredicate.IgnoreDelete[client.Object]{}). WithEventFilter( predicate.And( IsControlFlowStage(false), diff --git a/internal/controller/warehouses/warehouses.go b/internal/controller/warehouses/warehouses.go index 0ebc772c8..3a85c1fe0 100644 --- a/internal/controller/warehouses/warehouses.go +++ b/internal/controller/warehouses/warehouses.go @@ -10,7 +10,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/predicate" @@ -24,6 +23,7 @@ import ( "github.com/akuity/kargo/internal/kargo" "github.com/akuity/kargo/internal/kubeclient" "github.com/akuity/kargo/internal/logging" + intpredicate "github.com/akuity/kargo/internal/predicate" ) type ReconcilerConfig struct { @@ -91,14 +91,7 @@ func SetupReconcilerWithManager( if err := ctrl.NewControllerManagedBy(mgr). For(&kargoapi.Warehouse{}). - WithEventFilter( - predicate.Funcs{ - DeleteFunc: func(event.DeleteEvent) bool { - // We're not interested in any deletes - return false - }, - }, - ). + WithEventFilter(intpredicate.IgnoreDelete[client.Object]{}). WithEventFilter( predicate.Or( predicate.GenerationChangedPredicate{}, diff --git a/internal/predicate/ignore_delete.go b/internal/predicate/ignore_delete.go new file mode 100644 index 000000000..a06e11388 --- /dev/null +++ b/internal/predicate/ignore_delete.go @@ -0,0 +1,20 @@ +package predicate + +import ( + "sigs.k8s.io/controller-runtime/pkg/event" + "sigs.k8s.io/controller-runtime/pkg/predicate" +) + +// IgnoreDelete is a predicate that filters out Delete events. +// +// Typically, a reconciler will not need to do anything when it receives an +// event.TypedDeleteEvent, as it acts on the event.TypedUpdateEvent which sets +// the deletion timestamp. +type IgnoreDelete[T any] struct { + predicate.TypedFuncs[T] +} + +// Delete always returns false, ignoring the event. +func (i IgnoreDelete[T]) Delete(event.TypedDeleteEvent[T]) bool { + return false +}