diff --git a/cmd/operator/main.go b/cmd/operator/main.go index ecb582881..0fa96392a 100644 --- a/cmd/operator/main.go +++ b/cmd/operator/main.go @@ -19,6 +19,7 @@ import ( "context" "log" "os" + "time" // Allows us to pull in things generated from `go generate` _ "embed" @@ -33,8 +34,10 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth" "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" + "k8s.io/client-go/util/workqueue" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1" + "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/manager" @@ -89,13 +92,19 @@ func addReconcilersToManager(mgr manager.Manager, restCfg *rest.Config) { return } + // Create a custom option with our own rate limiter + rateLimiter := workqueue.NewItemExponentialFailureRateLimiter(1*time.Millisecond, + time.Duration(opcfg.GetVdbMaxBackoffDuration())*time.Millisecond) + options := controller.Options{ + RateLimiter: rateLimiter, + } if err := (&vdb.VerticaDBReconciler{ Client: mgr.GetClient(), Log: ctrl.Log.WithName("controllers").WithName("VerticaDB"), Scheme: mgr.GetScheme(), Cfg: restCfg, EVRec: mgr.GetEventRecorderFor(vmeta.OperatorName), - }).SetupWithManager(mgr); err != nil { + }).SetupWithManager(mgr, options); err != nil { setupLog.Error(err, "unable to create controller", "controller", "VerticaDB") os.Exit(1) } diff --git a/pkg/controllers/vdb/verticadb_controller.go b/pkg/controllers/vdb/verticadb_controller.go index 5da269e2a..0870518b3 100644 --- a/pkg/controllers/vdb/verticadb_controller.go +++ b/pkg/controllers/vdb/verticadb_controller.go @@ -28,7 +28,6 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" - "k8s.io/client-go/util/workqueue" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" @@ -41,7 +40,6 @@ import ( "github.com/vertica/vertica-kubernetes/pkg/events" vmeta "github.com/vertica/vertica-kubernetes/pkg/meta" "github.com/vertica/vertica-kubernetes/pkg/metrics" - "github.com/vertica/vertica-kubernetes/pkg/opcfg" "github.com/vertica/vertica-kubernetes/pkg/podfacts" "github.com/vertica/vertica-kubernetes/pkg/vadmin" "github.com/vertica/vertica-kubernetes/pkg/vk8s" @@ -75,12 +73,9 @@ type VerticaDBReconciler struct { // +kubebuilder:rbac:groups="",resources=events,verbs=create;patch // SetupWithManager sets up the controller with the Manager. -func (r *VerticaDBReconciler) SetupWithManager(mgr ctrl.Manager) error { - rateLimiter := workqueue.NewItemExponentialFailureRateLimiter(1*time.Millisecond, - time.Duration(opcfg.GetVdbMaxBackoffDuration())*time.Millisecond) - +func (r *VerticaDBReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error { return ctrl.NewControllerManagedBy(mgr). - WithOptions(controller.Options{RateLimiter: rateLimiter}). + WithOptions(options). For(&vapi.VerticaDB{}). Owns(&corev1.ServiceAccount{}). Owns(&rbacv1.Role{}). diff --git a/pkg/opcfg/config.go b/pkg/opcfg/config.go index 32134427e..d78854d08 100644 --- a/pkg/opcfg/config.go +++ b/pkg/opcfg/config.go @@ -378,10 +378,15 @@ func getStackTrace() zap.Option { // GetVdbMaxBackoffDuration returns maximum backoff requeue duration in milliseconds for vdb controller func GetVdbMaxBackoffDuration() int { - duration := lookupIntEnvVar("VDB_MAX_BACKOFF_DURATION", envCanNotExist) - if duration == 0 { + const envName = "VDB_MAX_BACKOFF_DURATION" + duration := lookupStringEnvVar(envName, envCanNotExist) + if duration == "" { // Default value is 1000ms return 1000 } - return duration + valInt, err := strconv.Atoi(duration) + if err != nil || valInt < 0 { + dieIfNotValid(envName) + } + return valInt }