@@ -21,6 +21,7 @@ import (
2121 "crypto/tls"
2222 "flag"
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.
@@ -30,6 +31,7 @@ import (
3031 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3132 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3233 ctrl "sigs.k8s.io/controller-runtime"
34+ "sigs.k8s.io/controller-runtime/pkg/cache"
3335 "sigs.k8s.io/controller-runtime/pkg/healthz"
3436 "sigs.k8s.io/controller-runtime/pkg/log/zap"
3537 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
@@ -58,6 +60,7 @@ func main() {
5860 var probeAddr string
5961 var secureMetrics bool
6062 var enableHTTP2 bool
63+ var watchNamespaces string
6164 flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" , "The address the metric endpoint binds to. " +
6265 "Use the port :8080. If not set, it will be '0 in order to disable the metrics server" )
6366 flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
@@ -68,6 +71,7 @@ func main() {
6871 "If set the metrics endpoint is served securely" )
6972 flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
7073 "If set, HTTP/2 will be enabled for the metrics and webhook servers" )
74+ flag .StringVar (& watchNamespaces , "watch-namespaces" , "" , "Comma separated list of namespaces that vals-operator will watch." )
7175 opts := zap.Options {
7276 Development : true ,
7377 }
@@ -96,6 +100,34 @@ func main() {
96100 TLSOpts : tlsOpts ,
97101 })
98102
103+ var cacheOptions cache.Options
104+
105+ if watchNamespaces != "" {
106+ setupLog .Info ("watching namespaces" , "namespaces" , watchNamespaces )
107+
108+ // Split the watchNamespaces string into a slice of namespaces
109+ namespaces := strings .Split (watchNamespaces , "," )
110+
111+ // Create a map to hold namespace configurations
112+ namespaceConfigs := make (map [string ]cache.Config )
113+
114+ // Add each namespace to the map
115+ for _ , ns := range namespaces {
116+ // Trim any whitespace from the namespace
117+ ns = strings .TrimSpace (ns )
118+ if ns != "" {
119+ namespaceConfigs [ns ] = cache.Config {}
120+ }
121+ }
122+
123+ // Set the cache options with the namespace configurations
124+ cacheOptions = cache.Options {
125+ DefaultNamespaces : namespaceConfigs ,
126+ }
127+
128+ setupLog .Info ("configured cache for namespaces" , "count" , len (namespaceConfigs ))
129+ }
130+
99131 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
100132 Scheme : scheme ,
101133 Metrics : metricsserver.Options {
@@ -107,17 +139,7 @@ func main() {
107139 HealthProbeBindAddress : probeAddr ,
108140 LeaderElection : enableLeaderElection ,
109141 LeaderElectionID : "c9da0915.axonops.com" ,
110- // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
111- // when the Manager ends. This requires the binary to immediately end when the
112- // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
113- // speeds up voluntary leader transitions as the new leader don't have to wait
114- // LeaseDuration time first.
115- //
116- // In the default scaffold provided, the program ends immediately after
117- // the manager stops, so would be fine to enable this option. However,
118- // if you are doing or is intended to do any operation such as perform cleanups
119- // after the manager stops then its usage might be unsafe.
120- // LeaderElectionReleaseOnCancel: true,
142+ Cache : cacheOptions ,
121143 })
122144 if err != nil {
123145 setupLog .Error (err , "unable to start manager" )
0 commit comments