llhuii / dive-into-k8s

Apache License 2.0
0 stars 0 forks source link

kubectl --field-selector字段过滤原理 #7

Open llhuii opened 3 years ago

llhuii commented 3 years ago
root@edgeai-linux-llh:~/kubernetes# kubectl get node --field-selector=metadata.name=edge-test -v6
I0729 00:09:49.339627 2532495 loader.go:375] Config loaded from file:  /root/.kube/config
I0729 00:09:49.357346 2532495 round_trippers.go:443] GET https://127.0.0.1:38663/api/v1/nodes?fieldSelector=metadata.name%3Dedge-test&limit=500 200 OK in 11 milliseconds
NAME        STATUS     ROLES        AGE   VERSION
edge-test   NotReady   agent,edge   18d   v1.19.3-kubeedge-v1.7.0-28+5fce8601e71fa3-dirty

client-go 过滤写法:

# list/watch 指定名字的节点
kubeInformers := informers.NewSharedInformerFactoryWithOptions(KubeClient, 0, informers.WithTweakListOptions(func(options *metav1.ListOptions) {
            options.FieldSelector = fields.Set{metav1.ObjectNameField: string(nodeName)}.String()
        }))
# list/watch 指定节点的pod
lw := cache.NewListWatchFromClient(c.CoreV1().RESTClient(), "pods", metav1.NamespaceAll, fields.OneTermEqualSelector("spec.nodeName", string(nodeName)))