Skip to content

Commit 54f3600

Browse files
author
txqiangguo
committed
add field selector for node store ListWatch if node argument given
1 parent c92541d commit 54f3600

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

internal/store/node.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package store
1818

1919
import (
2020
"context"
21+
"k8s.io/apimachinery/pkg/fields"
22+
"k8s.io/klog/v2"
2123
"strings"
2224

2325
basemetrics "k8s.io/component-base/metrics"
@@ -520,12 +522,23 @@ func wrapNodeFunc(f func(*v1.Node) *metric.Family) func(interface{}) *metric.Fam
520522
}
521523
}
522524

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+
}
524535
return &cache.ListWatch{
525536
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
537+
opts.FieldSelector = fieldSelector
526538
return kubeClient.CoreV1().Nodes().List(context.TODO(), opts)
527539
},
528540
WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
541+
opts.FieldSelector = fieldSelector
529542
return kubeClient.CoreV1().Nodes().Watch(context.TODO(), opts)
530543
},
531544
}

pkg/options/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (n *NodeType) GetNodeFieldSelector() string {
145145
klog.InfoS("Using node type is nil")
146146
return EmptyFieldSelector()
147147
}
148-
pattern := "[^a-zA-Z0-9_,-]+"
148+
pattern := "[^a-zA-Z0-9_,-\\.]+"
149149
re := regexp.MustCompile(pattern)
150150
result := re.ReplaceAllString(n.String(), "")
151151
klog.InfoS("Using node type", "node", result)

0 commit comments

Comments
 (0)