Skip to content

Commit dc9d21b

Browse files
committed
Move controller concurrency parsing to main
1 parent f53b1ca commit dc9d21b

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

cmd/postgres-operator/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"net/http"
2222
"os"
23+
"strconv"
2324
"strings"
2425
"time"
2526

@@ -40,6 +41,7 @@ import (
4041
"github.com/crunchydata/postgres-operator/internal/registration"
4142
"github.com/crunchydata/postgres-operator/internal/upgradecheck"
4243
"github.com/crunchydata/postgres-operator/internal/util"
44+
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
4345
)
4446

4547
var versionString string
@@ -66,6 +68,8 @@ func initLogging() {
6668
//+kubebuilder:rbac:groups="coordination.k8s.io",resources="leases",verbs={get,create,update}
6769

6870
func initManager() (runtime.Options, error) {
71+
log := logging.FromContext(context.Background())
72+
6973
options := runtime.Options{}
7074
options.Cache.SyncPeriod = initialize.Pointer(time.Hour)
7175

@@ -86,6 +90,18 @@ func initManager() (runtime.Options, error) {
8690
options.Cache.DefaultNamespaces = map[string]runtime.CacheConfig{namespace: {}}
8791
}
8892

93+
options.Controller.GroupKindConcurrency = map[string]int{
94+
"PostgresCluster." + v1beta1.GroupVersion.Group: 2,
95+
}
96+
97+
if s := os.Getenv("PGO_WORKERS"); s != "" {
98+
if i, err := strconv.Atoi(s); err == nil && i > 0 {
99+
options.Controller.GroupKindConcurrency["PostgresCluster."+v1beta1.GroupVersion.Group] = i
100+
} else {
101+
log.Error(err, "PGO_WORKERS must be a positive number")
102+
}
103+
}
104+
89105
return options, nil
90106
}
91107

cmd/postgres-operator/main_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
package main
1717

1818
import (
19+
"reflect"
1920
"testing"
2021
"time"
2122

@@ -32,8 +33,21 @@ func TestInitManager(t *testing.T) {
3233
assert.Equal(t, *options.Cache.SyncPeriod, time.Hour)
3334
}
3435

36+
assert.DeepEqual(t, options.Controller.GroupKindConcurrency,
37+
map[string]int{
38+
"PostgresCluster.postgres-operator.crunchydata.com": 2,
39+
})
40+
3541
assert.Assert(t, options.Cache.DefaultNamespaces == nil)
3642
assert.Assert(t, options.LeaderElection == false)
43+
44+
{
45+
options.Cache.SyncPeriod = nil
46+
options.Controller.GroupKindConcurrency = nil
47+
48+
assert.Assert(t, reflect.ValueOf(options).IsZero(),
49+
"expected remaining fields to be unset:\n%+v", options)
50+
}
3751
})
3852

3953
t.Run("PGO_CONTROLLER_LEASE_NAME", func(t *testing.T) {
@@ -73,4 +87,30 @@ func TestInitManager(t *testing.T) {
7387
assert.Equal(t, k, "some-such")
7488
}
7589
})
90+
91+
t.Run("PGO_WORKERS", func(t *testing.T) {
92+
t.Run("Invalid", func(t *testing.T) {
93+
for _, v := range []string{"-3", "0", "3.14"} {
94+
t.Setenv("PGO_WORKERS", v)
95+
96+
options, err := initManager()
97+
assert.NilError(t, err)
98+
assert.DeepEqual(t, options.Controller.GroupKindConcurrency,
99+
map[string]int{
100+
"PostgresCluster.postgres-operator.crunchydata.com": 2,
101+
})
102+
}
103+
})
104+
105+
t.Run("Valid", func(t *testing.T) {
106+
t.Setenv("PGO_WORKERS", "19")
107+
108+
options, err := initManager()
109+
assert.NilError(t, err)
110+
assert.DeepEqual(t, options.Controller.GroupKindConcurrency,
111+
map[string]int{
112+
"PostgresCluster.postgres-operator.crunchydata.com": 19,
113+
})
114+
})
115+
})
76116
}

internal/controller/postgrescluster/controller.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"context"
2020
"fmt"
2121
"io"
22-
"os"
23-
"strconv"
2422

2523
"github.com/pkg/errors"
2624
"go.opentelemetry.io/otel/trace"
@@ -36,7 +34,6 @@ import (
3634
"k8s.io/client-go/tools/record"
3735
"sigs.k8s.io/controller-runtime/pkg/builder"
3836
"sigs.k8s.io/controller-runtime/pkg/client"
39-
"sigs.k8s.io/controller-runtime/pkg/controller"
4037
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
4138
"sigs.k8s.io/controller-runtime/pkg/manager"
4239
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -457,24 +454,8 @@ func (r *Reconciler) SetupWithManager(mgr manager.Manager) error {
457454
}
458455
}
459456

460-
var opts controller.Options
461-
462-
// TODO(cbandy): Move this to main with controller-runtime v0.9+
463-
// - https://github.com/kubernetes-sigs/controller-runtime/commit/82fc2564cf
464-
if s := os.Getenv("PGO_WORKERS"); s != "" {
465-
if i, err := strconv.Atoi(s); err == nil && i > 0 {
466-
opts.MaxConcurrentReconciles = i
467-
} else {
468-
mgr.GetLogger().Error(err, "PGO_WORKERS must be a positive number")
469-
}
470-
}
471-
if opts.MaxConcurrentReconciles == 0 {
472-
opts.MaxConcurrentReconciles = 2
473-
}
474-
475457
return builder.ControllerManagedBy(mgr).
476458
For(&v1beta1.PostgresCluster{}).
477-
WithOptions(opts).
478459
Owns(&corev1.ConfigMap{}).
479460
Owns(&corev1.Endpoints{}).
480461
Owns(&corev1.PersistentVolumeClaim{}).

0 commit comments

Comments
 (0)