|
16 | 16 | import io.kubernetes.client.openapi.ApiClient; |
17 | 17 | import io.kubernetes.client.spring.extended.controller.KubernetesInformerFactoryProcessor; |
18 | 18 | import io.kubernetes.client.util.ClientBuilder; |
| 19 | +import org.apache.commons.logging.Log; |
| 20 | +import org.apache.commons.logging.LogFactory; |
| 21 | + |
19 | 22 | import java.io.IOException; |
20 | 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
21 | 24 | import org.springframework.context.annotation.Bean; |
|
25 | 28 | @ConditionalOnKubernetesInformerEnabled |
26 | 29 | public class KubernetesInformerAutoConfiguration { |
27 | 30 |
|
| 31 | + private static final Log LOG = LogFactory.getLog(KubernetesInformerAutoConfiguration.class); |
| 32 | + |
28 | 33 | @Bean |
29 | 34 | @ConditionalOnMissingBean |
30 | 35 | 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 | + } |
32 | 57 | } |
33 | 58 |
|
34 | 59 | @Bean |
|
0 commit comments