Skip to content

Commit b2c6f3b

Browse files
Adding option to configure MaxConcurrentReconciles for status controller (#177)
Co-authored-by: Jonathan Innis <jonathan.innis.ji@gmail.com>
1 parent d4fbec4 commit b2c6f3b

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

status/controller.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Controller[T Object] struct {
4141
observedFinalizers sync.Map // map[reconcile.Request]Finalizer
4242
terminatingObjects sync.Map // map[reconcile.Request]Object
4343
emitDeprecatedMetrics bool
44+
maxConcurrentReconciles int
4445
ConditionDuration pmetrics.ObservationMetric
4546
ConditionCount pmetrics.GaugeMetric
4647
ConditionCurrentStatusSeconds pmetrics.GaugeMetric
@@ -57,12 +58,13 @@ type Option struct {
5758
// - operator_status_condition_count
5859
// - operator_termination_current_time_seconds
5960
// - operator_termination_duration_seconds
60-
EmitDeprecatedMetrics bool
61-
MetricLabels []string
62-
GaugeMetricLabels []string
63-
MetricFields map[string]string
64-
GaugeMetricFields map[string]string
65-
HistogramBuckets []float64
61+
EmitDeprecatedMetrics bool
62+
MetricLabels []string
63+
GaugeMetricLabels []string
64+
MetricFields map[string]string
65+
GaugeMetricFields map[string]string
66+
HistogramBuckets []float64
67+
MaxConcurrentReconciles int
6668
}
6769

6870
func EmitDeprecatedMetrics(o *Option) {
@@ -99,6 +101,12 @@ func WithHistogramBuckets(buckets []float64) func(*Option) {
99101
}
100102
}
101103

104+
func WitMaxConcurrentReconciles(m int) func(*Option) {
105+
return func(o *Option) {
106+
o.MaxConcurrentReconciles = m
107+
}
108+
}
109+
102110
func NewController[T Object](client client.Client, eventRecorder record.EventRecorder, opts ...option.Function[Option]) *Controller[T] {
103111
options := option.Resolve(opts...)
104112
obj := reflect.New(reflect.TypeOf(*new(T)).Elem()).Interface().(runtime.Object)
@@ -114,6 +122,7 @@ func NewController[T Object](client client.Client, eventRecorder record.EventRec
114122
kubeClient: client,
115123
eventRecorder: eventRecorder,
116124
emitDeprecatedMetrics: options.EmitDeprecatedMetrics,
125+
maxConcurrentReconciles: lo.Ternary(options.MaxConcurrentReconciles <= 0, 10, options.MaxConcurrentReconciles),
117126
ConditionDuration: conditionDurationMetric(strings.ToLower(gvk.Kind), options.HistogramBuckets, lo.Map(
118127
append(options.MetricLabels, lo.Keys(options.MetricFields)...),
119128
func(k string, _ int) string { return toPrometheusLabel(k) })...),
@@ -144,7 +153,7 @@ func NewController[T Object](client client.Client, eventRecorder record.EventRec
144153
func (c *Controller[T]) Register(_ context.Context, m manager.Manager) error {
145154
return controllerruntime.NewControllerManagedBy(m).
146155
For(object.New[T]()).
147-
WithOptions(controller.Options{MaxConcurrentReconciles: 10}).
156+
WithOptions(controller.Options{MaxConcurrentReconciles: c.maxConcurrentReconciles}).
148157
Named(fmt.Sprintf("operatorpkg.%s.status", strings.ToLower(c.gvk.Kind))).
149158
Complete(c)
150159
}
@@ -166,7 +175,7 @@ func NewGenericObjectController[T client.Object](client client.Client, eventReco
166175
func (c *GenericObjectController[T]) Register(_ context.Context, m manager.Manager) error {
167176
return controllerruntime.NewControllerManagedBy(m).
168177
For(object.New[T]()).
169-
WithOptions(controller.Options{MaxConcurrentReconciles: 10}).
178+
WithOptions(controller.Options{MaxConcurrentReconciles: c.maxConcurrentReconciles}).
170179
Named(fmt.Sprintf("operatorpkg.%s.status", strings.ToLower(reflect.TypeOf(object.New[T]()).Elem().Name()))).
171180
Complete(c)
172181
}

0 commit comments

Comments
 (0)