diff --git a/controllers/gitrepo_controller.go b/controllers/gitrepo_controller.go index c08651cf..0fb4c2db 100644 --- a/controllers/gitrepo_controller.go +++ b/controllers/gitrepo_controller.go @@ -3,6 +3,7 @@ package controllers import ( "context" "fmt" + "time" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" @@ -25,6 +26,9 @@ type GitRepoReconciler struct { Scheme *runtime.Scheme DefaultCreationPolicy synv1alpha1.CreationPolicy + + // MaxReconcileInterval is the maximum time between two reconciliations. + MaxReconcileInterval time.Duration } //+kubebuilder:rbac:groups=syn.tools,resources=gitrepos,verbs=get;list;watch;create;update;patch;delete @@ -70,7 +74,15 @@ func (r *GitRepoReconciler) Reconcile(ctx context.Context, request ctrl.Request) res := pipeline.RunPipeline(instance, data, steps) - return reconcile.Result{Requeue: res.Requeue}, res.Err + // Immediately requeue if the result says so + if res.Requeue { + return reconcile.Result{Requeue: true}, res.Err + } + + // Requeue after the maximum interval + return reconcile.Result{ + RequeueAfter: r.MaxReconcileInterval, + }, res.Err } // SetupWithManager sets up the controller with the Manager. diff --git a/main.go b/main.go index 700ebb29..06bc0696 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strconv" + "time" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. @@ -59,8 +60,10 @@ func main() { var metricsAddr string var enableLeaderElection bool var probeAddr string + var gitRepoMaxReconcileInterval time.Duration flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") + flag.DurationVar(&gitRepoMaxReconcileInterval, "git-repo-max-reconcile-interval", 3*time.Hour, "The maximum time between reconciliations of GitRepos.") flag.BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") @@ -140,6 +143,8 @@ func main() { Client: mgr.GetClient(), Scheme: mgr.GetScheme(), DefaultCreationPolicy: creationPolicy, + + MaxReconcileInterval: gitRepoMaxReconcileInterval, }).SetupWithManager(ctx, mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "GitRepo") os.Exit(1)