Configurable dependency requeuing

Add command argument for configuring the interval at which failing dependencies are reevaluated.
This commit is contained in:
stefanprodan 2020-06-20 10:15:39 +03:00
parent e4b5a29f3a
commit d11e76d322
2 changed files with 14 additions and 8 deletions

View File

@ -47,8 +47,9 @@ import (
// KustomizationReconciler reconciles a Kustomization object // KustomizationReconciler reconciles a Kustomization object
type KustomizationReconciler struct { type KustomizationReconciler struct {
client.Client client.Client
Log logr.Logger requeueDependency time.Duration
Scheme *runtime.Scheme Log logr.Logger
Scheme *runtime.Scheme
} }
// +kubebuilder:rbac:groups=kustomize.fluxcd.io,resources=kustomizations,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=kustomize.fluxcd.io,resources=kustomizations,verbs=get;list;watch;create;update;patch;delete
@ -126,12 +127,11 @@ func (r *KustomizationReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
return ctrl.Result{Requeue: true}, err return ctrl.Result{Requeue: true}, err
} }
// we can't rely on exponential backoff because it will prolong the execution too much, // we can't rely on exponential backoff because it will prolong the execution too much,
// instead we requeue every half a minute. // instead we requeue on a fix interval.
requeueAfter := 30 * time.Second msg := fmt.Sprintf("Dependencies do not meet ready condition, retrying in %s", r.requeueDependency.String())
msg := fmt.Sprintf("Dependencies do not meet ready condition, retrying in %s", requeueAfter.String())
log.Error(err, msg) log.Error(err, msg)
r.alert(kustomization, msg, "info") r.alert(kustomization, msg, "info")
return ctrl.Result{RequeueAfter: requeueAfter}, nil return ctrl.Result{RequeueAfter: r.requeueDependency}, nil
} }
log.Info("All dependencies area ready, proceeding with apply") log.Info("All dependencies area ready, proceeding with apply")
} }
@ -160,10 +160,12 @@ func (r *KustomizationReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
} }
type KustomizationReconcilerOptions struct { type KustomizationReconcilerOptions struct {
MaxConcurrentReconciles int MaxConcurrentReconciles int
DependencyRequeueInterval time.Duration
} }
func (r *KustomizationReconciler) SetupWithManager(mgr ctrl.Manager, opts KustomizationReconcilerOptions) error { func (r *KustomizationReconciler) SetupWithManager(mgr ctrl.Manager, opts KustomizationReconcilerOptions) error {
r.requeueDependency = opts.DependencyRequeueInterval
return ctrl.NewControllerManagedBy(mgr). return ctrl.NewControllerManagedBy(mgr).
For(&kustomizev1.Kustomization{}). For(&kustomizev1.Kustomization{}).
WithEventFilter(KustomizationGarbageCollectPredicate{Log: r.Log}). WithEventFilter(KustomizationGarbageCollectPredicate{Log: r.Log}).

View File

@ -19,6 +19,7 @@ package main
import ( import (
"flag" "flag"
"os" "os"
"time"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme" clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@ -50,6 +51,7 @@ func main() {
metricsAddr string metricsAddr string
enableLeaderElection bool enableLeaderElection bool
concurrent int concurrent int
requeueDependency time.Duration
logJSON bool logJSON bool
) )
@ -58,6 +60,7 @@ func main() {
"Enable leader election for controller manager. "+ "Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.") "Enabling this will ensure there is only one active controller manager.")
flag.IntVar(&concurrent, "concurrent", 4, "The number of concurrent kustomize reconciles.") flag.IntVar(&concurrent, "concurrent", 4, "The number of concurrent kustomize reconciles.")
flag.DurationVar(&requeueDependency, "requeue-dependency", 30*time.Second, "The interval at which failing dependencies are reevaluated.")
flag.BoolVar(&logJSON, "log-json", false, "Set logging to JSON format.") flag.BoolVar(&logJSON, "log-json", false, "Set logging to JSON format.")
flag.Parse() flag.Parse()
@ -89,7 +92,8 @@ func main() {
Log: ctrl.Log.WithName("controllers").WithName("Kustomization"), Log: ctrl.Log.WithName("controllers").WithName("Kustomization"),
Scheme: mgr.GetScheme(), Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr, controllers.KustomizationReconcilerOptions{ }).SetupWithManager(mgr, controllers.KustomizationReconcilerOptions{
MaxConcurrentReconciles: concurrent, MaxConcurrentReconciles: concurrent,
DependencyRequeueInterval: requeueDependency,
}); err != nil { }); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Kustomization") setupLog.Error(err, "unable to create controller", "controller", "Kustomization")
os.Exit(1) os.Exit(1)