From a77c8c0996ddf0e72f6c5703299817684ae4ac49 Mon Sep 17 00:00:00 2001 From: erabii Date: Thu, 21 Dec 2023 22:15:45 +0200 Subject: [PATCH] fix issue (#1542) --- .../kubernetes/client/KubernetesClientUtils.java | 12 ++++++++++-- ...ernetesClientConfigDataLocationResolverTests.java | 10 +++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/spring-cloud-kubernetes-client-autoconfig/src/main/java/org/springframework/cloud/kubernetes/client/KubernetesClientUtils.java b/spring-cloud-kubernetes-client-autoconfig/src/main/java/org/springframework/cloud/kubernetes/client/KubernetesClientUtils.java index b931e07dd6..d1ecdeb8bf 100644 --- a/spring-cloud-kubernetes-client-autoconfig/src/main/java/org/springframework/cloud/kubernetes/client/KubernetesClientUtils.java +++ b/spring-cloud-kubernetes-client-autoconfig/src/main/java/org/springframework/cloud/kubernetes/client/KubernetesClientUtils.java @@ -49,8 +49,16 @@ public static ApiClient kubernetesApiClient() { return apiClient; } catch (Exception e) { - LOG.info("Could not create the Kubernetes ApiClient in a cluster environment, because : ", e); - LOG.info("Trying to use a \"standard\" configuration to create the Kubernetes ApiClient"); + if (e instanceof IllegalStateException illegalStateException + && illegalStateException.getCause() instanceof NumberFormatException) { + LOG.info("Could not create the Kubernetes ApiClient in a cluster environment, because connection port " + + "was not provided."); + } + else { + LOG.info("Could not create the Kubernetes ApiClient in a cluster environment, because : ", e); + } + LOG.info(""" + Trying to use a "standard" configuration to create the Kubernetes ApiClient"""); try { ApiClient apiClient = ClientBuilder.defaultClient(); LOG.info("Created standard API client. Unless $KUBECONFIG or $HOME/.kube/config is defined, " diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigDataLocationResolverTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigDataLocationResolverTests.java index 3e234007d1..8442cf5591 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigDataLocationResolverTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/KubernetesClientConfigDataLocationResolverTests.java @@ -22,6 +22,7 @@ import io.kubernetes.client.openapi.apis.CoreV1Api; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; import org.springframework.boot.DefaultBootstrapContext; @@ -31,6 +32,8 @@ import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.source.ConfigurationPropertySources; import org.springframework.boot.logging.DeferredLogFactory; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties; import org.springframework.cloud.kubernetes.commons.config.ConfigDataRetryableConfigMapPropertySourceLocator; import org.springframework.cloud.kubernetes.commons.config.ConfigDataRetryableSecretsPropertySourceLocator; @@ -43,6 +46,7 @@ /** * @author wind57 */ +@ExtendWith(OutputCaptureExtension.class) class KubernetesClientConfigDataLocationResolverTests { private static final DeferredLogFactory FACTORY = Supplier::get; @@ -142,7 +146,7 @@ void testBothPresent() { * these are not retryable beans. */ @Test - void testBothPresentExplicitly() { + void testBothPresentExplicitly(CapturedOutput capturedOutput) { MockEnvironment environment = new MockEnvironment(); environment.setProperty("spring.cloud.kubernetes.config.enabled", "true"); environment.setProperty("spring.cloud.kubernetes.secrets.enabled", "true"); @@ -172,6 +176,10 @@ void testBothPresentExplicitly() { SecretsPropertySourceLocator secretsPropertySourceLocator = context.get(SecretsPropertySourceLocator.class); Assertions.assertSame(KubernetesClientSecretsPropertySourceLocator.class, secretsPropertySourceLocator.getClass()); + + Assertions.assertTrue(capturedOutput.getOut() + .contains("Could not create the Kubernetes ApiClient in a cluster environment, because connection port " + + "was not provided.")); } /*