diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index 36aa9309..70b05722 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -32,6 +32,8 @@ spec: valueFrom: fieldRef: fieldPath: metadata.namespace + - name: MAX_CONCURRENT_RECONCILES + value: 1 image: controller:latest name: manager resources: diff --git a/controllers/binding_controller.go b/controllers/binding_controller.go index 90dbe223..5c3ba454 100644 --- a/controllers/binding_controller.go +++ b/controllers/binding_controller.go @@ -39,6 +39,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" ) const ( @@ -81,8 +82,9 @@ type ControllerReferenceSetter func(owner, controlled metav1.Object, scheme *run type IBMCloudInfoGetter func(logt logr.Logger, r client.Client, instance *ibmcloudv1.Service) (*ibmcloud.Info, error) -func (r *BindingReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *BindingReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error { return ctrl.NewControllerManagedBy(mgr). + WithOptions(options). For(&ibmcloudv1.Binding{}). Complete(r) } diff --git a/controllers/manager_setup.go b/controllers/manager_setup.go index 5fb93e79..7a78add7 100644 --- a/controllers/manager_setup.go +++ b/controllers/manager_setup.go @@ -3,6 +3,7 @@ package controllers import ( "net/http" + "github.com/ibm/cloud-operators/internal/config" "github.com/ibm/cloud-operators/internal/ibmcloud" "github.com/ibm/cloud-operators/internal/ibmcloud/auth" "github.com/ibm/cloud-operators/internal/ibmcloud/cfservice" @@ -10,6 +11,7 @@ import ( "github.com/ibm/cloud-operators/internal/ibmcloud/resource" "github.com/pkg/errors" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) @@ -22,13 +24,17 @@ type Controllers struct { func SetUpControllers(mgr ctrl.Manager) (*Controllers, error) { c := setUpControllerDependencies(mgr) - if err := c.BindingReconciler.SetupWithManager(mgr); err != nil { + + options := controller.Options{ + MaxConcurrentReconciles: config.Get().MaxConcurrentReconciles, + } + if err := c.BindingReconciler.SetupWithManager(mgr, options); err != nil { return nil, errors.Wrap(err, "Unable to setup binding controller") } - if err := c.ServiceReconciler.SetupWithManager(mgr); err != nil { + if err := c.ServiceReconciler.SetupWithManager(mgr, options); err != nil { return nil, errors.Wrap(err, "Unable to setup service controller") } - if err := c.TokenReconciler.SetupWithManager(mgr); err != nil { + if err := c.TokenReconciler.SetupWithManager(mgr, options); err != nil { return nil, errors.Wrap(err, "Unable to setup token controller") } diff --git a/controllers/service_controller.go b/controllers/service_controller.go index a1d231ea..2e5c00c5 100644 --- a/controllers/service_controller.go +++ b/controllers/service_controller.go @@ -30,6 +30,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller" ibmcloudv1 "github.com/ibm/cloud-operators/api/v1" "github.com/ibm/cloud-operators/internal/config" @@ -69,8 +70,9 @@ type ServiceReconciler struct { UpdateResourceServiceInstance resource.ServiceInstanceUpdater } -func (r *ServiceReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *ServiceReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error { return ctrl.NewControllerManagedBy(mgr). + WithOptions(options). For(&ibmcloudv1.Service{}). Complete(r) } diff --git a/controllers/token_controller.go b/controllers/token_controller.go index 3b50dca6..fb6a35ea 100644 --- a/controllers/token_controller.go +++ b/controllers/token_controller.go @@ -29,6 +29,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" 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/event" "sigs.k8s.io/controller-runtime/pkg/predicate" ) @@ -123,8 +124,9 @@ func (r *TokenReconciler) Reconcile(request ctrl.Request) (ctrl.Result, error) { return ctrl.Result{RequeueAfter: 10 * time.Minute}, nil } -func (r *TokenReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *TokenReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error { return ctrl.NewControllerManagedBy(mgr). + WithOptions(options). For(&corev1.Secret{}). WithEventFilter(predicate.Funcs{ CreateFunc: func(e event.CreateEvent) bool { return shouldProcessSecret(e.Meta) }, diff --git a/internal/config/config.go b/internal/config/config.go index ee5bce41..138b2a52 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -13,20 +13,22 @@ var ( ) type Config struct { - APIKey string `envconfig:"bluemix_api_key"` - AccountID string `envconfig:"bluemix_account_id"` - ControllerNamespace string `envconfig:"controller_namespace"` - Org string `envconfig:"bluemix_org"` - Region string `envconfig:"bluemix_region"` - ResourceGroupName string `envconfig:"bluemix_resource_group"` - Space string `envconfig:"bluemix_space"` - SyncPeriod time.Duration `envconfig:"sync_period"` + APIKey string `envconfig:"bluemix_api_key"` + AccountID string `envconfig:"bluemix_account_id"` + ControllerNamespace string `envconfig:"controller_namespace"` + MaxConcurrentReconciles int `envconfig:"max_concurrent_reconciles"` + Org string `envconfig:"bluemix_org"` + Region string `envconfig:"bluemix_region"` + ResourceGroupName string `envconfig:"bluemix_resource_group"` + Space string `envconfig:"bluemix_space"` + SyncPeriod time.Duration `envconfig:"sync_period"` } func Get() Config { loadOnce.Do(func() { config = Config{ // default values - SyncPeriod: 150 * time.Second, + MaxConcurrentReconciles: 1, + SyncPeriod: 150 * time.Second, } envconfig.MustProcess("", &config) })