Search code examples
kubernetesgetannotations

Filter kubectl get based on annotation


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

Solution

  • 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