<p>I'm trying to connect to an existing kubernetes that's running on AWS and run arbitrary commands on it using Java. Specifically, we are using fabric8 (although I am open to another api if you can provide a sufficient answer using one). The reason I need to do this in Java is because we plan to eventually incorporate this into our existing junit live tests.</p>

<p>For now I just need an example of how to connect to the sever and get all of the pod names as an array of Strings. Can somebody show me a simple, concise example of how to do this.</p>

<p>i.e. I want the equivalent of this bash script using a java api (again preferably using fabric8, but I'll accept another api if you know one)</p>

<pre><code>#!bin/bash
kops export kubecfg --name $CLUSTER --state=s3://$STATESTORE

kubectl get pod -o=custom-colums=NAME:.metadata.name -n=$NAMESPACE
</code></pre>

<p>Here is the official Java client for Kubernetes.</p>

<p><a href="https://github.com/kubernetes-client/java" rel="nofollow noreferrer">https://github.com/kubernetes-client/java</a></p>

<p>It gives you a clean interface and write code in java to execute against kubernetes.</p>

<p>As listed in the documentation page to list all pods,</p>

<pre><code>import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.models.V1Pod;
import io.kubernetes.client.models.V1PodList;
import io.kubernetes.client.util.Config;

import java.io.IOException;

public class Example {
    public static void main(String[] args) throws IOException, ApiException{
        ApiClient client = Config.defaultClient();
        Configuration.setDefaultApiClient(client);

        CoreV1Api api = new CoreV1Api();
        V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
        for (V1Pod item : list.getItems()) {
            System.out.println(item.getMetadata().getName());
        }
    }
}
</code></pre>

<p>Hope it helps.</p>
