@@ -22,14 +22,17 @@ import (
2222 "flag"
2323 "os"
2424
25- // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
26- // to ensure that exec-entrypoint and run can make use of them.
27- _ "k8s.io/client-go/plugin/pkg/client/auth"
28-
25+ "k8s.io/apimachinery/pkg/labels"
2926 "k8s.io/apimachinery/pkg/runtime"
27+ "k8s.io/apimachinery/pkg/selection"
3028 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3129 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
30+
31+ // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
32+ // to ensure that exec-entrypoint and run can make use of them.
33+ _ "k8s.io/client-go/plugin/pkg/client/auth"
3234 ctrl "sigs.k8s.io/controller-runtime"
35+ "sigs.k8s.io/controller-runtime/pkg/cache"
3336 "sigs.k8s.io/controller-runtime/pkg/healthz"
3437 "sigs.k8s.io/controller-runtime/pkg/log/zap"
3538 "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
@@ -38,6 +41,7 @@ import (
3841
3942 kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
4043 "github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/controller"
44+ "github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/global"
4145
4246 // +kubebuilder:scaffold:imports
4347
@@ -78,6 +82,7 @@ func main() {
7882 "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead." )
7983 flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
8084 "If set, HTTP/2 will be enabled for the metrics and webhook servers" )
85+ flag .StringVar (& global .LabelSelector , "label-selector" , "" , "Label selector to filter watched resources (namely nodes)." )
8186
8287 flag .StringVar (& certificateNamespace , "certificate-namespace" , "monsoon3" , "The namespace for the certificates. " )
8388 flag .StringVar (& certificateIssuerName , "certificate-issuer-name" , "nova-hypervisor-agents-ca-issuer" ,
@@ -135,6 +140,21 @@ func main() {
135140 }
136141 restConfig := ctrl .GetConfigOrDie ()
137142
143+ var cacheOptions cache.Options
144+ if global .LabelSelector != "" {
145+ setupLog .Info ("setting up cache with label selector" , "selector" , global .LabelSelector )
146+ selector := labels .NewSelector ()
147+ req , err := labels .NewRequirement (global .LabelSelector , selection .Exists , nil )
148+ if err != nil {
149+ setupLog .Error (err , "unable to parse label selector" )
150+ os .Exit (1 )
151+ }
152+
153+ cacheOptions = cache.Options {
154+ DefaultLabelSelector : selector .Add (* req ),
155+ }
156+ }
157+
138158 mgr , err := ctrl .NewManager (restConfig , ctrl.Options {
139159 Scheme : scheme ,
140160 Metrics : metricsServerOptions ,
@@ -153,6 +173,9 @@ func main() {
153173 // if you are doing or is intended to do any operation such as perform cleanups
154174 // after the manager stops then its usage might be unsafe.
155175 // LeaderElectionReleaseOnCancel: true,
176+
177+ // Optionally configure the cache to listen/watch for specific labeled resources only
178+ Cache : cacheOptions ,
156179 })
157180
158181 if err != nil {
0 commit comments