Reputation: 63
Say that I have a job history limit > 1, is there a way to use kubectl to find which jobs that have been spawned by a CronJob?
Upvotes: 0
Views: 2594
Reputation: 23
I use to do next:
kubectl get cronjob hello -o jsonpath='{.status.active[0].name}'
If is active the hello cronjob at that moment it will returns job's name created by the cronjob, otherwise it will empty.
Notice: "hello" is you cron job name
Upvotes: 0
Reputation: 36
Ideally you would use the --field-selector label to get all jobs belonging to a particular CronJob, but field selector does not support all the fields. The best way I could find is using jsonPath:
Substitute CronJobName to the name of your cronjob to get all jobs that belong to that CronJob
kubectl get jobs $(kubectl get jobs -o=jsonpath='{.items[?(@.metadata.ownerReferences[*].name=="CronJobName")].metadata.name}')
Upvotes: 1
Reputation: 669
Use label.
$kubectl get jobs -n namespace -l created-by=cronjob
created-by=cronjob which define at your cronjob.
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: "* * * * *"
jobTemplate:
metadata:
labels:
created-by: cronjob
spec:
template:
spec:
containers:
- name: hello
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
Upvotes: 4