Kubernetes Java Client API Exception

Ketan Kumbhar :

I am using Kubernetes Java client API https://github.com/kubernetes-client/java for fetching all namespaces present. I am Getting Error-

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)

I tried creating cluster role binding and giving permission to the user.

Here is my code snippet:

public static List<String> fetchAllNamespaces(){
        try {
            return COREV1_API.listNamespace(null, "true", null, null, null, 0, null, Integer.MAX_VALUE, Boolean.FALSE)
                    .getItems().stream().map(v1Namespace -> v1Namespace.getMetadata().getName())
                    .collect(Collectors.toList());
        }catch(Exception e) {
            e.printStackTrace();
            return new ArrayList<>(); 
        }
    }

Please let me know if I am missing anything. Thanks in advance.

shi huashen :

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.

  • first of all, can you access your api-server?
  • secondly, you need to check your ApiClient bootstrap order.

Which way do you use to config your connection

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.

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

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

How you bootstrap your client.

You must keep in mind to invoke

Configuration.setDefaultApiClient(apiClient);

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


public class CoreV1Api {
    private ApiClient apiClient;

    public CoreV1Api() {
        this(Configuration.getDefaultApiClient());
    }
...
}

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

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

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());
    }
  }
}

Just keeps in mind, order is important if you are using default constructor to init a XXXApi.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=157629&siteId=1