From 51605feaffea4a53706e466cebb53a5e51a9d253 Mon Sep 17 00:00:00 2001
From: Jayesh Srivastava <jayesh0200@gmail.com>
Date: Sun, 31 Mar 2024 14:02:07 +0530
Subject: [PATCH] Support watchNamespace for controller

---
 main.go | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/main.go b/main.go
index 4b3dbfb..b246f0b 100644
--- a/main.go
+++ b/main.go
@@ -58,11 +58,14 @@ func main() {
 	var metricsAddr string
 	var enableLeaderElection bool
 	var probeAddr string
+	var watchNamespace string
 	flag.StringVar(&metricsAddr, "metrics-bind-address", ":8082", "The address the metric endpoint binds to.")
 	flag.StringVar(&probeAddr, "health-probe-bind-address", ":8083", "The address the probe endpoint binds to.")
 	flag.BoolVar(&enableLeaderElection, "leader-elect", false,
 		"Enable leader election for controller manager. "+
 			"Enabling this will ensure there is only one active controller manager.")
+	flag.StringVar(&watchNamespace, "namespace", "", "Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.")
+
 	opts := zap.Options{
 		Development: true,
 	}
@@ -71,6 +74,10 @@ func main() {
 
 	ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
 
+	if watchNamespace != "" {
+		setupLog.Info("Watching cluster-api objects only in namespace for reconciliation\", \"namespace", watchNamespace)
+	}
+
 	mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
 		Scheme:                 scheme,
 		MetricsBindAddress:     metricsAddr,
@@ -78,6 +85,7 @@ func main() {
 		HealthProbeBindAddress: probeAddr,
 		LeaderElection:         enableLeaderElection,
 		LeaderElectionID:       "microk8s-control-plane-manager-leader-election-capi",
+		Namespace:              watchNamespace,
 	})
 	if err != nil {
 		setupLog.Error(err, "unable to start manager")