fix (#1229)
This commit is contained in:
@@ -20,8 +20,8 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.fabric8.kubernetes.api.model.EndpointAddress;
|
||||
import io.fabric8.kubernetes.api.model.EndpointPort;
|
||||
@@ -29,14 +29,13 @@ import io.fabric8.kubernetes.api.model.EndpointSubset;
|
||||
import io.fabric8.kubernetes.api.model.Endpoints;
|
||||
import io.fabric8.kubernetes.api.model.Service;
|
||||
import io.fabric8.kubernetes.client.KubernetesClient;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.core.log.LogAccessor;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -55,7 +54,7 @@ import static org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesD
|
||||
*/
|
||||
public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
private static final Log log = LogFactory.getLog(KubernetesDiscoveryClient.class);
|
||||
private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(KubernetesDiscoveryClient.class));
|
||||
|
||||
private final KubernetesDiscoveryProperties properties;
|
||||
|
||||
@@ -97,20 +96,20 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Kubernetes Discovery Client";
|
||||
return "Fabric8 Kubernetes Discovery Client";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServiceInstance> getInstances(String serviceId) {
|
||||
Assert.notNull(serviceId, "[Assertion failed] - the object argument must not be null");
|
||||
Objects.requireNonNull(serviceId);
|
||||
|
||||
List<EndpointSubsetNS> subsetsNS = this.getEndPointsList(serviceId).stream()
|
||||
.map(x -> subsetsFromEndpoints(x, () -> client.getNamespace())).collect(Collectors.toList());
|
||||
List<EndpointSubsetNS> subsetsNS = getEndPointsList(serviceId).stream()
|
||||
.map(x -> subsetsFromEndpoints(x, () -> client.getNamespace())).toList();
|
||||
|
||||
List<ServiceInstance> instances = new ArrayList<>();
|
||||
if (!subsetsNS.isEmpty()) {
|
||||
for (EndpointSubsetNS es : subsetsNS) {
|
||||
instances.addAll(this.getNamespaceServiceInstances(es, serviceId));
|
||||
instances.addAll(getNamespaceServiceInstances(es, serviceId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,12 +117,12 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
}
|
||||
|
||||
public List<Endpoints> getEndPointsList(String serviceId) {
|
||||
if (this.properties.allNamespaces()) {
|
||||
return this.client.endpoints().inAnyNamespace().withField("metadata.name", serviceId)
|
||||
if (properties.allNamespaces()) {
|
||||
return client.endpoints().inAnyNamespace().withField("metadata.name", serviceId)
|
||||
.withLabels(properties.serviceLabels()).list().getItems();
|
||||
}
|
||||
if (properties.namespaces().isEmpty()) {
|
||||
return this.client.endpoints().withField("metadata.name", serviceId).withLabels(properties.serviceLabels())
|
||||
return client.endpoints().withField("metadata.name", serviceId).withLabels(properties.serviceLabels())
|
||||
.list().getItems();
|
||||
}
|
||||
return findEndPointsFilteredByNamespaces(serviceId);
|
||||
@@ -143,9 +142,9 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
List<EndpointSubset> subsets = es.endpointSubset();
|
||||
List<ServiceInstance> instances = new ArrayList<>();
|
||||
if (!subsets.isEmpty()) {
|
||||
final Service service = this.client.services().inNamespace(namespace).withName(serviceId).get();
|
||||
final Map<String, String> serviceMetadata = serviceMetadata(serviceId, service, properties);
|
||||
KubernetesDiscoveryProperties.Metadata metadataProps = this.properties.metadata();
|
||||
Service service = client.services().inNamespace(namespace).withName(serviceId).get();
|
||||
Map<String, String> serviceMetadata = serviceMetadata(serviceId, service, properties);
|
||||
KubernetesDiscoveryProperties.Metadata metadataProps = properties.metadata();
|
||||
|
||||
for (EndpointSubset s : subsets) {
|
||||
// Extend the service metadata map with per-endpoint port information (if
|
||||
@@ -156,19 +155,17 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
.filter(port -> StringUtils.hasText(port.getName()))
|
||||
.collect(toMap(EndpointPort::getName, port -> Integer.toString(port.getPort())));
|
||||
Map<String, String> portMetadata = keysWithPrefix(ports, metadataProps.portsPrefix());
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Adding port metadata: " + portMetadata);
|
||||
}
|
||||
LOG.debug(() -> "Adding port metadata: " + portMetadata);
|
||||
endpointMetadata.putAll(portMetadata);
|
||||
}
|
||||
|
||||
if (this.properties.allNamespaces()) {
|
||||
if (properties.allNamespaces()) {
|
||||
endpointMetadata.put(NAMESPACE_METADATA_KEY, namespace);
|
||||
}
|
||||
|
||||
List<EndpointAddress> addresses = s.getAddresses();
|
||||
|
||||
if (this.properties.includeNotReadyAddresses() && !CollectionUtils.isEmpty(s.getNotReadyAddresses())) {
|
||||
if (properties.includeNotReadyAddresses() && !CollectionUtils.isEmpty(s.getNotReadyAddresses())) {
|
||||
if (addresses == null) {
|
||||
addresses = new ArrayList<>();
|
||||
}
|
||||
@@ -183,7 +180,7 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
}
|
||||
instances.add(new DefaultKubernetesServiceInstance(instanceId, serviceId, endpointAddress.getIp(),
|
||||
endpointPort, endpointMetadata,
|
||||
this.servicePortSecureResolver.resolve(new ServicePortSecureResolver.Input(endpointPort,
|
||||
servicePortSecureResolver.resolve(new ServicePortSecureResolver.Input(endpointPort,
|
||||
service.getMetadata().getName(), service.getMetadata().getLabels(),
|
||||
service.getMetadata().getAnnotations()))));
|
||||
}
|
||||
@@ -206,7 +203,7 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return this.properties.order();
|
||||
return properties.order();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user