From 1652c15cfdfe9ebb3e98ae6dcd9ccac1f2673e42 Mon Sep 17 00:00:00 2001 From: Andrew Block Date: Thu, 8 Feb 2024 14:58:51 -0600 Subject: [PATCH] Support for accessing resources explicitly in multiple namespaces Signed-off-by: Andrew Block --- main.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 081701ce..ac262f43 100644 --- a/main.go +++ b/main.go @@ -18,11 +18,14 @@ package main import ( "flag" - v1 "k8s.io/api/core/v1" "os" - "sigs.k8s.io/controller-runtime/pkg/client" + "strings" "time" + v1 "k8s.io/api/core/v1" + "sigs.k8s.io/controller-runtime/pkg/cache" + "sigs.k8s.io/controller-runtime/pkg/client" + userv1 "github.com/openshift/api/user/v1" "github.com/redhat-cop/operator-utils/pkg/util" "k8s.io/apimachinery/pkg/runtime" @@ -86,7 +89,7 @@ func main() { watchNamespace := getWatchNamespace() - mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ + options := ctrl.Options{ Scheme: scheme, ClientDisableCacheFor: []client.Object{&v1.Secret{}}, MetricsBindAddress: metricsAddr, @@ -99,7 +102,15 @@ func main() { RenewDeadline: &renewDeadline, RetryPeriod: &retryPeriod, Namespace: watchNamespace, - }) + } + + if strings.Contains(watchNamespace, ",") { + setupLog.Info("manager set up with multiple namespaces", "namespaces", watchNamespace) + options.Namespace = "" + options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(watchNamespace, ",")) + } + + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options) if err != nil { setupLog.Error(err, "unable to start manager") os.Exit(1)