improve number of method calls to k8s api server for endpoints catalog watcher (#1393)

This commit is contained in:
erabii
2023-08-02 16:16:24 +03:00
committed by GitHub
parent 504fe36422
commit a9c072b822
3 changed files with 32 additions and 5 deletions

View File

@@ -28,6 +28,7 @@ import io.fabric8.kubernetes.api.model.ObjectReference;
import org.springframework.cloud.kubernetes.commons.discovery.EndpointNameAndNamespace;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.ALWAYS_TRUE;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.endpoints;
/**
@@ -41,7 +42,7 @@ final class Fabric8EndpointsCatalogWatch
@Override
public List<EndpointNameAndNamespace> apply(Fabric8CatalogWatchContext context) {
List<Endpoints> endpoints = endpoints(context.properties(), context.kubernetesClient(),
context.namespaceProvider(), "catalog-watcher", null, x -> true);
context.namespaceProvider(), "catalog-watcher", null, ALWAYS_TRUE);
/**
* <pre>

View File

@@ -70,6 +70,8 @@ final class Fabric8KubernetesDiscoveryClientUtils {
private static final LogAccessor LOG = new LogAccessor(
LogFactory.getLog(Fabric8KubernetesDiscoveryClientUtils.class));
static final Predicate<Service> ALWAYS_TRUE = x -> true;
private Fabric8KubernetesDiscoveryClientUtils() {
}
@@ -225,7 +227,7 @@ final class Fabric8KubernetesDiscoveryClientUtils {
static List<Endpoints> withFilter(List<Endpoints> endpoints, KubernetesDiscoveryProperties properties,
KubernetesClient client, Predicate<Service> filter) {
if (properties.filter() == null || properties.filter().isBlank()) {
if (properties.filter() == null || properties.filter().isBlank() || filter == ALWAYS_TRUE) {
LOG.debug(() -> "filter not present");
return endpoints;
}

View File

@@ -33,6 +33,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.ALWAYS_TRUE;
/**
* @author wind57
*/
@@ -53,7 +55,7 @@ class Fabric8KubernetesDiscoveryClientUtilsFilterTests {
@Test
void withFilterEmptyInput() {
List<Endpoints> result = Fabric8KubernetesDiscoveryClientUtils.withFilter(List.of(), PROPERTIES, client,
x -> true);
ALWAYS_TRUE);
Assertions.assertEquals(result.size(), 0);
}
@@ -87,7 +89,7 @@ class Fabric8KubernetesDiscoveryClientUtilsFilterTests {
Endpoints endpoints = createEndpoints("a", "namespace-a");
createService("a", "namespace-a");
List<Endpoints> result = Fabric8KubernetesDiscoveryClientUtils.withFilter(List.of(endpoints), PROPERTIES,
client, x -> true);
client, ALWAYS_TRUE);
Assertions.assertEquals(result.size(), 1);
}
@@ -112,6 +114,28 @@ class Fabric8KubernetesDiscoveryClientUtilsFilterTests {
Assertions.assertEquals(result.get(0).getMetadata().getNamespace(), "namespace-a");
}
/**
* <pre>
* - Endpoints with name : "a" and namespace "namespace-a", present
* - Endpoints with name : "b" and namespace "namespace-b", present
* - Service with name "a" and namespace "namespace-a" present
* - Predicate that we use is "ALWAYS_TRUE", so no service filter is applied
*
* As such, there is a match, single endpoints as result.
* This test is the same as above with the difference in the predicate.
* It simulates Fabric8EndpointsCatalogWatch::apply
* </pre>
*/
@Test
void withFilterTwoEndpointsOneMatchInServiceAlwaysTruePredicate() {
Endpoints endpointsA = createEndpoints("a", "namespace-a");
Endpoints endpointsB = createEndpoints("b", "namespace-b");
createService("a", "namespace-a");
List<Endpoints> result = Fabric8KubernetesDiscoveryClientUtils.withFilter(List.of(endpointsA, endpointsB),
PROPERTIES, client, ALWAYS_TRUE);
Assertions.assertEquals(result.size(), 2);
}
/**
* <pre>
* - Endpoints with name : "a" and namespace "namespace-a", present
@@ -132,7 +156,7 @@ class Fabric8KubernetesDiscoveryClientUtilsFilterTests {
createService("c", "namespace-c");
List<Endpoints> result = Fabric8KubernetesDiscoveryClientUtils.withFilter(List.of(endpointsA, endpointsB),
PROPERTIES, client, x -> true);
PROPERTIES, client, ALWAYS_TRUE);
Assertions.assertEquals(result.size(), 2);
result = result.stream().sorted(Comparator.comparing(x -> x.getMetadata().getName())).toList();
Assertions.assertEquals(result.get(0).getMetadata().getName(), "a");