Fix 5.1.3 on branch 2.1.x (#1263)

This commit is contained in:
erabii
2023-03-16 16:34:01 +02:00
committed by GitHub
parent 0fb6b2d52f
commit 1145c01f16
4 changed files with 41 additions and 2 deletions

View File

@@ -143,6 +143,10 @@ public class KubernetesServiceInstance implements ServiceInstance {
}
private URI createUri(String scheme, String host, int port) {
// assume an endpoint without ports
if (port == 0) {
return URI.create(scheme + COLON + DSL + host);
}
return URI.create(scheme + COLON + DSL + host + COLON + port);
}

View File

@@ -218,6 +218,12 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
private int findEndpointPort(EndpointSubset s, String serviceId, String primaryPortName) {
List<EndpointPort> endpointPorts = s.getPorts();
if (endpointPorts.size() == 0) {
log.debug("no ports found for service : " + serviceId + ", will return zero");
return 0;
}
if (endpointPorts.size() == 1) {
return endpointPorts.get(0).getPort();
}

View File

@@ -467,4 +467,33 @@ public class KubernetesDiscoveryClientTest {
.hasSize(1);
}
@Test
public void instanceWithoutPorts() {
Map<String, String> labels = new HashMap<>();
Endpoints endPoint1 = new EndpointsBuilder().withNewMetadata().withName("endpoint5").withNamespace("test")
.withLabels(labels).endMetadata().addNewSubset().addNewAddress().withIp("ip1").withNewTargetRef()
.withUid("130").endTargetRef().endAddress().endSubset().build();
mockClient.endpoints().inNamespace("test").create(endPoint1);
Service service = new ServiceBuilder().withNewMetadata().withName("endpoint5").withNamespace("test")
.withLabels(labels).withAnnotations(labels).endMetadata().build();
mockClient.services().inNamespace("test").create(service);
final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties,
KubernetesClient::services, new ServicePortSecureResolver(properties));
final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint5");
// We're returning the first discovered port to not change previous behaviour
assertThat(instances).hasSize(1).filteredOn(s -> s.getHost().equals("ip1") && !s.isSecure()).hasSize(1)
.filteredOn(s -> s.getUri().toASCIIString().equals("http://ip1"))
.filteredOn(s -> s.getInstanceId().equals("130")).hasSize(1).filteredOn(s -> 0 == s.getPort())
.hasSize(1);
}
}

View File

@@ -68,7 +68,7 @@ class KubernetesServiceListSupplierTests {
@Test
void testPositiveMatch() {
when(environment.getProperty("loadbalancer.client.name")).thenReturn("test-service");
when(mapper.map(any(Service.class))).thenReturn(new KubernetesServiceInstance("", "", "", 0, null, false));
when(mapper.map(any(Service.class))).thenReturn(new KubernetesServiceInstance("", "", "", 8080, null, false));
when(this.client.getNamespace()).thenReturn("test");
when(this.client.services()).thenReturn(this.serviceOperation);
when(this.serviceOperation.inNamespace("test")).thenReturn(namespaceOperation);
@@ -84,7 +84,7 @@ class KubernetesServiceListSupplierTests {
@Test
void testPositiveMatchAllNamespaces() {
when(environment.getProperty("loadbalancer.client.name")).thenReturn("test-service");
when(mapper.map(any(Service.class))).thenReturn(new KubernetesServiceInstance("", "", "", 0, null, false));
when(mapper.map(any(Service.class))).thenReturn(new KubernetesServiceInstance("", "", "", 8080, null, false));
when(this.client.services()).thenReturn(this.serviceOperation);
when(this.serviceOperation.inAnyNamespace()).thenReturn(this.multiDeletable);
when(this.multiDeletable.withField("metadata.name", "test-service")).thenReturn(this.multiDeletable);