Solving ConnectException
s with the Kubernetes Java ApiClient
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!