0

Can you please let me know if this command is valid ?

kubectl get pods -A | awk {'printf "%s %s %s %s\n", $1,$2,$4,$6'} | grep -E "Evicted" |
while read line; do ns=$(echo $line | cut -d , -f 1); pod=$(echo $line | cut -d , -f 2); age=$(echo $line | cut -d, -f 6) ;
final_age=$(awk -F "d" '{print (NF > 1) ? $1 : 0}' <<< "$age");
if [[ $final_age -gt 30 ]]; then kubectl delete pod --dry-run='server' -n "$ns" "$pod"; fi done 
 

The intention is to fetch the old evicted pods which their age is greater than 30 days and delete them .. can you please check as it returns null on my machine..

expected output: the names of pods which will be deleted
resulted output: null

doksha
  • 47
  • 1
  • 2
  • 8
  • 2
    You should probably use Python or something, this is kind of a mess and hard to read/maintain. Seems likely to have a bug somewhere but I don't think anyone is going to take the time to find it given it's a single huge line of uncommented code. – coderanger Jun 07 '21 at 19:49
  • Please format the code so each call is on it's own line, after you've fixed your syntax errors, with our help if needed, you can convert it back to a one-liner – 0stone0 Jun 07 '21 at 19:55
  • Why so many separate moving parts? `awk` can do the work of `grep` and `cut` internally. – Charles Duffy Jun 07 '21 at 19:57
  • Consider running your code through http://shellcheck.net/ and fixing what it finds. And when asking a question here, try to make it about a specific, narrow problem; instead of asking people to analyze your code and find its problems themselves. (And as coderanger says, this really should be split into separate lines). – Charles Duffy Jun 07 '21 at 19:57
  • To name a few specific bugs: `age=$(...) | final_age=...` tries to run both the `age=...` and the `final_age=` in parallel with each other, so the latter can't see the result of the former. `echo $line` itself corrupts your line before printing it, in the manner described in [I just assigned a variable, but `echo $variable` shows something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else). `[[$anything]]` isn't valid syntax unless you have spaces after the `[[` and before the `]]`. – Charles Duffy Jun 07 '21 at 20:00
  • Also: Parameter expansions should always be quoted (`-n "$ns" "$pod"`, not `-n $ns $pod`). `age=...` needs a command substitution: `age=$(...)`. – Charles Duffy Jun 07 '21 at 20:01
  • @all.... I'm so sorry about posting my code in that format.. but i'm still a beginner here and trying to know exactly what is the issue with this code above.. my method is to get the data (all the pods from all namespaces)...then fetch only the evicted pods.. then get only the numerical values of the age.. so for example..if the age is 12m..it will return a zero.. if the age is 12d3m it will return only the number of days which is 12.. then if this age is greater than 30 days.. it will delete the pod from its namespace .. i hope this is clearer now somehow.. – doksha Jun 07 '21 at 20:14
  • Please fix the problems other than the specific one you're most interested in help with (http://shellcheck.net/ will help with that), or simplify the code so it only has that one problem, taking the other issues out-of-scope. – Charles Duffy Jun 07 '21 at 20:16
  • BTW, something I left out above -- instead of the repeated `ns=$(echo $line | cut -d, -f1); pod=$(echo $line | cut -d, -f2)` and so forth, you can use `IFS=, read -r ns pod _ _ _ age _ <<<"$line"` to read `ns`, `pod`, and `age` all at once (they're listed in order of position, using `_` to fill in for positions you don't want to read into a variable). Mind, if you don't have any other use for `line` you could just make it `... | while IFS=, read -r ns pod _ _ _ age _; do ...` and eliminate `line` altogether. – Charles Duffy Jun 07 '21 at 20:17
  • What's the purpose behind using a `|` to separate the `age=` command and the `final_age=` command? Why do you choose that instead of `;`? – Charles Duffy Jun 07 '21 at 20:20
  • 1
    try this , it should make things less dependent on shell tools like `awk` etc. `kubectl get pods --field-selector 'status.phase==Evicted' -o=jsonpath='{range .items[*]}{.metadata.name}{.status.}{"\t"}{.status.startTime}{"\n"}{end}'` this should filter out the evicted pods and their uptimes. – P.... Jun 07 '21 at 20:41
  • For the curious -- after an out-of-band discussion, https://ideone.com/5VJGUP was arrived at as a means to meet the OP's requirements. – Charles Duffy Jun 07 '21 at 22:41
  • Adding to my above comment, convert time to epoch for comparison. – P.... Jun 08 '21 at 01:36

0 Answers0