Anders Strömberg
Anders Strömberg

Reputation: 63

Kubernetes: How do I find which jobs a cronjob has generated

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

Answers (3)

Israel Diaz
Israel Diaz

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

D Grewal
D Grewal

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

HKBN-ITDS
HKBN-ITDS

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

Related Questions