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

4/12/2019

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.

-- Mustafa
docker
kubectl
kubernetes
linux
shell

1 Answer

4/12/2019

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

-- Prafull Ladha
Source: StackOverflow