From 27c0fdee7b173094e7aa78ed94102d58c77826f5 Mon Sep 17 00:00:00 2001 From: AdheipSingh <34169002+AdheipSingh@users.noreply.github.com> Date: Mon, 16 May 2022 14:51:55 +0530 Subject: [PATCH] Watch Multiple Namespaces (#240) * WIP: on druid k8s extension, do, and watch ns * support for multiple namespaces to watch * support for multiple namespaces to watch * fix typo * fix identation --- chart/values.yaml | 2 +- main.go | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/chart/values.yaml b/chart/values.yaml index 94123674..b458685d 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -5,7 +5,7 @@ env: DENY_LIST: "default,kube-system" # Comma-separated list of namespaces to ignore RECONCILE_WAIT: "10s" # Reconciliation delay - WATCH_NAMESPACE: "" # Namespace to watch or empty string to watch all namespaces + WATCH_NAMESPACE: "" # Namespace to watch or empty string to watch all namespaces, To watch multiple namespaces add , into string. Ex: WATCH_NAMESPACE: "ns1,ns2,ns3" replicaCount: 1 diff --git a/main.go b/main.go index 36555d18..efa2dae5 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,13 @@ package main import ( "flag" "os" + "strings" "k8s.io/apimachinery/pkg/runtime" 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" @@ -17,8 +19,9 @@ import ( ) var ( - scheme = runtime.NewScheme() - setupLog = ctrl.Log.WithName("setup") + scheme = runtime.NewScheme() + setupLog = ctrl.Log.WithName("setup") + watchNamespace = os.Getenv("WATCH_NAMESPACE") ) func init() { @@ -50,6 +53,7 @@ func main() { LeaderElection: enableLeaderElection, LeaderElectionID: "e6946145.apache.org", Namespace: os.Getenv("WATCH_NAMESPACE"), + NewCache: watchNamespaceCache(), }) if err != nil { @@ -79,3 +83,17 @@ func main() { os.Exit(1) } } + +func watchNamespaceCache() cache.NewCacheFunc { + var managerWatchCache cache.NewCacheFunc + if watchNamespace != "" { + ns := strings.Split(watchNamespace, ",") + for i := range ns { + ns[i] = strings.TrimSpace(ns[i]) + } + managerWatchCache = cache.MultiNamespacedCacheBuilder(ns) + return managerWatchCache + } + managerWatchCache = (cache.NewCacheFunc)(nil) + return managerWatchCache +}