@@ -19,7 +19,9 @@ package main
1919import (
2020 "crypto/tls"
2121 "flag"
22+ "fmt"
2223 "os"
24+ "strings"
2325
2426 // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2527 // to ensure that exec-entrypoint and run can make use of them.
@@ -29,13 +31,15 @@ import (
2931 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3032 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3133 ctrl "sigs.k8s.io/controller-runtime"
34+ "sigs.k8s.io/controller-runtime/pkg/cache"
3235 "sigs.k8s.io/controller-runtime/pkg/healthz"
3336 "sigs.k8s.io/controller-runtime/pkg/log/zap"
3437 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3538 "sigs.k8s.io/controller-runtime/pkg/webhook"
3639
3740 csiv1alpha1 "github.com/ceph/ceph-csi-operator/api/v1alpha1"
3841 "github.com/ceph/ceph-csi-operator/internal/controller"
42+ "github.com/ceph/ceph-csi-operator/internal/utils"
3943 //+kubebuilder:scaffold:imports
4044)
4145
@@ -94,6 +98,23 @@ func main() {
9498 TLSOpts : tlsOpts ,
9599 })
96100
101+ defaultNamespaces := map [string ]cache.Config {}
102+ operatorNamespace , err := utils .GetOperatorNamespace ()
103+ if err != nil {
104+ setupLog .Error (err , "manager requires namespace to be registered for controllers to reconcile" )
105+ os .Exit (1 )
106+ }
107+ // ensure we always cache items from operator namespace
108+ defaultNamespaces [operatorNamespace ] = cache.Config {}
109+
110+ watchNamespace , err := getWatchNamespace ()
111+ if err != nil {
112+ setupLog .Error (err , "manager will only watch for resources in the operator deployed namespace" )
113+ } else {
114+ for _ , namespace := range strings .Split (watchNamespace , "," ) {
115+ defaultNamespaces [namespace ] = cache.Config {}
116+ }
117+ }
97118 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
98119 Scheme : scheme ,
99120 Metrics : metricsserver.Options {
@@ -116,6 +137,7 @@ func main() {
116137 // if you are doing or is intended to do any operation such as perform cleanups
117138 // after the manager stops then its usage might be unsafe.
118139 // LeaderElectionReleaseOnCancel: true,
140+ Cache : cache.Options {DefaultNamespaces : defaultNamespaces },
119141 })
120142 if err != nil {
121143 setupLog .Error (err , "unable to start manager" )
@@ -160,3 +182,14 @@ func main() {
160182 os .Exit (1 )
161183 }
162184}
185+
186+ // getWatchNamespace returns the Namespace the operator should be watching for changes
187+ func getWatchNamespace () (string , error ) {
188+ var watchNamespaceEnvVar = "WATCH_NAMESPACE"
189+
190+ ns := os .Getenv (watchNamespaceEnvVar )
191+ if ns == "" {
192+ return "" , fmt .Errorf ("%s must be set" , watchNamespaceEnvVar )
193+ }
194+ return ns , nil
195+ }
0 commit comments