Skip to content

Commit

Permalink
fix(controller): add IgnoreDelete predicate (#3003)
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco authored Nov 26, 2024
1 parent b72be05 commit 59c2db9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
2 changes: 2 additions & 0 deletions internal/controller/promotions/promotions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{},
Expand Down
2 changes: 2 additions & 0 deletions internal/controller/stages/control_flow_stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions internal/controller/stages/regular_stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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),
Expand Down
11 changes: 2 additions & 9 deletions internal/controller/warehouses/warehouses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 {
Expand Down Expand Up @@ -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{},
Expand Down
20 changes: 20 additions & 0 deletions internal/predicate/ignore_delete.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 59c2db9

Please sign in to comment.