List non-suspended cronjobs using kubectl

4/14/2021

How to select SUSPEND=False cronjob?

--selector can't be used as suspend is not added in the labels.

-- Dev
kubectl
kubernetes

1 Answer

4/14/2021

you can't select spec.suspend on a kubectl filter.

You could make your own jq filter

(this is just an example and could be a lot cleaner)

k get cronjob --output=json | 
  jq -c 'select( .items[].spec.suspend == false)' | 
  jq  '.items[] | .metadata.name + " " + .spec.schedule + " "
  + (.spec.suspend|tostring) + " " + .spec.active'

Or if you don't mind a little golang

// Look at https://github.com/kubernetes/client-go/blob/master/examples/out-of-cluster-client-configuration/main.go 
// for the other parameters (like using current kubectl context).
func getCronsBySuspend(clientset *kubernetes.Clientset, namespace string) {
	cjs, err := clientset.BatchV1().CronJobs(namespace).List(context.TODO(), metav1.ListOptions{})
	if err != nil {
		panic(err.Error())
	}

	var c []string
	for _, cron := range cjs.Items {
		if *cron.Spec.Suspend == false {
			c = append(c, cron.Name)
		}
	}

	fmt.Printf("%v", c)
}
-- Olivercodes
Source: StackOverflow