Skip to content

Commit 34d67b2

Browse files
controller: restrict manager to watch selective namespaces
Cache resources from namespaces where the operator is deployed or that are specified in the `WATCH_NAMESPACE` env variable. (manual cherry pick from commit 4531319) Signed-off-by: ShravaniVangur <[email protected]>
1 parent 45b3302 commit 34d67b2

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
import (
2020
"flag"
2121
"os"
22+
"strings"
2223

2324
apiv1alpha1 "github.com/red-hat-storage/ocs-client-operator/api/v1alpha1"
2425
"github.com/red-hat-storage/ocs-client-operator/controllers"
@@ -94,6 +95,20 @@ func main() {
9495
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
9596

9697
storageclustersSelector := fields.SelectorFromSet(fields.Set{"metadata.name": "storageclusters.ocs.openshift.io"})
98+
99+
defaultNamespaces := map[string]cache.Config{}
100+
operatorNamespace := utils.GetOperatorNamespace()
101+
defaultNamespaces[operatorNamespace] = cache.Config{}
102+
103+
watchNamespace := utils.GetWatchNamespace()
104+
if watchNamespace == "" {
105+
setupLog.Info("No value for env WATCH_NAMESPACE is set. Manager will only watch for resources in the operator deployed namespace.")
106+
} else {
107+
for _, namespace := range strings.Split(watchNamespace, ",") {
108+
defaultNamespaces[namespace] = cache.Config{}
109+
}
110+
}
111+
97112
subscriptionwebhookSelector := fields.SelectorFromSet(fields.Set{"metadata.name": templates.SubscriptionWebhookName})
98113
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
99114
Scheme: scheme,
@@ -112,6 +127,7 @@ func main() {
112127
Field: subscriptionwebhookSelector,
113128
},
114129
},
130+
DefaultNamespaces: defaultNamespaces,
115131
},
116132
WebhookServer: webhook.NewServer(webhook.Options{
117133
Port: webhookPort,

pkg/utils/k8sutils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import (
2828
// which is the namespace where operator pod is deployed.
2929
const OperatorNamespaceEnvVar = "OPERATOR_NAMESPACE"
3030

31+
// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
32+
// which indicates any other namespace to watch for resources.
33+
const WatchNamespaceEnvVar = "WATCH_NAMESPACE"
34+
3135
// OperatorPodNameEnvVar is the constant for env variable OPERATOR_POD_NAME
3236
const OperatorPodNameEnvVar = "OPERATOR_POD_NAME"
3337

@@ -46,6 +50,10 @@ func GetOperatorNamespace() string {
4650
return os.Getenv(OperatorNamespaceEnvVar)
4751
}
4852

53+
func GetWatchNamespace() string {
54+
return os.Getenv(WatchNamespaceEnvVar)
55+
}
56+
4957
func ValidateOperatorNamespace() error {
5058
ns := GetOperatorNamespace()
5159
if ns == "" {

0 commit comments

Comments
 (0)