<p>I am using Kubernetes Java client API <a href="https://github.com/kubernetes-client/java" rel="nofollow noreferrer">https://github.com/kubernetes-client/java</a> for fetching all namespaces present. I am Getting Error-</p>
<pre><code>io.kubernetes.client.ApiException: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:443
    at io.kubernetes.client.ApiClient.execute(ApiClient.java:801)
    at io.kubernetes.client.apis.CoreV1Api.listNamespaceWithHttpInfo(CoreV1Api.java:15939)
    at io.kubernetes.client.apis.CoreV1Api.listNamespace(CoreV1Api.java:15917)
    at com.cloud.kubernetes.KubernetesNamespacesAPI.fetchAllNamespaces(KubernetesNamespacesAPI.java:25)
    at com.cloud.spark.sharedvariable.ClouzerConfigurations.setKubernetesEnvironment(ClouzerConfigurations.java:45)
</code></pre>
<p>I tried creating cluster role binding and giving permission to the user.</p>
<p>Here is my code snippet:</p>
<pre><code>public static List&lt;String&gt; fetchAllNamespaces(){
        try {
            return COREV1_API.listNamespace(null, &quot;true&quot;, null, null, null, 0, null, Integer.MAX_VALUE, Boolean.FALSE)
                    .getItems().stream().map(v1Namespace -&gt; v1Namespace.getMetadata().getName())
                    .collect(Collectors.toList());
        }catch(Exception e) {
            e.printStackTrace();
            return new ArrayList&lt;&gt;(); 
        }
    }
</code></pre>
<p>Please let me know if I am missing anything. Thanks in advance.</p>

<p>I'm facing the same exception too.
After several survey to the client lib's source code, I think you need to make sure of two things.</p>

<ul>
<li>first of all, can you access your api-server?</li>
<li>secondly, you need to check your ApiClient bootstrap order.</li>
</ul>

<p><strong>Which way do you use to config your connection</strong></p>

<p>The first thing here may not correlated to your case or the lib.
The api client lib supports three ways of configuration, to communicate with K8S apiserver from both inside of pod or out of cluster.</p>

<ul>
<li>read env KUBECONFIG </li>
<li>read ${home}/.kube/config</li>
<li>read the service account resides under /var/run/secrets/kubernetes.io/serviceaccount/ca.crt</li>
</ul>

<p>If you are using the lib inside a Pod, normally it will try to using the third way.</p>

<p><strong>How you bootstrap your client.</strong></p>

<p>You must keep in mind to invoke </p>

<pre class="lang-java prettyprint-override"><code>Configuration.setDefaultApiClient(apiClient);
</code></pre>

<p>before you init a CoreV1Api or your CRD api.
The reason is quite simply, because under all of the Api class, for example under
the class of io.kubernetes.client.api.CoreV1Api</p>

<pre class="lang-java prettyprint-override"><code>
public class CoreV1Api {
    private ApiClient apiClient;

    public CoreV1Api() {
        this(Configuration.getDefaultApiClient());
    }
...
}
</code></pre>

<p>If you haven't set the Configuration's defaultApiClient, it will use all default config, which the basePath will be <strong>localhost:443</strong>, then you will face the error.</p>

<p>Under the example package, The client has already created lots of examples and use case.
The full configuration logic may be as below:</p>

<pre class="lang-java prettyprint-override"><code>public class Example {
  public static void main(String[] args) throws IOException, ApiException {
    ApiClient client = Config.defaultClient();
    Configuration.setDefaultApiClient(client);
    // now you are safe to construct a CoreV1Api.
    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>Just keeps in mind, order is important if you are using default constructor to init a XXXApi.</p>
