Solving ConnectExceptions with the Kubernetes Java ApiClient

Featured image for sharing metadata for article

As mentioned in Things I Learned Migrating My Personal APIs To Kubernetes, I had an issue plaguing my Kubernetes configuration which was causing any interactions with the API to fail.

The most frustrating thing about it was that it was inconsistent, meaning that between cluster patching there were some periods of time it worked and some it failed.

The error I was seeing was a ConnectException:

{
  "@timestamp": "2021-09-28T08:00:00.482+00:00",
  "@version": 1,
  "exception": {
    "exception_class": "java.lang.IllegalStateException",
    "exception_message": "io.kubernetes.client.openapi.ApiException: java.net.ConnectException: Failed to connect to localhost/[0:0:0:0:0:0:0:1]:80",
    "stacktrace": "java.lang.IllegalStateException: io.kubernetes.client.openapi.ApiException: java.net.ConnectException: Failed to connect to localhost/[0:0:0:0:0:0:0:1]:80\n\tat me.jvt.www.indieauthcontroller.store.kubernetes.KubernetesSecretTokenStore.get(KubernetesSecretTokenStore.java:53)"
  },
  "level": "ERROR",
  "logger_name": "org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler",
  "message": "Unexpected error occurred in scheduled task",
  "source_host": "google-fit-7bbcb4ddc7-n26qn",
  "thread_name": "scheduling-1"
}

I couldn't see what was causing this for quite some time, but today I managed to track it down to me using the default ApiClient:

Config.defaultClient();

It appears that the way the default client was being built was picking up the wrong configuration, and authentication, and that I could instead force it to cluster-based configuration:

Config.fromCluster();

This solved these issues, and now configures the API endpoint and authentication correctly!

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #kubernetes #java.

Also on:

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.