Skip to content

Commit

Permalink
GenerationChangedPredicate prevented Cache Sync to Trigger Reconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Manno committed Jan 8, 2025
1 parent a62038c commit 2dd28b7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
3 changes: 3 additions & 0 deletions internal/cmd/agent/controller/bundledeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func (r *BundleDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
WithEventFilter(
// we do not trigger for status changes
predicate.Or(
// Note: These predicates prevent cache
// syncPeriod from triggering reconcile, since
// cache sync is an Update event.
predicate.GenerationChangedPredicate{},
predicate.AnnotationChangedPredicate{},
predicate.LabelChangedPredicate{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ func (r *GitJobReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.GitRepo{},
builder.WithPredicates(
// do not trigger for GitRepo status changes (except for commit changes)
// do not trigger for GitRepo status changes (except for commit changes and cache sync)
predicate.Or(
TypedResourceVersionUnchangedPredicate[client.Object]{},
predicate.GenerationChangedPredicate{},
predicate.AnnotationChangedPredicate{},
predicate.LabelChangedPredicate{},
Expand Down
51 changes: 51 additions & 0 deletions internal/cmd/controller/gitops/reconciler/predicate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package reconciler

import (
"reflect"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

// TypedResourceVersionUnchangedPredicate implements a update predicate to
// allow syncPeriod to trigger the reconciler
type TypedResourceVersionUnchangedPredicate[T metav1.Object] struct {
predicate.TypedFuncs[T]
}

func isNil(arg any) bool {
if v := reflect.ValueOf(arg); !v.IsValid() || ((v.Kind() == reflect.Ptr ||
v.Kind() == reflect.Interface ||
v.Kind() == reflect.Slice ||
v.Kind() == reflect.Map ||
v.Kind() == reflect.Chan ||
v.Kind() == reflect.Func) && v.IsNil()) {
return true
}
return false
}

func (TypedResourceVersionUnchangedPredicate[T]) Create(e event.CreateEvent) bool {
return false
}

func (TypedResourceVersionUnchangedPredicate[T]) Delete(e event.DeleteEvent) bool {
return false
}

// Update implements default UpdateEvent filter for validating resource version change.
func (TypedResourceVersionUnchangedPredicate[T]) Update(e event.TypedUpdateEvent[T]) bool {
if isNil(e.ObjectOld) {
return false
}
if isNil(e.ObjectNew) {
return false
}

return e.ObjectNew.GetResourceVersion() == e.ObjectOld.GetResourceVersion()
}

func (TypedResourceVersionUnchangedPredicate[T]) Generic(e event.GenericEvent) bool {
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func (r *HelmAppReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&fleet.HelmApp{},
builder.WithPredicates(
predicate.Or(
// Note: These predicates prevent cache
// syncPeriod from triggering reconcile, since
// cache sync is an Update event.
predicate.GenerationChangedPredicate{},
predicate.AnnotationChangedPredicate{},
predicate.LabelChangedPredicate{},
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/controller/reconciler/config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (r *ConfigReconciler) SetupWithManager(mgr ctrl.Manager) error {
object.GetName() == config.ManagerConfigName
}),
predicate.Or(
// Note: These predicates prevent cache
// syncPeriod from triggering reconcile, since
// cache sync is an Update event.
predicate.ResourceVersionChangedPredicate{},
predicate.GenerationChangedPredicate{},
predicate.AnnotationChangedPredicate{},
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/controller/reconciler/imagescan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func (r *ImageScanReconciler) SetupWithManager(mgr ctrl.Manager) error {
predicate.And(
sharding.FilterByShardID(r.ShardID),
predicate.Or(
// Note: These predicates prevent cache
// syncPeriod from triggering reconcile, since
// cache sync is an Update event.
predicate.GenerationChangedPredicate{},
predicate.AnnotationChangedPredicate{},
predicate.LabelChangedPredicate{},
Expand Down

0 comments on commit 2dd28b7

Please sign in to comment.