使用 kubectl 列出非挂起的 cronjobs

List non-suspended cronjobs using kubectl

如何 select SUSPEND=False 定时任务?

--selector 无法使用,因为 suspend 未添加到标签中。

你不能在 kubectl 过滤器上 select spec.suspend。

您可以制作自己的 jq 过滤器

(这只是一个示例,可能会更简洁)

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

或者如果你不介意一点 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)
}