From e4982f888d70069c96d62d79cf53f8e8548c06d5 Mon Sep 17 00:00:00 2001 From: erabii Date: Thu, 25 Jul 2024 15:16:00 +0300 Subject: [PATCH] ryanjbaxter Add back missing logging. Fixes 1679 --- .../discovery/DiscoveryClientUtils.java | 18 +++++++++++++++--- .../discovery/DiscoveryClientUtilsTests.java | 8 ++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/DiscoveryClientUtils.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/DiscoveryClientUtils.java index e95db15e4..132a35bc7 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/DiscoveryClientUtils.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/DiscoveryClientUtils.java @@ -28,6 +28,7 @@ import org.springframework.cloud.client.ServiceInstance; import org.springframework.core.log.LogAccessor; +import org.springframework.util.StringUtils; import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.keysWithPrefix; import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.EXTERNAL_NAME; @@ -126,7 +127,8 @@ public static ServicePortNameAndNumber endpointsPort(Map existi return portData.get(); } - logWarnings(); + logServiceSpecificWarning(serviceMetadata.name(), primaryPortName); + logGenericWarning(); Map.Entry first = existingPorts.entrySet().iterator().next(); return new ServicePortNameAndNumber(first.getValue(), first.getKey()); @@ -223,11 +225,21 @@ private static Optional fromMap(Map e } } - private static void logWarnings() { + private static void logServiceSpecificWarning(String serviceName, String primaryPortName) { + if (StringUtils.hasText(primaryPortName)) { + LOG.warn(() -> "Could not find a port named '" + primaryPortName + "', 'https', or 'http' for service '" + + serviceName + "'."); + } + else { + LOG.warn(() -> "Could not find a port named 'https' or 'http' for service '" + serviceName + "'."); + } + } + + private static void logGenericWarning() { LOG.warn(() -> """ Make sure that either the primary-port-name label has been added to the service, or spring.cloud.kubernetes.discovery.primary-port-name has been configured. - Alternatively name the primary port 'https' or 'http' + Alternatively name the primary port 'https' or 'http'. An incorrect configuration may result in non-deterministic behaviour."""); } diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/discovery/DiscoveryClientUtilsTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/discovery/DiscoveryClientUtilsTests.java index 2c16ced06..4b02c4230 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/discovery/DiscoveryClientUtilsTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/discovery/DiscoveryClientUtilsTests.java @@ -526,10 +526,12 @@ void testEndpointsPortNullPrimaryPortName(CapturedOutput output) { .contains("not found primary-port-name (with value: 'null') via properties or service labels")); Assertions.assertTrue(output.getOut().contains("not found primary-port-name via 'https' to match port")); Assertions.assertTrue(output.getOut().contains("not found primary-port-name via 'http' to match port")); + Assertions.assertTrue( + output.getOut().contains("Could not find a port named 'https' or 'http' for service 'spring-k8s'.")); Assertions.assertTrue(output.getOut().contains(""" Make sure that either the primary-port-name label has been added to the service, or spring.cloud.kubernetes.discovery.primary-port-name has been configured. - Alternatively name the primary port 'https' or 'http' + Alternatively name the primary port 'https' or 'http'. An incorrect configuration may result in non-deterministic behaviour.""")); } @@ -562,10 +564,12 @@ void testEndpointsPortPrimaryPortNameIsPresentButNotFound(CapturedOutput output) .contains("not found primary-port-name (with value: 'three') via properties or service labels")); Assertions.assertTrue(output.getOut().contains("not found primary-port-name via 'https' to match port")); Assertions.assertTrue(output.getOut().contains("not found primary-port-name via 'http' to match port")); + Assertions.assertTrue(output.getOut() + .contains("Could not find a port named 'three', 'https', or 'http' for service 'spring-k8s'.")); Assertions.assertTrue(output.getOut().contains(""" Make sure that either the primary-port-name label has been added to the service, or spring.cloud.kubernetes.discovery.primary-port-name has been configured. - Alternatively name the primary port 'https' or 'http' + Alternatively name the primary port 'https' or 'http'. An incorrect configuration may result in non-deterministic behaviour.""")); }