Although kubectl has the native ability to query on labels, it does not provide the same support for annotations. This is unfortunate, because the need to target deployments and services by annotation is commonplace.
You can, however, use the power of jsonpath to find these objects. Below is an example of finding all Services that have the ‘prometheus.io/scrape’ annotation, which is a common annotation used for monitoring.
# service that has annotation, regardless of value kubectl get service -A -o jsonpath='{.items[?(@.metadata.annotations.prometheus\.io/scrape)].metadata.name}' # has annotation set to "true" kubectl get service -A -o jsonpath='{.items[?(@.metadata.annotations.prometheus\.io/scrape=="true")].metadata.name}'
If you wanted to pull multiple values for the object, you can use range on each object found.
kubectl get service -A -o jsonpath='{range .items[?(@.metadata.annotations.prometheus\.io/scrape=="true")]}{ .metadata.namespace },{ .metadata.name}{"\n"}{end}'
REFERENCES