<p>I'm trying to filter jobs that are complete using golang kubernetes client-go lib by their status.</p>

<p>I've checked <a href="https://stackoverflow.com/a/53412583/268673">other answers</a> explaining how to get the jobs using kubectl, like this:</p>

<pre><code>kubectl get job -o=jsonpath='{.items[?(@.status.succeeded==1)].metadata.name}'
</code></pre>

<p>But I don't know how to "turn" that jsonpath output into a filter or list options</p>

<p>If I were searching for pods by their status phase and a label I would do something like this:</p>

<pre class="lang-golang prettyprint-override"><code>listOptions := metav1.ListOptions{
    LabelSelector: "app.kubernetes.io/name=my-custom-job",
    FieldSelector: "status.phase=Running",
}
result, err := clientset.CoreV1().Pods("default").List(listOptions) 
</code></pre>

<p>But if I am going to implement the jsonpath <code>{.items[?(@.status.succeeded==1)].metadata.name}</code></p>

<p>This is going to iterate through all jobs and check if the succeeded key under status is equal to one. For all jobs.</p>

<p><strong>Is there a way to look for those jobs more "memory friendly" or a way to use jsonpaths like that in ListOptions?</strong></p>

<p><strong>Yes</strong>, you can filter out, on the server-side, only finished jobs.</p>

<pre class="lang-golang prettyprint-override"><code>listOptions := metav1.ListOptions{
    FieldSelector: "status.successful=1",
}
result, err := clientset.BatchV1().Jobs("").List(listOptions) 
</code></pre>

<p><em>status.successful</em> field from the job's spec is being directly mapped to <em>status.succeeded</em> field from <strong>metav1.ListOptions.FieldSelector</strong>. <a href="https://github.com/kubernetes/kubernetes/blob/release-1.18/pkg/registry/batch/job/strategy.go#L205" rel="nofollow noreferrer">More info about that.</a></p>

<p>That being said, the list of available options to filter on the server-side is highly restricted. You <strong>can not</strong> filter using arbitrary fields from the spec (e.g. <em>status.active</em> or <em>spec.parallelism</em>). <a href="https://github.com/kubernetes/kubernetes/issues/53459" rel="nofollow noreferrer">Github Issue on that.</a></p>
