From b6548207357131b876ccef58dcb0a285e182b2bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Puczyn=CC=81ski?= Date: Tue, 23 Jul 2024 10:14:36 +0200 Subject: [PATCH] Fix poll jittering in Object controller MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mateusz Puczyński --- cmd/provider/main.go | 2 +- internal/controller/kubernetes.go | 4 ++-- internal/controller/object/object.go | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/provider/main.go b/cmd/provider/main.go index 7e9d78e1..ca8cdcc9 100644 --- a/cmd/provider/main.go +++ b/cmd/provider/main.go @@ -166,7 +166,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, pollJitter), "Cannot setup controller") + kingpin.FatalIfError(object.Setup(mgr, o, *sanitizeSecrets, pollJitter, *pollJitterPercentage), "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 d5601332..3afbf1a5 100644 --- a/internal/controller/kubernetes.go +++ b/internal/controller/kubernetes.go @@ -30,11 +30,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, pollJitter time.Duration) error { +func Setup(mgr ctrl.Manager, o controller.Options, sanitizeSecrets bool, pollJitter time.Duration, pollJitterPercentage uint) error { if err := config.Setup(mgr, o); err != nil { return err } - if err := object.Setup(mgr, o, sanitizeSecrets, pollJitter); err != nil { + if err := object.Setup(mgr, o, sanitizeSecrets, pollJitterPercentage); err != nil { return err } if err := observedobjectcollection.Setup(mgr, o, pollJitter); err != nil { diff --git a/internal/controller/object/object.go b/internal/controller/object/object.go index ebc0c696..af3ee2e8 100644 --- a/internal/controller/object/object.go +++ b/internal/controller/object/object.go @@ -118,7 +118,7 @@ type KindObserver interface { } // Setup adds a controller that reconciles Object managed resources. -func Setup(mgr ctrl.Manager, o controller.Options, sanitizeSecrets bool, pollJitter time.Duration) error { +func Setup(mgr ctrl.Manager, o controller.Options, sanitizeSecrets bool, pollJitterPercentage uint) error { name := managed.ControllerName(v1alpha2.ObjectGroupKind) l := o.Logger.WithValues("controller", name) @@ -132,6 +132,7 @@ func Setup(mgr ctrl.Manager, o controller.Options, sanitizeSecrets bool, pollJit // If the resource is not ready, we should poll more frequently not to delay time to readiness. pollInterval = 30 * time.Second } + pollJitter := time.Duration(float64(pollInterval) * (float64(pollJitterPercentage) / 100.0)) // This is the same as runtime default poll interval with jitter, see: // https://github.com/crossplane/crossplane-runtime/blob/7fcb8c5cad6fc4abb6649813b92ab92e1832d368/pkg/reconciler/managed/reconciler.go#L573 return pollInterval + time.Duration((rand.Float64()-0.5)*2*float64(pollJitter)) //nolint G404 // No need for secure randomness