Home > OS >  How to filter resources by annotations using kubectl?
How to filter resources by annotations using kubectl?

Time:12-04

I would like to return all ingress resources that do not contain a specific annotation. Using the following command returns an error:

kubectl get ingress --all-namespaces -o=jsonpath='{.items[?(!(@.metadata.annotations.kubernetes\.io/ingress\.class))].metadata.name}'

error:

error parsing jsonpath {.items[?(!(@.metadata.annotations.kubernetes\.io/ingress\.class))].metadata.name}, unclosed array expect ]

CodePudding user response:

Could you try that one?

kubectl get ingress --all-namespaces -o=jsonpath='{.items[?(!(@.metadata.annotations["kubernetes.io/ingress.class"]))].metadata.name}'

CodePudding user response:

The error message is indicating that there is an unclosed array in your JSONPath query. This is likely because you have an extra . before the items field in your query.

Here is the correct JSONPath query that should work:

kubectl get ingress --all-namespaces -o=jsonpath='{.items[?(!(@.metadata.annotations.kubernetes.io/ingress.class))].metadata.name}'

Notice that the . before items has been removed, which should fix the error you are seeing.

Keep in mind that JSONPath queries can be difficult to read and understand, especially when they contain complex filters like the one in your example. It might be easier to use the --field-selector option instead, which allows you to specify the fields you want to include or exclude in the output. For example, the following command would return the names of all ingress resources that do not have the kubernetes.io/ingress.class annotation:

kubectl get ingress --all-namespaces --field-selector metadata.annotations.kubernetes.io/ingress.class!=
  • Related