Skip to content

Commit ebe5a3c

Browse files
committed
Flags for namespaces to watch and max concurrent reconciles.
1 parent 9c6da16 commit ebe5a3c

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ Work in progress.
99
Name "kubedb" clashes with an open-source project: https://github.com/kubedb
1010
We're going to have naming "clashes" (not technical, but on human level) if we ever decide to use that one.
1111

12+
TODO: Write proper README file.
13+
14+
Config options:
15+
16+
Argument | Default value | Example | Purpose |
17+
---------|---------------|---------|---------|
18+
--metrics-addr | :8080 | :8080 | Metrics port |
19+
--enable-leader-election | false | true | Enable leader election |
20+
--namespaces | "" | devops,default | Namespaces to watch. Default: watch all namespaces |
21+
--max-concurrent-reconciles | 1 | 5 | Maximum concurrent reconciles. This config covers all controllers. TODO maybe have a separate flag for each controller? |
1222

1323

1424

controllers/mongodbdatabase_controller.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"k8s.io/apimachinery/pkg/runtime"
2828
ctrl "sigs.k8s.io/controller-runtime"
2929
"sigs.k8s.io/controller-runtime/pkg/client"
30+
"sigs.k8s.io/controller-runtime/pkg/controller"
3031
)
3132

3233
// MongoDBDatabaseReconciler reconciles a MongoDBDatabase object
@@ -133,10 +134,12 @@ func (r *MongoDBDatabaseReconciler) Reconcile(req ctrl.Request) (ctrl.Result, er
133134
return r.updateAndReturn(&ctx, &database, &log)
134135
}
135136

136-
func (r *MongoDBDatabaseReconciler) SetupWithManager(mgr ctrl.Manager) error {
137+
func (r *MongoDBDatabaseReconciler) SetupWithManager(mgr ctrl.Manager, maxConcurrentReconciles int) error {
137138
return ctrl.NewControllerManagedBy(mgr).
138139
For(&infrav1beta1.MongoDBDatabase{}).
139-
Complete(r)
140+
WithOptions(controller.Options{
141+
MaxConcurrentReconciles: maxConcurrentReconciles,
142+
}).Complete(r)
140143
}
141144

142145
func (r *MongoDBDatabaseReconciler) updateAndReturn(ctx *context.Context, database *infrav1beta1.MongoDBDatabase, log *logr.Logger) (ctrl.Result, error) {

controllers/postgresqldatabase_controller.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"k8s.io/apimachinery/pkg/runtime"
2828
ctrl "sigs.k8s.io/controller-runtime"
2929
"sigs.k8s.io/controller-runtime/pkg/client"
30+
"sigs.k8s.io/controller-runtime/pkg/controller"
3031
)
3132

3233
// PostgreSQLDatabaseReconciler reconciles a PostgreSQLDatabase object
@@ -144,10 +145,12 @@ func (r *PostgreSQLDatabaseReconciler) Reconcile(req ctrl.Request) (ctrl.Result,
144145
return r.updateAndReturn(&ctx, &database, &log)
145146
}
146147

147-
func (r *PostgreSQLDatabaseReconciler) SetupWithManager(mgr ctrl.Manager) error {
148+
func (r *PostgreSQLDatabaseReconciler) SetupWithManager(mgr ctrl.Manager, maxConcurrentReconciles int) error {
148149
return ctrl.NewControllerManagedBy(mgr).
149150
For(&infrav1beta1.PostgreSQLDatabase{}).
150-
Complete(r)
151+
WithOptions(controller.Options{
152+
MaxConcurrentReconciles: maxConcurrentReconciles,
153+
}).Complete(r)
151154
}
152155

153156
func (r *PostgreSQLDatabaseReconciler) updateAndReturn(ctx *context.Context, database *infrav1beta1.PostgreSQLDatabase, log *logr.Logger) (ctrl.Result, error) {

main.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
mongodbAPI "github.com/doodlescheduling/kubedb/common/db/mongodb"
2222
"github.com/doodlescheduling/kubedb/common/vault"
2323
"os"
24+
"sigs.k8s.io/controller-runtime/pkg/cache"
2425
"sigs.k8s.io/controller-runtime/pkg/healthz"
26+
"strings"
2527

2628
postgresqlAPI "github.com/doodlescheduling/kubedb/common/db/postgresql"
2729
"k8s.io/apimachinery/pkg/runtime"
@@ -50,21 +52,34 @@ func init() {
5052
func main() {
5153
var metricsAddr string
5254
var enableLeaderElection bool
55+
var namespacesConfig string
56+
var maxConcurrentReconciles int
5357
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
5458
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
5559
"Enable leader election for controller manager. "+
5660
"Enabling this will ensure there is only one active controller manager.")
61+
flag.StringVar(&namespacesConfig, "namespaces", "", "Comma-separated list of namespaces to watch. If not set, all namespaces will be watched.")
62+
flag.IntVar(&maxConcurrentReconciles, "max-concurrent-reconciles", 1, "Maximum number of concurrent reconciles. Default is 1.")
5763
flag.Parse()
5864

5965
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
6066

61-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
67+
namespaces := strings.Split(namespacesConfig, ",")
68+
options := ctrl.Options{
6269
Scheme: scheme,
6370
MetricsBindAddress: metricsAddr,
6471
Port: 9443,
6572
LeaderElection: enableLeaderElection,
6673
LeaderElectionID: "99a96989.doodle.com",
67-
})
74+
}
75+
if len(namespaces) > 0 && namespaces[0] != "" {
76+
options.NewCache = cache.MultiNamespacedCacheBuilder(namespaces)
77+
setupLog.Info("watching configured namespaces", "namespaces", namespaces)
78+
} else {
79+
setupLog.Info("watching all namespaces")
80+
}
81+
82+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
6883
if err != nil {
6984
setupLog.Error(err, "unable to start manager")
7085
os.Exit(1)
@@ -93,7 +108,7 @@ func main() {
93108
Scheme: mgr.GetScheme(),
94109
ServerCache: mongoDBServerCache,
95110
VaultCache: vaultCache,
96-
}).SetupWithManager(mgr); err != nil {
111+
}).SetupWithManager(mgr, maxConcurrentReconciles); err != nil {
97112
setupLog.Error(err, "unable to create controller", "controller", "MongoDBDatabase")
98113
os.Exit(1)
99114
}
@@ -106,7 +121,7 @@ func main() {
106121
Scheme: mgr.GetScheme(),
107122
ServerCache: postgreSQLServerCache,
108123
VaultCache: vaultCache,
109-
}).SetupWithManager(mgr); err != nil {
124+
}).SetupWithManager(mgr, maxConcurrentReconciles); err != nil {
110125
setupLog.Error(err, "unable to create controller", "controller", "PostgreSQLDatabase")
111126
os.Exit(1)
112127
}

0 commit comments

Comments
 (0)