1

I have a cronjob running every couple of minutes in kubernetes and would like to set up an alert that notifies me when the cronjob stops working.

I'm expecting it to fail sometimes, it's calling two REST endpoints and they won't always be available.

I want to know if the last successful run happened more than x minutes ago. For this I need the last successful completion timestamp.

I'd like to use the kubectl command line tool. How do I do this?

fafl
  • 7,222
  • 3
  • 27
  • 50
  • You have not provided an example with a sample `kubectl` output, so most readers of this question cannot think of possible answers by parsing that output and get the result, only readers that can test with kubernetes and can reproduce a case. – thanasisp Oct 08 '20 at 12:25
  • There is no example output because i'm not even sure which `kubectl` command i'm supposed to run. Maybe there is a better way to solve this using the yaml output option. – fafl Oct 08 '20 at 17:26

1 Answers1

3

It was easier than I thought, this is my solution using kubectl and jq:

kubectl get job -l component=<cronjob> -n <namespace> -o json | \
jq -r '.items[] | select(.status.succeeded) | .status.completionTime' | \
sort -r | head -n 1

kubectl fetches all job executions of the cronjob and prints them to json.

jq then selects all successful jobs and prints their completion-time.

sort and head then select the latest timestamp.

fafl
  • 7,222
  • 3
  • 27
  • 50
  • You can omit additional calls to `sort` and `head` as `jq` can sort, get min/max values itself, see for example [this](https://stackoverflow.com/questions/56758079/get-json-object-that-has-latest-timestamp-using-jq). In case of unknown values selected, where there is no quarantee results will be line-oriented and one per line, it would be safer also. – thanasisp Oct 08 '20 at 12:22
  • I know the schema of the response, it should always produce either a date or an empty string in case the job has not finished yet. Empty strings are sorted last here. I didn't know that `jq` can do so much, thanks for highlighting. But i find it confusing enough and would rather use simple bash commands. – fafl Oct 08 '20 at 17:31