I need to delete deployments that are older than 14 days on clusters. if I run:
oc get deploy --sort-by=.metadata.creationTimestamp | grep wml-os-rt-hybrid | awk '{ print $1 "\t\t" $6;}'
It gives me a list of those deployments and the age of them:
wml-os-rt-hybrid0.1-gz3vczfc 15d
wml-os-rt-hybrid0.1-fj167pbt 8d
wml-os-rt-hybrid0.1-bdzkqi7z 7d
wml-os-rt-hybrid0.1-g4hclw4v 7d
wml-os-rt-hybrid0.1-j6x9tzt6 7d
wml-os-rt-hybrid0.1-qplkkilw 4d
wml-os-rt-hybrid0.1-sadgz9cz 6h
I am brand new to this and can not figure out what I need to add so that it would also delete anything the deployments older then 14d, in this case it would just be the one deployment but in other clusters it would be hundreds. Currently I manually delete it looking at the list and deleting anything older then 14 days by running(using above as example):
oc delete deployment wml-os-rt-hybrid0.1-gz3vczfc
This is the part I am trying to find an answer on how to automate
To list all entries older than 14 days, try:
oc get deploy --sort-by=.metadata.creationTimestamp | awk '/wml-os-rt-hybrid/ && $6+0>14 { print $1 "\t\t" $6;}'
To delete those deployments:
oc get deploy --sort-by=.metadata.creationTimestamp | awk '/wml-os-rt-hybrid/ && $6+0>14 { system("oc delete deployment "$1);}'
Here, /wml-os-rt-hybrid/ && $6+0>14
selects all lines that (a) contain wml-os-rt-hybrid
and (b) have a sixth field greater than 14. We can then either print, as in the first command, or run a system command as in the second.
We add zero to $6
before comparing it to 14. We do this because it tells awk
to treat $6
as a number so that the comparison is numeric not lexical.
Note that the awk command eliminates the need for the grep
process. This makes the resulting command both more efficient (one less process created) and more concise.