Suspend All Kubernetes Cronjobs

Recently, I had the need to suspend all Kubernetes cronjobs in a cluster. Looking around for an existing one-liner I came across this helpful gist. It loops over all namespaces, gets all cronjobs in each and patches them to spec.suspend: true.

for ns in $(kubectl get ns -o jsonpath="{.items[*].metadata.name}"); do
  for cj in $(kubectl get cronjobs -n "$ns" -o name); do
    kubectl patch "$cj" -n "$ns" -p '{"spec" : {"suspend" : true }}';
  done
done

And to enable them again, we can patch them to spec.suspend: false.

for ns in $(kubectl get ns -o jsonpath="{.items[*].metadata.name}"); do
  for cj in $(kubectl get cronjobs -n "$ns" -o name); do
    kubectl patch "$cj" -n "$ns" -p '{"spec" : {"suspend" : false }}';
  done
done

Keep in mind that resuming overdue cronjobs immediately schedules new jobs, so depending on your jobs this might cause some additional load.


Posted

in

by

Tags: