Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the list of field selectors supported by kubectl for a given resource type?

I've recently learned about kubectl --field-selector flag, but ran into errors when trying to use it with various objects.

For example :

$ kubectl delete jobs.batch --field-selector status.succeeded==1 Error from server (BadRequest): Unable to find "batch/v1, Resource=jobs" that match label selector "", field selector "status.succeeded==1": field label "status.succeeded" not supported for batchv1.Job 

According to the documentation, Supported field selectors vary by Kubernetes resource type., so I guess this behaviour was to be expected.

The annoying part is that I had to try individually each field to know if I could use them or not.

Is there any way to get all the fields supported for a given resource type / resource version / kubectl version ?

like image 231
toadjaune Avatar asked Apr 19 '19 12:04

toadjaune


People also ask

How do I check available resources in Kubernetes?

kubectl top pods or kubectl top nodes . This way you will be able to check current usage of pods/nodes. You can also narrow it to namespace . If you will execute kubectl describe node , in output you will be able to see Capacity of that node and how much allocated resources left.

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!=

Which command is used to list all the Kubernetes objects?

The most basic command for viewing Kubernetes objects via kubectl is get . If you run kubectl get <resource-name> you will get a listing of all resources in the current namespace. If you want to get a specific resource, you can use kubectl get <resource-name> <object-name> .


1 Answers

The issue in your case is that you mistakenly use status.succeeded instead of status.successful, so right command is

kubectl delete jobs.batch --field-selector status.successful==1 No resources found 

Regarding your question about all the fields: my suggestion is to deep into the code and search for proper resources types in conversion.go for each API.

Example: Batch Jobs conversion.go

    return scheme.AddFieldLabelConversionFunc(SchemeGroupVersion.WithKind("Job"),         func(label, value string) (string, string, error) {             switch label {             case "metadata.name", "metadata.namespace", "status.successful":                 return label, value, nil             default:                 return "", "", fmt.Errorf("field label %q not supported for batchv1.Job", label)             }         },     ) } 
like image 129
Vit Avatar answered Oct 13 '22 07:10

Vit