Reputation: 433
I would like to filter my kubectl get deploy
command based on the value of an annotation.
Something similar to kubectl get deploy --annotation stork.libopenstorage.org/skipresource!="true"
Currently no clue how to do this and we don't want to add an extra label. Output of both commands above should be something like below:
kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
elastalert 1/1 1 1 33d
es-hq 1/1 1 1 33d
etcdsnapshots 1/1 1 1 33d
fluentd-aggregator 2/2 2 2 33d
kibana 1/1 1 1 33d
kubectl get deploy --annotation stork.libopenstorage.org/skipresource!="true"
NAME READY UP-TO-DATE AVAILABLE AGE
etcdsnapshots 1/1 1 1 33d
fluentd-aggregator 2/2 2 2 33d
kibana 1/1 1 1 33d
kubectl get deploy --annotation stork.libopenstorage.org/skipresource="true"
NAME READY UP-TO-DATE AVAILABLE AGE
elastalert 1/1 1 1 33d
es-hq 1/1 1 1 33d
Upvotes: 25
Views: 42067
Reputation: 24776
There is not way to do the filtering by annotation at the server side, but you can get the list of all pods or deployements and filter it locally, with clever use of -o=jsonpath
or using jq
which I find more intuitive.
For example to get all deployments where that particular annotation is not "true"
kubectl get deployment -o json| jq '.items[].metadata|select(.annotations."stork.libopenstorage.org/skipresource"!="true")|.name'
or for pods that have the annotation aws.amazon.com/cloudwatch-agent-ignore
:
kubectl get pods -n mynamespace -o json| jq '.items[].metadata|select(.annotations."aws.amazon.com/cloudwatch-agent-ignore"!="true")|.name'
Upvotes: 9
Reputation: 5950
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.
Upvotes: 5
Reputation: 1360
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
Upvotes: 44