45

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
  • 801
  • 1
  • 6
  • 11

3 Answers3

16

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)
            }
        },
    )
}
Vit
  • 7,740
  • 15
  • 40
  • 1
    A successful job returns `"succeeded":1` in the status json, so is it a bug that the field selector doesn't match the name of the field? – Tavin Dec 17 '19 at 12:30
  • 17
    This answer doesn't answer the question. Is there any way to get all the fields supported for a given resource type / resource version / kubectl version ? – Todd Walton Oct 07 '20 at 19:01
  • 1
    You are a hero! Though cryptic my quest for a list of supported fields for the label selector has finally yielded some results – Wouter van Vliet Oct 23 '20 at 14:05
  • 1
    Just realized answer is old and url should be updated. done – Vit Oct 23 '20 at 14:21
  • 1
    filters for apps v1 https://github.com/kubernetes/kubernetes/blob/691d4c3989f18e0be22c4499d22eff95d516d32b/pkg/apis/core/v1/conversion.go#L38 – Ahmad Ahmadi Apr 30 '22 at 17:55
5

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
  • 801
  • 1
  • 6
  • 11
1

Here's how to list resources (e.g. of type "Machine") in all namespaces, with status phase not running:

kubectl get Machine -A -o jsonpath='{.items[?(@.status.phase!="Running")].metadata.name}'

To print the error message of those failed resources:

kubectl get Machine -A -o jsonpath='{.items[?(@.status.phase=="Failed")].status.errorMessage}'

Output example:

nmanos-cluster-a-v5jcx-submariner-gw-us-west-1b-pwwrk: reconciler failed to Create machine: failed to launch instance: error launching instance: The requested configuration is currently not supported. Please check the documentation for supported configurations.

Noam Manos
  • 15,216
  • 3
  • 86
  • 85
  • In case someone want to use it in an shell if-statement (example with tekton pipelinerun ressource, same behaviour with any other ressource depending on json): `if [ $(kubectl get pipelinerun --selector='notification-provider' -o jsonpath='{.items[?(@.status.conditions[*].reason=="Running")].metadata.name}' | wc -c) -gt 0 ]; then echo Found running pipelines!; fi` – snukone Jul 27 '22 at 14:42