Reputation: 13773
How to select SUSPEND=False
cronjob?
--selector
can't be used as suspend
is not added in the labels.
Upvotes: 0
Views: 926
Reputation: 2052
By using a JSONPath expression and passing it via the --output=jsonpath=
option to kubectl
I was able to select only cronjobs which are unsuspended and print their names as follows:
kubectl get cronjob --output=jsonpath='{range .items[?(.spec.suspend==false)]}{.metadata.name}{"\n"}{end}'
In order yo invert the selection you can simply filter on spec.suspend==true
instead of false
, i.e.:
kubectl get cronjob --output=jsonpath='{range .items[?(.spec.suspend==true)]}{.metadata.name}{"\n"}{end}'
For explicitness you can additionally pass the --no-headers
like so, even though the former command already does not print any headers:
kubectl get cronjob --no-headers --output=jsonpath='{range .items[?(.spec.suspend==false)]}{.metadata.name}{"\n"}{end}'
Last but not least you can combine the commands above with Kubernetes selector expressions; for example:
kubectl get cronjob --selector=sometagkey==sometagvalue --no-headers --output=jsonpath='{range .items[?(.spec.suspend==false)]}{.metadata.name}{"\n"}{end}'
Upvotes: 1
Reputation: 1058
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)
}
Upvotes: 1