Skip to content

Commit 62f01be

Browse files
authored
Merge pull request #912 from AntonAleksandrov13/main
Allow to change MaxConcurrentReconciles via MAX_CONCURRENT_RECONCILES env variable
2 parents 8204c1b + a2df60e commit 62f01be

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

controllers/common.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ const (
3434
EnableWebhooksEnvVar = "ENABLE_WEBHOOKS"
3535
ControllerSyncPeriodEnvVar = "SYNC_PERIOD"
3636
ConnectUsingPlainHTTPEnvVar = "CONNECT_USING_PLAIN_HTTP"
37+
MaxConcurrentReconciles = "MAX_CONCURRENT_RECONCILES"
3738
)

controllers/super_stream_controller.go

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package controllers
1212
import (
1313
"context"
1414
"fmt"
15+
"sigs.k8s.io/controller-runtime/pkg/controller"
1516
"strconv"
1617

1718
"github.com/go-logr/logr"
@@ -39,6 +40,7 @@ type SuperStreamReconciler struct {
3940
Recorder record.EventRecorder
4041
RabbitmqClientFactory rabbitmqclient.Factory
4142
KubernetesClusterDomain string
43+
MaxConcurrentReconciles int
4244
}
4345

4446
// +kubebuilder:rbac:groups=rabbitmq.com,resources=exchanges,verbs=get;create;update;patch;delete
@@ -205,5 +207,6 @@ func (r *SuperStreamReconciler) SetupWithManager(mgr ctrl.Manager) error {
205207
Owns(&topology.Exchange{}).
206208
Owns(&topology.Binding{}).
207209
Owns(&topology.Queue{}).
210+
WithOptions(controller.Options{MaxConcurrentReconciles: r.MaxConcurrentReconciles}).
208211
Complete(r)
209212
}

controllers/topology_controller.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"reflect"
1818
ctrl "sigs.k8s.io/controller-runtime"
1919
"sigs.k8s.io/controller-runtime/pkg/client"
20+
"sigs.k8s.io/controller-runtime/pkg/controller"
2021
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2122
"strings"
2223
"time"
@@ -34,6 +35,7 @@ type TopologyReconciler struct {
3435
RabbitmqClientFactory rabbitmqclient.Factory
3536
KubernetesClusterDomain string
3637
ConnectUsingPlainHTTP bool
38+
MaxConcurrentReconciles int
3739
}
3840

3941
func (r *TopologyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
@@ -222,9 +224,12 @@ func (r *TopologyReconciler) SetupWithManager(mgr ctrl.Manager) error {
222224
if len(r.WatchTypes) == 0 {
223225
return ctrl.NewControllerManagedBy(mgr).
224226
For(r.Type).
227+
WithOptions(controller.Options{MaxConcurrentReconciles: r.MaxConcurrentReconciles}).
225228
Complete(r)
226229
}
227-
builder := ctrl.NewControllerManagedBy(mgr).For(r.Type)
230+
builder := ctrl.NewControllerManagedBy(mgr).
231+
For(r.Type).
232+
WithOptions(controller.Options{MaxConcurrentReconciles: r.MaxConcurrentReconciles})
228233
for _, t := range r.WatchTypes {
229234
if err := mgr.GetFieldIndexer().IndexField(context.Background(), t, ownerKey, addResourceToIndex); err != nil {
230235
return err

main.go

+36-5
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ func main() {
160160
managerOpts.RetryPeriod = &retryPeriod
161161
}
162162

163+
var maxConcurrentReconciles int
164+
if maxConcurrentReconcilesEnvValue := getIntEnv(controllers.MaxConcurrentReconciles); maxConcurrentReconcilesEnvValue > 0 {
165+
maxConcurrentReconciles = maxConcurrentReconcilesEnvValue
166+
log.Info(fmt.Sprintf("maxConcurrentReconciles set to %d", maxConcurrentReconciles))
167+
}
168+
163169
if enableDebugPprof, ok := os.LookupEnv("ENABLE_DEBUG_PPROF"); ok {
164170
pprofEnabled, err := strconv.ParseBool(enableDebugPprof)
165171
if err == nil && pprofEnabled {
@@ -187,6 +193,7 @@ func main() {
187193
KubernetesClusterDomain: clusterDomain,
188194
ReconcileFunc: &controllers.QueueReconciler{},
189195
ConnectUsingPlainHTTP: usePlainHTTP,
196+
MaxConcurrentReconciles: maxConcurrentReconciles,
190197
}).SetupWithManager(mgr); err != nil {
191198
log.Error(err, "unable to create controller", "controller", controllers.QueueControllerName)
192199
os.Exit(1)
@@ -202,6 +209,7 @@ func main() {
202209
KubernetesClusterDomain: clusterDomain,
203210
ReconcileFunc: &controllers.ExchangeReconciler{},
204211
ConnectUsingPlainHTTP: usePlainHTTP,
212+
MaxConcurrentReconciles: maxConcurrentReconciles,
205213
}).SetupWithManager(mgr); err != nil {
206214
log.Error(err, "unable to create controller", "controller", controllers.ExchangeControllerName)
207215
os.Exit(1)
@@ -217,6 +225,7 @@ func main() {
217225
KubernetesClusterDomain: clusterDomain,
218226
ReconcileFunc: &controllers.BindingReconciler{},
219227
ConnectUsingPlainHTTP: usePlainHTTP,
228+
MaxConcurrentReconciles: maxConcurrentReconciles,
220229
}).SetupWithManager(mgr); err != nil {
221230
log.Error(err, "unable to create controller", "controller", controllers.BindingControllerName)
222231
os.Exit(1)
@@ -233,6 +242,7 @@ func main() {
233242
WatchTypes: []client.Object{},
234243
ReconcileFunc: &controllers.UserReconciler{Client: mgr.GetClient(), Scheme: mgr.GetScheme()},
235244
ConnectUsingPlainHTTP: usePlainHTTP,
245+
MaxConcurrentReconciles: maxConcurrentReconciles,
236246
}).SetupWithManager(mgr); err != nil {
237247
log.Error(err, "unable to create controller", "controller", controllers.UserControllerName)
238248
os.Exit(1)
@@ -248,6 +258,7 @@ func main() {
248258
KubernetesClusterDomain: clusterDomain,
249259
ReconcileFunc: &controllers.VhostReconciler{Client: mgr.GetClient()},
250260
ConnectUsingPlainHTTP: usePlainHTTP,
261+
MaxConcurrentReconciles: maxConcurrentReconciles,
251262
}).SetupWithManager(mgr); err != nil {
252263
log.Error(err, "unable to create controller", "controller", controllers.VhostControllerName)
253264
os.Exit(1)
@@ -263,6 +274,7 @@ func main() {
263274
KubernetesClusterDomain: clusterDomain,
264275
ReconcileFunc: &controllers.PolicyReconciler{},
265276
ConnectUsingPlainHTTP: usePlainHTTP,
277+
MaxConcurrentReconciles: maxConcurrentReconciles,
266278
}).SetupWithManager(mgr); err != nil {
267279
log.Error(err, "unable to create controller", "controller", controllers.PolicyControllerName)
268280
os.Exit(1)
@@ -278,6 +290,7 @@ func main() {
278290
KubernetesClusterDomain: clusterDomain,
279291
ReconcileFunc: &controllers.OperatorPolicyReconciler{},
280292
ConnectUsingPlainHTTP: usePlainHTTP,
293+
MaxConcurrentReconciles: maxConcurrentReconciles,
281294
}).SetupWithManager(mgr); err != nil {
282295
log.Error(err, "unable to create controller", "controller", controllers.OperatorPolicyControllerName)
283296
os.Exit(1)
@@ -293,6 +306,7 @@ func main() {
293306
KubernetesClusterDomain: clusterDomain,
294307
ReconcileFunc: &controllers.PermissionReconciler{Client: mgr.GetClient(), Scheme: mgr.GetScheme()},
295308
ConnectUsingPlainHTTP: usePlainHTTP,
309+
MaxConcurrentReconciles: maxConcurrentReconciles,
296310
}).SetupWithManager(mgr); err != nil {
297311
log.Error(err, "unable to create controller", "controller", controllers.PermissionControllerName)
298312
os.Exit(1)
@@ -308,6 +322,7 @@ func main() {
308322
KubernetesClusterDomain: clusterDomain,
309323
ReconcileFunc: &controllers.SchemaReplicationReconciler{Client: mgr.GetClient()},
310324
ConnectUsingPlainHTTP: usePlainHTTP,
325+
MaxConcurrentReconciles: maxConcurrentReconciles,
311326
}).SetupWithManager(mgr); err != nil {
312327
log.Error(err, "unable to create controller", "controller", controllers.SchemaReplicationControllerName)
313328
os.Exit(1)
@@ -323,6 +338,7 @@ func main() {
323338
KubernetesClusterDomain: clusterDomain,
324339
ReconcileFunc: &controllers.FederationReconciler{Client: mgr.GetClient()},
325340
ConnectUsingPlainHTTP: usePlainHTTP,
341+
MaxConcurrentReconciles: maxConcurrentReconciles,
326342
}).SetupWithManager(mgr); err != nil {
327343
log.Error(err, "unable to create controller", "controller", controllers.FederationControllerName)
328344
os.Exit(1)
@@ -338,6 +354,7 @@ func main() {
338354
KubernetesClusterDomain: clusterDomain,
339355
ReconcileFunc: &controllers.ShovelReconciler{Client: mgr.GetClient()},
340356
ConnectUsingPlainHTTP: usePlainHTTP,
357+
MaxConcurrentReconciles: maxConcurrentReconciles,
341358
}).SetupWithManager(mgr); err != nil {
342359
log.Error(err, "unable to create controller", "controller", controllers.ShovelControllerName)
343360
os.Exit(1)
@@ -353,17 +370,19 @@ func main() {
353370
KubernetesClusterDomain: clusterDomain,
354371
ReconcileFunc: &controllers.TopicPermissionReconciler{Client: mgr.GetClient(), Scheme: mgr.GetScheme()},
355372
ConnectUsingPlainHTTP: usePlainHTTP,
373+
MaxConcurrentReconciles: maxConcurrentReconciles,
356374
}).SetupWithManager(mgr); err != nil {
357375
log.Error(err, "unable to create controller", "controller", controllers.TopicPermissionControllerName)
358376
os.Exit(1)
359377
}
360378

361379
if err = (&controllers.SuperStreamReconciler{
362-
Client: mgr.GetClient(),
363-
Log: ctrl.Log.WithName(controllers.SuperStreamControllerName),
364-
Scheme: mgr.GetScheme(),
365-
Recorder: mgr.GetEventRecorderFor(controllers.SuperStreamControllerName),
366-
RabbitmqClientFactory: rabbitmqclient.RabbitholeClientFactory,
380+
Client: mgr.GetClient(),
381+
Log: ctrl.Log.WithName(controllers.SuperStreamControllerName),
382+
Scheme: mgr.GetScheme(),
383+
Recorder: mgr.GetEventRecorderFor(controllers.SuperStreamControllerName),
384+
RabbitmqClientFactory: rabbitmqclient.RabbitholeClientFactory,
385+
MaxConcurrentReconciles: maxConcurrentReconciles,
367386
}).SetupWithManager(mgr); err != nil {
368387
log.Error(err, "unable to create controller", "controller", controllers.SuperStreamControllerName)
369388
os.Exit(1)
@@ -456,3 +475,15 @@ func getBoolEnv(envName string) bool {
456475
}
457476
return boolVar
458477
}
478+
479+
func getIntEnv(envName string) int {
480+
var intVar int
481+
if initStr, ok := os.LookupEnv(envName); ok {
482+
var err error
483+
if intVar, err = strconv.Atoi(initStr); err != nil {
484+
log.Error(err, fmt.Sprintf("unable to parse provided '%s'", envName))
485+
os.Exit(1)
486+
}
487+
}
488+
return intVar
489+
}

0 commit comments

Comments
 (0)