@@ -18,6 +18,8 @@ package store
18
18
19
19
import (
20
20
"context"
21
+ "k8s.io/apimachinery/pkg/fields"
22
+ "k8s.io/klog/v2"
21
23
"strings"
22
24
23
25
basemetrics "k8s.io/component-base/metrics"
@@ -520,12 +522,23 @@ func wrapNodeFunc(f func(*v1.Node) *metric.Family) func(interface{}) *metric.Fam
520
522
}
521
523
}
522
524
523
- func createNodeListWatch (kubeClient clientset.Interface , _ string , _ string ) cache.ListerWatcher {
525
+ func createNodeListWatch (kubeClient clientset.Interface , _ string , fieldSelector string ) cache.ListerWatcher {
526
+ // if given node name, it then lists and watches specified node by its name instead of all nodes.
527
+ if fieldSelector != "" {
528
+ selector , _ := fields .ParseSelector (fieldSelector )
529
+ nodeName , ok := selector .RequiresExactMatch ("spec.nodeName" )
530
+ if ok {
531
+ fieldSelector = fields .OneTermEqualSelector ("metadata.name" , nodeName ).String ()
532
+ klog .InfoS ("Transform fieldSelector for node store" , "fieldSelector" , fieldSelector )
533
+ }
534
+ }
524
535
return & cache.ListWatch {
525
536
ListFunc : func (opts metav1.ListOptions ) (runtime.Object , error ) {
537
+ opts .FieldSelector = fieldSelector
526
538
return kubeClient .CoreV1 ().Nodes ().List (context .TODO (), opts )
527
539
},
528
540
WatchFunc : func (opts metav1.ListOptions ) (watch.Interface , error ) {
541
+ opts .FieldSelector = fieldSelector
529
542
return kubeClient .CoreV1 ().Nodes ().Watch (context .TODO (), opts )
530
543
},
531
544
}
0 commit comments