<p>I'd like to use <code>kubectl wait</code> to block in a script until a job or pod I launched has either completed or until it has failed. I wanted to do something like <code>kubectl wait --for=condition=*</code> but I can't find good documentation on what the different condition options are. <code>kubectl wait --for=condition=completed</code> doesn't seem to work for jobs in an error state.</p>

software_intern
<p>Note that currently <code>kubectl wait</code> is marked as an experimental command on the kuberenetes documentation page, so it's likely a feature in development and not finished.<br />
Examples from <code>kubectl wait --help</code> and <a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#wait" rel="nofollow noreferrer">https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#wait</a> :</p>
<pre><code>Examples:
  # Wait for the pod &quot;busybox1&quot; to contain the status condition of type &quot;Ready&quot;.
  kubectl wait --for=condition=Ready pod/busybox1
  
  # Wait for the pod &quot;busybox1&quot; to be deleted, with a timeout of 60s, after having issued the &quot;delete&quot; command.
  kubectl delete pod/busybox1
  kubectl wait --for=delete pod/busybox1 --timeout=60s
</code></pre>
<p>The documentation also states the only options for the <code>--for</code> parameter are currently <code>delete</code> and <code>condition=condition-name</code></p>
<hr />
<p>It appears the <code>condition</code> field in</p>
<pre><code>kubectl wait --for condition=condition-name pod pod-name
</code></pre>
<p>refers to entries in <code>pod.status.conditions</code>.<br />
So we can launch a pod and look at the fields in <code>pod.status.conditions</code> for the kind of conditions we'd like to check.<br />
As an example I had a look at the yaml for one of my pods using <code>kubectl get pod pod-name -o yaml</code> and found the following conditions</p>
<pre class="lang-yaml prettyprint-override"><code>status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: &quot;2020-07-25T21:14:32Z&quot;
    status: &quot;True&quot;
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: &quot;2020-07-25T21:14:41Z&quot;
    status: &quot;True&quot;
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: &quot;2020-07-25T21:14:41Z&quot;
    status: &quot;True&quot;
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: &quot;2020-07-25T21:14:32Z&quot;
    status: &quot;True&quot;
    type: PodScheduled
</code></pre>
<p>So we can create pods and block until any of these conditions are met by using:</p>
<pre><code>kubectl wait --for condition=Initialized pod my-pod-name
kubectl wait --for condition=Ready pod my-pod-name
kubectl wait --for condition=ContainersReady pod my-pod-name
kubectl wait --for condition=PodScheduled pod my-pod-name
</code></pre>
<hr />
<p>Similarly for jobs:<br />
I created a job, waited for it to complete, then looked at it's yaml and found the following</p>
<pre class="lang-yaml prettyprint-override"><code>status:
  completionTime: &quot;2020-07-28T17:24:09Z&quot;
  conditions:
  - lastProbeTime: &quot;2020-07-28T17:24:09Z&quot;
    lastTransitionTime: &quot;2020-07-28T17:24:09Z&quot;
    status: &quot;True&quot;
    type: Complete
  startTime: &quot;2020-07-28T17:24:06Z&quot;
  succeeded: 1
</code></pre>
<p>Therefore we can use the conditions in <code>job.status.conditions</code>, such as</p>
<pre><code>kubectl wait --for condition=Complete job job-name
</code></pre>
<p>And we can of course add timeouts if we want to handle cases when the job fails to complete</p>
<pre><code>kubectl wait --for condition=Complete job job-name --timeout 60s
</code></pre>
