How to check less than condition in Kubernetes annotation?

7/24/2020

I have a annotation added like this for rbac definition.

Annotations:  expires-at: 2020-07-24T19:24:42Z

I want to filter only the Kubernetes resources that is already expired based upon this annotation. I tried below..but it does not work.. "<" is considering the following as file name.

kubectl get rbacdefinition  --field-selector metadata.annotations.expires-at<$(date -u  '+%Y-%m-%dT%H:%M:%SZ')

Please let me know how to solve this.

-- Rad4
annotations
kubernetes

2 Answers

7/27/2020

Posting this answer as community wiki for better visibility as good answers were posted in comments.

As was mentioned by @tarun khosla, when you are using selector operators, only =, == and != are supported. Thus operators like <, >, =< will not work. It's described in Kubernetes docs

Another good point was provided by @mchawre to use 3rd party software which is Kube Janitor.

As workaroud you could consider to use:

kubectl describe <resource> | grep expires-at: > expiration.txt

or

kubectl describe all -n <namespace> | grep expires-at: -A 5 > expiration.txt

However, depends on resource and number of annotations you could get many irrelevant information.

-- PjoterS
Source: StackOverflow

11/24/2021

The answer from what I'm seeing is that no, <, >, REGEXP(), and IN(), basically what you'd expect in a database, is not there. And you're right to ask because the documentation I've found:

https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/

Doesn't explicitly state this.

Takeaway rule: NEVER use kubernetes resources as you would use a database or you'll be dealing with issues like this. Besides = and !=, > and < are the most minimally intuitively expected comparators, and they're not there.

-- Oliver Williams
Source: StackOverflow