简体   繁体   中英

How to search by arbitrary fields using field selector with kubectl?

In this doc supported fields are not listed and I cannot find them properly. With some trial and experiments I noticed the following:

This works nicely and finds some pods:

kubectl get pods --field-selector=spec.restartPolicy=Never

But this produces error:

kubectl get pods --field-selector=spec.serviceAccount=default

No resources found.
Error from server (BadRequest): Unable to find {"" "v1" "pods"} that match label selector "", field selector "spec.serviceAccount=default": field label not supported: spec.serviceAccount

So how is this decided? I know I can find with JSONPath but it is client-side filtering AFAIK.

You can select the serviceAccount using following query:

kubectl get pods --field-selector=spec.serviceAccountName="default"

The --field-selector currently selects only equality based values and in that too it has very limited support to select the pod based on fields. The following fields are supported by --field-selector :

metadata.name
metadata.namespace
spec.nodeName
spec.restartPolicy
spec.schedulerName
spec.serviceAccountName
status.phase
status.podIP
status.nominatedNodeName

As you already know, you need to rely on the jsonpath to select any other field other than above fields.

You can visit following link to find out more:

https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/core/v1/conversion.go#L160-L167] 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM