@@ -21,6 +21,7 @@ import (
21
21
"crypto/tls"
22
22
"flag"
23
23
"os"
24
+ "strings"
24
25
25
26
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
26
27
// to ensure that exec-entrypoint and run can make use of them.
@@ -30,6 +31,7 @@ import (
30
31
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
31
32
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
32
33
ctrl "sigs.k8s.io/controller-runtime"
34
+ "sigs.k8s.io/controller-runtime/pkg/cache"
33
35
"sigs.k8s.io/controller-runtime/pkg/healthz"
34
36
"sigs.k8s.io/controller-runtime/pkg/log/zap"
35
37
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
@@ -58,6 +60,7 @@ func main() {
58
60
var probeAddr string
59
61
var secureMetrics bool
60
62
var enableHTTP2 bool
63
+ var watchNamespaces string
61
64
flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" , "The address the metric endpoint binds to. " +
62
65
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server" )
63
66
flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
@@ -68,6 +71,7 @@ func main() {
68
71
"If set the metrics endpoint is served securely" )
69
72
flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
70
73
"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." )
71
75
opts := zap.Options {
72
76
Development : true ,
73
77
}
@@ -96,6 +100,34 @@ func main() {
96
100
TLSOpts : tlsOpts ,
97
101
})
98
102
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
+
99
131
mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
100
132
Scheme : scheme ,
101
133
Metrics : metricsserver.Options {
@@ -107,17 +139,7 @@ func main() {
107
139
HealthProbeBindAddress : probeAddr ,
108
140
LeaderElection : enableLeaderElection ,
109
141
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 ,
121
143
})
122
144
if err != nil {
123
145
setupLog .Error (err , "unable to start manager" )
0 commit comments