From ef41fea5a0602818919c3dbec8f62789db73357a Mon Sep 17 00:00:00 2001 From: Lukas Hybner Date: Tue, 12 Jul 2022 17:04:18 +0200 Subject: [PATCH] Add support for watching multiple namespaces Signed-off-by: Lukas Hybner --- main.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 27078abe..56828afd 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "flag" "log" "os" + "strings" "github.com/evryfs/github-actions-runner-operator/controllers/githubapi" "github.com/redhat-cop/operator-utils/pkg/util" @@ -28,6 +29,7 @@ import ( clientgoscheme "k8s.io/client-go/kubernetes/scheme" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" @@ -61,16 +63,23 @@ func main() { ctrl.SetLogger(zap.New(zap.UseDevMode(true))) - mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ + namespace := os.Getenv("WATCH_NAMESPACE") + options := ctrl.Options{ Scheme: scheme, MetricsBindAddress: metricsAddr, HealthProbeBindAddress: healthProbeAddr, Port: 9443, - Namespace: os.Getenv("WATCH_NAMESPACE"), + Namespace: namespace, LeaderElection: enableLeaderElection, LeaderElectionID: "4ef9cd91.tietoevry.com", LeaderElectionResourceLock: "configmaps", - }) + } + if strings.Contains(namespace, ",") { + options.Namespace = "" + options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(namespace, ",")) + } + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options) + if err != nil { setupLog.Error(err, "unable to start manager") os.Exit(1)