Lb both clients fix issue (#1628)
This commit is contained in:
@@ -40,6 +40,7 @@ import org.springframework.core.log.LogAccessor;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static java.util.Optional.ofNullable;
|
||||
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.NON_DETERMINISTIC_PORT_MESSAGE;
|
||||
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.PORT_NAME_PROPERTY;
|
||||
import static org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver.Input;
|
||||
|
||||
@@ -102,9 +103,9 @@ public class KubernetesClientServiceInstanceMapper implements KubernetesServiceI
|
||||
}
|
||||
}
|
||||
else {
|
||||
LOG.warn(() -> PORT_NAME_PROPERTY + " is not set, as such will not consider service with name : "
|
||||
+ metadata.getName());
|
||||
return null;
|
||||
LOG.warn(() -> PORT_NAME_PROPERTY + " is not set");
|
||||
LOG.warn(() -> NON_DETERMINISTIC_PORT_MESSAGE);
|
||||
port = ports.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +136,7 @@ public class KubernetesClientServiceInstanceMapper implements KubernetesServiceI
|
||||
|
||||
private void logWarning(String portNameFromProperties) {
|
||||
LOG.warn(() -> "Did not find a port name that is equal to the value " + portNameFromProperties);
|
||||
LOG.warn(() -> "Will return 'first' port found, which is non-deterministic");
|
||||
LOG.warn(() -> NON_DETERMINISTIC_PORT_MESSAGE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -192,6 +192,25 @@ class KubernetesClientServiceInstanceMapperTests {
|
||||
Assertions.assertTrue(serviceInstance.getPort() == 80 || serviceInstance.getPort() == 443);
|
||||
}
|
||||
|
||||
@Test
|
||||
void multiPortsEmptyPortNameProperty(CapturedOutput output) {
|
||||
KubernetesLoadBalancerProperties loadBalancerProperties = new KubernetesLoadBalancerProperties();
|
||||
loadBalancerProperties.setPortName("");
|
||||
KubernetesClientServiceInstanceMapper mapper = new KubernetesClientServiceInstanceMapper(loadBalancerProperties,
|
||||
KubernetesDiscoveryProperties.DEFAULT);
|
||||
|
||||
Map<String, String> annotations = Map.of("org.springframework.cloud", "true");
|
||||
Map<String, String> labels = Map.of("beta", "true");
|
||||
List<V1ServicePort> servicePorts = List.of(new V1ServicePortBuilder().withName("http-api").withPort(80).build(),
|
||||
new V1ServicePortBuilder().withName("https").withPort(443).build());
|
||||
V1Service service = createService("database", "default", annotations, labels, servicePorts);
|
||||
KubernetesServiceInstance serviceInstance = mapper.map(service);
|
||||
Assertions.assertNotNull(serviceInstance);
|
||||
Assertions.assertTrue(output.getOut().contains("'spring.cloud.kubernetes.loadbalancer.portName' is not set"));
|
||||
Assertions.assertTrue(output.getOut().contains("Will return 'first' port found, which is non-deterministic"));
|
||||
Assertions.assertTrue(serviceInstance.getPort() == 80 || serviceInstance.getPort() == 443);
|
||||
}
|
||||
|
||||
private V1Service createService(String name, String namespace, Map<String, String> annotations,
|
||||
Map<String, String> labels, List<V1ServicePort> servicePorts) {
|
||||
return new V1ServiceBuilder()
|
||||
|
||||
@@ -103,4 +103,9 @@ public final class KubernetesDiscoveryConstants {
|
||||
*/
|
||||
public static final String PORT_NAME_PROPERTY = "'spring.cloud.kubernetes.loadbalancer.portName'";
|
||||
|
||||
/**
|
||||
* message for non-deterministic port.
|
||||
*/
|
||||
public static final String NON_DETERMINISTIC_PORT_MESSAGE = "Will return 'first' port found, which is non-deterministic";
|
||||
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import org.springframework.cloud.kubernetes.fabric8.Fabric8Utils;
|
||||
import org.springframework.core.log.LogAccessor;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.NON_DETERMINISTIC_PORT_MESSAGE;
|
||||
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.PORT_NAME_PROPERTY;
|
||||
import static org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver.Input;
|
||||
|
||||
@@ -101,9 +102,9 @@ public class Fabric8ServiceInstanceMapper implements KubernetesServiceInstanceMa
|
||||
}
|
||||
}
|
||||
else {
|
||||
LOG.warn(() -> PORT_NAME_PROPERTY + " is not set, as such will not consider service with name : "
|
||||
+ metadata.getName());
|
||||
return null;
|
||||
LOG.warn(() -> PORT_NAME_PROPERTY + " is not set");
|
||||
LOG.warn(() -> NON_DETERMINISTIC_PORT_MESSAGE);
|
||||
port = ports.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +131,7 @@ public class Fabric8ServiceInstanceMapper implements KubernetesServiceInstanceMa
|
||||
|
||||
private void logWarning(String portNameFromProperties) {
|
||||
LOG.warn(() -> "Did not find a port name that is equal to the value " + portNameFromProperties);
|
||||
LOG.warn(() -> "Will return 'first' port found, which is non-deterministic");
|
||||
LOG.warn(() -> NON_DETERMINISTIC_PORT_MESSAGE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -217,7 +217,7 @@ class Fabric8ServiceInstanceMapperTests {
|
||||
/**
|
||||
* <pre>
|
||||
* service has multiple ServicePorts, and 'spring.cloud.kubernetes.loadbalancer.portName' is empty.
|
||||
* in this case, service will be skipped.
|
||||
* in this case, a single, 'first', port will be returned.
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
@@ -234,16 +234,15 @@ class Fabric8ServiceInstanceMapperTests {
|
||||
KubernetesServiceInstance result = new Fabric8ServiceInstanceMapper(loadBalancerProperties, discoveryProperties)
|
||||
.map(service);
|
||||
|
||||
Assertions.assertNull(result);
|
||||
Assertions.assertTrue(output.getOut().contains(
|
||||
"'spring.cloud.kubernetes.loadbalancer.portName' is not set, as such will not consider service with name : test"));
|
||||
Assertions.assertNotNull(result);
|
||||
Assertions.assertTrue(output.getOut().contains("'spring.cloud.kubernetes.loadbalancer.portName' is not set"));
|
||||
Assertions.assertTrue(output.getOut().contains("Will return 'first' port found, which is non-deterministic"));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* service has multiple ServicePorts, and 'spring.cloud.kubernetes.loadbalancer.portName' is empty.
|
||||
* in this case, service will be skipped.
|
||||
* service has multiple ServicePorts, and 'spring.cloud.kubernetes.loadbalancer.portName' is not empty.
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
@@ -267,8 +266,8 @@ class Fabric8ServiceInstanceMapperTests {
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* service has multiple ServicePorts, and 'spring.cloud.kubernetes.loadbalancer.portName' is empty.
|
||||
* in this case, service will be skipped.
|
||||
* service has multiple ServicePorts, and 'spring.cloud.kubernetes.loadbalancer.portName' is not empty.
|
||||
* property name also does not match 'potName'
|
||||
* </pre>
|
||||
*/
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user