From 9f30e7f865b05404277377ea1069a981bd79e3ce Mon Sep 17 00:00:00 2001 From: Vignesh Goutham Ganesh Date: Thu, 1 Feb 2024 11:52:41 -0600 Subject: [PATCH] Make affinity group and failure domain controller run with multiple workers --- .../cloudstackaffinitygroup_controller.go | 4 +++- ...cloudstackaffinitygroup_controller_test.go | 3 ++- .../cloudstackcluster_controller_test.go | 6 ++--- .../cloudstackfailuredomain_controller.go | 9 +++++--- ...cloudstackfailuredomain_controller_test.go | 5 +++-- main.go | 22 +++++++++++++++---- 6 files changed, 35 insertions(+), 14 deletions(-) diff --git a/controllers/cloudstackaffinitygroup_controller.go b/controllers/cloudstackaffinitygroup_controller.go index d7617803..61acc115 100644 --- a/controllers/cloudstackaffinitygroup_controller.go +++ b/controllers/cloudstackaffinitygroup_controller.go @@ -20,6 +20,7 @@ import ( "context" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3" @@ -89,8 +90,9 @@ func (r *CloudStackAGReconciliationRunner) ReconcileDelete() (ctrl.Result, error } // SetupWithManager sets up the controller with the Manager. -func (reconciler *CloudStackAffinityGroupReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (reconciler *CloudStackAffinityGroupReconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error { return ctrl.NewControllerManagedBy(mgr). + WithOptions(opts). For(&infrav1.CloudStackAffinityGroup{}). Complete(reconciler) } diff --git a/controllers/cloudstackaffinitygroup_controller_test.go b/controllers/cloudstackaffinitygroup_controller_test.go index d04bdd3a..c6e19582 100644 --- a/controllers/cloudstackaffinitygroup_controller_test.go +++ b/controllers/cloudstackaffinitygroup_controller_test.go @@ -23,13 +23,14 @@ import ( infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3" dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta3" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller" ) var _ = Describe("CloudStackAffinityGroupReconciler", func() { BeforeEach(func() { SetupTestEnvironment() // Must happen before setting up managers/reconcilers. dummies.SetDummyVars() - Ω(AffinityGReconciler.SetupWithManager(k8sManager)).Should(Succeed()) // Register CloudStack AffinityGReconciler. + Ω(AffinityGReconciler.SetupWithManager(k8sManager, controller.Options{})).Should(Succeed()) // Register CloudStack AffinityGReconciler. }) It("Should patch back the affinity group as ready after calling GetOrCreateAffinityGroup.", func() { diff --git a/controllers/cloudstackcluster_controller_test.go b/controllers/cloudstackcluster_controller_test.go index b5d71f5a..caadc10b 100644 --- a/controllers/cloudstackcluster_controller_test.go +++ b/controllers/cloudstackcluster_controller_test.go @@ -30,9 +30,9 @@ import ( var _ = Describe("CloudStackClusterReconciler", func() { Context("With k8s like test environment.", func() { BeforeEach(func() { - SetupTestEnvironment() // Must happen before setting up managers/reconcilers. - Ω(ClusterReconciler.SetupWithManager(ctx, k8sManager, controller.Options{})).Should(Succeed()) // Register CloudStack ClusterReconciler. - Ω(FailureDomainReconciler.SetupWithManager(k8sManager)).Should(Succeed()) // Register CloudStack FailureDomainReconciler. + SetupTestEnvironment() // Must happen before setting up managers/reconcilers. + Ω(ClusterReconciler.SetupWithManager(ctx, k8sManager, controller.Options{})).Should(Succeed()) // Register CloudStack ClusterReconciler. + Ω(FailureDomainReconciler.SetupWithManager(k8sManager, controller.Options{})).Should(Succeed()) // Register CloudStack FailureDomainReconciler. }) It("Should create a CloudStackFailureDomain.", func() { diff --git a/controllers/cloudstackfailuredomain_controller.go b/controllers/cloudstackfailuredomain_controller.go index 9995796b..822d4ef7 100644 --- a/controllers/cloudstackfailuredomain_controller.go +++ b/controllers/cloudstackfailuredomain_controller.go @@ -24,6 +24,7 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 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" "sort" @@ -263,7 +264,9 @@ func (r *CloudStackFailureDomainReconciliationRunner) RemoveFinalizer() (ctrl.Re } // SetupWithManager sets up the controller with the Manager. -func (reconciler *CloudStackFailureDomainReconciler) SetupWithManager(mgr ctrl.Manager) error { - _, err := ctrl.NewControllerManagedBy(mgr).For(&infrav1.CloudStackFailureDomain{}).Build(reconciler) - return err +func (reconciler *CloudStackFailureDomainReconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error { + return ctrl.NewControllerManagedBy(mgr). + WithOptions(opts). + For(&infrav1.CloudStackFailureDomain{}). + Complete(reconciler) } diff --git a/controllers/cloudstackfailuredomain_controller_test.go b/controllers/cloudstackfailuredomain_controller_test.go index 5f6ae243..01adca61 100644 --- a/controllers/cloudstackfailuredomain_controller_test.go +++ b/controllers/cloudstackfailuredomain_controller_test.go @@ -28,14 +28,15 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/util/patch" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller" ) var _ = Describe("CloudStackFailureDomainReconciler", func() { Context("With k8s like test environment.", func() { BeforeEach(func() { dummies.SetDummyVars() - SetupTestEnvironment() // Must happen before setting up managers/reconcilers. - Ω(FailureDomainReconciler.SetupWithManager(k8sManager)).Should(Succeed()) // Register CloudStack FailureDomainReconciler. + SetupTestEnvironment() // Must happen before setting up managers/reconcilers. + Ω(FailureDomainReconciler.SetupWithManager(k8sManager, controller.Options{})).Should(Succeed()) // Register CloudStack FailureDomainReconciler. // Modify failure domain name the same way the cluster controller would. dummies.CSFailureDomain1.Name = dummies.CSFailureDomain1.Name + "-" + dummies.CSCluster.Name diff --git a/main.go b/main.go index 9a21a0d7..e5d632bb 100644 --- a/main.go +++ b/main.go @@ -75,8 +75,10 @@ type managerOpts struct { WatchFilterValue string CertDir string - CloudStackClusterConcurrency int - CloudStackMachineConcurrency int + CloudStackClusterConcurrency int + CloudStackMachineConcurrency int + CloudStackAffinityGroupConcurrency int + CloudStackFailureDomainConcurrency int } func setFlags() *managerOpts { @@ -133,6 +135,18 @@ func setFlags() *managerOpts { 10, "Maximum concurrent reconciles for CloudStackMachine resources", ) + flag.IntVar( + &opts.CloudStackAffinityGroupConcurrency, + "cloudstackaffinitygroup-concurrency", + 5, + "Maximum concurrent reconciles for CloudStackAffinityGroup resources", + ) + flag.IntVar( + &opts.CloudStackFailureDomainConcurrency, + "cloudstackfailuredomain-concurrency", + 5, + "Maximum concurrent reconciles for CloudStackFailureDomain resources", + ) return opts } @@ -222,11 +236,11 @@ func setupReconcilers(ctx context.Context, base utils.ReconcilerBase, opts manag setupLog.Error(err, "unable to create controller", "controller", "CloudStackIsoNetReconciler") os.Exit(1) } - if err := (&controllers.CloudStackAffinityGroupReconciler{ReconcilerBase: base}).SetupWithManager(mgr); err != nil { + if err := (&controllers.CloudStackAffinityGroupReconciler{ReconcilerBase: base}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: opts.CloudStackAffinityGroupConcurrency}); err != nil { setupLog.Error(err, "unable to create controller", "controller", "CloudStackAffinityGroup") os.Exit(1) } - if err := (&controllers.CloudStackFailureDomainReconciler{ReconcilerBase: base}).SetupWithManager(mgr); err != nil { + if err := (&controllers.CloudStackFailureDomainReconciler{ReconcilerBase: base}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: opts.CloudStackFailureDomainConcurrency}); err != nil { setupLog.Error(err, "unable to create controller", "controller", "CloudStackFailureDomain") os.Exit(1) }