223: Fix issue with nullable targetRefs and addresses in endpoints

This commit is contained in:
Stefan Hudelmaier
2018-08-12 14:25:36 +02:00
committed by Ioannis Canellos
parent e4ab0f7b0d
commit c48f1d128b
2 changed files with 43 additions and 2 deletions

View File

@@ -17,7 +17,10 @@
package org.springframework.cloud.kubernetes.discovery;
import io.fabric8.kubernetes.api.model.EndpointAddress;
import io.fabric8.kubernetes.api.model.EndpointSubset;
import io.fabric8.kubernetes.api.model.Endpoints;
import io.fabric8.kubernetes.api.model.ObjectReference;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -26,7 +29,9 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
@@ -62,8 +67,12 @@ public class KubernetesCatalogWatch implements ApplicationEventPublisherAware {
List<String> endpointsPodNames =
endpoints.stream()
.flatMap(endpoint -> endpoint.getSubsets().stream())
.flatMap(subset -> subset.getAddresses().stream())
.map(endpointAddress -> endpointAddress.getTargetRef().getName()) // pod name unique in namespace
.map(EndpointSubset::getAddresses)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(EndpointAddress::getTargetRef)
.filter(Objects::nonNull)
.map(ObjectReference::getName) // pod name unique in namespace
.sorted(String::compareTo).collect(Collectors.toList());
catalogEndpointsState.set(endpointsPodNames);

View File

@@ -114,6 +114,38 @@ public class KubernetesCatalogWatchTest {
assertEquals(expectedPodsList, event.getValue());
}
@Test
public void testEndpointsWithoutAddresses() {
EndpointsList endpoints = createSingleEndpointEndpointListByPodName("api-pod");
endpoints.getItems().get(0).getSubsets().get(0).setAddresses(null);
when(endpointsOperation.list()).thenReturn(endpoints);
when(kubernetesClient.endpoints()).thenReturn(endpointsOperation);
underTest.catalogServicesWatch();
// second execution on shuffleServices
underTest.catalogServicesWatch();
verify(applicationEventPublisher).publishEvent(any(HeartbeatEvent.class));
}
@Test
public void testEndpointsWithoutTargetRefs() {
EndpointsList endpoints = createSingleEndpointEndpointListByPodName("api-pod");
endpoints.getItems().get(0).getSubsets().get(0).getAddresses().get(0).setTargetRef(null);
when(endpointsOperation.list()).thenReturn(endpoints);
when(kubernetesClient.endpoints()).thenReturn(endpointsOperation);
underTest.catalogServicesWatch();
// second execution on shuffleServices
underTest.catalogServicesWatch();
verify(applicationEventPublisher).publishEvent(any(HeartbeatEvent.class));
}
private EndpointsList createEndpointsListByServiceName(String... serviceNames) {
List<Endpoints> endpoints = stream(serviceNames)