<p>I want to get the status code of my command executed in a container with Fabric8 Java Kubernetes client.</p>
<p>Here is the script located in my container:</p>
<pre><code>echo Bye Bye
exit 1
</code></pre>
<p>When I run the script with CLI or NodeJS Client I am able to get the output status code</p>
<p>Here is an example taken from fabric8 repository:</p>
<pre><code>package org.package;

import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.ExecListener;
import io.fabric8.kubernetes.client.dsl.ExecWatch;
import okhttp3.Response;

public class OtherMain {

  public static void main(String[] args) throws InterruptedException {


    String podName = &quot;my-pod&quot;;
    String namespace = &quot;my-namespace&quot;;

    try (
        KubernetesClient client = new DefaultKubernetesClient();
        ExecWatch watch = newExecWatch(client, namespace, podName)) {
      Thread.sleep(10 * 1000L);
    }
  }

  private static ExecWatch newExecWatch(KubernetesClient client, String namespace, String podName) {
    return client.pods()
        .inNamespace(namespace)
        .withName(podName)
        .readingInput(System.in)
        .writingOutput(System.out)
        .writingError(System.err)
        .withTTY()
        .usingListener(new SimpleListener())
        .exec(&quot;sh&quot;, &quot;test.sh&quot;);
  }

  private static class SimpleListener implements ExecListener {

    @Override
    public void onOpen(Response response) {
      System.out.println(&quot;The shell will remain open for 10 seconds.&quot;);
    }

    @Override
    public void onFailure(Throwable t, Response response) {
      System.err.println(&quot;shell barfed&quot;);
    }

    @Override
    public void onClose(int code, String reason) {
      System.out.println(&quot;The shell will now close.&quot;);
    }
  }
}
</code></pre>
<p>However when looking at the output it seems that everything went ok. Is there a way to get the output status code ?</p>

Geoffrey
<p>It is possible by using the <code>wri</code> method:</p>
<p>Can use the <code>.writingErrorChannel</code> and parse the response:</p>
<pre><code>{
    &quot;metadata&quot;: {},
    &quot;status&quot;: &quot;Failure&quot;,
    &quot;message&quot;: &quot;command terminated with non-zero exit code: exit status 1&quot;,
    &quot;reason&quot;: &quot;NonZeroExitCode&quot;,
    &quot;details&quot;: {
        &quot;causes&quot;: [
            {
                &quot;reason&quot;: &quot;ExitCode&quot;,
                &quot;message&quot;: &quot;1&quot;
            }
        ]
    }
}
</code></pre>
<p>Type is: <code>io.fabric8.kubernetes.api.model.Status</code></p>
