Skip to content

Commit a4148ee

Browse files
Gerrit91vknabel
authored andcommitted
Add --namespace flag to watch only specific namespace if necessary.
1 parent c18b5ef commit a4148ee

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

cmd/main.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3232
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3333
ctrl "sigs.k8s.io/controller-runtime"
34+
"sigs.k8s.io/controller-runtime/pkg/cache"
3435
"sigs.k8s.io/controller-runtime/pkg/healthz"
3536
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3637
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
@@ -59,15 +60,20 @@ func init() {
5960
}
6061

6162
func main() {
62-
var metricsAddr string
63-
var enableLeaderElection bool
64-
var probeAddr string
65-
var secureMetrics bool
66-
var enableHTTP2 bool
67-
var tlsOpts []func(*tls.Config)
63+
var (
64+
metricsAddr string
65+
enableLeaderElection bool
66+
probeAddr string
67+
namespace string
68+
secureMetrics bool
69+
enableHTTP2 bool
70+
tlsOpts []func(*tls.Config)
71+
)
72+
6873
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
6974
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
7075
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
76+
flag.StringVar(&namespace, "namespace", "", "The namespace to watch for resources to reconcile. If not specified, watches all namespaces.")
7177
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
7278
"Enable leader election for controller manager. "+
7379
"Enabling this will ensure there is only one active controller manager.")
@@ -133,13 +139,23 @@ func main() {
133139
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
134140
}
135141

142+
var mgrOpts cache.Options
143+
if namespace != "" {
144+
mgrOpts = cache.Options{
145+
DefaultNamespaces: map[string]cache.Config{
146+
namespace: {},
147+
},
148+
}
149+
}
150+
136151
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
137152
Scheme: scheme,
138153
Metrics: metricsServerOptions,
139154
WebhookServer: webhookServer,
140155
HealthProbeBindAddress: probeAddr,
141156
LeaderElection: enableLeaderElection,
142157
LeaderElectionID: "6fca141d.cluster.x-k8s.io",
158+
Cache: mgrOpts,
143159
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
144160
// when the Manager ends. This requires the binary to immediately end when the
145161
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly

0 commit comments

Comments
 (0)