Filter kubectl get based on anotation Filter kubectl get based on anotation kubernetes kubernetes

Filter kubectl get based on anotation


I have a deployment with the annotation prometheus.io/scrape="true"

I can get the deployments having that annotation by

kubectl get deploy -o=jsonpath='{.items[?(@.spec.template.metadata.annotations.prometheus\.io/scrape=="true")].metadata.name}'

The above uses the Jsonpath concept and the docs can be found at here

In your case the command might be like

kubectl get deploy -o=jsonpath='{.items[?(@.spec.template.metadata.annotations.stork\.libopenstorage\.org/skipresource=="true")].metadata.name}'

This concept can be applied to other kubernetes resources as well.One other command that might help in understanding the earlier commands is

 kubectl get deployment -o=json


You are trying to use annotations in the same way that labels are used. The thing is that annotations are not meant to be used like that. It's possible to achieve what you want as sachin described but this is not practical.

Here we can read:

You cannot query annotations in Kubernetes, and this will not change in the foreseeable future.

Using labels would be a much better solution. Here we can see many usage examples for labels and it makes very clear why using it makes sense.

I think this is not the exact answer you was looking for, but in my opinion you are trying to do something in the hard way and it doesn't need to be like that if you use the solution that was created for what you are trying to achieve.