-
Notifications
You must be signed in to change notification settings - Fork 1k
move ports metadata computation to the dedicated method #1231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
0c3ec3e
f143df6
7fc0bb9
01596ee
5cad385
2d2152a
f09961d
164c0a1
436fd0c
0f4d015
3ef6fa1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,14 +17,12 @@ | |
package org.springframework.cloud.kubernetes.fabric8.discovery; | ||
|
||
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 io.fabric8.kubernetes.api.model.EndpointAddress; | ||
import io.fabric8.kubernetes.api.model.EndpointPort; | ||
import io.fabric8.kubernetes.api.model.EndpointSubset; | ||
import io.fabric8.kubernetes.api.model.Endpoints; | ||
import io.fabric8.kubernetes.api.model.Service; | ||
|
@@ -37,10 +35,7 @@ | |
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; | ||
import org.springframework.core.log.LogAccessor; | ||
import org.springframework.util.CollectionUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
import static java.util.stream.Collectors.toMap; | ||
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.keysWithPrefix; | ||
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.NAMESPACE_METADATA_KEY; | ||
import static org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClientUtils.endpoints; | ||
import static org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClientUtils.endpointsPort; | ||
|
@@ -135,52 +130,47 @@ else if (properties.namespaces().isEmpty()) { | |
} | ||
|
||
private List<ServiceInstance> getNamespaceServiceInstances(EndpointSubsetNS es, String serviceId) { | ||
String namespace = es.namespace(); | ||
|
||
List<EndpointSubset> subsets = es.endpointSubset(); | ||
if (subsets.isEmpty()) { | ||
LOG.debug(() -> "serviceId : " + serviceId + " does not have any subsets"); | ||
return List.of(); | ||
} | ||
|
||
String namespace = es.namespace(); | ||
List<ServiceInstance> instances = new ArrayList<>(); | ||
if (!subsets.isEmpty()) { | ||
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 | ||
// requested) | ||
Map<String, String> endpointMetadata = new HashMap<>(serviceMetadata); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. port metadata has moved to the method that does all the service's metadata computation. imho, this is a lot cleaner, as there is a single entry point where metadata is computed. The rest of the changes is just formatting, because of the dropped if statement (the one that I inverted). |
||
if (metadataProps.addPorts()) { | ||
Map<String, String> ports = s.getPorts().stream() | ||
.filter(port -> StringUtils.hasText(port.getName())) | ||
.collect(toMap(EndpointPort::getName, port -> Integer.toString(port.getPort()))); | ||
Map<String, String> portMetadata = keysWithPrefix(ports, metadataProps.portsPrefix()); | ||
LOG.debug(() -> "Adding port metadata: " + portMetadata); | ||
endpointMetadata.putAll(portMetadata); | ||
} | ||
|
||
if (properties.allNamespaces()) { | ||
endpointMetadata.put(NAMESPACE_METADATA_KEY, namespace); | ||
} | ||
Service service = client.services().inNamespace(namespace).withName(serviceId).get(); | ||
Map<String, String> serviceMetadata = serviceMetadata(serviceId, service, properties, subsets); | ||
|
||
for (EndpointSubset endpointSubset : subsets) { | ||
|
||
List<EndpointAddress> addresses = s.getAddresses(); | ||
if (properties.allNamespaces()) { | ||
serviceMetadata.put(NAMESPACE_METADATA_KEY, namespace); | ||
} | ||
|
||
if (properties.includeNotReadyAddresses() && !CollectionUtils.isEmpty(s.getNotReadyAddresses())) { | ||
if (addresses == null) { | ||
addresses = new ArrayList<>(); | ||
} | ||
addresses.addAll(s.getNotReadyAddresses()); | ||
List<EndpointAddress> addresses = endpointSubset.getAddresses(); | ||
|
||
if (properties.includeNotReadyAddresses() | ||
&& !CollectionUtils.isEmpty(endpointSubset.getNotReadyAddresses())) { | ||
if (addresses == null) { | ||
addresses = new ArrayList<>(); | ||
} | ||
addresses.addAll(endpointSubset.getNotReadyAddresses()); | ||
} | ||
|
||
for (EndpointAddress endpointAddress : addresses) { | ||
int endpointPort = endpointsPort(s, serviceId, properties, service); | ||
String instanceId = null; | ||
if (endpointAddress.getTargetRef() != null) { | ||
instanceId = endpointAddress.getTargetRef().getUid(); | ||
} | ||
instances.add(new DefaultKubernetesServiceInstance(instanceId, serviceId, endpointAddress.getIp(), | ||
endpointPort, endpointMetadata, | ||
servicePortSecureResolver.resolve(new ServicePortSecureResolver.Input(endpointPort, | ||
service.getMetadata().getName(), service.getMetadata().getLabels(), | ||
service.getMetadata().getAnnotations())))); | ||
for (EndpointAddress endpointAddress : addresses) { | ||
int endpointPort = endpointsPort(endpointSubset, serviceId, properties, service); | ||
String instanceId = null; | ||
if (endpointAddress.getTargetRef() != null) { | ||
instanceId = endpointAddress.getTargetRef().getUid(); | ||
} | ||
instances | ||
.add(new DefaultKubernetesServiceInstance(instanceId, serviceId, endpointAddress.getIp(), | ||
endpointPort, serviceMetadata, | ||
servicePortSecureResolver.resolve(new ServicePortSecureResolver.Input(endpointPort, | ||
service.getMetadata().getName(), service.getMetadata().getLabels(), | ||
service.getMetadata().getAnnotations())))); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
import org.springframework.core.log.LogAccessor; | ||
import org.springframework.util.StringUtils; | ||
|
||
import static java.util.stream.Collectors.toMap; | ||
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.keysWithPrefix; | ||
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTP; | ||
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTPS; | ||
|
@@ -121,8 +122,11 @@ static String primaryPortName(KubernetesDiscoveryProperties properties, Service | |
return primaryPortName; | ||
} | ||
|
||
/** | ||
* labels, annotations and ports metadata. | ||
*/ | ||
static Map<String, String> serviceMetadata(String serviceId, Service service, | ||
KubernetesDiscoveryProperties properties) { | ||
KubernetesDiscoveryProperties properties, List<EndpointSubset> endpointSubsets) { | ||
Map<String, String> serviceMetadata = new HashMap<>(); | ||
KubernetesDiscoveryProperties.Metadata metadataProps = properties.metadata(); | ||
if (metadataProps.addLabels()) { | ||
|
@@ -138,6 +142,16 @@ static Map<String, String> serviceMetadata(String serviceId, Service service, | |
serviceMetadata.putAll(annotationMetadata); | ||
} | ||
|
||
if (metadataProps.addPorts()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add port metadata to be computed here. We have a single method now responsible for all service metadata. |
||
Map<String, String> ports = endpointSubsets.stream() | ||
.flatMap(endpointSubset -> endpointSubset.getPorts().stream()) | ||
.filter(port -> StringUtils.hasText(port.getName())) | ||
.collect(toMap(EndpointPort::getName, port -> Integer.toString(port.getPort()))); | ||
Map<String, String> portMetadata = keysWithPrefix(ports, properties.metadata().portsPrefix()); | ||
LOG.debug(() -> "Adding port metadata: " + portMetadata + " for serviceId : " + serviceId); | ||
serviceMetadata.putAll(portMetadata); | ||
} | ||
|
||
return serviceMetadata; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slightly invert the logic here, if
subsets
is empty, return early.