-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix 1641 part 3 : make fabric8 discovery client cacheable (3) #2064
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
Merged
ryanjbaxter
merged 8 commits into
spring-cloud:main
from
wind57:fix-1641-part-3-make-fabric8-discovery-client-cacheable
Oct 12, 2025
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
328f11d
fix-1641: fabric8
wind57 f091421
Merge branch 'fix-1641-part-2' into fix-1641-part-3-make-fabric8-disc…
wind57 b530aa9
Merge branch 'fix-1641-part-1' into fix-1641-part-3-make-fabric8-disc…
wind57 e711cb5
fix-1641: add fabric8 cacheable discovery implementation
wind57 8c91ccd
test
wind57 8b29d05
fix test
wind57 4e5a850
Merge branch 'main' into fix-1641-part-3-make-fabric8-discovery-clien…
wind57 0e51c75
review comments
wind57 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...k/cloud/kubernetes/commons/discovery/ConditionalOnDiscoveryCacheableBlockingDisabled.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright 2013-present 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.commons.discovery; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Inherited; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| import org.springframework.boot.autoconfigure.condition.NoneNestedConditions; | ||
| import org.springframework.context.annotation.Conditional; | ||
|
|
||
| /** | ||
| * Reverse of {@link ConditionalOnDiscoveryCacheableBlockingEnabled}. | ||
| * | ||
| * @author wind57 | ||
| */ | ||
| @Target({ ElementType.TYPE, ElementType.METHOD }) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Documented | ||
| @Inherited | ||
| @Conditional(ConditionalOnDiscoveryCacheableBlockingDisabled.OnDiscoveryCacheableBlockingDisabled.class) | ||
| public @interface ConditionalOnDiscoveryCacheableBlockingDisabled { | ||
|
|
||
| class OnDiscoveryCacheableBlockingDisabled extends NoneNestedConditions { | ||
|
|
||
| OnDiscoveryCacheableBlockingDisabled() { | ||
| super(ConfigurationPhase.REGISTER_BEAN); | ||
| } | ||
|
|
||
| @ConditionalOnDiscoveryCacheableBlockingEnabled | ||
| static class OnDisabled { | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
43 changes: 43 additions & 0 deletions
43
...rk/cloud/kubernetes/commons/discovery/ConditionalOnDiscoveryCacheableBlockingEnabled.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright 2013-present 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.commons.discovery; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Inherited; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
|
|
||
| /** | ||
| * Provides a more succinct conditional for: | ||
| * <code>spring.cloud.kubernetes.discovery.cacheable.blocking.enabled</code>. This | ||
| * annotation says that caching in the blocking implementation is enabled. | ||
| * | ||
| * @author wind57 | ||
| */ | ||
| @Target({ ElementType.TYPE, ElementType.METHOD }) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Documented | ||
| @Inherited | ||
| @ConditionalOnProperty(value = "spring.cloud.kubernetes.discovery.cacheable.blocking.enabled", havingValue = "true", | ||
| matchIfMissing = false) | ||
| public @interface ConditionalOnDiscoveryCacheableBlockingEnabled { | ||
|
|
||
| } |
54 changes: 54 additions & 0 deletions
54
...k/cloud/kubernetes/commons/discovery/ConditionalOnDiscoveryCacheableReactiveDisabled.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright 2013-present 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.commons.discovery; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Inherited; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| import org.springframework.boot.autoconfigure.condition.NoneNestedConditions; | ||
| import org.springframework.context.annotation.Conditional; | ||
|
|
||
| /** | ||
| * Reverse of {@link ConditionalOnDiscoveryCacheableReactiveEnabled}. | ||
| * | ||
| * @author wind57 | ||
| */ | ||
| @Target({ ElementType.TYPE, ElementType.METHOD }) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Documented | ||
| @Inherited | ||
| @Conditional(ConditionalOnDiscoveryCacheableReactiveDisabled.OnDiscoveryCacheableReactiveDisabled.class) | ||
| public @interface ConditionalOnDiscoveryCacheableReactiveDisabled { | ||
|
|
||
| class OnDiscoveryCacheableReactiveDisabled extends NoneNestedConditions { | ||
|
|
||
| OnDiscoveryCacheableReactiveDisabled() { | ||
| super(ConfigurationPhase.REGISTER_BEAN); | ||
| } | ||
|
|
||
| @ConditionalOnDiscoveryCacheableReactiveEnabled | ||
| static class OnDisabled { | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
43 changes: 43 additions & 0 deletions
43
...rk/cloud/kubernetes/commons/discovery/ConditionalOnDiscoveryCacheableReactiveEnabled.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright 2013-present 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.commons.discovery; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Inherited; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
|
|
||
| /** | ||
| * Provides a more succinct conditional for: | ||
| * <code>spring.cloud.kubernetes.discovery.cacheable.reactive.enabled</code>. This | ||
| * annotation says that caching in the reactive implementation is enabled. | ||
| * | ||
| * @author wind57 | ||
| */ | ||
| @Target({ ElementType.TYPE, ElementType.METHOD }) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Documented | ||
| @Inherited | ||
| @ConditionalOnProperty(value = "spring.cloud.kubernetes.discovery.cacheable.reactive.enabled", havingValue = "true", | ||
| matchIfMissing = false) | ||
| public @interface ConditionalOnDiscoveryCacheableReactiveEnabled { | ||
|
|
||
| } |
175 changes: 175 additions & 0 deletions
175
...gframework/cloud/kubernetes/fabric8/discovery/Fabric8AbstractBlockingDiscoveryClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| /* | ||
| * Copyright 2013-present 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.ArrayList; | ||
| 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.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.LogFactory; | ||
|
|
||
| import org.springframework.cloud.client.ServiceInstance; | ||
| import org.springframework.cloud.client.discovery.DiscoveryClient; | ||
| import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; | ||
| import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; | ||
| import org.springframework.cloud.kubernetes.commons.discovery.ServiceMetadata; | ||
| import org.springframework.cloud.kubernetes.commons.discovery.ServicePortNameAndNumber; | ||
| import org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver; | ||
| import org.springframework.core.log.LogAccessor; | ||
|
|
||
| import static org.springframework.cloud.kubernetes.commons.discovery.DiscoveryClientUtils.endpointsPort; | ||
| import static org.springframework.cloud.kubernetes.commons.discovery.DiscoveryClientUtils.externalNameServiceInstance; | ||
| import static org.springframework.cloud.kubernetes.commons.discovery.DiscoveryClientUtils.serviceInstance; | ||
| import static org.springframework.cloud.kubernetes.commons.discovery.DiscoveryClientUtils.serviceInstanceMetadata; | ||
| import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.EXTERNAL_NAME; | ||
| import static org.springframework.cloud.kubernetes.fabric8.Fabric8Utils.serviceMetadata; | ||
| import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.addresses; | ||
| import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.endpointSubsetsPortData; | ||
| import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.endpoints; | ||
| import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.services; | ||
| import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8InstanceIdHostPodNameSupplier.externalName; | ||
| import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8InstanceIdHostPodNameSupplier.nonExternalName; | ||
|
|
||
| /** | ||
| * @author wind57 | ||
| */ | ||
| abstract class Fabric8AbstractBlockingDiscoveryClient implements DiscoveryClient { | ||
|
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. all the existing code AS-IS, from the DiscoveryClient has moved in here, without any kind of changes. Now both cacheable and non-cacheable discovery clients extend it. |
||
|
|
||
| private static final LogAccessor LOG = new LogAccessor( | ||
| LogFactory.getLog(Fabric8AbstractBlockingDiscoveryClient.class)); | ||
|
|
||
| private final KubernetesDiscoveryProperties properties; | ||
|
|
||
| private final ServicePortSecureResolver servicePortSecureResolver; | ||
|
|
||
| private final KubernetesClient client; | ||
|
|
||
| private final KubernetesNamespaceProvider namespaceProvider; | ||
|
|
||
| private final Predicate<Service> predicate; | ||
|
|
||
| Fabric8AbstractBlockingDiscoveryClient(KubernetesClient client, | ||
| KubernetesDiscoveryProperties kubernetesDiscoveryProperties, | ||
| ServicePortSecureResolver servicePortSecureResolver, KubernetesNamespaceProvider namespaceProvider, | ||
| Predicate<Service> predicate) { | ||
|
|
||
| this.client = client; | ||
| this.properties = kubernetesDiscoveryProperties; | ||
| this.servicePortSecureResolver = servicePortSecureResolver; | ||
| this.namespaceProvider = namespaceProvider; | ||
| this.predicate = predicate; | ||
| } | ||
|
|
||
| public abstract String description(); | ||
|
|
||
| @Override | ||
| public List<ServiceInstance> getInstances(String serviceId) { | ||
| Objects.requireNonNull(serviceId); | ||
|
|
||
| List<Endpoints> allEndpoints = endpoints(properties, client, namespaceProvider, "fabric8-discovery", serviceId, | ||
| predicate); | ||
|
|
||
| List<ServiceInstance> instances = new ArrayList<>(); | ||
| for (Endpoints endpoints : allEndpoints) { | ||
| // endpoints are only those that matched the serviceId | ||
| instances.addAll(serviceInstances(endpoints, serviceId)); | ||
| } | ||
|
|
||
| if (properties.includeExternalNameServices()) { | ||
| LOG.debug(() -> "Searching for 'ExternalName' type of services with serviceId : " + serviceId); | ||
| List<Service> services = services(properties, client, namespaceProvider, | ||
| s -> s.getSpec().getType().equals(EXTERNAL_NAME), Map.of("metadata.name", serviceId), | ||
| "fabric8-discovery"); | ||
| for (Service service : services) { | ||
| ServiceMetadata serviceMetadata = serviceMetadata(service); | ||
| Map<String, String> serviceInstanceMetadata = serviceInstanceMetadata(Map.of(), serviceMetadata, | ||
| properties); | ||
|
|
||
| Fabric8InstanceIdHostPodNameSupplier supplierOne = externalName(service); | ||
|
|
||
| ServiceInstance externalNameServiceInstance = externalNameServiceInstance(serviceMetadata, supplierOne, | ||
| serviceInstanceMetadata); | ||
|
|
||
| instances.add(externalNameServiceInstance); | ||
| } | ||
| } | ||
|
|
||
| return instances; | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getServices() { | ||
| List<String> services = services(properties, client, namespaceProvider, predicate, null, "fabric8 discovery") | ||
| .stream() | ||
| .map(service -> service.getMetadata().getName()) | ||
| .distinct() | ||
| .toList(); | ||
| LOG.debug(() -> "will return services : " + services); | ||
| return services; | ||
| } | ||
|
|
||
| @Override | ||
| public int getOrder() { | ||
| return properties.order(); | ||
| } | ||
|
|
||
| private List<ServiceInstance> serviceInstances(Endpoints endpoints, String serviceId) { | ||
|
|
||
| List<EndpointSubset> subsets = endpoints.getSubsets(); | ||
| if (subsets.isEmpty()) { | ||
| LOG.debug(() -> "serviceId : " + serviceId + " does not have any subsets"); | ||
| return List.of(); | ||
| } | ||
|
|
||
| String namespace = endpoints.getMetadata().getNamespace(); | ||
| List<ServiceInstance> instances = new ArrayList<>(); | ||
|
|
||
| Service service = client.services().inNamespace(namespace).withName(serviceId).get(); | ||
| ServiceMetadata serviceMetadata = serviceMetadata(service); | ||
| Map<String, Integer> portsData = endpointSubsetsPortData(subsets); | ||
|
|
||
| Map<String, String> serviceInstanceMetadata = serviceInstanceMetadata(portsData, serviceMetadata, properties); | ||
|
|
||
| for (EndpointSubset endpointSubset : subsets) { | ||
|
|
||
| Map<String, Integer> endpointsPortData = endpointSubsetsPortData(List.of(endpointSubset)); | ||
| ServicePortNameAndNumber portData = endpointsPort(endpointsPortData, serviceMetadata, properties); | ||
|
|
||
| List<EndpointAddress> addresses = addresses(endpointSubset, properties); | ||
| for (EndpointAddress endpointAddress : addresses) { | ||
|
|
||
| Fabric8InstanceIdHostPodNameSupplier supplierOne = nonExternalName(endpointAddress, service); | ||
| Fabric8PodLabelsAndAnnotationsSupplier supplierTwo = Fabric8PodLabelsAndAnnotationsSupplier | ||
| .nonExternalName(client, namespace); | ||
|
|
||
| ServiceInstance serviceInstance = serviceInstance(servicePortSecureResolver, serviceMetadata, | ||
| supplierOne, supplierTwo, portData, serviceInstanceMetadata, properties); | ||
| instances.add(serviceInstance); | ||
| } | ||
| } | ||
|
|
||
| return instances; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
introduce 4 new conditionals (2 for blocking, 2 for reactive), to let users have the option to select a cacheable or a non-cacheable discovery client. The default one is non-cacheable.