Skip to content

Commit

Permalink
Reconcile GitRepos every three hours (#290)
Browse files Browse the repository at this point in the history
This helps to reconcile config drift and refresh expiring tokens even if the reconciler does not get triggered by cluster events.
  • Loading branch information
bastjan authored Jul 19, 2024
1 parent 8149235 commit 3bbe176
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 13 additions & 1 deletion controllers/gitrepo_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"context"
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3bbe176

Please sign in to comment.