1

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

mrbarker
  • 25
  • 5
  • I’m voting to close this question because it's a duplicate of https://unix.stackexchange.com/q/598516/133219 – Ed Morton Jul 18 '20 at 17:45

1 Answers1

2

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.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • 1
    awesome! Thank you. I will be testing Monday and then will mark answered. In as much as Im reading and understanding - that should be it. I really appreciate the explanation – mrbarker Jul 17 '20 at 19:35
  • Don't use awk as a shell. Having a call stack of `shell { oc; awk { system { shell { oc } } } }` is a worse approach than `shell { oc; awk; oc }` which I showed the OP how to do a couple of days ago in response to the same question at https://unix.stackexchange.com/q/598516/133219. Also, your code doesn't account for some `$6`s being hours rather than days, nor is it quoting the string passed to the shell via `$1`. – Ed Morton Jul 18 '20 at 17:33
  • This doesn't make the code more efficient btw as it's spawning a subshell once per line of output with every call to `system()` and the grep only letting some lines get through to awk is probably more efficient than having awk do field splitting, etc. on **every** line. – Ed Morton Jul 18 '20 at 17:40