From 098d0fbe7191bc0ea62ad8ad330a29a9b672c23d Mon Sep 17 00:00:00 2001 From: Mario Manno Date: Tue, 7 Jan 2025 19:21:51 +0100 Subject: [PATCH] wip --- .../gitops/reconciler/gitjob_controller.go | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/internal/cmd/controller/gitops/reconciler/gitjob_controller.go b/internal/cmd/controller/gitops/reconciler/gitjob_controller.go index 1a501c9ca2..f30d06ddea 100644 --- a/internal/cmd/controller/gitops/reconciler/gitjob_controller.go +++ b/internal/cmd/controller/gitops/reconciler/gitjob_controller.go @@ -99,12 +99,42 @@ type GitJobReconciler struct { Recorder record.EventRecorder } +// TypedResourceVersionUnchangedPredicate implements a update predicate to allow syncPeriod and RequeueAfter events +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 +} + +// 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 (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) predicate.Or( + TypedResourceVersionUnchangedPredicate[client.Object]{}, predicate.GenerationChangedPredicate{}, predicate.AnnotationChangedPredicate{}, predicate.LabelChangedPredicate{},