Skip to content

Commit

Permalink
Watch Multiple Namespaces (#240)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AdheipSingh authored May 16, 2022
1 parent 64e26a5 commit 27c0fde
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 20 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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() {
Expand Down Expand Up @@ -50,6 +53,7 @@ func main() {
LeaderElection: enableLeaderElection,
LeaderElectionID: "e6946145.apache.org",
Namespace: os.Getenv("WATCH_NAMESPACE"),
NewCache: watchNamespaceCache(),
})

if err != nil {
Expand Down Expand Up @@ -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
}

0 comments on commit 27c0fde

Please sign in to comment.