@@ -22,10 +22,12 @@ import (
2222 "flag"
2323 "os"
2424 "path/filepath"
25+ "strings"
2526 "time"
2627
2728 "github.com/functions-dev/func-operator/internal/git"
2829 "github.com/functions-dev/func-operator/internal/monitoring"
30+ "sigs.k8s.io/controller-runtime/pkg/cache"
2931
3032 // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
3133 // to ensure that exec-entrypoint and run can make use of them.
@@ -194,6 +196,20 @@ func main() {
194196 })
195197 }
196198
199+ watchNamespaces := getWatchNamespaces ()
200+ var cacheOpts cache.Options
201+ if len (watchNamespaces ) > 0 {
202+ setupLog .Info ("Operator watching specific namespaces" , "namespaces" , watchNamespaces )
203+
204+ // Map the namespaces into the Cache DefaultNamespaces map
205+ cacheOpts .DefaultNamespaces = make (map [string ]cache.Config )
206+ for _ , ns := range watchNamespaces {
207+ cacheOpts .DefaultNamespaces [strings .TrimSpace (ns )] = cache.Config {}
208+ }
209+ } else {
210+ setupLog .Info ("Operator watching all namespaces" )
211+ }
212+
197213 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
198214 Scheme : scheme ,
199215 Metrics : metricsServerOptions ,
@@ -212,6 +228,7 @@ func main() {
212228 // if you are doing or is intended to do any operation such as perform cleanups
213229 // after the manager stops then its usage might be unsafe.
214230 // LeaderElectionReleaseOnCancel: true,
231+ Cache : cacheOpts ,
215232 })
216233 if err != nil {
217234 setupLog .Error (err , "unable to start manager" )
@@ -282,3 +299,15 @@ func main() {
282299 os .Exit (1 )
283300 }
284301}
302+
303+ // getWatchNamespaces returns the Namespaces the operator should be watching for changes
304+ func getWatchNamespaces () []string {
305+ watchNamespaceEnvVar := "WATCH_NAMESPACE"
306+ ns , found := os .LookupEnv (watchNamespaceEnvVar )
307+ if ! found || ns == "" {
308+ return nil // Return nil to signify "watch all namespaces"
309+ }
310+
311+ // Split by comma to support multiple namespaces
312+ return strings .Split (ns , "," )
313+ }
0 commit comments