@@ -21,6 +21,7 @@ import (
2121 "flag"
2222 "fmt"
2323 "os"
24+ "strings"
2425
2526 // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2627 // to ensure that exec-entrypoint and run can make use of them.
@@ -126,13 +127,19 @@ func main() {
126127 metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
127128 }
128129
129- watchNamespace , err := getWatchNamespace ()
130+ watchNamespaces , err := getWatchNamespaces ()
130131 if err != nil {
131132 setupLog .Error (err , "unable to get WatchNamespace, " +
132133 "the manager will watch and manage resources in all namespaces" )
133134 os .Exit (1 )
134135 }
135136
137+ defaultNamespaces := make (map [string ]cache.Config )
138+ for _ , namespace := range watchNamespaces {
139+ defaultNamespaces [namespace ] = cache.Config {}
140+ setupLog .Info (fmt .Sprintf ("openstack-lightspeed operator watches %s namespace" , namespace ))
141+ }
142+
136143 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
137144 Scheme : scheme ,
138145 Metrics : metricsServerOptions ,
@@ -141,7 +148,7 @@ func main() {
141148 LeaderElection : enableLeaderElection ,
142149 LeaderElectionID : "c83b0a4f.lightspeed.openstack.org" ,
143150 Cache : cache.Options {
144- DefaultNamespaces : map [ string ]cache. Config { watchNamespace : {}} ,
151+ DefaultNamespaces : defaultNamespaces ,
145152 },
146153 // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
147154 // when the Manager ends. This requires the binary to immediately end when the
@@ -188,16 +195,18 @@ func main() {
188195 }
189196}
190197
191- // getWatchNamespace returns the Namespace the operator should be watching for changes
192- func getWatchNamespace () (string , error ) {
198+ // getWatchNamespaces returns a list of namespaces the operator should be watching for changes.
199+ // Note that this functions expects comma separated list in the WATCH_NAMESPACE env var.
200+ func getWatchNamespaces () ([]string , error ) {
193201 // WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
194202 // which specifies the Namespace to watch.
195203 // An empty value means the operator is running with cluster scope.
196204 var watchNamespaceEnvVar = "WATCH_NAMESPACE"
197205
198206 ns , found := os .LookupEnv (watchNamespaceEnvVar )
199207 if ! found {
200- return "" , fmt .Errorf ("%s must be set" , watchNamespaceEnvVar )
208+ return [] string {} , fmt .Errorf ("%s must be set" , watchNamespaceEnvVar )
201209 }
202- return ns , nil
210+
211+ return strings .Split (ns , "," ), nil
203212}
0 commit comments