Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter kubectl get based on anotation

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
like image 753
Jeromba6 Avatar asked Jun 08 '20 14:06

Jeromba6


People also ask

What is the difference between labels and annotations?

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.

What is Selector matchLabels?

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.

How do I look up a POD label?

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.

What does kubectl annotate do?

It is intended to store non-identifying auxiliary data, especially data manipulated by tools and system extensions.

What does kubectl get pods--field-selector status mean?

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.

How to use kubectl field selectors across multiple resource types?

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.

Can kubectl query on labels?

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.

How do I select all pods in kubectl?

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.


3 Answers

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
like image 80
sachin Avatar answered Oct 21 '22 18:10

sachin


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.

like image 41
Mark Watney Avatar answered Oct 21 '22 16:10

Mark Watney


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'
like image 1
RubenLaguna Avatar answered Oct 21 '22 18:10

RubenLaguna