From ba6e2744a3877e3640fa12f6119f60adb4773fbd Mon Sep 17 00:00:00 2001
From: Francisco Barros <francisco.borges.aurindo.barros@cern.ch>
Date: Thu, 15 Jul 2021 12:58:10 +0200
Subject: [PATCH 1/3] ExponentialFailureRate added to Drupal controller

---
 controllers/drupalsite_controller.go | 12 ++++++++++--
 main.go                              |  8 +++++---
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/controllers/drupalsite_controller.go b/controllers/drupalsite_controller.go
index 60916e60..b2b85f3e 100644
--- a/controllers/drupalsite_controller.go
+++ b/controllers/drupalsite_controller.go
@@ -34,6 +34,7 @@ import (
 	appsv1 "k8s.io/api/apps/v1"
 	batchv1 "k8s.io/api/batch/v1"
 	corev1 "k8s.io/api/core/v1"
+	"k8s.io/client-go/util/workqueue"
 
 	k8sapierrors "k8s.io/apimachinery/pkg/api/errors"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -41,6 +42,7 @@ import (
 	"k8s.io/apimachinery/pkg/types"
 	ctrl "sigs.k8s.io/controller-runtime"
 	"sigs.k8s.io/controller-runtime/pkg/client"
+	"sigs.k8s.io/controller-runtime/pkg/controller"
 	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
 	"sigs.k8s.io/controller-runtime/pkg/handler"
 	"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -72,8 +74,10 @@ var (
 // DrupalSiteReconciler reconciles a DrupalSite object
 type DrupalSiteReconciler struct {
 	client.Client
-	Log    logr.Logger
-	Scheme *runtime.Scheme
+	Log                     logr.Logger
+	Scheme                  *runtime.Scheme
+	MaxRateLimiterSeconds   int
+	StartRateLimiterSeconds int
 }
 
 // +kubebuilder:rbac:groups=drupal.webservices.cern.ch,resources=drupalsites,verbs=get;list;watch;create;update;patch;delete
@@ -100,6 +104,7 @@ type DrupalSiteReconciler struct {
 
 // SetupWithManager adds a manager which watches the resources
 func (r *DrupalSiteReconciler) SetupWithManager(mgr ctrl.Manager) error {
+	exponentialFailure := workqueue.NewItemExponentialFailureRateLimiter(time.Second*time.Duration(r.StartRateLimiterSeconds), time.Minute*time.Duration(r.MaxRateLimiterSeconds))
 	return ctrl.NewControllerManagedBy(mgr).
 		For(&webservicesv1a1.DrupalSite{}).
 		Owns(&appsv1.Deployment{}).
@@ -134,6 +139,9 @@ func (r *DrupalSiteReconciler) SetupWithManager(mgr ctrl.Manager) error {
 				return []reconcile.Request{}
 			}),
 		).
+		WithOptions(controller.Options{
+			RateLimiter: exponentialFailure,
+		}).
 		Complete(r)
 }
 
diff --git a/main.go b/main.go
index 0a0c37db..c348a95f 100644
--- a/main.go
+++ b/main.go
@@ -105,9 +105,11 @@ func main() {
 	}
 
 	if err = (&controllers.DrupalSiteReconciler{
-		Client: mgr.GetClient(),
-		Log:    ctrl.Log.WithName("controllers").WithName("DrupalSite"),
-		Scheme: mgr.GetScheme(),
+		Client:                  mgr.GetClient(),
+		Log:                     ctrl.Log.WithName("controllers").WithName("DrupalSite"),
+		Scheme:                  mgr.GetScheme(),
+		StartRateLimiterSeconds: 1,
+		MaxRateLimiterSeconds:   600, // 10 Minutes
 	}).SetupWithManager(mgr); err != nil {
 		setupLog.Error(err, "unable to create controller", "controller", "DrupalSite")
 		os.Exit(1)
-- 
GitLab


From b73609688c243c60f3995c388575671ff167c36c Mon Sep 17 00:00:00 2001
From: Konstantinos Samaras-Tsakiris <konstantinos.samaras-tsakiris@cern.ch>
Date: Wed, 29 Sep 2021 11:39:56 +0200
Subject: [PATCH 2/3] Fix max time representation

---
 controllers/drupalsite_controller.go | 5 ++++-
 main.go                              | 4 ++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/controllers/drupalsite_controller.go b/controllers/drupalsite_controller.go
index b2b85f3e..bb99b5e6 100644
--- a/controllers/drupalsite_controller.go
+++ b/controllers/drupalsite_controller.go
@@ -104,7 +104,10 @@ type DrupalSiteReconciler struct {
 
 // SetupWithManager adds a manager which watches the resources
 func (r *DrupalSiteReconciler) SetupWithManager(mgr ctrl.Manager) error {
-	exponentialFailure := workqueue.NewItemExponentialFailureRateLimiter(time.Second*time.Duration(r.StartRateLimiterSeconds), time.Minute*time.Duration(r.MaxRateLimiterSeconds))
+	exponentialFailure := workqueue.NewItemExponentialFailureRateLimiter(
+		time.Second*time.Duration(r.StartRateLimiterSeconds),
+		time.Second*time.Duration(r.MaxRateLimiterSeconds),
+	)
 	return ctrl.NewControllerManagedBy(mgr).
 		For(&webservicesv1a1.DrupalSite{}).
 		Owns(&appsv1.Deployment{}).
diff --git a/main.go b/main.go
index c348a95f..71888cee 100644
--- a/main.go
+++ b/main.go
@@ -108,8 +108,8 @@ func main() {
 		Client:                  mgr.GetClient(),
 		Log:                     ctrl.Log.WithName("controllers").WithName("DrupalSite"),
 		Scheme:                  mgr.GetScheme(),
-		StartRateLimiterSeconds: 1,
-		MaxRateLimiterSeconds:   600, // 10 Minutes
+		StartRateLimiterSeconds: 0.5,
+		MaxRateLimiterSeconds:   300, // 5 Minutes
 	}).SetupWithManager(mgr); err != nil {
 		setupLog.Error(err, "unable to create controller", "controller", "DrupalSite")
 		os.Exit(1)
-- 
GitLab


From ebc8e1164d0f9b678776bf54ea577e7bd7ac0f1e Mon Sep 17 00:00:00 2001
From: Konstantinos Samaras-Tsakiris <konstantinos.samaras-tsakiris@cern.ch>
Date: Wed, 29 Sep 2021 12:19:51 +0200
Subject: [PATCH 3/3] Fix duration

---
 controllers/drupalsite_controller.go | 10 +++++-----
 main.go                              | 10 +++++-----
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/controllers/drupalsite_controller.go b/controllers/drupalsite_controller.go
index bb99b5e6..6bbf2b3e 100644
--- a/controllers/drupalsite_controller.go
+++ b/controllers/drupalsite_controller.go
@@ -74,10 +74,10 @@ var (
 // DrupalSiteReconciler reconciles a DrupalSite object
 type DrupalSiteReconciler struct {
 	client.Client
-	Log                     logr.Logger
-	Scheme                  *runtime.Scheme
-	MaxRateLimiterSeconds   int
-	StartRateLimiterSeconds int
+	Log                    logr.Logger
+	Scheme                 *runtime.Scheme
+	StartRateLimiterMillis int
+	MaxRateLimiterSeconds  int
 }
 
 // +kubebuilder:rbac:groups=drupal.webservices.cern.ch,resources=drupalsites,verbs=get;list;watch;create;update;patch;delete
@@ -105,7 +105,7 @@ type DrupalSiteReconciler struct {
 // SetupWithManager adds a manager which watches the resources
 func (r *DrupalSiteReconciler) SetupWithManager(mgr ctrl.Manager) error {
 	exponentialFailure := workqueue.NewItemExponentialFailureRateLimiter(
-		time.Second*time.Duration(r.StartRateLimiterSeconds),
+		time.Millisecond*time.Duration(r.StartRateLimiterMillis),
 		time.Second*time.Duration(r.MaxRateLimiterSeconds),
 	)
 	return ctrl.NewControllerManagedBy(mgr).
diff --git a/main.go b/main.go
index 71888cee..7b873112 100644
--- a/main.go
+++ b/main.go
@@ -105,11 +105,11 @@ func main() {
 	}
 
 	if err = (&controllers.DrupalSiteReconciler{
-		Client:                  mgr.GetClient(),
-		Log:                     ctrl.Log.WithName("controllers").WithName("DrupalSite"),
-		Scheme:                  mgr.GetScheme(),
-		StartRateLimiterSeconds: 0.5,
-		MaxRateLimiterSeconds:   300, // 5 Minutes
+		Client:                 mgr.GetClient(),
+		Log:                    ctrl.Log.WithName("controllers").WithName("DrupalSite"),
+		Scheme:                 mgr.GetScheme(),
+		StartRateLimiterMillis: 500,
+		MaxRateLimiterSeconds:  300, // 5 Minutes
 	}).SetupWithManager(mgr); err != nil {
 		setupLog.Error(err, "unable to create controller", "controller", "DrupalSite")
 		os.Exit(1)
-- 
GitLab