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

4/19/2019

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 ?

-- toadjaune
kubectl
kubernetes

2 Answers

4/23/2019

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)
            }
        },
    )
}

Another examples: https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/core/v1/conversion.go#L462-L510

-- VKR
Source: StackOverflow

4/19/2019

For the record, although that doesn't answer the question, it's possible to work around this limitation with jsonPath.

For instance, the example above can be done like this :

kubectl delete job $(kubectl get job -o=jsonpath='{.items[?(@.status.succeeded==1)].metadata.name}')

(solution inspired from https://stackoverflow.com/a/53540996/5771067)

-- toadjaune
Source: StackOverflow