Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Manno committed Jan 7, 2025
1 parent f33ef66 commit 098d0fb
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions internal/cmd/controller/gitops/reconciler/gitjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down

0 comments on commit 098d0fb

Please sign in to comment.