From f33ef6686aa0ab6bcf68b9d6cc04423f6cf5f80e Mon Sep 17 00:00:00 2001 From: Mario Manno Date: Fri, 13 Dec 2024 17:43:55 +0100 Subject: [PATCH] Gitops controller resyncs every hour If gitrepos are lost from requeueAfter polling, resync should add them again. --- .../controller/gitops/reconciler/gitjob_controller.go | 2 +- internal/cmd/controller/operator.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/cmd/controller/gitops/reconciler/gitjob_controller.go b/internal/cmd/controller/gitops/reconciler/gitjob_controller.go index 372d227e91..1a501c9ca2 100644 --- a/internal/cmd/controller/gitops/reconciler/gitjob_controller.go +++ b/internal/cmd/controller/gitops/reconciler/gitjob_controller.go @@ -213,7 +213,7 @@ func (r *GitJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr // addJitter to the requeue time to avoid thundering herd // generate a random number between -10% and +10% of the duration func addJitter(d time.Duration) time.Duration { - return d + time.Duration(rand.Int64N(int64(d)/5)-int64(d)/10) + return d + time.Duration(rand.Int64N(int64(d)/5)-int64(d)/10) // nolint:gosec // gosec G404 false positive, not used for crypto } // manageGitJob is responsible for creating, updating and deleting the GitJob and setting the GitRepo's status accordingly diff --git a/internal/cmd/controller/operator.go b/internal/cmd/controller/operator.go index 73881c62a9..3442f38239 100644 --- a/internal/cmd/controller/operator.go +++ b/internal/cmd/controller/operator.go @@ -3,6 +3,7 @@ package controller import ( "context" "fmt" + "time" "github.com/reugn/go-quartz/quartz" @@ -18,6 +19,7 @@ import ( clientgoscheme "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/healthz" metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" ) @@ -59,6 +61,8 @@ func start( leaderElectionSuffix = fmt.Sprintf("-%s", shardID) } + hour := 60 * time.Minute + mgr, err := ctrl.NewManager(config, ctrl.Options{ Scheme: scheme, Metrics: metricServerOptions, @@ -70,6 +74,11 @@ func start( LeaseDuration: leaderOpts.LeaseDuration, RenewDeadline: leaderOpts.RenewDeadline, RetryPeriod: leaderOpts.RetryPeriod, + + // resync to pick up lost gitrepos + Cache: cache.Options{ + SyncPeriod: &hour, + }, }) if err != nil { setupLog.Error(err, "unable to start manager")