Skip to content

Commit 471c84e

Browse files
author
Ryan Baxter
committed
Return a default APIClient if we run into an exception
1 parent 24a0684 commit 471c84e

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

spring/src/main/java/io/kubernetes/client/spring/extended/controller/config/KubernetesInformerAutoConfiguration.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import io.kubernetes.client.openapi.ApiClient;
1717
import io.kubernetes.client.spring.extended.controller.KubernetesInformerFactoryProcessor;
1818
import io.kubernetes.client.util.ClientBuilder;
19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
21+
1922
import java.io.IOException;
2023
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2124
import org.springframework.context.annotation.Bean;
@@ -25,10 +28,32 @@
2528
@ConditionalOnKubernetesInformerEnabled
2629
public class KubernetesInformerAutoConfiguration {
2730

31+
private static final Log LOG = LogFactory.getLog(KubernetesInformerAutoConfiguration.class);
32+
2833
@Bean
2934
@ConditionalOnMissingBean
3035
public ApiClient defaultApiClient() throws IOException {
31-
return ClientBuilder.defaultClient();
36+
try {
37+
// Assume we are running in a cluster
38+
ApiClient apiClient = ClientBuilder.cluster().build();
39+
LOG.info("Created API client in the cluster.");
40+
return apiClient;
41+
}
42+
catch (Exception e) {
43+
LOG.info("Could not create the Kubernetes ApiClient in a cluster environment, because : ", e);
44+
LOG.info("Trying to use a \"standard\" configuration to create the Kubernetes ApiClient");
45+
try {
46+
ApiClient apiClient = ClientBuilder.defaultClient();
47+
LOG.info("Created standard API client. Unless $KUBECONFIG or $HOME/.kube/config is defined, "
48+
+ "this client will try to connect to localhost:8080");
49+
return apiClient;
50+
}
51+
catch (Exception e1) {
52+
LOG.warn("Could not create a Kubernetes ApiClient from either a cluster or standard environment. "
53+
+ "Will return one that always connects to localhost:8080", e1);
54+
return new ClientBuilder().build();
55+
}
56+
}
3257
}
3358

3459
@Bean

0 commit comments

Comments
 (0)