I would like to filter my kubectl get deploy
command based on the value of an annotation.
Something similar like 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
Labels can be used to select objects and to find collections of objects that satisfy certain conditions. In contrast, annotations are not used to identify and select objects. The metadata in an annotation can be small or large, structured or unstructured, and can include characters not permitted by labels.
matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions , whose key field is "key", the operator is "In", and the values array contains only "value". matchExpressions is a list of pod selector requirements.
You can also do a kubectl get pod example-pod -o yaml to see all of the fields and labels. Let us now add another label to the above pod using the kubectl command. kubectl label --overwrite pods example-pod env=prod will update the value of key env in the labels, and if the label does not exist, it will create one.
It is intended to store non-identifying auxiliary data, especially data manipulated by tools and system extensions.
status.phase=Pending This kubectl command selects all Pods for which the value of the status.phase field is Running: kubectl get pods --field-selector status.phase=Running Note: Field selectors are essentially resource filters.
As with label and other selectors, field selectors can be chained together as a comma-separated list. This kubectl command selects all Pods for which the status.phase does not equal Running and the spec.restartPolicy field equals Always: You can use field selectors across multiple resource types.
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.
This kubectl command selects all Pods for which the value of the status.phase field is Running: kubectl get pods --field-selector status.phase=Running Note: Field selectors are essentially resource filters. By default, no selectors/filters are applied, meaning that all resources of the specified type are selected.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With