drop internally used record (discovery clean-up part-3) (#1498)

This commit is contained in:
erabii
2023-11-08 16:26:12 +02:00
committed by GitHub
parent 088e27a134
commit 2859fcb09a
4 changed files with 7 additions and 67 deletions

View File

@@ -1,27 +0,0 @@
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.fabric8.discovery;
import java.util.List;
import io.fabric8.kubernetes.api.model.EndpointSubset;
/**
* @author Haytham Mohamed
**/
record EndpointSubsetNS(String namespace, List<EndpointSubset> endpointSubset) {
}

View File

@@ -65,10 +65,6 @@ final class Fabric8KubernetesDiscoveryClientUtils {
}
static EndpointSubsetNS subsetsFromEndpoints(Endpoints endpoints) {
return new EndpointSubsetNS(endpoints.getMetadata().getNamespace(), endpoints.getSubsets());
}
static List<Endpoints> endpoints(KubernetesDiscoveryProperties properties, KubernetesClient client,
KubernetesNamespaceProvider namespaceProvider, String target, @Nullable String serviceName,
Predicate<Service> filter) {

View File

@@ -113,13 +113,12 @@ public class KubernetesDiscoveryClient implements DiscoveryClient, EnvironmentAw
public List<ServiceInstance> getInstances(String serviceId) {
Objects.requireNonNull(serviceId);
List<EndpointSubsetNS> subsetsNS = getEndPointsList(serviceId).stream()
.map(Fabric8KubernetesDiscoveryClientUtils::subsetsFromEndpoints).toList();
List<Endpoints> allEndpoints = getEndPointsList(serviceId).stream().toList();
List<ServiceInstance> instances = new ArrayList<>();
for (EndpointSubsetNS es : subsetsNS) {
// subsetsNS are only those that matched the serviceId
instances.addAll(serviceInstances(es, serviceId));
for (Endpoints endpoints : allEndpoints) {
// endpoints are only those that matched the serviceId
instances.addAll(serviceInstances(endpoints, serviceId));
}
if (properties.includeExternalNameServices()) {
@@ -148,15 +147,15 @@ public class KubernetesDiscoveryClient implements DiscoveryClient, EnvironmentAw
return endpoints(properties, client, namespaceProvider, "fabric8-discovery", serviceId, adapter.filter());
}
private List<ServiceInstance> serviceInstances(EndpointSubsetNS es, String serviceId) {
private List<ServiceInstance> serviceInstances(Endpoints endpoints, String serviceId) {
List<EndpointSubset> subsets = es.endpointSubset();
List<EndpointSubset> subsets = endpoints.getSubsets();
if (subsets.isEmpty()) {
LOG.debug(() -> "serviceId : " + serviceId + " does not have any subsets");
return List.of();
}
String namespace = es.namespace();
String namespace = endpoints.getMetadata().getNamespace();
List<ServiceInstance> instances = new ArrayList<>();
Service service = client.services().inNamespace(namespace).withName(serviceId).get();

View File

@@ -22,12 +22,8 @@ import java.util.Set;
import io.fabric8.kubernetes.api.model.EndpointAddress;
import io.fabric8.kubernetes.api.model.EndpointAddressBuilder;
import io.fabric8.kubernetes.api.model.EndpointPortBuilder;
import io.fabric8.kubernetes.api.model.EndpointSubset;
import io.fabric8.kubernetes.api.model.EndpointSubsetBuilder;
import io.fabric8.kubernetes.api.model.Endpoints;
import io.fabric8.kubernetes.api.model.EndpointsBuilder;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -41,30 +37,6 @@ import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscover
@ExtendWith(OutputCaptureExtension.class)
class KubernetesDiscoveryClientUtilsTests {
@Test
void testSubsetsFromEndpointsEmptySubsets() {
Endpoints endpoints = new EndpointsBuilder()
.withMetadata(new ObjectMetaBuilder().withNamespace("non-default").build()).build();
EndpointSubsetNS result = Fabric8KubernetesDiscoveryClientUtils.subsetsFromEndpoints(endpoints);
Assertions.assertNotNull(result);
Assertions.assertEquals(result.endpointSubset(), List.of());
Assertions.assertEquals(result.namespace(), "non-default");
}
@Test
void testSubsetsFromEndpointsNonEmptySubsets() {
Endpoints endpoints = new EndpointsBuilder().withSubsets((List<EndpointSubset>) null)
.withMetadata(new ObjectMetaBuilder().withNamespace("default").build())
.withSubsets(
new EndpointSubsetBuilder().withPorts(new EndpointPortBuilder().withPort(8080).build()).build())
.build();
EndpointSubsetNS result = Fabric8KubernetesDiscoveryClientUtils.subsetsFromEndpoints(endpoints);
Assertions.assertNotNull(result);
Assertions.assertEquals(result.endpointSubset().size(), 1);
Assertions.assertEquals(result.endpointSubset().get(0).getPorts().get(0).getPort(), 8080);
Assertions.assertEquals(result.namespace(), "default");
}
/**
* <pre>
* - ready addresses are empty