<p>I have several CronJobs, and I want to be able to query for a list of completed jobs for a specific one. I tried <code>--field-selector metadata.ownerReferences.uid</code> but that's not a supported field selector for batchv1.job (Looking at <a href="https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/batch/v1/conversion.go#L40-L50" rel="nofollow noreferrer">this</a> it appears there are no useful field selectors for this use case).</p>

<p>I don't completely understand the DownwardAPI, but I was wondering if i can set a label on the job_spec (in the cronjob definition) that references the cronjobs uid, so I can then use label selectors to filter. I can't tell if thats possible.</p>

<p>Filtering for jobs by owner seems like a reasonable thing to be able to do, but I can't seem to find any useful information when searching around.</p>

Joe Ceresini
<p>You can use this two commands to get a list of jobs belonging to a Cronjob:</p>

<pre><code>uid=$(kubectl get cronjob [MY-CRONJOB-NAME] --output jsonpath='{.metadata.uid}')
kubectl get jobs -o json | jq -r --arg uid "$uid" '.items | map(select(.metadata.ownerReferences[]? | .uid==$uid) | .metadata.name) | .[]'
</code></pre>

<p>The first command fetches the <code>metadata.uid</code> from your specified <code>[MY-CRONJOB-NAME]</code>. Then, a <code>kubectl get</code> all jobs, and the output is processed with <a href="https://github.com/stedolan/jq" rel="nofollow noreferrer">jq</a> to filter them by the <code>.metadata.ownerReferences.uid</code>.</p>

<hr>

<p>If you want a more simple approach, you can play with the <code>--custom-columns</code>  flag. The following example list all jobs and their respective controller names:</p>

<pre><code>kubectl get jobs --all-namespaces -o custom-columns=NAME:.metadata.name,CONTROLLER:.metadata.ownerReferences[].name,NAMESPACE:.metadata.namespace
</code></pre>
