diff --git a/cmd/provider/main.go b/cmd/provider/main.go index cb764550..18019c12 100644 --- a/cmd/provider/main.go +++ b/cmd/provider/main.go @@ -54,6 +54,7 @@ func main() { debug = app.Flag("debug", "Run with debug logging.").Short('d').Bool() syncInterval = app.Flag("sync", "Controller manager sync period such as 300ms, 1.5h, or 2h45m").Short('s').Default("1h").Duration() pollInterval = app.Flag("poll", "Poll interval controls how often an individual resource should be checked for drift.").Default("1m").Duration() + pollJitterPercentage = app.Flag("poll-jitter-percentage", "Percentage of jitter to apply to poll interval. It cannot be negative, and must be less than 100.").Default("10").Uint() leaderElection = app.Flag("leader-election", "Use leader election for the controller manager.").Short('l').Default("false").Envar("LEADER_ELECTION").Bool() maxReconcileRate = app.Flag("max-reconcile-rate", "The number of concurrent reconciliations that may be running at one time.").Default("10").Int() enableManagementPolicies = app.Flag("enable-management-policies", "Enable support for Management Policies.").Default("true").Envar("ENABLE_MANAGEMENT_POLICIES").Bool() @@ -72,6 +73,16 @@ func main() { ctrl.SetLogger(zl) } + if *pollJitterPercentage >= 100 { + kingpin.Fatalf("invalid --poll-jitter-percentage %v must be less than 100", *pollJitterPercentage) + } + pollJitter := time.Duration(float64(*pollInterval) * (float64(*pollJitterPercentage) / 100.0)) + log.Debug("Starting", + "sync-interval", syncInterval.String(), + "poll-interval", pollInterval.String(), + "poll-jitter", pollJitter.String(), + "max-reconcile-rate", *maxReconcileRate) + cfg, err := ctrl.GetConfig() kingpin.FatalIfError(err, "Cannot get API server rest config") @@ -130,7 +141,7 @@ func main() { // notice and remove when we drop support for v1alpha1. kingpin.FatalIfError(ctrl.NewWebhookManagedBy(mgr).For(&v1alpha1.Object{}).Complete(), "Cannot create Object webhook") - kingpin.FatalIfError(object.Setup(mgr, o, *sanitizeSecrets), "Cannot setup controller") + kingpin.FatalIfError(object.Setup(mgr, o, *sanitizeSecrets, pollJitter), "Cannot setup controller") kingpin.FatalIfError(mgr.Start(ctrl.SetupSignalHandler()), "Cannot start controller manager") } diff --git a/internal/controller/kubernetes.go b/internal/controller/kubernetes.go index 7b957c61..5fb3bf49 100644 --- a/internal/controller/kubernetes.go +++ b/internal/controller/kubernetes.go @@ -17,6 +17,8 @@ limitations under the License. package controller import ( + "time" + ctrl "sigs.k8s.io/controller-runtime" "github.com/crossplane/crossplane-runtime/pkg/controller" @@ -27,11 +29,11 @@ import ( // Setup creates all Template controllers with the supplied logger and adds them to // the supplied manager. -func Setup(mgr ctrl.Manager, o controller.Options, sanitizeSecrets bool) error { +func Setup(mgr ctrl.Manager, o controller.Options, sanitizeSecrets bool, pollJitter time.Duration) error { if err := config.Setup(mgr, o); err != nil { return err } - if err := object.Setup(mgr, o, sanitizeSecrets); err != nil { + if err := object.Setup(mgr, o, sanitizeSecrets, pollJitter); err != nil { return err } return nil diff --git a/internal/controller/object/object.go b/internal/controller/object/object.go index dc054ebc..a160872c 100644 --- a/internal/controller/object/object.go +++ b/internal/controller/object/object.go @@ -21,6 +21,7 @@ import ( "encoding/base64" "fmt" "strings" + "time" "github.com/pkg/errors" v1 "k8s.io/api/core/v1" @@ -91,7 +92,7 @@ const ( ) // Setup adds a controller that reconciles Object managed resources. -func Setup(mgr ctrl.Manager, o controller.Options, sanitizeSecrets bool) error { +func Setup(mgr ctrl.Manager, o controller.Options, sanitizeSecrets bool, pollJitter time.Duration) error { name := managed.ControllerName(v1alpha2.ObjectGroupKind) cps := []managed.ConnectionPublisher{managed.NewAPISecretPublisher(mgr.GetClient(), mgr.GetScheme())} @@ -112,6 +113,7 @@ func Setup(mgr ctrl.Manager, o controller.Options, sanitizeSecrets bool) error { }), managed.WithFinalizer(&objFinalizer{client: mgr.GetClient()}), managed.WithPollInterval(o.PollInterval), + managed.WithPollJitterHook(pollJitter), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), managed.WithConnectionPublishers(cps...),