Skip to content
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

move ports metadata computation to the dedicated method #1231

Merged
merged 11 commits into from
Feb 21, 2023

Conversation

wind57
Copy link
Contributor

@wind57 wind57 commented Feb 18, 2023

No description provided.

@@ -39,14 +40,14 @@ private Fabric8Utils() {
private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(Fabric8Utils.class));

/**
* this method does the namespace resolution for both config map and secrets
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to update this documentation a while ago, as it is re-used in other places as well, and will be used in discovery also at some point in time, its on my TODO list

return findEndPointsFilteredByNamespaces(serviceId);
}

private List<Endpoints> findEndPointsFilteredByNamespaces(String serviceId) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first, I inlined findEndPointsFilteredByNamespaces into getEndPointsList, so the code became a bit simpler to read:

	public List<Endpoints> getEndPointsList(String serviceId) {
		if (properties.allNamespaces()) {
			LOG.debug(() -> "searching for endpoints in all namespaces");
			return client.endpoints().inAnyNamespace().withField("metadata.name", serviceId)
					.withLabels(properties.serviceLabels()).list().getItems();
		}
		else if (properties.namespaces().isEmpty()) {
			LOG.debug(() -> "searching for endpoints in namespace : " + client.getNamespace());
			return client.endpoints().withField("metadata.name", serviceId).withLabels(properties.serviceLabels())
					.list().getItems();
		}
		else {
			LOG.debug(() -> "searching for endpoints in namespaces : " + properties.namespaces());
			List<Endpoints> endpoints = new ArrayList<>();
			for (String namespace : properties.namespaces()) {
				endpoints.addAll(client.endpoints().inNamespace(namespace).withField("metadata.name", serviceId)
					.withLabels(properties.serviceLabels()).list().getItems());
			}
			return endpoints;
		}
	}

Then I noticed that we re-use this withField("metadata.name", serviceId) .withLabels(properties.serviceLabels()).list().getItems() in all 3 branches, so I had to find a way to extract it. I re-used an addition in the fabric8 client called FilterNested, basically you can do:

FilterNested<FilterWatchListDeletable<Endpoints, EndpointsList, Resource<Endpoints>>> filterNested = client.endpoints().inAnyNamespace().withNewFilter();

and then chain the filters you want. So I created a static method:

	static List<Endpoints> endpoints(
			FilterNested<FilterWatchListDeletable<Endpoints, EndpointsList, Resource<Endpoints>>> filterNested,
			KubernetesDiscoveryProperties properties, String serviceId) {
		return filterNested.withField("metadata.name", serviceId).withLabels(properties.serviceLabels()).endFilter()
				.list().getItems();
	}

in KubernetesDiscoveryClientUtils and refactored the code to:

	public List<Endpoints> getEndPointsList(String serviceId) {
		if (properties.allNamespaces()) {
			LOG.debug(() -> "searching for endpoints in all namespaces");
			return endpoints(client.endpoints().inAnyNamespace().withNewFilter(), properties, serviceId);
		}
		else if (properties.namespaces().isEmpty()) {
			LOG.debug(() -> "searching for endpoints in namespace : " + client.getNamespace());
			return endpoints(client.endpoints().withNewFilter(), properties, serviceId);
		}
		else {
			LOG.debug(() -> "searching for endpoints in namespaces : " + properties.namespaces());
			List<Endpoints> endpoints = new ArrayList<>();
			for (String namespace : properties.namespaces()) {
				endpoints.addAll(
						endpoints(client.endpoints().inNamespace(namespace).withNewFilter(), properties, serviceId));
			}
			return endpoints;
		}
	}

I've also adjusted some failing tests and added some more.

@@ -138,6 +142,16 @@ static Map<String, String> serviceMetadata(String serviceId, Service service,
serviceMetadata.putAll(annotationMetadata);
}

if (metadataProps.addPorts()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

List<EndpointSubset> subsets = es.endpointSubset();
if (subsets.isEmpty()) {
Copy link
Contributor Author

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.

for (EndpointSubset s : subsets) {
// Extend the service metadata map with per-endpoint port information (if
// requested)
Map<String, String> endpointMetadata = new HashMap<>(serviceMetadata);
Copy link
Contributor Author

@wind57 wind57 Feb 21, 2023

Choose a reason for hiding this comment

The 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).

@wind57 wind57 changed the title Simplify the logic a bit move ports metadata computation to the dedicated method Feb 21, 2023
@wind57 wind57 marked this pull request as ready for review February 21, 2023 09:11
@wind57
Copy link
Contributor Author

wind57 commented Feb 21, 2023

@ryanjbaxter ready to be looked at. thank you.

@ryanjbaxter ryanjbaxter added this to the 3.0.2 milestone Feb 21, 2023
@ryanjbaxter ryanjbaxter merged commit 36dce02 into spring-cloud:main Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants