Get timestamp of the last successful cronjob completion via kubectl

1.8k views Asked by At

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?

1

There are 1 answers

2
fafl On BEST ANSWER

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.