@@ -27,10 +27,15 @@ import (
2727 _ "k8s.io/client-go/plugin/pkg/client/auth"
2828
2929 "go.uber.org/zap/zapcore"
30+ corev1 "k8s.io/api/core/v1"
31+ "k8s.io/apimachinery/pkg/labels"
3032 "k8s.io/apimachinery/pkg/runtime"
33+ "k8s.io/apimachinery/pkg/selection"
3134 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3235 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3336 ctrl "sigs.k8s.io/controller-runtime"
37+ "sigs.k8s.io/controller-runtime/pkg/cache"
38+ "sigs.k8s.io/controller-runtime/pkg/client"
3439 "sigs.k8s.io/controller-runtime/pkg/healthz"
3540 "sigs.k8s.io/controller-runtime/pkg/log/zap"
3641 "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
@@ -39,6 +44,7 @@ import (
3944
4045 kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
4146 "github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/controller"
47+ "github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/global"
4248 "github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/logger"
4349
4450 // +kubebuilder:scaffold:imports
@@ -80,6 +86,7 @@ func main() {
8086 "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead." )
8187 flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
8288 "If set, HTTP/2 will be enabled for the metrics and webhook servers" )
89+ flag .StringVar (& global .LabelSelector , "label-selector" , "" , "Label selector to filter watched resources (namely nodes)." )
8390
8491 flag .StringVar (& certificateNamespace , "certificate-namespace" , "monsoon3" , "The namespace for the certificates. " )
8592 flag .StringVar (& certificateIssuerName , "certificate-issuer-name" , "nova-hypervisor-agents-ca-issuer" ,
@@ -138,6 +145,28 @@ func main() {
138145 }
139146 restConfig := ctrl .GetConfigOrDie ()
140147
148+ var cacheOptions cache.Options
149+ if global .LabelSelector != "" {
150+ setupLog .Info ("setting up cache with label selector" , "selector" , global .LabelSelector )
151+ selector := labels .NewSelector ()
152+ req , err := labels .NewRequirement (global .LabelSelector , selection .Exists , nil )
153+ if err != nil {
154+ setupLog .Error (err , "unable to parse label selector" )
155+ os .Exit (1 )
156+ }
157+
158+ cacheOptions = cache.Options {
159+ ByObject : map [client.Object ]cache.ByObject {
160+ & corev1.Node {}: {
161+ Label : selector .Add (* req ),
162+ },
163+ & kvmv1.Hypervisor {}: {
164+ Label : selector .Add (* req ),
165+ },
166+ },
167+ }
168+ }
169+
141170 mgr , err := ctrl .NewManager (restConfig , ctrl.Options {
142171 Scheme : scheme ,
143172 Metrics : metricsServerOptions ,
@@ -156,6 +185,9 @@ func main() {
156185 // if you are doing or is intended to do any operation such as perform cleanups
157186 // after the manager stops then its usage might be unsafe.
158187 // LeaderElectionReleaseOnCancel: true,
188+
189+ // Optionally configure the cache to listen/watch for specific labeled resources only
190+ Cache : cacheOptions ,
159191 })
160192
161193 if err != nil {
0 commit comments