Annotating Kubernetes resource with expiry time

Viewed 280

I want to add Annotation as expiry time in a Kubernetes resource (rbac definition object).

How to add annotation as the expiry time.

Pseudo code is something like below,

 annotations:
   expiry-time: {{ current date + 1 hour }}

How to add this custom annotation? What's the language of code needs to be added for custom annotation?

2 Answers

If you are using *nix shell like bash you can use the date command and the kubectl patch command .

kubectl patch <k8s-resource> <resource-name> -p \
 "{\"metadata\":{\"annotations\":{\"expiry-time\":\"`date -d '1 hour' '+%m-%d-%Y-%H:%M:%S'`\"}}}"

If you are on Mac you can substitute the date command with this:

date -v+1d '+%m/%d/%Y -%H:%M:%S'

✌️☮️

This worked..

 kubectl annotate rbacdefinition  joe-access "expires-at=$(date -v+1H  '+%m%d/%Y -%H:%M:%S')"
Related