Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 394
Mustafa Avatar asked Apr 12 '19 11:04

Mustafa


People also ask

What is field selector in Kubernetes?

Field selectors let you select Kubernetes resources based on the value of one or more resource fields. Here are some examples of field selector queries: metadata.name=my-service. metadata. namespace!=

What are used to identify and select objects in Kubernetes?

You can use either labels or annotations to attach metadata to Kubernetes objects. Labels can be used to select objects and to find collections of objects that satisfy certain conditions. In contrast, annotations are not used to identify and select objects.

How do you check nodes in kubectl?

You can use “kubectl get nodes” to look up pod and node information. One example is when you need to find pods by label or name with a specific port number. You should see all the nodes in your cluster.


1 Answers

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

like image 149
Prafull Ladha Avatar answered Sep 24 '22 02:09

Prafull Ladha