diff --git a/docs/modules/ROOT/pages/discovery-client.adoc b/docs/modules/ROOT/pages/discovery-client.adoc index 12294f741e..3803101fd6 100644 --- a/docs/modules/ROOT/pages/discovery-client.adoc +++ b/docs/modules/ROOT/pages/discovery-client.adoc @@ -236,7 +236,7 @@ milliseconds (by default it is `30000`). For the http discovery server this must ---- The heartbeat event will contain the target references (and their namespaces of the addresses of all endpoints -(for the exact details of what will get returned you can take a look inside `KubernetesCatalogWatch`). This is an implementation detail, and listeners of the heartbeat event +(for the exact details of what will get returned you can take a look inside `Fabric8CatalogWatch`). This is an implementation detail, and listeners of the heartbeat event should not rely on the details. Instead, they should see if there are differences between two subsequent heartbeats via `equals` method. We will take care to return a correct implementation that adheres to the equals contract. The endpoints will be queried in either : - `all-namespaces` (enabled via `spring.cloud.kubernetes.discovery.all-namespaces=true`) diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/KubernetesExternalNameServiceInstance.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/KubernetesExternalNameServiceInstance.java new file mode 100644 index 0000000000..5cf9740dc5 --- /dev/null +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/KubernetesExternalNameServiceInstance.java @@ -0,0 +1,71 @@ +/* + * Copyright 2019-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.net.URI; +import java.util.Map; + +import org.springframework.cloud.client.ServiceInstance; + +/** + * Type of {@link org.springframework.cloud.client.ServiceInstance} when + * "spec.type=ExternalName". + * + * @author wind57 + */ +public record KubernetesExternalNameServiceInstance(String serviceId, String host, String instanceId, + Map metadata) implements ServiceInstance { + + @Override + public String getServiceId() { + return serviceId; + } + + @Override + public String getHost() { + return host; + } + + @Override + public int getPort() { + return -1; + } + + @Override + public boolean isSecure() { + return false; + } + + @Override + public URI getUri() { + return URI.create(host); + } + + @Override + public Map getMetadata() { + return metadata; + } + + public String getInstanceId() { + return instanceId; + } + + public String type() { + return "ExternalName"; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogWatch.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatch.java similarity index 88% rename from spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogWatch.java rename to spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatch.java index a1e6db178e..71e8374ee8 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogWatch.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatch.java @@ -31,7 +31,6 @@ import org.springframework.cloud.kubernetes.commons.discovery.EndpointNameAndNamespace; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; import org.springframework.context.ApplicationEventPublisher; -import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.core.log.LogAccessor; import org.springframework.scheduling.annotation.Scheduled; @@ -43,11 +42,11 @@ /** * @author Oleg Vyukov */ -public class KubernetesCatalogWatch implements ApplicationEventPublisherAware { +final class Fabric8CatalogWatch { private static final String DISCOVERY_GROUP_VERSION = DISCOVERY_GROUP + "/" + DISCOVERY_VERSION; - private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(KubernetesCatalogWatch.class)); + private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(Fabric8CatalogWatch.class)); private final Fabric8CatalogWatchContext context; @@ -55,20 +54,16 @@ public class KubernetesCatalogWatch implements ApplicationEventPublisherAware { private volatile List catalogEndpointsState = null; - private ApplicationEventPublisher publisher; + private final ApplicationEventPublisher publisher; - public KubernetesCatalogWatch(KubernetesClient kubernetesClient, KubernetesDiscoveryProperties properties, - KubernetesNamespaceProvider namespaceProvider) { + Fabric8CatalogWatch(KubernetesClient kubernetesClient, KubernetesDiscoveryProperties properties, + KubernetesNamespaceProvider namespaceProvider, ApplicationEventPublisher publisher) { context = new Fabric8CatalogWatchContext(kubernetesClient, properties, namespaceProvider); - } - - @Override - public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { this.publisher = publisher; } @Scheduled(fixedDelayString = "${" + CATALOG_WATCH_PROPERTY_WITH_DEFAULT_VALUE + "}") - public void catalogServicesWatch() { + void catalogServicesWatch() { try { List currentState = stateGenerator.apply(context); @@ -97,7 +92,7 @@ Function> stateGenera if (context.properties().useEndpointSlices()) { // can't use try with resources here as it will close the client KubernetesClient client = context.kubernetesClient(); - // this emulates : 'kubectl api-resources | grep -i EndpointSlice' + // this emulates: 'kubectl api-resources | grep -i EndpointSlice' boolean found = client.getApiGroups() .getGroups() .stream() diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogWatchAutoConfiguration.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchAutoConfiguration.java similarity index 82% rename from spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogWatchAutoConfiguration.java rename to spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchAutoConfiguration.java index 573ed19f87..f1d62fffa5 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogWatchAutoConfiguration.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchAutoConfiguration.java @@ -25,6 +25,7 @@ import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryPropertiesAutoConfiguration; import org.springframework.cloud.kubernetes.fabric8.Fabric8AutoConfiguration; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; @@ -37,13 +38,13 @@ @Configuration(proxyBeanMethods = false) @ConditionalOnKubernetesCatalogWatcherEnabled @AutoConfigureAfter({ Fabric8AutoConfiguration.class, KubernetesDiscoveryPropertiesAutoConfiguration.class }) -public class KubernetesCatalogWatchAutoConfiguration { +final class Fabric8CatalogWatchAutoConfiguration { @Bean @ConditionalOnMissingBean - public KubernetesCatalogWatch kubernetesCatalogWatch(KubernetesClient client, - KubernetesDiscoveryProperties properties, Environment environment) { - return new KubernetesCatalogWatch(client, properties, new KubernetesNamespaceProvider(environment)); + Fabric8CatalogWatch fabric8CatalogWatch(KubernetesClient client, KubernetesDiscoveryProperties properties, + Environment environment, ApplicationEventPublisher publisher) { + return new Fabric8CatalogWatch(client, properties, new KubernetesNamespaceProvider(environment), publisher); } } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ConfigServerBootstrapper.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ConfigServerBootstrapper.java index e5e5565014..e3e4fb0bad 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ConfigServerBootstrapper.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ConfigServerBootstrapper.java @@ -20,6 +20,7 @@ import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; +import jakarta.annotation.Nonnull; import org.springframework.boot.bootstrap.BootstrapRegistry; import org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver.PropertyResolver; @@ -37,7 +38,7 @@ class Fabric8ConfigServerBootstrapper extends KubernetesConfigServerBootstrapper { @Override - public void initialize(BootstrapRegistry registry) { + public void initialize(@Nonnull BootstrapRegistry registry) { if (hasConfigServerInstanceProvider()) { return; } @@ -61,8 +62,8 @@ public void initialize(BootstrapRegistry registry) { if (!getDiscoveryEnabled(context)) { return (id) -> Collections.emptyList(); } - if (context.isRegistered(KubernetesDiscoveryClient.class)) { - KubernetesDiscoveryClient client = context.get(KubernetesDiscoveryClient.class); + if (context.isRegistered(Fabric8DiscoveryClient.class)) { + Fabric8DiscoveryClient client = context.get(Fabric8DiscoveryClient.class); return client::getInstances; } else { @@ -72,12 +73,11 @@ public void initialize(BootstrapRegistry registry) { .kubernetesClientConfig(context.get(KubernetesClientProperties.class)); KubernetesClient kubernetesClient = fabric8AutoConfiguration.kubernetesClient(config); KubernetesDiscoveryProperties discoveryProperties = context.get(KubernetesDiscoveryProperties.class); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(kubernetesClient, - discoveryProperties, - KubernetesClientServicesFunctionProvider.servicesFunction(discoveryProperties, - new KubernetesNamespaceProvider(propertyResolver - .get(KubernetesNamespaceProvider.NAMESPACE_PROPERTY, String.class, null))), - null, new ServicePortSecureResolver(discoveryProperties)); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(kubernetesClient, + discoveryProperties, new ServicePortSecureResolver(discoveryProperties), + new KubernetesNamespaceProvider(propertyResolver + .get(KubernetesNamespaceProvider.NAMESPACE_PROPERTY, String.class, null)), + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(discoveryProperties)); return discoveryClient::getInstances; } }); diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClient.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClient.java similarity index 74% rename from spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClient.java rename to spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClient.java index d0acdacc58..21bfde7486 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClient.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClient.java @@ -36,8 +36,6 @@ 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.context.EnvironmentAware; -import org.springframework.core.env.Environment; import org.springframework.core.log.LogAccessor; import static org.springframework.cloud.kubernetes.commons.discovery.DiscoveryClientUtils.endpointsPort; @@ -45,12 +43,12 @@ 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; -import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.addresses; -import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.endpointSubsetsPortData; -import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.endpoints; -import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.services; import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8PodLabelsAndAnnotationsSupplier.externalName; import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8PodLabelsAndAnnotationsSupplier.nonExternalName; @@ -60,40 +58,29 @@ * @author Ioannis Canellos * @author Tim Ysewyn */ -public class KubernetesDiscoveryClient implements DiscoveryClient, EnvironmentAware { +final class Fabric8DiscoveryClient implements DiscoveryClient { - private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(KubernetesDiscoveryClient.class)); + private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(Fabric8DiscoveryClient.class)); private final KubernetesDiscoveryProperties properties; - private final KubernetesClientServicesFunction kubernetesClientServicesFunction; - private final ServicePortSecureResolver servicePortSecureResolver; - private final Fabric8DiscoveryServicesAdapter adapter; - private final KubernetesClient client; - private KubernetesNamespaceProvider namespaceProvider; + private final KubernetesNamespaceProvider namespaceProvider; - public KubernetesDiscoveryClient(KubernetesClient client, - KubernetesDiscoveryProperties kubernetesDiscoveryProperties, - KubernetesClientServicesFunction kubernetesClientServicesFunction) { + private final Predicate predicate; - this(client, kubernetesDiscoveryProperties, kubernetesClientServicesFunction, null, - new ServicePortSecureResolver(kubernetesDiscoveryProperties)); - } - - KubernetesDiscoveryClient(KubernetesClient client, KubernetesDiscoveryProperties kubernetesDiscoveryProperties, - KubernetesClientServicesFunction kubernetesClientServicesFunction, Predicate filter, - ServicePortSecureResolver servicePortSecureResolver) { + Fabric8DiscoveryClient(KubernetesClient client, KubernetesDiscoveryProperties kubernetesDiscoveryProperties, + ServicePortSecureResolver servicePortSecureResolver, KubernetesNamespaceProvider namespaceProvider, + Predicate predicate) { this.client = client; this.properties = kubernetesDiscoveryProperties; this.servicePortSecureResolver = servicePortSecureResolver; - this.kubernetesClientServicesFunction = kubernetesClientServicesFunction; - this.adapter = new Fabric8DiscoveryServicesAdapter(kubernetesClientServicesFunction, - kubernetesDiscoveryProperties, filter); + this.namespaceProvider = namespaceProvider; + this.predicate = predicate; } @Override @@ -105,7 +92,8 @@ public String description() { public List getInstances(String serviceId) { Objects.requireNonNull(serviceId); - List allEndpoints = getEndPointsList(serviceId).stream().toList(); + List allEndpoints = endpoints(properties, client, namespaceProvider, "fabric8-discovery", serviceId, + predicate); List instances = new ArrayList<>(); for (Endpoints endpoints : allEndpoints) { @@ -135,8 +123,20 @@ public List getInstances(String serviceId) { return instances; } - public List getEndPointsList(String serviceId) { - return endpoints(properties, client, namespaceProvider, "fabric8-discovery", serviceId, adapter.filter()); + @Override + public List getServices() { + List 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 serviceInstances(Endpoints endpoints, String serviceId) { @@ -176,31 +176,4 @@ private List serviceInstances(Endpoints endpoints, String servi return instances; } - @Override - public List getServices() { - List services = adapter.apply(client).stream().map(s -> s.getMetadata().getName()).distinct().toList(); - LOG.debug(() -> "will return services : " + services); - return services; - } - - @Deprecated(forRemoval = true) - public List getServices(Predicate filter) { - return new Fabric8DiscoveryServicesAdapter(kubernetesClientServicesFunction, properties, filter).apply(client) - .stream() - .map(s -> s.getMetadata().getName()) - .distinct() - .toList(); - } - - @Override - public int getOrder() { - return properties.order(); - } - - @Deprecated(forRemoval = true) - @Override - public final void setEnvironment(Environment environment) { - namespaceProvider = new KubernetesNamespaceProvider(environment); - } - } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientAutoConfiguration.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientAutoConfiguration.java similarity index 55% rename from spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientAutoConfiguration.java rename to spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientAutoConfiguration.java index 9d72dddff3..feab2273e2 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientAutoConfiguration.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientAutoConfiguration.java @@ -16,23 +16,31 @@ package org.springframework.cloud.kubernetes.fabric8.discovery; +import java.util.function.Predicate; + +import io.fabric8.kubernetes.api.model.Service; import io.fabric8.kubernetes.client.KubernetesClient; +import org.apache.commons.logging.LogFactory; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.cloud.client.CommonsClientAutoConfiguration; import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration; +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; +import org.springframework.cloud.kubernetes.commons.PodUtils; import org.springframework.cloud.kubernetes.commons.discovery.ConditionalOnSpringCloudKubernetesBlockingDiscovery; -import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryClientHealthConfiguration; +import org.springframework.cloud.kubernetes.commons.discovery.ConditionalOnSpringCloudKubernetesBlockingDiscoveryHealthInitializer; +import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryClientHealthIndicatorInitializer; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryPropertiesAutoConfiguration; import org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver; import org.springframework.cloud.kubernetes.fabric8.Fabric8AutoConfiguration; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; import org.springframework.core.env.Environment; +import org.springframework.core.log.LogAccessor; /** * Auto configuration for discovery clients. @@ -43,24 +51,28 @@ @Configuration(proxyBeanMethods = false) @ConditionalOnSpringCloudKubernetesBlockingDiscovery @AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class, CommonsClientAutoConfiguration.class }) -@AutoConfigureAfter({ Fabric8AutoConfiguration.class, KubernetesDiscoveryPropertiesAutoConfiguration.class }) -@Import(KubernetesDiscoveryClientHealthConfiguration.class) -public class KubernetesDiscoveryClientAutoConfiguration { +@AutoConfigureAfter({ Fabric8AutoConfiguration.class, KubernetesDiscoveryPropertiesAutoConfiguration.class, + Fabric8DiscoveryClientSpelAutoConfiguration.class, Fabric8DiscoveryClientSpelAutoConfiguration.class }) +class Fabric8DiscoveryClientAutoConfiguration { + + private static final LogAccessor LOG = new LogAccessor( + LogFactory.getLog(Fabric8DiscoveryClientAutoConfiguration.class)); @Bean @ConditionalOnMissingBean - public KubernetesClientServicesFunction servicesFunction(KubernetesDiscoveryProperties properties, - Environment environment) { - return KubernetesClientServicesFunctionProvider.servicesFunction(properties, environment); + Fabric8DiscoveryClient fabric8DiscoveryClient(KubernetesClient client, KubernetesDiscoveryProperties properties, + Predicate predicate, Environment environment) { + ServicePortSecureResolver servicePortSecureResolver = new ServicePortSecureResolver(properties); + KubernetesNamespaceProvider namespaceProvider = new KubernetesNamespaceProvider(environment); + return new Fabric8DiscoveryClient(client, properties, servicePortSecureResolver, namespaceProvider, predicate); } @Bean - @ConditionalOnMissingBean - public KubernetesDiscoveryClient kubernetesDiscoveryClient(KubernetesClient client, - KubernetesDiscoveryProperties properties, - KubernetesClientServicesFunction kubernetesClientServicesFunction) { - return new KubernetesDiscoveryClient(client, properties, kubernetesClientServicesFunction, null, - new ServicePortSecureResolver(properties)); + @ConditionalOnSpringCloudKubernetesBlockingDiscoveryHealthInitializer + KubernetesDiscoveryClientHealthIndicatorInitializer indicatorInitializer( + ApplicationEventPublisher applicationEventPublisher, PodUtils podUtils) { + LOG.debug(() -> "Will publish InstanceRegisteredEvent from blocking implementation"); + return new KubernetesDiscoveryClientHealthIndicatorInitializer(podUtils, applicationEventPublisher); } } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientConfigClientBootstrapConfiguration.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientConfigClientBootstrapConfiguration.java similarity index 90% rename from spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientConfigClientBootstrapConfiguration.java rename to spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientConfigClientBootstrapConfiguration.java index bea270fc04..b78be01abb 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientConfigClientBootstrapConfiguration.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientConfigClientBootstrapConfiguration.java @@ -31,8 +31,8 @@ */ @Configuration(proxyBeanMethods = false) @ConditionalOnProperty("spring.cloud.config.discovery.enabled") -@Import({ Fabric8AutoConfiguration.class, KubernetesDiscoveryClientAutoConfiguration.class }) +@Import({ Fabric8AutoConfiguration.class, Fabric8DiscoveryClientAutoConfiguration.class }) @EnableConfigurationProperties({ KubernetesDiscoveryProperties.class, KubernetesClientProperties.class }) -public class KubernetesDiscoveryClientConfigClientBootstrapConfiguration { +final class Fabric8DiscoveryClientConfigClientBootstrapConfiguration { } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientSpelAutoConfiguration.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientSpelAutoConfiguration.java new file mode 100644 index 0000000000..c765ac3415 --- /dev/null +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientSpelAutoConfiguration.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012-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.Optional; +import java.util.function.Predicate; + +import io.fabric8.kubernetes.api.model.Service; + +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.cloud.CloudPlatform; +import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled; +import org.springframework.cloud.kubernetes.commons.discovery.ConditionalOnBlockingOrReactiveDiscoveryEnabled; +import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; +import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryPropertiesAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.expression.Expression; +import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.expression.spel.support.SimpleEvaluationContext; + +/** + * @author wind57 + */ +@Configuration(proxyBeanMethods = false) +@ConditionalOnDiscoveryEnabled +@ConditionalOnBlockingOrReactiveDiscoveryEnabled +@ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES) +@AutoConfigureAfter(KubernetesDiscoveryPropertiesAutoConfiguration.class) +class Fabric8DiscoveryClientSpelAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + Predicate predicate(KubernetesDiscoveryProperties properties) { + SpelExpressionParser parser = new SpelExpressionParser(); + SimpleEvaluationContext evaluationContext = SimpleEvaluationContext.forReadOnlyDataBinding() + .withInstanceMethods() + .build(); + + String spelExpression = properties.filter(); + Predicate predicate; + if (spelExpression == null || spelExpression.isEmpty()) { + predicate = service -> true; + } + else { + Expression filterExpr = parser.parseExpression(spelExpression); + predicate = service -> { + Boolean include = filterExpr.getValue(evaluationContext, service, Boolean.class); + return Optional.ofNullable(include).orElse(false); + }; + } + return predicate; + } + +} diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientUtils.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientUtils.java similarity index 80% rename from spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientUtils.java rename to spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientUtils.java index 844385d7b3..dcac8b50a2 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientUtils.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientUtils.java @@ -31,6 +31,8 @@ import io.fabric8.kubernetes.api.model.EndpointsList; import io.fabric8.kubernetes.api.model.Service; import io.fabric8.kubernetes.api.model.ServiceList; +import io.fabric8.kubernetes.api.model.discovery.v1.EndpointSlice; +import io.fabric8.kubernetes.api.model.discovery.v1.EndpointSliceList; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.dsl.FilterNested; import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable; @@ -51,17 +53,44 @@ /** * @author wind57 */ -final class Fabric8KubernetesDiscoveryClientUtils { +final class Fabric8DiscoveryClientUtils { - private static final LogAccessor LOG = new LogAccessor( - LogFactory.getLog(Fabric8KubernetesDiscoveryClientUtils.class)); + private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(Fabric8DiscoveryClientUtils.class)); static final Predicate ALWAYS_TRUE = x -> true; - private Fabric8KubernetesDiscoveryClientUtils() { + private Fabric8DiscoveryClientUtils() { } + static List endpointSlices(KubernetesDiscoveryProperties properties, KubernetesClient client, + KubernetesNamespaceProvider namespaceProvider, String target) { + + List endpointSlices; + + if (properties.allNamespaces()) { + LOG.debug(() -> "discovering endpoint slices in all namespaces"); + endpointSlices = filteredEndpointSlices( + client.discovery().v1().endpointSlices().inAnyNamespace().withNewFilter(), properties); + } + else if (!properties.namespaces().isEmpty()) { + LOG.debug(() -> "discovering endpoint slices in namespaces : " + properties.namespaces()); + List inner = new ArrayList<>(properties.namespaces().size()); + properties.namespaces() + .forEach(namespace -> inner.addAll(filteredEndpointSlices( + client.discovery().v1().endpointSlices().inNamespace(namespace).withNewFilter(), properties))); + endpointSlices = inner; + } + else { + String namespace = Fabric8Utils.getApplicationNamespace(client, null, target, namespaceProvider); + LOG.debug(() -> "discovering endpoint slices in namespace : " + namespace); + endpointSlices = filteredEndpointSlices( + client.discovery().v1().endpointSlices().inNamespace(namespace).withNewFilter(), properties); + } + + return endpointSlices; + } + static List endpoints(KubernetesDiscoveryProperties properties, KubernetesClient client, KubernetesNamespaceProvider namespaceProvider, String target, @Nullable String serviceName, Predicate filter) { @@ -226,4 +255,15 @@ private static List filteredServices( } + private static List filteredEndpointSlices( + FilterNested>> filterNested, + KubernetesDiscoveryProperties properties) { + + FilterNested>> partial = filterNested + .withLabels(properties.serviceLabels()); + + return partial.endFilter().list().getItems(); + + } + } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryServicesAdapter.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryServicesAdapter.java deleted file mode 100644 index 28b005dfdb..0000000000 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryServicesAdapter.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright 2012-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.Optional; -import java.util.function.Function; -import java.util.function.Predicate; - -import io.fabric8.kubernetes.api.model.Service; -import io.fabric8.kubernetes.client.KubernetesClient; -import org.apache.commons.logging.LogFactory; - -import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; -import org.springframework.core.log.LogAccessor; -import org.springframework.expression.Expression; -import org.springframework.expression.spel.standard.SpelExpressionParser; -import org.springframework.expression.spel.support.SimpleEvaluationContext; - -/** - * Adapts a {@link KubernetesClientServicesFunction} to a Function that takes a - * KubernetesClient as input and returns a List of Services(s), plus adds functionality - * not supported by it. - * - * @author wind57 - */ -final class Fabric8DiscoveryServicesAdapter implements Function> { - - private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(Fabric8DiscoveryServicesAdapter.class)); - - private static final SpelExpressionParser PARSER = new SpelExpressionParser(); - - private static final SimpleEvaluationContext EVALUATION_CONTEXT = SimpleEvaluationContext.forReadOnlyDataBinding() - .withInstanceMethods() - .build(); - - private final KubernetesClientServicesFunction function; - - private final KubernetesDiscoveryProperties properties; - - private final Predicate filter; - - Fabric8DiscoveryServicesAdapter(KubernetesClientServicesFunction function, KubernetesDiscoveryProperties properties, - Predicate filter) { - this.function = function; - this.properties = properties; - if (filter == null) { - this.filter = filter(); - } - else { - this.filter = filter; - } - } - - @Override - public List apply(KubernetesClient client) { - if (!properties.namespaces().isEmpty()) { - LOG.debug(() -> "searching in namespaces : " + properties.namespaces() + " with filter : " - + properties.filter()); - List services = new ArrayList<>(); - properties.namespaces() - .forEach(namespace -> services.addAll(client.services() - .inNamespace(namespace) - .withLabels(properties.serviceLabels()) - .list() - .getItems() - .stream() - .filter(filter) - .toList())); - return services; - } - return function.apply(client).list().getItems().stream().filter(filter).toList(); - } - - Predicate filter() { - String spelExpression = properties.filter(); - Predicate predicate; - if (spelExpression == null || spelExpression.isEmpty()) { - predicate = service -> true; - } - else { - Expression filterExpr = PARSER.parseExpression(spelExpression); - predicate = service -> { - Boolean include = filterExpr.getValue(EVALUATION_CONTEXT, service, Boolean.class); - return Optional.ofNullable(include).orElse(false); - }; - } - return predicate; - } - -} diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8EndpointSliceV1CatalogWatch.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8EndpointSliceV1CatalogWatch.java index 7ada0a2350..612b4c7711 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8EndpointSliceV1CatalogWatch.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8EndpointSliceV1CatalogWatch.java @@ -16,7 +16,6 @@ package org.springframework.cloud.kubernetes.fabric8.discovery; -import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.function.Function; @@ -25,12 +24,10 @@ import io.fabric8.kubernetes.api.model.ObjectReference; import io.fabric8.kubernetes.api.model.discovery.v1.Endpoint; import io.fabric8.kubernetes.api.model.discovery.v1.EndpointSlice; -import io.fabric8.kubernetes.client.KubernetesClient; -import org.apache.commons.logging.LogFactory; import org.springframework.cloud.kubernetes.commons.discovery.EndpointNameAndNamespace; -import org.springframework.cloud.kubernetes.fabric8.Fabric8Utils; -import org.springframework.core.log.LogAccessor; + +import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.endpointSlices; /** * Implementation that is based on EndpointSlice V1. @@ -40,38 +37,10 @@ final class Fabric8EndpointSliceV1CatalogWatch implements Function> { - private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(Fabric8EndpointSliceV1CatalogWatch.class)); - @Override public List apply(Fabric8CatalogWatchContext context) { - // take only pods that have endpoints - List endpointSlices; - KubernetesClient client = context.kubernetesClient(); - - if (context.properties().allNamespaces()) { - LOG.debug(() -> "discovering endpoint slices in all namespaces"); - endpointSlices = client.discovery() - .v1() - .endpointSlices() - .inAnyNamespace() - .withLabels(context.properties().serviceLabels()) - .list() - .getItems(); - } - else if (!context.properties().namespaces().isEmpty()) { - LOG.debug(() -> "discovering endpoint slices in " + context.properties().namespaces()); - List inner = new ArrayList<>(context.properties().namespaces().size()); - context.properties() - .namespaces() - .forEach(namespace -> inner.addAll(endpointSlices(context, namespace, client))); - endpointSlices = inner; - } - else { - String namespace = Fabric8Utils.getApplicationNamespace(context.kubernetesClient(), null, "catalog-watcher", - context.namespaceProvider()); - LOG.debug(() -> "discovering endpoint slices in namespace : " + namespace); - endpointSlices = endpointSlices(context, namespace, client); - } + List endpointSlices = endpointSlices(context.properties(), context.kubernetesClient(), + context.namespaceProvider(), "catalog-watcher"); return generateState(endpointSlices); } @@ -90,15 +59,4 @@ List generateState(List endpointSlices) return Fabric8CatalogWatchContext.state(references); } - private List endpointSlices(Fabric8CatalogWatchContext context, String namespace, - KubernetesClient client) { - return client.discovery() - .v1() - .endpointSlices() - .inNamespace(namespace) - .withLabels(context.properties().serviceLabels()) - .list() - .getItems(); - } - } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8EndpointsCatalogWatch.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8EndpointsCatalogWatch.java index f1735e97de..80dd550266 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8EndpointsCatalogWatch.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8EndpointsCatalogWatch.java @@ -28,8 +28,8 @@ import org.springframework.cloud.kubernetes.commons.discovery.EndpointNameAndNamespace; -import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.ALWAYS_TRUE; -import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.endpoints; +import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.ALWAYS_TRUE; +import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.endpoints; /** * Implementation that is based on Endpoints. diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClient.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClient.java similarity index 55% rename from spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClient.java rename to spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClient.java index c1d35f77c6..720eab96fa 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClient.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClient.java @@ -14,34 +14,28 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.discovery.reactive; +package org.springframework.cloud.kubernetes.fabric8.discovery; import java.util.Objects; -import io.fabric8.kubernetes.client.KubernetesClient; import reactor.core.publisher.Flux; import reactor.core.scheduler.Schedulers; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; -import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; -import org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesClientServicesFunction; -import org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClient; /** * Kubernetes implementation of {@link ReactiveDiscoveryClient}. Currently relies on the - * {@link KubernetesDiscoveryClient} for feature parity. + * {@link Fabric8DiscoveryClient} for feature parity. * * @author Tim Ysewyn */ -public class KubernetesReactiveDiscoveryClient implements ReactiveDiscoveryClient { +final class Fabric8ReactiveDiscoveryClient implements ReactiveDiscoveryClient { - private final KubernetesDiscoveryClient kubernetesDiscoveryClient; + private final Fabric8DiscoveryClient fabric8DiscoveryClient; - public KubernetesReactiveDiscoveryClient(KubernetesClient client, KubernetesDiscoveryProperties properties, - KubernetesClientServicesFunction kubernetesClientServicesFunction) { - this.kubernetesDiscoveryClient = new KubernetesDiscoveryClient(client, properties, - kubernetesClientServicesFunction); + Fabric8ReactiveDiscoveryClient(Fabric8DiscoveryClient fabric8DiscoveryClient) { + this.fabric8DiscoveryClient = fabric8DiscoveryClient; } @Override @@ -52,19 +46,19 @@ public String description() { @Override public Flux getInstances(String serviceId) { Objects.requireNonNull(serviceId, "serviceId must not be null"); - return Flux.defer(() -> Flux.fromIterable(kubernetesDiscoveryClient.getInstances(serviceId))) + return Flux.defer(() -> Flux.fromIterable(fabric8DiscoveryClient.getInstances(serviceId))) .subscribeOn(Schedulers.boundedElastic()); } @Override public Flux getServices() { - return Flux.defer(() -> Flux.fromIterable(kubernetesDiscoveryClient.getServices())) + return Flux.defer(() -> Flux.fromIterable(fabric8DiscoveryClient.getServices())) .subscribeOn(Schedulers.boundedElastic()); } @Override public int getOrder() { - return kubernetesDiscoveryClient.getOrder(); + return fabric8DiscoveryClient.getOrder(); } } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClientAutoConfiguration.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientAutoConfiguration.java similarity index 72% rename from spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClientAutoConfiguration.java rename to spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientAutoConfiguration.java index e59a79ae05..3283878503 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClientAutoConfiguration.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientAutoConfiguration.java @@ -14,8 +14,11 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.discovery.reactive; +package org.springframework.cloud.kubernetes.fabric8.discovery; +import java.util.function.Predicate; + +import io.fabric8.kubernetes.api.model.Service; import io.fabric8.kubernetes.client.KubernetesClient; import org.apache.commons.logging.LogFactory; @@ -29,15 +32,14 @@ import org.springframework.cloud.client.discovery.health.DiscoveryClientHealthIndicatorProperties; import org.springframework.cloud.client.discovery.health.reactive.ReactiveDiscoveryClientHealthIndicator; import org.springframework.cloud.client.discovery.simple.reactive.SimpleReactiveDiscoveryClientAutoConfiguration; +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.PodUtils; import org.springframework.cloud.kubernetes.commons.discovery.ConditionalOnSpringCloudKubernetesReactiveDiscovery; import org.springframework.cloud.kubernetes.commons.discovery.ConditionalOnSpringCloudKubernetesReactiveDiscoveryHealthInitializer; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryClientHealthIndicatorInitializer; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryPropertiesAutoConfiguration; -import org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesClientServicesFunction; -import org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesClientServicesFunctionProvider; -import org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClientAutoConfiguration; +import org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -54,25 +56,22 @@ @AutoConfigureBefore({ SimpleReactiveDiscoveryClientAutoConfiguration.class, ReactiveCommonsClientAutoConfiguration.class }) @AutoConfigureAfter({ ReactiveCompositeDiscoveryClientAutoConfiguration.class, - KubernetesDiscoveryClientAutoConfiguration.class, KubernetesDiscoveryPropertiesAutoConfiguration.class }) -public class KubernetesReactiveDiscoveryClientAutoConfiguration { + Fabric8DiscoveryClientAutoConfiguration.class, KubernetesDiscoveryPropertiesAutoConfiguration.class, + Fabric8DiscoveryClientSpelAutoConfiguration.class }) +public class Fabric8ReactiveDiscoveryClientAutoConfiguration { private static final LogAccessor LOG = new LogAccessor( - LogFactory.getLog(KubernetesReactiveDiscoveryClientAutoConfiguration.class)); - - @Bean - @ConditionalOnMissingBean - public KubernetesClientServicesFunction servicesFunction(KubernetesDiscoveryProperties properties, - Environment environment) { - return KubernetesClientServicesFunctionProvider.servicesFunction(properties, environment); - } + LogFactory.getLog(Fabric8ReactiveDiscoveryClientAutoConfiguration.class)); @Bean @ConditionalOnMissingBean - public KubernetesReactiveDiscoveryClient kubernetesReactiveDiscoveryClient(KubernetesClient client, - KubernetesDiscoveryProperties properties, - KubernetesClientServicesFunction kubernetesClientServicesFunction) { - return new KubernetesReactiveDiscoveryClient(client, properties, kubernetesClientServicesFunction); + Fabric8ReactiveDiscoveryClient fabric8ReactiveDiscoveryClient(KubernetesClient client, + KubernetesDiscoveryProperties properties, Predicate predicate, Environment environment) { + ServicePortSecureResolver servicePortSecureResolver = new ServicePortSecureResolver(properties); + KubernetesNamespaceProvider namespaceProvider = new KubernetesNamespaceProvider(environment); + Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(client, properties, + servicePortSecureResolver, namespaceProvider, predicate); + return new Fabric8ReactiveDiscoveryClient(fabric8DiscoveryClient); } /** @@ -89,8 +88,8 @@ KubernetesDiscoveryClientHealthIndicatorInitializer reactiveIndicatorInitializer @Bean @ConditionalOnSpringCloudKubernetesReactiveDiscoveryHealthInitializer - public ReactiveDiscoveryClientHealthIndicator kubernetesReactiveDiscoveryClientHealthIndicator( - KubernetesReactiveDiscoveryClient client, DiscoveryClientHealthIndicatorProperties properties) { + ReactiveDiscoveryClientHealthIndicator kubernetesReactiveDiscoveryClientHealthIndicator( + Fabric8ReactiveDiscoveryClient client, DiscoveryClientHealthIndicatorProperties properties) { return new ReactiveDiscoveryClientHealthIndicator(client, properties); } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesClientServicesFunction.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesClientServicesFunction.java deleted file mode 100644 index f83b86c6dd..0000000000 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesClientServicesFunction.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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.function.Function; - -import io.fabric8.kubernetes.api.model.Service; -import io.fabric8.kubernetes.api.model.ServiceList; -import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable; -import io.fabric8.kubernetes.client.dsl.ServiceResource; - -/** - * A regular java.util.function that is used to hide the complexity of the - * KubernetesClient interfaces. - * - * It's meant to be used to abstract things like: - * - * client.services() client.services().withLabel("key", "value") - * client.services().withoutLabel("key") - * - * The result of the application of the function can then be used for example to list the - * services like so: - * - * function.apply(client).list() - * - * See KubernetesDiscoveryClientAutoConfiguration.servicesFunction - * - * @author Georgios Andrianakis - */ -public interface KubernetesClientServicesFunction - extends Function>> { - -} diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesClientServicesFunctionProvider.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesClientServicesFunctionProvider.java deleted file mode 100644 index ffb0a64677..0000000000 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesClientServicesFunctionProvider.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; -import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; -import org.springframework.cloud.kubernetes.fabric8.Fabric8Utils; -import org.springframework.core.env.Environment; - -/** - * @author wind57 - */ -public final class KubernetesClientServicesFunctionProvider { - - private KubernetesClientServicesFunctionProvider() { - } - - public static KubernetesClientServicesFunction servicesFunction(KubernetesDiscoveryProperties properties, - Environment environment) { - - if (properties.allNamespaces()) { - return (client) -> client.services().inAnyNamespace().withLabels(properties.serviceLabels()); - } - - return client -> { - String namespace = Fabric8Utils.getApplicationNamespace(client, null, "discovery-service", - new KubernetesNamespaceProvider(environment)); - return client.services().inNamespace(namespace).withLabels(properties.serviceLabels()); - }; - - } - - public static KubernetesClientServicesFunction servicesFunction(KubernetesDiscoveryProperties properties, - KubernetesNamespaceProvider namespaceProvider) { - - if (properties.allNamespaces()) { - return (client) -> client.services().inAnyNamespace().withLabels(properties.serviceLabels()); - } - - return client -> { - String namespace = Fabric8Utils.getApplicationNamespace(client, null, "discovery-service", - namespaceProvider); - return client.services().inNamespace(namespace).withLabels(properties.serviceLabels()); - }; - - } - -} diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/resources/META-INF/spring.factories b/spring-cloud-kubernetes-fabric8-discovery/src/main/resources/META-INF/spring.factories index af26a9722d..90109afed4 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/resources/META-INF/spring.factories @@ -1,4 +1,4 @@ org.springframework.cloud.bootstrap.BootstrapConfiguration=\ -org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClientConfigClientBootstrapConfiguration +org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientConfigClientBootstrapConfiguration org.springframework.boot.bootstrap.BootstrapRegistryInitializer=\ org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8ConfigServerBootstrapper diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-kubernetes-fabric8-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 2c7e95bc99..8cba896bfb 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,3 +1,4 @@ -org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesCatalogWatchAutoConfiguration -org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClientAutoConfiguration -org.springframework.cloud.kubernetes.fabric8.discovery.reactive.KubernetesReactiveDiscoveryClientAutoConfiguration +org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8CatalogWatchAutoConfiguration +org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientAutoConfiguration +org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8ReactiveDiscoveryClientAutoConfiguration +org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientSpelAutoConfiguration diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogServicesWatchConfigurationTest.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogServicesWatchConfigurationTest.java similarity index 89% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogServicesWatchConfigurationTest.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogServicesWatchConfigurationTest.java index ebbfb9c105..8702c0c6f9 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogServicesWatchConfigurationTest.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogServicesWatchConfigurationTest.java @@ -42,7 +42,7 @@ * @author Oleg Vyukov * @author Tim Ysewyn */ -class KubernetesCatalogServicesWatchConfigurationTest { +class Fabric8CatalogServicesWatchConfigurationTest { private ConfigurableApplicationContext context; @@ -74,15 +74,16 @@ void kubernetesCatalogWatchWhenServiceDiscoveryDisabled() { @Test void kubernetesCatalogWatchDefaultEnabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.discovery.use-endpoint-slices=false"); - assertThat(context.containsBean("kubernetesCatalogWatch")).isTrue(); + assertThat(context.containsBean("fabric8CatalogWatch")).isTrue(); } private void setup(String... env) { List envList = new ArrayList<>(Arrays.asList(env)); envList.add("spring.cloud.config.enabled=false"); context = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class, - KubernetesClientTestConfiguration.class, KubernetesCatalogWatchAutoConfiguration.class, - KubernetesDiscoveryClientAutoConfiguration.class, KubernetesDiscoveryPropertiesAutoConfiguration.class) + KubernetesClientTestConfiguration.class, Fabric8CatalogWatchAutoConfiguration.class, + Fabric8DiscoveryClientAutoConfiguration.class, KubernetesDiscoveryPropertiesAutoConfiguration.class, + Fabric8DiscoveryClientSpelAutoConfiguration.class) .web(WebApplicationType.NONE) .properties(envList.toArray(new String[0])) .run(); diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogWatchAutoConfigurationApplicationContextTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchAutoConfigurationApplicationContextTests.java similarity index 71% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogWatchAutoConfigurationApplicationContextTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchAutoConfigurationApplicationContextTests.java index 5a7432fde0..3dd003089b 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogWatchAutoConfigurationApplicationContextTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchAutoConfigurationApplicationContextTests.java @@ -23,42 +23,41 @@ import org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryPropertiesAutoConfiguration; import org.springframework.cloud.kubernetes.fabric8.Fabric8AutoConfiguration; -import org.springframework.cloud.kubernetes.fabric8.discovery.reactive.KubernetesReactiveDiscoveryClient; import static org.assertj.core.api.Assertions.assertThat; /** * @author wind57 */ -class KubernetesCatalogWatchAutoConfigurationApplicationContextTests { +class Fabric8CatalogWatchAutoConfigurationApplicationContextTests { private ApplicationContextRunner applicationContextRunner; @Test void discoveryEnabledDefault() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false"); - applicationContextRunner.run(context -> assertThat(context).hasSingleBean(KubernetesCatalogWatch.class)); + applicationContextRunner.run(context -> assertThat(context).hasSingleBean(Fabric8CatalogWatch.class)); } @Test void discoveryEnabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.enabled=true"); - applicationContextRunner.run(context -> assertThat(context).hasSingleBean(KubernetesCatalogWatch.class)); + applicationContextRunner.run(context -> assertThat(context).hasSingleBean(Fabric8CatalogWatch.class)); } @Test void discoveryDisabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.enabled=false"); - applicationContextRunner.run(context -> assertThat(context).doesNotHaveBean(KubernetesCatalogWatch.class)); + applicationContextRunner.run(context -> assertThat(context).doesNotHaveBean(Fabric8CatalogWatch.class)); } @Test void kubernetesDiscoveryEnabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.kubernetes.discovery.enabled=true"); - applicationContextRunner.run(context -> assertThat(context).hasSingleBean(KubernetesCatalogWatch.class)); + applicationContextRunner.run(context -> assertThat(context).hasSingleBean(Fabric8CatalogWatch.class)); } // disabling discovery should disable catalog watcher. @@ -66,7 +65,7 @@ void kubernetesDiscoveryEnabled() { void kubernetesDiscoveryDisabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.kubernetes.discovery.enabled=false"); - applicationContextRunner.run(context -> assertThat(context).doesNotHaveBean(KubernetesCatalogWatch.class)); + applicationContextRunner.run(context -> assertThat(context).doesNotHaveBean(Fabric8CatalogWatch.class)); } /** @@ -77,10 +76,9 @@ void disableBlockingAndReactive() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.blocking.enabled=false", "spring.cloud.discovery.reactive.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).doesNotHaveBean(KubernetesCatalogWatch.class); - assertThat(context).doesNotHaveBean(KubernetesClientServicesFunction.class); - assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); - assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).doesNotHaveBean(Fabric8CatalogWatch.class); + assertThat(context).doesNotHaveBean(Fabric8DiscoveryClient.class); + assertThat(context).doesNotHaveBean(Fabric8ReactiveDiscoveryClient.class); }); } @@ -92,10 +90,9 @@ void disableBlockingEnableReactive() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.blocking.enabled=false", "spring.cloud.discovery.reactive.enabled=true"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesCatalogWatch.class); - assertThat(context).doesNotHaveBean(KubernetesClientServicesFunction.class); - assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); - assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8CatalogWatch.class); + assertThat(context).doesNotHaveBean(Fabric8DiscoveryClient.class); + assertThat(context).doesNotHaveBean(Fabric8ReactiveDiscoveryClient.class); }); } @@ -107,10 +104,9 @@ void enableBlockingDisableReactive() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.blocking.enabled=true", "spring.cloud.discovery.reactive.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesCatalogWatch.class); - assertThat(context).doesNotHaveBean(KubernetesClientServicesFunction.class); - assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); - assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8CatalogWatch.class); + assertThat(context).doesNotHaveBean(Fabric8DiscoveryClient.class); + assertThat(context).doesNotHaveBean(Fabric8ReactiveDiscoveryClient.class); }); } @@ -123,16 +119,15 @@ void disableKubernetesDiscovery() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.kubernetes.discovery.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).doesNotHaveBean(KubernetesCatalogWatch.class); - assertThat(context).doesNotHaveBean(KubernetesClientServicesFunction.class); - assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); - assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).doesNotHaveBean(Fabric8CatalogWatch.class); + assertThat(context).doesNotHaveBean(Fabric8DiscoveryClient.class); + assertThat(context).doesNotHaveBean(Fabric8ReactiveDiscoveryClient.class); }); } private void setup(String... properties) { applicationContextRunner = new ApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(KubernetesCatalogWatchAutoConfiguration.class, + .withConfiguration(AutoConfigurations.of(Fabric8CatalogWatchAutoConfiguration.class, Fabric8AutoConfiguration.class, KubernetesCommonsAutoConfiguration.class, KubernetesDiscoveryPropertiesAutoConfiguration.class)) .withPropertyValues(properties); diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesCatalogWatchEndpointSlicesSupportTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointSlicesSupportTests.java similarity index 88% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesCatalogWatchEndpointSlicesSupportTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointSlicesSupportTests.java index cd56d46694..bfa0efd731 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesCatalogWatchEndpointSlicesSupportTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointSlicesSupportTests.java @@ -39,6 +39,7 @@ import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; +import org.springframework.context.ApplicationEventPublisher; /** * Tests that only assert the needed support for EndpointSlices in the cluster. @@ -46,11 +47,14 @@ * @author wind57 */ @EnableKubernetesMockClient -class Fabric8KubernetesCatalogWatchEndpointSlicesSupportTests { +class Fabric8CatalogWatchEndpointSlicesSupportTests { private static final KubernetesNamespaceProvider NAMESPACE_PROVIDER = Mockito .mock(KubernetesNamespaceProvider.class); + private static final ApplicationEventPublisher APPLICATION_EVENT_PUBLISHER = Mockito + .mock(ApplicationEventPublisher.class); + private static KubernetesMockServer mockServer; private static KubernetesClient mockClient; @@ -68,7 +72,8 @@ void testEndpointSlicesEnabledButNotSupportedViaApiGroups() { APIGroupList groupList = new APIGroupListBuilder().build(); mockServer.expect().withPath("/apis").andReturn(200, groupList).always(); - KubernetesCatalogWatch watch = new KubernetesCatalogWatch(mockClient, properties, NAMESPACE_PROVIDER); + Fabric8CatalogWatch watch = new Fabric8CatalogWatch(mockClient, properties, NAMESPACE_PROVIDER, + APPLICATION_EVENT_PUBLISHER); Assertions.assertThatThrownBy(watch::postConstruct) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("EndpointSlices are not supported on the cluster"); @@ -96,7 +101,8 @@ void testEndpointSlicesEnabledButNotSupportedViaApiVersions() { APIResourceList apiResourceList = new APIResourceListBuilder().build(); mockServer.expect().withPath("/apis/discovery.k8s.io/v1").andReturn(200, apiResourceList).always(); - KubernetesCatalogWatch watch = new KubernetesCatalogWatch(mockClient, properties, NAMESPACE_PROVIDER); + Fabric8CatalogWatch watch = new Fabric8CatalogWatch(mockClient, properties, NAMESPACE_PROVIDER, + APPLICATION_EVENT_PUBLISHER); Assertions.assertThatThrownBy(watch::postConstruct) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("EndpointSlices are not supported on the cluster"); @@ -110,7 +116,8 @@ void testEndpointSlicesEnabledButNotSupportedViaApiVersions() { void testEndpointsSupport() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60, false, "", Set.of(), Map.of(), "", null, 0, false, false, null); - KubernetesCatalogWatch watch = new KubernetesCatalogWatch(mockClient, properties, NAMESPACE_PROVIDER); + Fabric8CatalogWatch watch = new Fabric8CatalogWatch(mockClient, properties, NAMESPACE_PROVIDER, + APPLICATION_EVENT_PUBLISHER); Assertions.assertThat(watch.stateGenerator().getClass()).isEqualTo(Fabric8EndpointsCatalogWatch.class); } @@ -123,7 +130,8 @@ void testEndpointsSupport() { void testEndpointSlicesSupport() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60, false, "", Set.of(), Map.of(), "", null, 0, true, false, null); - KubernetesCatalogWatch watch = new KubernetesCatalogWatch(mockClient, properties, NAMESPACE_PROVIDER); + Fabric8CatalogWatch watch = new Fabric8CatalogWatch(mockClient, properties, NAMESPACE_PROVIDER, + APPLICATION_EVENT_PUBLISHER); GroupVersionForDiscovery forDiscovery = new GroupVersionForDiscoveryBuilder() .withGroupVersion("discovery.k8s.io/v1") diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesCatalogWatchEndpointSlicesTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointSlicesTests.java similarity index 85% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesCatalogWatchEndpointSlicesTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointSlicesTests.java index 826f3f598f..ed88bcb000 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesCatalogWatchEndpointSlicesTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointSlicesTests.java @@ -35,7 +35,7 @@ * @author wind57 */ @EnableKubernetesMockClient(crud = true, https = false) -class Fabric8KubernetesCatalogWatchEndpointSlicesTests extends Fabric8EndpointsAndEndpointSlicesTests { +class Fabric8CatalogWatchEndpointSlicesTests extends Fabric8EndpointsAndEndpointSlicesTests { private static final Boolean ENDPOINT_SLICES = true; @@ -45,7 +45,7 @@ class Fabric8KubernetesCatalogWatchEndpointSlicesTests extends Fabric8EndpointsA @Override void testInSpecificNamespaceWithServiceLabels() { - KubernetesCatalogWatch watch = createWatcherInSpecificNamespaceWithLabels("namespaceA", Map.of("color", "blue"), + Fabric8CatalogWatch watch = createWatcherInSpecificNamespaceWithLabels("namespaceA", Map.of("color", "blue"), ENDPOINT_SLICES); endpointSlice("namespaceA", Map.of(), "podA"); @@ -61,8 +61,7 @@ void testInSpecificNamespaceWithServiceLabels() { @Override void testInSpecificNamespaceWithoutServiceLabels() { - KubernetesCatalogWatch watch = createWatcherInSpecificNamespaceWithLabels("namespaceA", Map.of(), - ENDPOINT_SLICES); + Fabric8CatalogWatch watch = createWatcherInSpecificNamespaceWithLabels("namespaceA", Map.of(), ENDPOINT_SLICES); endpointSlice("namespaceA", Map.of(), "podA"); endpointSlice("namespaceA", Map.of("color", "blue"), "podB"); @@ -80,7 +79,7 @@ void testInSpecificNamespaceWithoutServiceLabels() { @Override void testInAllNamespacesWithServiceLabels() { - KubernetesCatalogWatch watch = createWatcherInAllNamespacesWithLabels(Map.of("color", "blue"), Set.of(), + Fabric8CatalogWatch watch = createWatcherInAllNamespacesWithLabels(Map.of("color", "blue"), Set.of(), ENDPOINT_SLICES); endpointSlice("namespaceA", Map.of(), "podA"); @@ -97,7 +96,7 @@ void testInAllNamespacesWithServiceLabels() { @Override void testInAllNamespacesWithoutServiceLabels() { - KubernetesCatalogWatch watch = createWatcherInAllNamespacesWithLabels(Map.of(), Set.of(), ENDPOINT_SLICES); + Fabric8CatalogWatch watch = createWatcherInAllNamespacesWithLabels(Map.of(), Set.of(), ENDPOINT_SLICES); endpointSlice("namespaceA", Map.of(), "podA"); endpointSlice("namespaceA", Map.of("color", "blue"), "podB"); @@ -115,7 +114,7 @@ void testInAllNamespacesWithoutServiceLabels() { @Override void testAllNamespacesTrueOtherBranchesNotCalled() { - KubernetesCatalogWatch watch = createWatcherInAllNamespacesWithLabels(Map.of("color", "blue"), Set.of("B"), + Fabric8CatalogWatch watch = createWatcherInAllNamespacesWithLabels(Map.of("color", "blue"), Set.of("B"), ENDPOINT_SLICES); endpointSlice("namespaceA", Map.of(), "podA"); @@ -132,7 +131,7 @@ void testAllNamespacesTrueOtherBranchesNotCalled() { @Override void testAllNamespacesFalseNamespacesPresent() { - KubernetesCatalogWatch watch = createWatcherInSpecificNamespacesWithLabels(Set.of("namespaceA"), + Fabric8CatalogWatch watch = createWatcherInSpecificNamespacesWithLabels(Set.of("namespaceA"), Map.of("color", "blue"), ENDPOINT_SLICES); endpointSlice("namespaceA", Map.of(), "podA"); @@ -148,7 +147,7 @@ void testAllNamespacesFalseNamespacesPresent() { @Override void testAllNamespacesFalseNamespacesNotPresent() { - KubernetesCatalogWatch watch = createWatcherInSpecificNamespaceWithLabels("namespaceA", Map.of("color", "blue"), + Fabric8CatalogWatch watch = createWatcherInSpecificNamespaceWithLabels("namespaceA", Map.of("color", "blue"), ENDPOINT_SLICES); endpointSlice("namespaceA", Map.of(), "podA"); @@ -164,7 +163,7 @@ void testAllNamespacesFalseNamespacesNotPresent() { @Override void testTwoNamespacesOutOfThree() { - KubernetesCatalogWatch watch = createWatcherInSpecificNamespacesWithLabels(Set.of("namespaceA", "namespaceB"), + Fabric8CatalogWatch watch = createWatcherInSpecificNamespacesWithLabels(Set.of("namespaceA", "namespaceB"), Map.of("color", "blue"), ENDPOINT_SLICES); endpointSlice("namespaceA", Map.of(), "podA"); @@ -184,7 +183,7 @@ void testTwoNamespacesOutOfThree() { @Test @Override void testWithoutSubsetsOrEndpoints() { - KubernetesCatalogWatch watch = createWatcherInSpecificNamespacesWithLabels(Set.of("namespaceA"), + Fabric8CatalogWatch watch = createWatcherInSpecificNamespacesWithLabels(Set.of("namespaceA"), Map.of("color", "blue"), ENDPOINT_SLICES); endpointSliceWithoutEndpoints("namespaceA", Map.of("color", "blue"), "podA"); diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesCatalogWatchEndpointsTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointsTests.java similarity index 89% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesCatalogWatchEndpointsTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointsTests.java index afbb79d42c..576b6ac188 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesCatalogWatchEndpointsTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointsTests.java @@ -35,7 +35,7 @@ * @author wind57 */ @EnableKubernetesMockClient(crud = true, https = false) -class Fabric8KubernetesCatalogWatchEndpointsTests extends Fabric8EndpointsAndEndpointSlicesTests { +class Fabric8CatalogWatchEndpointsTests extends Fabric8EndpointsAndEndpointSlicesTests { private static final Boolean ENDPOINT_SLICES = false; @@ -49,7 +49,7 @@ void afterEach() { @Override void testInSpecificNamespaceWithServiceLabels() { - KubernetesCatalogWatch watch = createWatcherInSpecificNamespaceWithLabels("namespaceA", Map.of("color", "blue"), + Fabric8CatalogWatch watch = createWatcherInSpecificNamespaceWithLabels("namespaceA", Map.of("color", "blue"), ENDPOINT_SLICES); endpoints("namespaceA", Map.of(), "podA"); @@ -71,8 +71,7 @@ void testInSpecificNamespaceWithServiceLabels() { @Override void testInSpecificNamespaceWithoutServiceLabels() { - KubernetesCatalogWatch watch = createWatcherInSpecificNamespaceWithLabels("namespaceA", Map.of(), - ENDPOINT_SLICES); + Fabric8CatalogWatch watch = createWatcherInSpecificNamespaceWithLabels("namespaceA", Map.of(), ENDPOINT_SLICES); endpoints("namespaceA", Map.of(), "podA"); endpoints("namespaceA", Map.of("color", "blue"), "podB"); @@ -96,7 +95,7 @@ void testInSpecificNamespaceWithoutServiceLabels() { @Override void testInAllNamespacesWithServiceLabels() { - KubernetesCatalogWatch watch = createWatcherInAllNamespacesWithLabels(Map.of("color", "blue"), Set.of(), + Fabric8CatalogWatch watch = createWatcherInAllNamespacesWithLabels(Map.of("color", "blue"), Set.of(), ENDPOINT_SLICES); endpoints("namespaceA", Map.of(), "podA"); @@ -119,7 +118,7 @@ void testInAllNamespacesWithServiceLabels() { @Override void testInAllNamespacesWithoutServiceLabels() { - KubernetesCatalogWatch watch = createWatcherInAllNamespacesWithLabels(Map.of(), Set.of(), ENDPOINT_SLICES); + Fabric8CatalogWatch watch = createWatcherInAllNamespacesWithLabels(Map.of(), Set.of(), ENDPOINT_SLICES); endpoints("namespaceA", Map.of(), "podA"); endpoints("namespaceA", Map.of("color", "blue"), "podB"); @@ -143,7 +142,7 @@ void testInAllNamespacesWithoutServiceLabels() { @Override void testAllNamespacesTrueOtherBranchesNotCalled() { - KubernetesCatalogWatch watch = createWatcherInAllNamespacesWithLabels(Map.of("color", "blue"), Set.of(), + Fabric8CatalogWatch watch = createWatcherInAllNamespacesWithLabels(Map.of("color", "blue"), Set.of(), ENDPOINT_SLICES); endpoints("namespaceA", Map.of(), "podA"); @@ -166,7 +165,7 @@ void testAllNamespacesTrueOtherBranchesNotCalled() { @Override void testAllNamespacesFalseNamespacesPresent() { - KubernetesCatalogWatch watch = createWatcherInSpecificNamespacesWithLabels(Set.of("namespaceA"), + Fabric8CatalogWatch watch = createWatcherInSpecificNamespacesWithLabels(Set.of("namespaceA"), Map.of("color", "blue"), ENDPOINT_SLICES); endpoints("namespaceA", Map.of(), "podA"); @@ -188,7 +187,7 @@ void testAllNamespacesFalseNamespacesPresent() { @Override void testAllNamespacesFalseNamespacesNotPresent() { - KubernetesCatalogWatch watch = createWatcherInSpecificNamespaceWithLabels("namespaceA", Map.of("color", "blue"), + Fabric8CatalogWatch watch = createWatcherInSpecificNamespaceWithLabels("namespaceA", Map.of("color", "blue"), ENDPOINT_SLICES); endpoints("namespaceA", Map.of(), "podA"); @@ -210,7 +209,7 @@ void testAllNamespacesFalseNamespacesNotPresent() { @Override void testTwoNamespacesOutOfThree() { - KubernetesCatalogWatch watch = createWatcherInSpecificNamespacesWithLabels(Set.of("namespaceA", "namespaceB"), + Fabric8CatalogWatch watch = createWatcherInSpecificNamespacesWithLabels(Set.of("namespaceA", "namespaceB"), Map.of("color", "blue"), ENDPOINT_SLICES); endpoints("namespaceA", Map.of(), "podA"); @@ -238,7 +237,7 @@ void testTwoNamespacesOutOfThree() { @Test @Override void testWithoutSubsetsOrEndpoints() { - KubernetesCatalogWatch watch = createWatcherInSpecificNamespacesWithLabels(Set.of("namespaceA"), + Fabric8CatalogWatch watch = createWatcherInSpecificNamespacesWithLabels(Set.of("namespaceA"), Map.of("color", "blue"), ENDPOINT_SLICES); endpointsWithoutSubsets("namespaceA", Map.of("color", "blue"), "podA"); diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogWatchTest.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchTest.java similarity index 97% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogWatchTest.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchTest.java index abe7bb672f..8f8332879b 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesCatalogWatchTest.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchTest.java @@ -60,13 +60,13 @@ * @author Oleg Vyukov */ @SuppressWarnings({ "unchecked" }) -class KubernetesCatalogWatchTest { +class Fabric8CatalogWatchTest { private static final KubernetesClient CLIENT = Mockito.mock(KubernetesClient.class); private final KubernetesNamespaceProvider namespaceProvider = Mockito.mock(KubernetesNamespaceProvider.class); - private KubernetesCatalogWatch kubernetesCatalogWatch; + private Fabric8CatalogWatch kubernetesCatalogWatch; private static final ApplicationEventPublisher APPLICATION_EVENT_PUBLISHER = Mockito .mock(ApplicationEventPublisher.class); @@ -405,8 +405,8 @@ private void createInAllNamespaceWatcher() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60, false, "", Set.of(), Map.of(), "", null, 0, false, false, null); - kubernetesCatalogWatch = new KubernetesCatalogWatch(CLIENT, properties, namespaceProvider); - kubernetesCatalogWatch.setApplicationEventPublisher(APPLICATION_EVENT_PUBLISHER); + kubernetesCatalogWatch = new Fabric8CatalogWatch(CLIENT, properties, namespaceProvider, + APPLICATION_EVENT_PUBLISHER); kubernetesCatalogWatch.postConstruct(); when(CLIENT.endpoints()).thenReturn(MIXED_OPERATION); @@ -424,8 +424,8 @@ private void createInSpecificNamespaceWatcher() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, "", Set.of(), Map.of(), "", null, 0, false, false, null); - kubernetesCatalogWatch = new KubernetesCatalogWatch(CLIENT, properties, namespaceProvider); - kubernetesCatalogWatch.setApplicationEventPublisher(APPLICATION_EVENT_PUBLISHER); + kubernetesCatalogWatch = new Fabric8CatalogWatch(CLIENT, properties, namespaceProvider, + APPLICATION_EVENT_PUBLISHER); kubernetesCatalogWatch.postConstruct(); when(namespaceProvider.getNamespace()).thenReturn("catalog-watcher-namespace"); diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientAutoConfigurationApplicationContextTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientAutoConfigurationApplicationContextTests.java similarity index 71% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientAutoConfigurationApplicationContextTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientAutoConfigurationApplicationContextTests.java index 89b02cea8a..be0ccbac4c 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientAutoConfigurationApplicationContextTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientAutoConfigurationApplicationContextTests.java @@ -30,11 +30,11 @@ import static org.assertj.core.api.Assertions.assertThat; /** - * Test various conditionals for {@link KubernetesDiscoveryClientAutoConfiguration} + * Test various conditionals for {@link Fabric8DiscoveryClientAutoConfiguration} * * @author wind57 */ -class KubernetesDiscoveryClientAutoConfigurationApplicationContextTests { +class Fabric8DiscoveryClientAutoConfigurationApplicationContextTests { private ApplicationContextRunner applicationContextRunner; @@ -42,8 +42,7 @@ class KubernetesDiscoveryClientAutoConfigurationApplicationContextTests { void discoveryEnabledDefault() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); - assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8DiscoveryClient.class); assertThat(context).hasSingleBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class); }); } @@ -53,8 +52,7 @@ void discoveryEnabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.enabled=true"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); - assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8DiscoveryClient.class); assertThat(context).hasSingleBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class); }); } @@ -64,8 +62,7 @@ void discoveryDisabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).doesNotHaveBean(KubernetesClientServicesFunction.class); - assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); + assertThat(context).doesNotHaveBean(Fabric8DiscoveryClient.class); assertThat(context).doesNotHaveBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class); }); } @@ -75,8 +72,7 @@ void kubernetesDiscoveryEnabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.kubernetes.discovery.enabled=true"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); - assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8DiscoveryClient.class); assertThat(context).hasSingleBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class); }); } @@ -86,8 +82,7 @@ void kubernetesDiscoveryDisabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.kubernetes.discovery.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).doesNotHaveBean(KubernetesClientServicesFunction.class); - assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); + assertThat(context).doesNotHaveBean(Fabric8DiscoveryClient.class); assertThat(context).doesNotHaveBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class); }); } @@ -97,8 +92,7 @@ void kubernetesDiscoveryBlockingEnabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.blocking.enabled=true"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); - assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8DiscoveryClient.class); assertThat(context).hasSingleBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class); }); } @@ -108,8 +102,7 @@ void kubernetesDiscoveryBlockingDisabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.blocking.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).doesNotHaveBean(KubernetesClientServicesFunction.class); - assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); + assertThat(context).doesNotHaveBean(Fabric8DiscoveryClient.class); assertThat(context).doesNotHaveBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class); }); } @@ -119,8 +112,7 @@ void kubernetesDiscoveryHealthIndicatorEnabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.client.health-indicator.enabled=true"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); - assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8DiscoveryClient.class); assertThat(context).hasSingleBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class); }); } @@ -130,8 +122,7 @@ void kubernetesDiscoveryHealthIndicatorDisabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.client.health-indicator.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); - assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8DiscoveryClient.class); assertThat(context).doesNotHaveBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class); }); } @@ -141,8 +132,7 @@ void kubernetesDiscoveryHealthIndicatorEnabledHealthIndicatorMissing() { setupWithFilteredClassLoader(HealthIndicator.class, "spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.client.health-indicator.enabled=true"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); - assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8DiscoveryClient.class); assertThat(context).doesNotHaveBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class); }); } @@ -155,25 +145,25 @@ void reactiveDisabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.reactive.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); - assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8DiscoveryClient.class); assertThat(context).hasSingleBean(KubernetesDiscoveryClientHealthIndicatorInitializer.class); }); } private void setup(String... properties) { - applicationContextRunner = new ApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(KubernetesDiscoveryClientAutoConfiguration.class, - Fabric8AutoConfiguration.class, KubernetesCommonsAutoConfiguration.class, - KubernetesDiscoveryPropertiesAutoConfiguration.class)) + applicationContextRunner = new ApplicationContextRunner().withConfiguration( + AutoConfigurations.of(Fabric8DiscoveryClientAutoConfiguration.class, Fabric8AutoConfiguration.class, + KubernetesCommonsAutoConfiguration.class, KubernetesDiscoveryPropertiesAutoConfiguration.class, + Fabric8DiscoveryClientSpelAutoConfiguration.class)) .withPropertyValues(properties); } private void setupWithFilteredClassLoader(Class cls, String... properties) { applicationContextRunner = new ApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(KubernetesDiscoveryClientAutoConfiguration.class, + .withConfiguration(AutoConfigurations.of(Fabric8DiscoveryClientAutoConfiguration.class, Fabric8AutoConfiguration.class, KubernetesCommonsAutoConfiguration.class, - KubernetesDiscoveryPropertiesAutoConfiguration.class)) + KubernetesDiscoveryPropertiesAutoConfiguration.class, + Fabric8DiscoveryClientSpelAutoConfiguration.class)) .withClassLoader(new FilteredClassLoader(cls)) .withPropertyValues(properties); } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientAutoConfigurationPropertiesTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientAutoConfigurationPropertiesTests.java similarity index 81% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientAutoConfigurationPropertiesTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientAutoConfigurationPropertiesTests.java index 80539fc2f9..9833e41123 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientAutoConfigurationPropertiesTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientAutoConfigurationPropertiesTests.java @@ -25,6 +25,7 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.kubernetes.commons.PodUtils; @@ -41,7 +42,7 @@ * @author Ryan Dawson * @author Tim Ysewyn */ -class KubernetesDiscoveryClientAutoConfigurationPropertiesTests { +class Fabric8DiscoveryClientAutoConfigurationPropertiesTests { private ConfigurableApplicationContext context; @@ -56,34 +57,34 @@ void afterEach() { void kubernetesDiscoveryDisabled() { setup("spring.cloud.kubernetes.discovery.enabled=false", "spring.cloud.kubernetes.discovery.catalog-services-watch.enabled=false"); - assertThat(context.getBeanNamesForType(KubernetesDiscoveryClient.class)).isEmpty(); + assertThat(context.getBeanNamesForType(Fabric8DiscoveryClient.class)).isEmpty(); } @Test void kubernetesDiscoveryWhenKubernetesDisabled() { setup(); - assertThat(context.getBeanNamesForType(KubernetesDiscoveryClient.class)).isEmpty(); + assertThat(context.getBeanNamesForType(Fabric8DiscoveryClient.class)).isEmpty(); } @Test void kubernetesDiscoveryWhenDiscoveryDisabled() { setup("spring.cloud.discovery.enabled=false"); - assertThat(context.getBeanNamesForType(KubernetesDiscoveryClient.class)).isEmpty(); + assertThat(context.getBeanNamesForType(Fabric8DiscoveryClient.class)).isEmpty(); } @Test void kubernetesDiscoveryDefaultEnabled() { setup("spring.main.cloud-platform=KUBERNETES"); - assertThat(context.getBeanNamesForType(KubernetesDiscoveryClient.class)).hasSize(1); + assertThat(context.getBeanNamesForType(Fabric8DiscoveryClient.class)).hasSize(1); } private void setup(String... env) { List envList = new ArrayList<>(Arrays.asList(env)); envList.add("spring.cloud.config.enabled=false"); context = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class, - KubernetesClientTestConfiguration.class, KubernetesDiscoveryClientAutoConfiguration.class, - KubernetesDiscoveryPropertiesAutoConfiguration.class) - .web(org.springframework.boot.WebApplicationType.NONE) + KubernetesClientTestConfiguration.class, Fabric8DiscoveryClientAutoConfiguration.class, + KubernetesDiscoveryPropertiesAutoConfiguration.class, Fabric8DiscoveryClientSpelAutoConfiguration.class) + .web(WebApplicationType.NONE) .properties(envList.toArray(new String[0])) .run(); } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientAutoConfigurationTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientAutoConfigurationTests.java similarity index 94% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientAutoConfigurationTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientAutoConfigurationTests.java index 1761c4ca26..657ce207bc 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientAutoConfigurationTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientAutoConfigurationTests.java @@ -29,7 +29,7 @@ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.kubernetes.discovery.use-endpoint-slices=false" }) -class KubernetesDiscoveryClientAutoConfigurationTests { +class Fabric8DiscoveryClientAutoConfigurationTests { @Autowired private DiscoveryClient discoveryClient; @@ -39,7 +39,7 @@ void kubernetesDiscoveryClientCreated() { assertThat(this.discoveryClient).isInstanceOf(CompositeDiscoveryClient.class); CompositeDiscoveryClient composite = (CompositeDiscoveryClient) this.discoveryClient; - assertThat(composite.getDiscoveryClients().stream().anyMatch(dc -> dc instanceof KubernetesDiscoveryClient)) + assertThat(composite.getDiscoveryClients().stream().anyMatch(dc -> dc instanceof Fabric8DiscoveryClient)) .isTrue(); } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientConfigClientBootstrapConfigurationTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientConfigClientBootstrapConfigurationTests.java similarity index 91% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientConfigClientBootstrapConfigurationTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientConfigClientBootstrapConfigurationTests.java index e9978bcbd7..a3e67bf76b 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientConfigClientBootstrapConfigurationTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientConfigClientBootstrapConfigurationTests.java @@ -43,7 +43,7 @@ /** * @author Zhanwei Wang */ -class KubernetesDiscoveryClientConfigClientBootstrapConfigurationTests { +class Fabric8DiscoveryClientConfigClientBootstrapConfigurationTests { private AnnotationConfigApplicationContext context; @@ -74,13 +74,13 @@ private void setup(String... env) { TestPropertyValues.of(env).applyTo(parent); parent.register(UtilAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, EnvironmentKnobbler.class, KubernetesCommonsAutoConfiguration.class, - KubernetesDiscoveryClientConfigClientBootstrapConfiguration.class, + Fabric8DiscoveryClientConfigClientBootstrapConfiguration.class, DiscoveryClientConfigServiceBootstrapConfiguration.class, ConfigClientProperties.class); parent.refresh(); context = new AnnotationConfigApplicationContext(); context.setParent(parent); context.register(PropertyPlaceholderAutoConfiguration.class, KubernetesCommonsAutoConfiguration.class, - KubernetesDiscoveryClientAutoConfiguration.class); + Fabric8DiscoveryClientAutoConfiguration.class); context.refresh(); } @@ -88,8 +88,8 @@ private void setup(String... env) { protected static class EnvironmentKnobbler { @Bean - KubernetesDiscoveryClient kubernetesDiscoveryClient() { - KubernetesDiscoveryClient client = mock(KubernetesDiscoveryClient.class); + Fabric8DiscoveryClient kubernetesDiscoveryClient() { + Fabric8DiscoveryClient client = mock(Fabric8DiscoveryClient.class); ServiceInstance instance = new DefaultServiceInstance("configserver1", "configserver", "fake", 8888, false); given(client.getInstances("configserver")).willReturn(Collections.singletonList(instance)); return client; diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientFilterMetadataTest.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientFilterMetadataTest.java similarity index 82% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientFilterMetadataTest.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientFilterMetadataTest.java index 5099410fdb..5829ecc5b1 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientFilterMetadataTest.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientFilterMetadataTest.java @@ -45,7 +45,9 @@ import org.mockito.Mockito; import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; +import org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver; import org.springframework.core.env.Environment; import org.springframework.mock.env.MockEnvironment; @@ -59,10 +61,16 @@ import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties.Metadata; @SuppressWarnings("unchecked") -class KubernetesDiscoveryClientFilterMetadataTest { +class Fabric8DiscoveryClientFilterMetadataTest { private static final KubernetesClient CLIENT = Mockito.mock(KubernetesClient.class); + private static final ServicePortSecureResolver SERVICE_PORT_SECURE_RESOLVER = new ServicePortSecureResolver( + KubernetesDiscoveryProperties.DEFAULT); + + private static final KubernetesNamespaceProvider NAMESPACE_PROVIDER = new KubernetesNamespaceProvider( + mockEnvironment()); + private final MixedOperation> serviceOperation = Mockito .mock(MixedOperation.class); @@ -85,13 +93,14 @@ void testAllExtraMetadataDisabled() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, null, Set.of(), Map.of(), null, metadata, 0, true, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); - discoveryClient.setEnvironment(withClientNamespace()); + Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(CLIENT, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties)); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "lab"), Map.of("l1", "lab"), Map.of(80, "http", 5555, "")); - List instances = discoveryClient.getInstances(serviceId); + List instances = fabric8DiscoveryClient.getInstances(serviceId); assertThat(instances).hasSize(1); assertThat(instances.get(0).getMetadata()).isEqualTo(Map.of("k8s_namespace", "ns", "type", "ClusterIP")); } @@ -104,13 +113,14 @@ void testLabelsEnabled() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, null, Set.of(), Map.of(), null, metadata, 0, true, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); - discoveryClient.setEnvironment(withClientNamespace()); + Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(CLIENT, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties)); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1", "l2", "v2"), Map.of("l1", "lab"), Map.of(80, "http", 5555, "")); - List instances = discoveryClient.getInstances(serviceId); + List instances = fabric8DiscoveryClient.getInstances(serviceId); assertThat(instances).hasSize(1); assertThat(instances.get(0).getMetadata()).containsOnly(entry("l1", "v1"), entry("l2", "v2"), entry("k8s_namespace", "ns"), entry("type", "ClusterIP")); @@ -124,8 +134,9 @@ void testLabelsEnabledWithPrefix() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, null, Set.of(), Map.of(), null, metadata, 0, true, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); - discoveryClient.setEnvironment(withClientNamespace()); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(CLIENT, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties)); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1", "l2", "v2"), Map.of("l1", "lab"), Map.of(80, "http", 5555, "")); @@ -144,8 +155,9 @@ void testAnnotationsEnabled() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, null, Set.of(), Map.of(), null, metadata, 0, true, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); - discoveryClient.setEnvironment(withClientNamespace()); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(CLIENT, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties)); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1"), Map.of("a1", "v1", "a2", "v2"), Map.of(80, "http", 5555, "")); @@ -164,8 +176,9 @@ void testAnnotationsEnabledWithPrefix() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, null, Set.of(), Map.of(), null, metadata, 0, true, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); - discoveryClient.setEnvironment(withClientNamespace()); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(CLIENT, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties)); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1"), Map.of("a1", "v1", "a2", "v2"), Map.of(80, "http", 5555, "")); @@ -184,8 +197,9 @@ void testPortsEnabled() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, null, Set.of(), Map.of(), null, metadata, 0, true, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); - discoveryClient.setEnvironment(withClientNamespace()); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(CLIENT, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties)); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "test", Map.of("l1", "v1"), Map.of("a1", "v1", "a2", "v2"), Map.of(80, "http", 5555, "")); @@ -204,8 +218,9 @@ void testPortsEnabledWithPrefix() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, null, Set.of(), Map.of(), null, metadata, 0, true, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); - discoveryClient.setEnvironment(withClientNamespace()); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(CLIENT, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties)); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1"), Map.of("a1", "v1", "a2", "v2"), Map.of(80, "http", 5555, "")); @@ -224,8 +239,9 @@ void testLabelsAndAnnotationsAndPortsEnabledWithPrefix() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, null, Set.of(), Map.of(), null, metadata, 0, true, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); - discoveryClient.setEnvironment(withClientNamespace()); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(CLIENT, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties)); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "la1"), Map.of("a1", "an1", "a2", "an2"), Map.of(80, "http", 5555, "")); @@ -313,4 +329,10 @@ private static Environment withClientNamespace() { return mockEnvironment; } + private static Environment mockEnvironment() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("spring.cloud.kubernetes.client.namespace", "test"); + return environment; + } + } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientFilterTest.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientFilterTest.java similarity index 58% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientFilterTest.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientFilterTest.java index 4d5365daf2..d6ce3c84ba 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientFilterTest.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientFilterTest.java @@ -26,25 +26,44 @@ import io.fabric8.kubernetes.api.model.Service; import io.fabric8.kubernetes.api.model.ServiceList; import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.FilterNested; +import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable; import io.fabric8.kubernetes.client.dsl.MixedOperation; import io.fabric8.kubernetes.client.dsl.ServiceResource; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; +import org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver; +import org.springframework.core.env.Environment; +import org.springframework.mock.env.MockEnvironment; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; -class KubernetesDiscoveryClientFilterTest { +class Fabric8DiscoveryClientFilterTest { - private final KubernetesClient kubernetesClient = Mockito.mock(KubernetesClient.class); + private static final ServicePortSecureResolver SERVICE_PORT_SECURE_RESOLVER = new ServicePortSecureResolver( + KubernetesDiscoveryProperties.DEFAULT); + + private static final KubernetesNamespaceProvider NAMESPACE_PROVIDER = new KubernetesNamespaceProvider( + mockEnvironment()); - private final KubernetesClientServicesFunction kubernetesClientServicesFunction = KubernetesClient::services; + private final KubernetesClient kubernetesClient = Mockito.mock(KubernetesClient.class); + @SuppressWarnings("unchecked") private final MixedOperation> serviceOperation = Mockito .mock(MixedOperation.class); + @SuppressWarnings("unchecked") + private final FilterNested>> filterNested = Mockito + .mock(FilterNested.class); + + @SuppressWarnings("unchecked") + private final FilterWatchListDeletable> filter = Mockito + .mock(FilterWatchListDeletable.class); + @Test void testFilteredServices() { List springBootServiceNames = Arrays.asList("serviceA", "serviceB"); @@ -59,15 +78,20 @@ void testFilteredServices() { ServiceList serviceList = new ServiceList(); serviceList.setItems(services); - when(this.serviceOperation.list()).thenReturn(serviceList); - when(this.kubernetesClient.services()).thenReturn(this.serviceOperation); + when(kubernetesClient.services()).thenReturn(serviceOperation); + when(serviceOperation.inNamespace(Mockito.anyString())).thenReturn(serviceOperation); + when(serviceOperation.withNewFilter()).thenReturn(filterNested); + when(filterNested.withLabels(Mockito.anyMap())).thenReturn(filterNested); + when(filterNested.endFilter()).thenReturn(filter); + when(filter.list()).thenReturn(serviceList); KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, "metadata.additionalProperties['spring-boot']", Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, true, false, null); - KubernetesDiscoveryClient client = new KubernetesDiscoveryClient(this.kubernetesClient, properties, - this.kubernetesClientServicesFunction); + Fabric8DiscoveryClient client = new Fabric8DiscoveryClient(kubernetesClient, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties)); List filteredServices = client.getServices(); assertThat(filteredServices).isEqualTo(springBootServiceNames); @@ -87,14 +111,19 @@ void testFilteredServicesByPrefix() { ServiceList serviceList = new ServiceList(); serviceList.setItems(services); - when(this.serviceOperation.list()).thenReturn(serviceList); - when(this.kubernetesClient.services()).thenReturn(this.serviceOperation); + when(kubernetesClient.services()).thenReturn(serviceOperation); + when(serviceOperation.inNamespace(Mockito.anyString())).thenReturn(serviceOperation); + when(serviceOperation.withNewFilter()).thenReturn(filterNested); + when(filterNested.withLabels(Mockito.anyMap())).thenReturn(filterNested); + when(filterNested.endFilter()).thenReturn(filter); + when(filter.list()).thenReturn(serviceList); KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, "metadata.name.startsWith('service')", Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, true, false, null); - KubernetesDiscoveryClient client = new KubernetesDiscoveryClient(this.kubernetesClient, properties, - this.kubernetesClientServicesFunction); + Fabric8DiscoveryClient client = new Fabric8DiscoveryClient(kubernetesClient, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties)); List filteredServices = client.getServices(); assertThat(filteredServices).isEqualTo(springBootServiceNames); @@ -108,14 +137,19 @@ void testNoExpression() { ServiceList serviceList = new ServiceList(); serviceList.setItems(services); - when(this.serviceOperation.list()).thenReturn(serviceList); - when(this.kubernetesClient.services()).thenReturn(this.serviceOperation); + when(kubernetesClient.services()).thenReturn(serviceOperation); + when(serviceOperation.inNamespace(Mockito.anyString())).thenReturn(serviceOperation); + when(serviceOperation.withNewFilter()).thenReturn(filterNested); + when(filterNested.withLabels(Mockito.anyMap())).thenReturn(filterNested); + when(filterNested.endFilter()).thenReturn(filter); + when(filter.list()).thenReturn(serviceList); KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, "", Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, true, false, null); - KubernetesDiscoveryClient client = new KubernetesDiscoveryClient(this.kubernetesClient, properties, - this.kubernetesClientServicesFunction); + Fabric8DiscoveryClient client = new Fabric8DiscoveryClient(kubernetesClient, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties)); List filteredServices = client.getServices(); @@ -136,4 +170,10 @@ private List createSpringBootServiceByName(List serviceNames) { return serviceCollection; } + private static Environment mockEnvironment() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("spring.cloud.kubernetes.client.namespace", "test"); + return environment; + } + } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientTest.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientOneTests.java similarity index 74% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientTest.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientOneTests.java index 90c7ce4c04..806a26df3d 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientTest.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientOneTests.java @@ -24,7 +24,6 @@ import io.fabric8.kubernetes.api.model.Endpoints; import io.fabric8.kubernetes.api.model.EndpointsBuilder; -import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.api.model.Service; import io.fabric8.kubernetes.api.model.ServiceBuilder; import io.fabric8.kubernetes.api.model.ServiceSpecBuilder; @@ -37,15 +36,24 @@ 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.KubernetesServiceInstance; import org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver; +import org.springframework.core.env.Environment; +import org.springframework.mock.env.MockEnvironment; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties.Metadata; @EnableKubernetesMockClient(crud = true, https = false) -class Fabric8KubernetesDiscoveryClientTest { +class Fabric8DiscoveryClientOneTests { + + private static final ServicePortSecureResolver SERVICE_PORT_SECURE_RESOLVER = new ServicePortSecureResolver( + KubernetesDiscoveryProperties.DEFAULT); + + private static final KubernetesNamespaceProvider NAMESPACE_PROVIDER = new KubernetesNamespaceProvider( + mockEnvironment()); private KubernetesClient mockClient; @@ -97,9 +105,8 @@ void getInstancesShouldBeAbleToHandleEndpointsSingleAddress() { mockClient.services().inNamespace("test").resource(service).create(); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services, null, - new ServicePortSecureResolver(KubernetesDiscoveryProperties.DEFAULT)); + DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, KubernetesDiscoveryProperties.DEFAULT, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); List instances = discoveryClient.getInstances("endpoint"); @@ -147,8 +154,8 @@ void getInstancesShouldBeAbleToHandleEndpointsSingleAddressAndMultiplePorts() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60, false, null, Set.of(), labels, "http_tcp", Metadata.DEFAULT, 0, true, false, null); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, - KubernetesClient::services, null, new ServicePortSecureResolver(properties)); + DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); List instances = discoveryClient.getInstances("endpoint"); @@ -161,148 +168,6 @@ void getInstancesShouldBeAbleToHandleEndpointsSingleAddressAndMultiplePorts() { .hasSize(1); } - @Test - void getEndPointsListTest() { - Map labels = Map.of("l", "v"); - - Endpoints endPoint = new EndpointsBuilder().withNewMetadata() - .withName("endpoint") - .withNamespace("test") - .withLabels(labels) - .endMetadata() - .addNewSubset() - .addNewAddress() - .withIp("ip1") - .withNewTargetRef() - .withUid("30") - .endTargetRef() - .endAddress() - .addNewPort("http", "http_tcp", 80, "TCP") - .endSubset() - .build(); - Service service = new ServiceBuilder().withNewMetadata() - .withName("endpoint") - .withNamespace("test") - .endMetadata() - .build(); - - mockClient.endpoints().inNamespace("test").resource(endPoint).create(); - mockClient.services().inNamespace("test").resource(service).create(); - - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services, null, - new ServicePortSecureResolver(KubernetesDiscoveryProperties.DEFAULT)); - - List result_endpoints = discoveryClient.getEndPointsList("endpoint"); - - assertThat(result_endpoints).hasSize(1); - } - - @Test - void getEndPointsListTestAllNamespaces() { - - String namespace1 = "ns1"; - String namespace2 = "ns2"; - - Endpoints endPoint1 = new EndpointsBuilder().withNewMetadata() - .withName("endpoint") - .withNamespace(namespace1) - .endMetadata() - .build(); - Endpoints endPoint2 = new EndpointsBuilder().withNewMetadata() - .withName("endpoint") - .withNamespace(namespace2) - .endMetadata() - .build(); - - Service service1 = new ServiceBuilder().withNewMetadata() - .withName("endpoint") - .withNamespace(namespace1) - .endMetadata() - .build(); - Service service2 = new ServiceBuilder().withNewMetadata() - .withName("endpoint") - .withNamespace(namespace2) - .endMetadata() - .build(); - - mockClient.endpoints().inNamespace(namespace1).resource(endPoint1).create(); - mockClient.endpoints().inNamespace(namespace2).resource(endPoint2).create(); - mockClient.services().inNamespace(namespace1).resource(service1).create(); - mockClient.services().inNamespace(namespace2).resource(service2).create(); - - KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60, - false, null, Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false, false, - null); - - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, - KubernetesClient::services, x -> true, new ServicePortSecureResolver(properties)); - - List result_endpoints = discoveryClient.getEndPointsList("endpoint"); - - assertThat(result_endpoints).hasSize(2); - } - - @Test - void getEndPointsListShouldHandleNamespaces() { - - String namespace1 = "ns1"; - String namespace2 = "ns2"; - String namespace3 = "ns3"; - - Endpoints endPoint1 = new EndpointsBuilder().withNewMetadata() - .withName("endpoint") - .withNamespace(namespace1) - .endMetadata() - .build(); - Endpoints endPoint2 = new EndpointsBuilder().withNewMetadata() - .withName("endpoint") - .withNamespace(namespace2) - .endMetadata() - .build(); - Endpoints endPoint3 = new EndpointsBuilder().withNewMetadata() - .withName("endpoint") - .withNamespace(namespace3) - .endMetadata() - .build(); - - Service service1 = new ServiceBuilder().withNewMetadata() - .withName("endpoint") - .withNamespace(namespace1) - .endMetadata() - .build(); - Service service2 = new ServiceBuilder().withNewMetadata() - .withName("endpoint") - .withNamespace(namespace2) - .endMetadata() - .build(); - Service service3 = new ServiceBuilder().withNewMetadata() - .withName("endpoint") - .withNamespace(namespace3) - .endMetadata() - .build(); - - mockClient.endpoints().inNamespace(namespace1).resource(endPoint1).create(); - mockClient.endpoints().inNamespace(namespace2).resource(endPoint2).create(); - mockClient.endpoints().inNamespace(namespace3).resource(endPoint3).create(); - mockClient.services().inNamespace(namespace1).resource(service1).create(); - mockClient.services().inNamespace(namespace2).resource(service2).create(); - mockClient.services().inNamespace(namespace3).resource(service3).create(); - - KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, - Set.of(namespace1, namespace3), true, 60, false, null, Set.of(), Map.of(), null, - KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false, false, null); - - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, - KubernetesClient::services, null, new ServicePortSecureResolver(properties)); - - List result_endpoints = discoveryClient.getEndPointsList("endpoint"); - - assertThat(result_endpoints).hasSize(2); - assertThat(result_endpoints.stream().map(Endpoints::getMetadata).map(ObjectMeta::getNamespace).toList()) - .containsOnly(namespace1, namespace3); - } - @Test void getInstancesShouldBeAbleToHandleEndpointsMultipleAddresses() { Map labels = Map.of("l1", "v1"); @@ -345,8 +210,8 @@ void getInstancesShouldBeAbleToHandleEndpointsMultipleAddresses() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60, false, null, Set.of(443, 8443), labels, null, metadata, 0, true, false, null); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, - KubernetesClient::services, null, new ServicePortSecureResolver(properties)); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, properties, + new ServicePortSecureResolver(properties), NAMESPACE_PROVIDER, x -> true); List instances = discoveryClient.getInstances("endpoint"); @@ -384,9 +249,8 @@ void getServicesShouldReturnAllServicesWhenNoLabelsAreAppliedToTheClient() { mockClient.services().inNamespace("test").resource(service2).create(); mockClient.services().inNamespace("test").resource(service3).create(); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services, null, - new ServicePortSecureResolver(KubernetesDiscoveryProperties.DEFAULT)); + DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, KubernetesDiscoveryProperties.DEFAULT, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); List services = discoveryClient.getServices(); @@ -415,10 +279,8 @@ void getServicesShouldReturnOnlyMatchingServicesWhenLabelsAreAppliedToTheClient( mockClient.services().inNamespace("test").resource(service1).create(); mockClient.services().inNamespace("test").resource(service2).create(); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, - KubernetesDiscoveryProperties.DEFAULT, - client -> client.services().withLabels(Collections.singletonMap("label", "value")), null, - new ServicePortSecureResolver(KubernetesDiscoveryProperties.DEFAULT)); + DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, KubernetesDiscoveryProperties.DEFAULT, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); List services = discoveryClient.getServices(); @@ -458,8 +320,8 @@ void getServicesShouldReturnServicesInNamespaces() { Set.of(nameSpace1, nameSpace2), true, 60, false, null, Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false, false, null); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, - KubernetesClient::services, null, new ServicePortSecureResolver(properties)); + DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); List services = discoveryClient.getServices(); @@ -523,8 +385,8 @@ void getInstancesShouldBeAbleToHandleEndpointsFromMultipleNamespaces() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60, false, null, Set.of(), Map.of(), null, Metadata.DEFAULT, 0, true, false, null); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, - KubernetesClient::services, null, new ServicePortSecureResolver(properties)); + DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); List instances = discoveryClient.getInstances("endpoint"); @@ -554,9 +416,8 @@ void instanceWithoutSubsetsShouldBeSkipped() { mockClient.endpoints().inNamespace("test").resource(endPoint).create(); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services, null, - new ServicePortSecureResolver(KubernetesDiscoveryProperties.DEFAULT)); + DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, KubernetesDiscoveryProperties.DEFAULT, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); List instances = discoveryClient.getInstances("endpoint1"); @@ -600,8 +461,8 @@ void getInstancesShouldBeAbleToHandleEndpointsSingleAddressAndMultiplePortsUsing KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60, false, null, Set.of(443, 8443), Map.of(), null, Metadata.DEFAULT, 0, true, false, null); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, - KubernetesClient::services, null, new ServicePortSecureResolver(properties)); + DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); List instances = discoveryClient.getInstances("endpoint2"); @@ -653,8 +514,8 @@ void instanceWithMultiplePortsAndMisconfiguredPrimaryPortNameInLabelWithoutFallb KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, null, Set.of(443, 8443), Map.of(), null, Metadata.DEFAULT, 0, true, false, null); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, - KubernetesClient::services, null, new ServicePortSecureResolver(properties)); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, properties, + new ServicePortSecureResolver(properties), NAMESPACE_PROVIDER, x -> true); List instances = discoveryClient.getInstances("endpoint3"); @@ -705,8 +566,8 @@ void instanceWithMultiplePortsAndMisconfiguredGenericPrimaryPortNameWithoutFallb KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, null, Set.of(443, 8443), Map.of(), "oops", Metadata.DEFAULT, 0, true, false, null); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, - KubernetesClient::services, null, new ServicePortSecureResolver(properties)); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, properties, + new ServicePortSecureResolver(properties), NAMESPACE_PROVIDER, x -> true); List instances = discoveryClient.getInstances("endpoint4"); @@ -755,8 +616,8 @@ void instanceWithMultiplePortsAndWithoutPrimaryPortNameSpecifiedShouldFallBackTo KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60, false, null, Set.of(443, 8443), Map.of(), null, Metadata.DEFAULT, 0, true, false, null); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, - KubernetesClient::services, null, new ServicePortSecureResolver(properties)); + DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); List instances = discoveryClient.getInstances("endpoint5"); @@ -804,9 +665,8 @@ void instanceWithMultiplePortsAndWithoutPrimaryPortNameSpecifiedOrHttpsPortShoul mockClient.services().inNamespace("test").resource(service).create(); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services, null, - new ServicePortSecureResolver(KubernetesDiscoveryProperties.DEFAULT)); + DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, KubernetesDiscoveryProperties.DEFAULT, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); List instances = discoveryClient.getInstances("endpoint5"); @@ -856,8 +716,8 @@ void instanceWithMultiplePortsAndWithoutPrimaryPortNameSpecifiedShouldLogWarning KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60, true, null, Set.of(443, 8443), Map.of(), null, Metadata.DEFAULT, 0, true, false, null); - DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, - KubernetesClient::services, null, new ServicePortSecureResolver(properties)); + DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); List instances = discoveryClient.getInstances("endpoint5"); @@ -905,8 +765,8 @@ public void instanceWithoutPorts() { final KubernetesDiscoveryProperties properties = KubernetesDiscoveryProperties.DEFAULT; - final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, - KubernetesClient::services, null, new ServicePortSecureResolver(properties)); + DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(mockClient, properties, + SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); final List instances = discoveryClient.getInstances("endpoint5"); @@ -921,4 +781,10 @@ public void instanceWithoutPorts() { .hasSize(1); } + private static Environment mockEnvironment() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("spring.cloud.kubernetes.client.namespace", "test"); + return environment; + } + } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientServiceWithoutPortNameTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientServiceWithoutPortNameTests.java similarity index 74% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientServiceWithoutPortNameTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientServiceWithoutPortNameTests.java index 4a7bbe1b81..c9c1956db6 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientServiceWithoutPortNameTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientServiceWithoutPortNameTests.java @@ -36,13 +36,23 @@ import org.junit.jupiter.api.Test; import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; +import org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver; +import org.springframework.core.env.Environment; +import org.springframework.mock.env.MockEnvironment; /** * @author wind57 */ @EnableKubernetesMockClient(crud = true, https = false) -class Fabric8KubernetesDiscoveryClientServiceWithoutPortNameTests { +class Fabric8DiscoveryClientServiceWithoutPortNameTests { + + private static final ServicePortSecureResolver SERVICE_PORT_SECURE_RESOLVER = new ServicePortSecureResolver( + KubernetesDiscoveryProperties.DEFAULT); + + private static final KubernetesNamespaceProvider NAMESPACE_PROVIDER = new KubernetesNamespaceProvider( + mockEnvironment()); private static final String NAMESPACE = "spring-k8s"; @@ -69,13 +79,20 @@ void testDiscoveryWithoutAServicePortName() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(NAMESPACE), true, 60, false, null, Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, true, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient, properties, a -> null); + Fabric8DiscoveryClient client = new Fabric8DiscoveryClient(mockClient, properties, SERVICE_PORT_SECURE_RESOLVER, + NAMESPACE_PROVIDER, new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties)); - List serviceInstances = discoveryClient.getInstances("no-port-name-service"); + List serviceInstances = client.getInstances("no-port-name-service"); Assertions.assertThat(serviceInstances.size()).isEqualTo(1); Assertions.assertThat(serviceInstances.get(0).getMetadata()) .containsExactlyInAnyOrderEntriesOf( Map.of("port.", "8080", "k8s_namespace", "spring-k8s", "type", "ClusterIP")); } + private static Environment mockEnvironment() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("spring.cloud.kubernetes.client.namespace", "test"); + return environment; + } + } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientTwoTests.java similarity index 87% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientTwoTests.java index 00af87541b..c7f3253018 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientTwoTests.java @@ -42,15 +42,21 @@ import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; +import org.springframework.core.env.Environment; +import org.springframework.mock.env.MockEnvironment; /** * @author wind57 */ @EnableKubernetesMockClient(crud = true, https = false) @ExtendWith(OutputCaptureExtension.class) -class Fabric8KubernetesDiscoveryClientTests { +class Fabric8DiscoveryClientTwoTests { + + private static final KubernetesNamespaceProvider NAMESPACE_PROVIDER = new KubernetesNamespaceProvider( + mockEnvironment()); private static KubernetesClient client; @@ -75,8 +81,8 @@ void testAllNamespacesEmpty(CapturedOutput output) { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("serviceId"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "serviceId", x -> true); Assertions.assertThat(result).isEmpty(); Assertions.assertThat(output.getOut()).contains("discovering endpoints in all namespaces"); } @@ -101,8 +107,8 @@ void testAllNamespacesSingleEndpointsMatchExactLabels(CapturedOutput output) { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("blue-service"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "blue-service", x -> true); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(output.getOut()).contains("discovering endpoints in all namespaces"); } @@ -127,8 +133,8 @@ void testAllNamespacesSingleEndpointsMatchPartialLabels(CapturedOutput output) { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("blue-service"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "blue-service", x -> true); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(output.getOut()).contains("discovering endpoints in all namespaces"); } @@ -152,8 +158,8 @@ void testAllNamespacesSingleEndpointsNameMatchesLabelsDont(CapturedOutput output KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("blue-service"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "blue-service", x -> true); Assertions.assertThat(result).isEmpty(); Assertions.assertThat(output.getOut()).contains("discovering endpoints in all namespaces"); } @@ -181,8 +187,8 @@ void testAllNamespacesTwoEndpointsOneMatches(CapturedOutput output) { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("service-one"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "service-one", x -> true); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(output.getOut()).contains("discovering endpoints in all namespaces"); } @@ -210,8 +216,8 @@ void testAllNamespacesTwoEndpointsInDifferentNamespaces(CapturedOutput output) { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("service-one"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "service-one", x -> true); Assertions.assertThat(result.size()).isEqualTo(2); Assertions .assertThat(result.stream().map(Endpoints::getMetadata).map(ObjectMeta::getNamespace).sorted().toList()) @@ -234,8 +240,8 @@ void testClientNamespaceEmpty(CapturedOutput output) { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("serviceId"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "serviceId", x -> true); Assertions.assertThat(result).isEmpty(); Assertions.assertThat(output.getOut()).contains("discovering endpoints in namespace : test"); } @@ -260,8 +266,8 @@ void testClientNamespaceSingleEndpointsMatchExactLabels(CapturedOutput output) { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("blue-service"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "blue-service", x -> true); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(output.getOut()).contains("discovering endpoints in namespace : test"); } @@ -286,8 +292,8 @@ void testClientNamespaceSingleEndpointsMatchPartialLabels(CapturedOutput output) KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("blue-service"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "blue-service", x -> true); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(output.getOut()).contains("discovering endpoints in namespace : test"); } @@ -311,8 +317,8 @@ void testClientNamespaceSingleEndpointsNameMatchesLabelsDont(CapturedOutput outp KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("blue-service"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "blue-service", x -> true); Assertions.assertThat(result).isEmpty(); Assertions.assertThat(output.getOut()).contains("discovering endpoints in namespace : test"); } @@ -340,8 +346,8 @@ void testClientNamespaceTwoEndpointsOneMatches(CapturedOutput output) { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("service-one"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "service-one", x -> true); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(output.getOut()).contains("discovering endpoints in namespace : test"); } @@ -369,8 +375,8 @@ void testClientNamespaceTwoEndpointsInDifferentNamespaces(CapturedOutput output) KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("service-one"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "service-one", x -> true); Assertions.assertThat(result.size()).isEqualTo(1); Assertions .assertThat(result.stream().map(Endpoints::getMetadata).map(ObjectMeta::getNamespace).sorted().toList()) @@ -393,8 +399,8 @@ void testSelectiveNamespacesEmpty(CapturedOutput output) { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("serviceId"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "serviceId", x -> true); Assertions.assertThat(result).isEmpty(); Assertions.assertThat(output.getOut()).contains("discovering endpoints in namespaces : [test]"); } @@ -419,9 +425,8 @@ void testSelectiveNamespacesSingleEndpointsMatchExactLabels(CapturedOutput outpu KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, x -> true, - null); - List result = discoveryClient.getEndPointsList("blue-service"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "blue-service", x -> true); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(output.getOut()).contains("discovering endpoints in namespaces : [test]"); } @@ -450,8 +455,8 @@ void testSelectiveNamespacesMultipleNamespacesSingleMatch(CapturedOutput output) KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("blue-service"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "blue-service", x -> true); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(result.get(0).getMetadata().getLabels()) .containsExactlyInAnyOrderEntriesOf(Map.of("color", "blue", "shape", "round")); @@ -484,8 +489,8 @@ void testSelectiveNamespacesMultipleNamespacesAllMatch(CapturedOutput output) { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60L, false, "", Set.of(), serviceLabels, "", null, 0, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); - List result = discoveryClient.getEndPointsList("blue-service"); + List result = Fabric8DiscoveryClientUtils.endpoints(properties, client, NAMESPACE_PROVIDER, + "fabric8", "blue-service", x -> true); Assertions.assertThat(result.size()).isEqualTo(2); Assertions.assertThat(output.getOut()).contains("discovering endpoints in namespaces : " + namespacesAsString); } @@ -516,12 +521,11 @@ void testGetServicesWithExternalNameService() { .build(); client.services().inNamespace("b").resource(externalNameService).create(); - // last argument is irrelevant, as getServices does not care about that flag KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of("a", "b"), true, - 60L, false, "", Set.of(), Map.of(), "", KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false, false, + 60L, false, "", Set.of(), Map.of(), "", KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false, true, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(client, properties, null, null, x -> true); List result = discoveryClient.getServices(); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(result.get(0)).isEqualTo("blue-service"); @@ -545,7 +549,7 @@ void testExternalNameService() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of("a", "b"), true, 60L, false, "", Set.of(), Map.of(), "", metadata, 0, false, true, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(client, properties, null, null, null); List result = discoveryClient.getInstances("blue-service"); Assertions.assertThat(result.size()).isEqualTo(1); DefaultKubernetesServiceInstance externalNameServiceInstance = (DefaultKubernetesServiceInstance) result.get(0); @@ -596,7 +600,7 @@ void testPodMetadata() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of("a", "b"), true, 60L, false, "", Set.of(), Map.of(), "", metadata, 0, false, true, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(client, properties, null, null, null); List result = discoveryClient.getInstances("blue-service"); Assertions.assertThat(result.size()).isEqualTo(1); DefaultKubernetesServiceInstance serviceInstance = (DefaultKubernetesServiceInstance) result.get(0); @@ -619,7 +623,7 @@ void testOrder() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, Set.of(), true, 60L, false, "", Set.of(), Map.of(), "", null, 57, false, false, null); - KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(client, properties, null, null, null); + Fabric8DiscoveryClient discoveryClient = new Fabric8DiscoveryClient(client, properties, null, null, null); Assertions.assertThat(discoveryClient.getOrder()).isEqualTo(57); } @@ -642,4 +646,10 @@ private void createService(String namespace, String name, Map la .create(); } + private static Environment mockEnvironment() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("spring.cloud.kubernetes.client.namespace", "test"); + return environment; + } + } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientUtilsFilterTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientUtilsFilterTests.java similarity index 86% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientUtilsFilterTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientUtilsFilterTests.java index 0d46dfc71c..5f2308a088 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientUtilsFilterTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientUtilsFilterTests.java @@ -33,13 +33,13 @@ import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; -import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.ALWAYS_TRUE; +import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.ALWAYS_TRUE; /** * @author wind57 */ @EnableKubernetesMockClient(crud = true, https = false) -class Fabric8KubernetesDiscoveryClientUtilsFilterTests { +class Fabric8DiscoveryClientUtilsFilterTests { private static KubernetesClient client; @@ -54,8 +54,7 @@ void afterEach() { @Test void withFilterEmptyInput() { - List result = Fabric8KubernetesDiscoveryClientUtils.withFilter(List.of(), PROPERTIES, client, - ALWAYS_TRUE); + List result = Fabric8DiscoveryClientUtils.withFilter(List.of(), PROPERTIES, client, ALWAYS_TRUE); Assertions.assertThat(result).isEmpty(); } @@ -71,8 +70,8 @@ void withFilterEmptyInput() { void withFilterOneEndpointsNoMatchInService() { Endpoints endpoints = createEndpoints("a", "namespace-a"); createService("a", "namespace-not-a"); - List result = Fabric8KubernetesDiscoveryClientUtils.withFilter(List.of(endpoints), PROPERTIES, - client, x -> true); + List result = Fabric8DiscoveryClientUtils.withFilter(List.of(endpoints), PROPERTIES, client, + x -> true); Assertions.assertThat(result).isEmpty(); } @@ -88,8 +87,8 @@ void withFilterOneEndpointsNoMatchInService() { void withFilterOneEndpointsMatchInService() { Endpoints endpoints = createEndpoints("a", "namespace-a"); createService("a", "namespace-a"); - List result = Fabric8KubernetesDiscoveryClientUtils.withFilter(List.of(endpoints), PROPERTIES, - client, ALWAYS_TRUE); + List result = Fabric8DiscoveryClientUtils.withFilter(List.of(endpoints), PROPERTIES, client, + ALWAYS_TRUE); Assertions.assertThat(result.size()).isEqualTo(1); } @@ -107,8 +106,8 @@ void withFilterTwoEndpointsOneMatchInService() { Endpoints endpointsA = createEndpoints("a", "namespace-a"); Endpoints endpointsB = createEndpoints("b", "namespace-b"); createService("a", "namespace-a"); - List result = Fabric8KubernetesDiscoveryClientUtils.withFilter(List.of(endpointsA, endpointsB), - PROPERTIES, client, x -> true); + List result = Fabric8DiscoveryClientUtils.withFilter(List.of(endpointsA, endpointsB), PROPERTIES, + client, x -> true); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("a"); Assertions.assertThat(result.get(0).getMetadata().getNamespace()).isEqualTo("namespace-a"); @@ -131,8 +130,8 @@ void withFilterTwoEndpointsOneMatchInServiceAlwaysTruePredicate() { Endpoints endpointsA = createEndpoints("a", "namespace-a"); Endpoints endpointsB = createEndpoints("b", "namespace-b"); createService("a", "namespace-a"); - List result = Fabric8KubernetesDiscoveryClientUtils.withFilter(List.of(endpointsA, endpointsB), - PROPERTIES, client, ALWAYS_TRUE); + List result = Fabric8DiscoveryClientUtils.withFilter(List.of(endpointsA, endpointsB), PROPERTIES, + client, ALWAYS_TRUE); Assertions.assertThat(result.size()).isEqualTo(2); } @@ -155,8 +154,8 @@ void withFilterTwoEndpointsAndThreeServices() { createService("b", "namespace-b"); createService("c", "namespace-c"); - List result = Fabric8KubernetesDiscoveryClientUtils.withFilter(List.of(endpointsA, endpointsB), - PROPERTIES, client, ALWAYS_TRUE); + List result = Fabric8DiscoveryClientUtils.withFilter(List.of(endpointsA, endpointsB), PROPERTIES, + client, ALWAYS_TRUE); Assertions.assertThat(result.size()).isEqualTo(2); result = result.stream().sorted(Comparator.comparing(x -> x.getMetadata().getName())).toList(); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("a"); @@ -181,8 +180,8 @@ void withFilterSingleEndpointsMatchesFilter() { Endpoints endpointsA = createEndpoints("a", "namespace-a"); createService("a", "namespace-a"); - List result = Fabric8KubernetesDiscoveryClientUtils.withFilter(List.of(endpointsA), PROPERTIES, - client, x -> x.getMetadata().getNamespace().equals("namespace-a")); + List result = Fabric8DiscoveryClientUtils.withFilter(List.of(endpointsA), PROPERTIES, client, + x -> x.getMetadata().getNamespace().equals("namespace-a")); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("a"); Assertions.assertThat(result.get(0).getMetadata().getNamespace()).isEqualTo("namespace-a"); @@ -207,9 +206,8 @@ void withFilterTwoEndpointsMatchesFilter() { createService("a-1", "default"); createService("b-1", "default"); - List result = Fabric8KubernetesDiscoveryClientUtils.withFilter( - List.of(endpointsA, endpointsB, endpointsC), PROPERTIES, client, - x -> x.getMetadata().getName().contains("1")); + List result = Fabric8DiscoveryClientUtils.withFilter(List.of(endpointsA, endpointsB, endpointsC), + PROPERTIES, client, x -> x.getMetadata().getName().contains("1")); Assertions.assertThat(result.size()).isEqualTo(2); result = result.stream().sorted(Comparator.comparing(x -> x.getMetadata().getName())).toList(); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("a-1"); @@ -233,8 +231,8 @@ void withFilterSingleEndpointsNoPredicateMatch() { createService("a-1", "default"); createService("b-1", "default"); - List result = Fabric8KubernetesDiscoveryClientUtils.withFilter(List.of(endpointsA), PROPERTIES, - client, x -> !x.getMetadata().getName().contains("1")); + List result = Fabric8DiscoveryClientUtils.withFilter(List.of(endpointsA), PROPERTIES, client, + x -> !x.getMetadata().getName().contains("1")); Assertions.assertThat(result).isEmpty(); } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientUtilsTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientUtilsTests.java similarity index 79% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientUtilsTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientUtilsTests.java index ef90ad40d6..4217b0cc16 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8KubernetesDiscoveryClientUtilsTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryClientUtilsTests.java @@ -20,6 +20,8 @@ import java.util.Map; 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; @@ -36,14 +38,14 @@ import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; import org.springframework.mock.env.MockEnvironment; -import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.endpointSubsetsPortData; -import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8KubernetesDiscoveryClientUtils.services; +import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.endpointSubsetsPortData; +import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.services; /** * @author wind57 */ @EnableKubernetesMockClient(crud = true, https = false) -class Fabric8KubernetesDiscoveryClientUtilsTests { +class Fabric8DiscoveryClientUtilsTests { private static KubernetesClient client; @@ -347,6 +349,85 @@ void endpointSubsetPortsDataSinglePortNoName() { Assertions.assertThat(result.get("")).isEqualTo(80); } + /** + *
+	 *      - ready addresses are empty
+	 *      - not ready addresses are not included
+	 * 
+ */ + @Test + void testEmptyAddresses() { + boolean includeNotReadyAddresses = false; + KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60L, + includeNotReadyAddresses, "", Set.of(), Map.of(), "", null, 0, false, false, null); + EndpointSubset endpointSubset = new EndpointSubsetBuilder().build(); + List addresses = Fabric8DiscoveryClientUtils.addresses(endpointSubset, properties); + Assertions.assertThat(addresses).isEmpty(); + } + + /** + *
+	 *      - ready addresses has two entries
+	 *      - not ready addresses are not included
+	 * 
+ */ + @Test + void testReadyAddressesOnly() { + boolean includeNotReadyAddresses = false; + KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60L, + includeNotReadyAddresses, "", Set.of(), Map.of(), "", null, 0, false, false, null); + EndpointSubset endpointSubset = new EndpointSubsetBuilder() + .withAddresses(new EndpointAddressBuilder().withHostname("one").build(), + new EndpointAddressBuilder().withHostname("two").build()) + .build(); + List addresses = Fabric8DiscoveryClientUtils.addresses(endpointSubset, properties); + Assertions.assertThat(addresses.size()).isEqualTo(2); + } + + /** + *
+	 *      - ready addresses has two entries
+	 *      - not ready addresses has a single entry, but we do not take it
+	 * 
+ */ + @Test + void testReadyAddressesTakenNotReadyAddressesNotTaken() { + boolean includeNotReadyAddresses = false; + KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60L, + includeNotReadyAddresses, "", Set.of(), Map.of(), "", null, 0, false, false, null); + EndpointSubset endpointSubset = new EndpointSubsetBuilder() + .withAddresses(new EndpointAddressBuilder().withHostname("one").build(), + new EndpointAddressBuilder().withHostname("two").build()) + .withNotReadyAddresses(new EndpointAddressBuilder().withHostname("three").build()) + .build(); + List addresses = Fabric8DiscoveryClientUtils.addresses(endpointSubset, properties); + Assertions.assertThat(addresses.size()).isEqualTo(2); + List hostNames = addresses.stream().map(EndpointAddress::getHostname).sorted().toList(); + Assertions.assertThat(hostNames).containsExactlyInAnyOrder("one", "two"); + } + + /** + *
+	 *      - ready addresses has two entries
+	 *      - not ready addresses has a single entry, but we do not take it
+	 * 
+ */ + @Test + void testBothAddressesTaken() { + boolean includeNotReadyAddresses = true; + KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60L, + includeNotReadyAddresses, "", Set.of(), Map.of(), "", null, 0, false, false, null); + EndpointSubset endpointSubset = new EndpointSubsetBuilder() + .withAddresses(new EndpointAddressBuilder().withHostname("one").build(), + new EndpointAddressBuilder().withHostname("two").build()) + .withNotReadyAddresses(new EndpointAddressBuilder().withHostname("three").build()) + .build(); + List addresses = Fabric8DiscoveryClientUtils.addresses(endpointSubset, properties); + Assertions.assertThat(addresses.size()).isEqualTo(3); + List hostNames = addresses.stream().map(EndpointAddress::getHostname).sorted().toList(); + Assertions.assertThat(hostNames).containsExactlyInAnyOrder("one", "three", "two"); + } + private void service(String name, String namespace, Map labels) { Service service = new ServiceBuilder().withNewMetadata() .withName(name) diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryServicesAdapterTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryFilterTests.java similarity index 80% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryServicesAdapterTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryFilterTests.java index 6dba1056f7..addddf5930 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryServicesAdapterTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DiscoveryFilterTests.java @@ -35,13 +35,19 @@ import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; import org.springframework.cloud.kubernetes.fabric8.Fabric8Utils; +import org.springframework.core.env.Environment; import org.springframework.mock.env.MockEnvironment; +import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.services; + /** * @author wind57 */ @EnableKubernetesMockClient(crud = true, https = false) -class Fabric8DiscoveryServicesAdapterTests { +class Fabric8DiscoveryFilterTests { + + private static final KubernetesNamespaceProvider NAMESPACE_PROVIDER = new KubernetesNamespaceProvider( + mockEnvironment()); private static KubernetesClient client; @@ -74,21 +80,16 @@ void afterEach() { void testAllNamespacesWithoutLabelsWithoutFilter() { boolean allNamespaces = true; Map labels = Map.of(); - String spelFilter = null; - - MockEnvironment environment = new MockEnvironment(); KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(false, allNamespaces, Set.of(), - true, 60L, false, spelFilter, Set.of(), labels, null, null, 0, false, false, null); - - Fabric8DiscoveryServicesAdapter adapter = new Fabric8DiscoveryServicesAdapter( - new KubernetesDiscoveryClientAutoConfiguration().servicesFunction(properties, environment), properties, - null); + true, 60L, false, null, Set.of(), labels, null, null, 0, false, false, null); service("namespaceA", "serviceA", Map.of("color", "red")); service("namespaceB", "serviceB", Map.of("color", "blue")); - List result = adapter.apply(client); + List result = services(properties, client, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties), null, "dummy-target"); + Assertions.assertThat(result.size()).isEqualTo(2); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("serviceA"); Assertions.assertThat(result.get(0).getMetadata().getNamespace()).isEqualTo("namespaceA"); @@ -112,21 +113,14 @@ void testAllNamespacesWithoutLabelsWithoutFilter() { void testAllNamespacesWithLabelsWithoutFilter() { boolean allNamespaces = true; Map labels = Map.of("color", "red"); - String spelFilter = null; - - MockEnvironment environment = new MockEnvironment(); KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(false, allNamespaces, Set.of(), - true, 60L, false, spelFilter, Set.of(), labels, null, null, 0, false, false, null); - - Fabric8DiscoveryServicesAdapter adapter = new Fabric8DiscoveryServicesAdapter( - new KubernetesDiscoveryClientAutoConfiguration().servicesFunction(properties, environment), properties, - null); - + true, 60L, false, null, Set.of(), labels, null, null, 0, false, false, null); service("namespaceA", "serviceA", Map.of("color", "red")); service("namespaceB", "serviceB", Map.of("color", "blue")); - List result = adapter.apply(client); + List result = services(properties, client, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties), null, "dummy-target"); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("serviceA"); Assertions.assertThat(result.get(0).getMetadata().getNamespace()).isEqualTo("namespaceA"); @@ -153,19 +147,15 @@ void testAllNamespacesWithoutLabelsWithNamespaceFilter() { #root.metadata.namespace matches "^.+A$" """; - MockEnvironment environment = new MockEnvironment(); - KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(false, allNamespaces, Set.of(), true, 60L, false, spelFilter, Set.of(), labels, null, null, 0, false, false, null); - Fabric8DiscoveryServicesAdapter adapter = new Fabric8DiscoveryServicesAdapter( - new KubernetesDiscoveryClientAutoConfiguration().servicesFunction(properties, environment), properties, - null); - service("namespaceA", "serviceA", Map.of("color", "red")); service("namespaceB", "serviceB", Map.of("color", "blue")); - List result = adapter.apply(client); + List result = services(properties, client, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties), null, "dummy-target"); + Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("serviceA"); Assertions.assertThat(result.get(0).getMetadata().getNamespace()).isEqualTo("namespaceA"); @@ -193,20 +183,15 @@ void testAllNamespacesWithoutLabelsWithNamespacesFilter() { #root.metadata.namespace matches "^namespace[A|B]$" """; - MockEnvironment environment = new MockEnvironment(); - KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(false, allNamespaces, Set.of(), true, 60L, false, spelFilter, Set.of(), labels, null, null, 0, false, false, null); - Fabric8DiscoveryServicesAdapter adapter = new Fabric8DiscoveryServicesAdapter( - new KubernetesDiscoveryClientAutoConfiguration().servicesFunction(properties, environment), properties, - null); - service("namespaceA", "serviceA", Map.of("color", "red")); service("namespaceB", "serviceB", Map.of("color", "blue")); service("namespaceC", "serviceC", Map.of("color", "purple")); - List result = adapter.apply(client); + List result = services(properties, client, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties), null, "dummy-target"); Assertions.assertThat(result.size()).isEqualTo(2); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("serviceA"); Assertions.assertThat(result.get(0).getMetadata().getNamespace()).isEqualTo("namespaceA"); @@ -230,17 +215,9 @@ void testAllNamespacesWithoutLabelsWithNamespacesFilter() { void testSpecificNamespaceWithoutLabelsWithoutFilter() { boolean allNamespaces = false; Map labels = Map.of(); - String spelFilter = null; - - MockEnvironment environment = new MockEnvironment(); KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(false, allNamespaces, Set.of(), - true, 60L, false, spelFilter, Set.of(), labels, null, null, 0, false, false, null); - - Fabric8DiscoveryServicesAdapter adapter = new Fabric8DiscoveryServicesAdapter( - new KubernetesDiscoveryClientAutoConfiguration().servicesFunction(properties, environment), properties, - null); - + true, 60L, false, null, Set.of(), labels, null, null, 0, false, false, null); utils.when(() -> Fabric8Utils.getApplicationNamespace(Mockito.any(KubernetesClient.class), Mockito.nullable(String.class), Mockito.anyString(), Mockito.any(KubernetesNamespaceProvider.class))) .thenReturn("namespaceA"); @@ -248,7 +225,8 @@ void testSpecificNamespaceWithoutLabelsWithoutFilter() { service("namespaceA", "serviceA", Map.of("color", "red")); service("namespaceB", "serviceB", Map.of("color", "blue")); - List result = adapter.apply(client); + List result = services(properties, client, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties), null, "dummy-target"); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("serviceA"); Assertions.assertThat(result.get(0).getMetadata().getNamespace()).isEqualTo("namespaceA"); @@ -272,16 +250,9 @@ void testSpecificNamespaceWithoutLabelsWithoutFilter() { void testSpecificNamespaceWithLabelsWithoutFilter() { boolean allNamespaces = false; Map labels = Map.of("color", "purple"); - String spelFilter = null; - - MockEnvironment environment = new MockEnvironment(); KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(false, allNamespaces, Set.of(), - true, 60L, false, spelFilter, Set.of(), labels, null, null, 0, false, false, null); - - Fabric8DiscoveryServicesAdapter adapter = new Fabric8DiscoveryServicesAdapter( - new KubernetesDiscoveryClientAutoConfiguration().servicesFunction(properties, environment), properties, - null); + true, 60L, false, null, Set.of(), labels, null, null, 0, false, false, null); utils.when(() -> Fabric8Utils.getApplicationNamespace(Mockito.any(KubernetesClient.class), Mockito.nullable(String.class), Mockito.anyString(), Mockito.any(KubernetesNamespaceProvider.class))) @@ -291,7 +262,8 @@ void testSpecificNamespaceWithLabelsWithoutFilter() { service("namespaceA", "serviceB", Map.of("color", "purple")); service("namespaceC", "serviceC", Map.of("color", "purple")); - List result = adapter.apply(client); + List result = services(properties, client, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties), null, "dummy-target"); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("serviceB"); Assertions.assertThat(result.get(0).getMetadata().getNamespace()).isEqualTo("namespaceA"); @@ -321,15 +293,9 @@ void testSpecificNamespaceWithoutLabelsWithFilter() { #root.metadata.labels.containsKey("number") """.stripLeading(); - MockEnvironment environment = new MockEnvironment(); - KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(false, allNamespaces, Set.of(), true, 60L, false, spelFilter, Set.of(), labels, null, null, 0, false, false, null); - Fabric8DiscoveryServicesAdapter adapter = new Fabric8DiscoveryServicesAdapter( - new KubernetesDiscoveryClientAutoConfiguration().servicesFunction(properties, environment), properties, - null); - utils.when(() -> Fabric8Utils.getApplicationNamespace(Mockito.any(KubernetesClient.class), Mockito.nullable(String.class), Mockito.anyString(), Mockito.any(KubernetesNamespaceProvider.class))) .thenReturn("namespaceA"); @@ -338,7 +304,8 @@ void testSpecificNamespaceWithoutLabelsWithFilter() { service("namespaceA", "serviceB", Map.of("color", "purple", "cycle", "create")); service("namespaceC", "serviceC", Map.of("color", "purple", "number", "1")); - List result = adapter.apply(client); + List result = services(properties, client, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties), null, "dummy-target"); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("serviceA"); Assertions.assertThat(result.get(0).getMetadata().getNamespace()).isEqualTo("namespaceA"); @@ -365,23 +332,16 @@ void testSomeNamespacesWithoutLabelsWithoutFilter() { boolean allNamespaces = false; Set someNamespaces = Set.of("namespaceA", "namespaceB"); Map labels = Map.of(); - String spelFilter = null; - - MockEnvironment environment = new MockEnvironment(); KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(false, allNamespaces, - someNamespaces, true, 60L, false, spelFilter, Set.of(), labels, null, null, 0, false, false, null); - - Fabric8DiscoveryServicesAdapter adapter = new Fabric8DiscoveryServicesAdapter( - new KubernetesDiscoveryClientAutoConfiguration().servicesFunction(properties, environment), properties, - null); + someNamespaces, true, 60L, false, null, Set.of(), labels, null, null, 0, false, false, null); service("namespaceA", "serviceA", Map.of()); service("namespaceB", "serviceB", Map.of()); service("namespaceC", "serviceC", Map.of()); - List result = adapter.apply(client); - Assertions.assertThat(result.size()).isEqualTo(2); + List result = services(properties, client, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties), null, "dummy-target"); result = result.stream().sorted(Comparator.comparing(x -> x.getMetadata().getName())).toList(); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("serviceA"); Assertions.assertThat(result.get(0).getMetadata().getNamespace()).isEqualTo("namespaceA"); @@ -410,20 +370,15 @@ void testSomeNamespacesWithLabelsWithoutFilter() { Map labels = Map.of("color", "purple"); String spelFilter = null; - MockEnvironment environment = new MockEnvironment(); - KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(false, allNamespaces, someNamespaces, true, 60L, false, spelFilter, Set.of(), labels, null, null, 0, false, false, null); - Fabric8DiscoveryServicesAdapter adapter = new Fabric8DiscoveryServicesAdapter( - new KubernetesDiscoveryClientAutoConfiguration().servicesFunction(properties, environment), properties, - null); - service("namespaceA", "serviceA", Map.of("color", "purple")); service("namespaceB", "serviceB", Map.of("color", "red")); service("namespaceC", "serviceC", Map.of("color", "purple")); - List result = adapter.apply(client); + List result = services(properties, client, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties), null, "dummy-target"); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("serviceA"); Assertions.assertThat(result.get(0).getMetadata().getNamespace()).isEqualTo("namespaceA"); @@ -452,20 +407,15 @@ void testSomeNamespacesWithLabelsWithFilter() { #root.metadata.labels.containsKey("number") """.stripLeading(); - MockEnvironment environment = new MockEnvironment(); - KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(false, allNamespaces, someNamespaces, true, 60L, false, spelFilter, Set.of(), labels, null, null, 0, false, false, null); - Fabric8DiscoveryServicesAdapter adapter = new Fabric8DiscoveryServicesAdapter( - new KubernetesDiscoveryClientAutoConfiguration().servicesFunction(properties, environment), properties, - null); - service("namespaceA", "serviceA", Map.of("color", "purple", "number", "1")); service("namespaceB", "serviceB", Map.of("color", "purple", "cycle", "create")); service("namespaceC", "serviceC", Map.of("color", "purple", "number", "1")); - List result = adapter.apply(client); + List result = services(properties, client, NAMESPACE_PROVIDER, + new Fabric8DiscoveryClientSpelAutoConfiguration().predicate(properties), null, "dummy-target"); Assertions.assertThat(result.size()).isEqualTo(1); Assertions.assertThat(result.get(0).getMetadata().getName()).isEqualTo("serviceA"); Assertions.assertThat(result.get(0).getMetadata().getNamespace()).isEqualTo("namespaceA"); @@ -478,4 +428,10 @@ private void service(String namespace, String name, Map labels) .create(); } + private static Environment mockEnvironment() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("spring.cloud.kubernetes.client.namespace", "test"); + return environment; + } + } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/DummyConfigDataLocationResolver.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DummyConfigDataLocationResolver.java similarity index 94% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/DummyConfigDataLocationResolver.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DummyConfigDataLocationResolver.java index 6d6b6a5384..33b7026d1c 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/DummyConfigDataLocationResolver.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8DummyConfigDataLocationResolver.java @@ -28,12 +28,11 @@ * @author wind57 */ @ConditionalOnProperty(value = "dummy.config.loader.enabled", havingValue = "true", matchIfMissing = false) -class DummyConfigDataLocationResolver extends KubernetesConfigDataLocationResolver { +class Fabric8DummyConfigDataLocationResolver extends KubernetesConfigDataLocationResolver { @Override protected void registerBeans(ConfigDataLocationResolverContext resolverContext, ConfigDataLocation location, Profiles profiles, ConfigDataPropertiesHolder properties, KubernetesNamespaceProvider namespaceProvider) { - } } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8EndpointsAndEndpointSlicesTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8EndpointsAndEndpointSlicesTests.java index 24bb20e2f5..4dd6cb68e4 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8EndpointsAndEndpointSlicesTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8EndpointsAndEndpointSlicesTests.java @@ -62,10 +62,11 @@ abstract class Fabric8EndpointsAndEndpointSlicesTests { static final KubernetesNamespaceProvider NAMESPACE_PROVIDER = Mockito.mock(KubernetesNamespaceProvider.class); - static final ArgumentCaptor HEARTBEAT_EVENT_ARGUMENT_CAPTOR = ArgumentCaptor + private static final ArgumentCaptor HEARTBEAT_EVENT_ARGUMENT_CAPTOR = ArgumentCaptor .forClass(HeartbeatEvent.class); - static final ApplicationEventPublisher APPLICATION_EVENT_PUBLISHER = Mockito.mock(ApplicationEventPublisher.class); + private static final ApplicationEventPublisher APPLICATION_EVENT_PUBLISHER = Mockito + .mock(ApplicationEventPublisher.class); @BeforeAll static void setUp() { @@ -247,13 +248,14 @@ void afterEach() { */ abstract void testWithoutSubsetsOrEndpoints(); - KubernetesCatalogWatch createWatcherInAllNamespacesWithLabels(Map labels, Set namespaces, + Fabric8CatalogWatch createWatcherInAllNamespacesWithLabels(Map labels, Set namespaces, boolean endpointSlices) { boolean allNamespaces = true; KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, namespaces, true, 60, false, "", Set.of(), labels, "", null, 0, endpointSlices, false, null); - KubernetesCatalogWatch watch = new KubernetesCatalogWatch(mockClient, properties, NAMESPACE_PROVIDER); + Fabric8CatalogWatch watch = new Fabric8CatalogWatch(mockClient, properties, NAMESPACE_PROVIDER, + APPLICATION_EVENT_PUBLISHER); if (endpointSlices) { watch = Mockito.spy(watch); @@ -261,12 +263,11 @@ KubernetesCatalogWatch createWatcherInAllNamespacesWithLabels(Map labels, + Fabric8CatalogWatch createWatcherInSpecificNamespaceWithLabels(String namespace, Map labels, boolean endpointSlices) { when(NAMESPACE_PROVIDER.getNamespace()).thenReturn(namespace); @@ -274,7 +275,8 @@ KubernetesCatalogWatch createWatcherInSpecificNamespaceWithLabels(String namespa boolean allNamespaces = false; KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, allNamespaces, Set.of(namespace), true, 60, false, "", Set.of(), labels, "", null, 0, endpointSlices, false, null); - KubernetesCatalogWatch watch = new KubernetesCatalogWatch(mockClient, properties, NAMESPACE_PROVIDER); + Fabric8CatalogWatch watch = new Fabric8CatalogWatch(mockClient, properties, NAMESPACE_PROVIDER, + APPLICATION_EVENT_PUBLISHER); if (endpointSlices) { watch = Mockito.spy(watch); @@ -282,25 +284,24 @@ KubernetesCatalogWatch createWatcherInSpecificNamespaceWithLabels(String namespa } watch.postConstruct(); - watch.setApplicationEventPublisher(APPLICATION_EVENT_PUBLISHER); return watch; } - KubernetesCatalogWatch createWatcherInSpecificNamespacesWithLabels(Set namespaces, - Map labels, boolean endpointSlices) { + Fabric8CatalogWatch createWatcherInSpecificNamespacesWithLabels(Set namespaces, Map labels, + boolean endpointSlices) { // all-namespaces = false KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, false, namespaces, true, 60, false, "", Set.of(), labels, "", null, 0, false, false, null); - KubernetesCatalogWatch watch = new KubernetesCatalogWatch(mockClient, properties, NAMESPACE_PROVIDER); + Fabric8CatalogWatch watch = new Fabric8CatalogWatch(mockClient, properties, NAMESPACE_PROVIDER, + APPLICATION_EVENT_PUBLISHER); if (endpointSlices) { watch = Mockito.spy(watch); Mockito.doReturn(new Fabric8EndpointSliceV1CatalogWatch()).when(watch).stateGenerator(); } - watch.setApplicationEventPublisher(APPLICATION_EVENT_PUBLISHER); watch.postConstruct(); return watch; @@ -379,7 +380,7 @@ static EndpointSlice endpointSliceWithoutEndpoints(String namespace, Map state) { + static void invokeAndAssert(Fabric8CatalogWatch watch, List state) { watch.catalogServicesWatch(); verify(APPLICATION_EVENT_PUBLISHER, Mockito.atLeastOnce()) diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClientAutoConfigurationApplicationContextTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientAutoConfigurationApplicationContextTests.java similarity index 71% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClientAutoConfigurationApplicationContextTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientAutoConfigurationApplicationContextTests.java index 5328dea28b..f991e4cdd5 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClientAutoConfigurationApplicationContextTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientAutoConfigurationApplicationContextTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.discovery.reactive; +package org.springframework.cloud.kubernetes.fabric8.discovery; import org.junit.jupiter.api.Test; @@ -27,17 +27,15 @@ import org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryPropertiesAutoConfiguration; import org.springframework.cloud.kubernetes.fabric8.Fabric8AutoConfiguration; -import org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesClientServicesFunction; import static org.assertj.core.api.Assertions.assertThat; /** - * Test various conditionals for - * {@link KubernetesReactiveDiscoveryClientAutoConfiguration} + * Test various conditionals for {@link Fabric8ReactiveDiscoveryClientAutoConfiguration} * * @author wind57 */ -class KubernetesReactiveDiscoveryClientAutoConfigurationApplicationContextTests { +class Fabric8ReactiveDiscoveryClientAutoConfigurationApplicationContextTests { private ApplicationContextRunner applicationContextRunner; @@ -45,9 +43,8 @@ class KubernetesReactiveDiscoveryClientAutoConfigurationApplicationContextTests void discoveryEnabledDefault() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8ReactiveDiscoveryClient.class); assertThat(context).hasSingleBean(ReactiveDiscoveryClientHealthIndicator.class); - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); }); } @@ -56,9 +53,8 @@ void discoveryEnabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.enabled=true"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8ReactiveDiscoveryClient.class); assertThat(context).hasSingleBean(ReactiveDiscoveryClientHealthIndicator.class); - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); }); } @@ -67,9 +63,8 @@ void discoveryDisabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).doesNotHaveBean(Fabric8ReactiveDiscoveryClient.class); assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class); - assertThat(context).doesNotHaveBean(KubernetesClientServicesFunction.class); }); } @@ -78,9 +73,8 @@ void kubernetesDiscoveryEnabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.kubernetes.discovery.enabled=true"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8ReactiveDiscoveryClient.class); assertThat(context).hasSingleBean(ReactiveDiscoveryClientHealthIndicator.class); - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); }); } @@ -89,9 +83,8 @@ void kubernetesDiscoveryDisabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.kubernetes.discovery.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).doesNotHaveBean(Fabric8ReactiveDiscoveryClient.class); assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class); - assertThat(context).doesNotHaveBean(KubernetesClientServicesFunction.class); }); } @@ -100,9 +93,8 @@ void kubernetesReactiveDiscoveryEnabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.reactive.enabled=true"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8ReactiveDiscoveryClient.class); assertThat(context).hasSingleBean(ReactiveDiscoveryClientHealthIndicator.class); - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); }); } @@ -111,9 +103,8 @@ void kubernetesReactiveDiscoveryDisabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.reactive.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).doesNotHaveBean(Fabric8ReactiveDiscoveryClient.class); assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class); - assertThat(context).doesNotHaveBean(KubernetesClientServicesFunction.class); }); } @@ -125,9 +116,8 @@ void blockingDisabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.blocking.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8ReactiveDiscoveryClient.class); assertThat(context).hasSingleBean(ReactiveDiscoveryClientHealthIndicator.class); - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); }); } @@ -136,9 +126,8 @@ void healthDisabled() { setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.client.health-indicator.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8ReactiveDiscoveryClient.class); assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class); - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); }); } @@ -148,9 +137,8 @@ void healthEnabledClassNotPresent() { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.config.enabled=false", "spring.cloud.discovery.client.health-indicator.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).hasSingleBean(Fabric8ReactiveDiscoveryClient.class); assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class); - assertThat(context).hasSingleBean(KubernetesClientServicesFunction.class); }); } @@ -158,8 +146,9 @@ private void setup(String... properties) { applicationContextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class, ReactiveCommonsClientAutoConfiguration.class, KubernetesCommonsAutoConfiguration.class, - Fabric8AutoConfiguration.class, KubernetesReactiveDiscoveryClientAutoConfiguration.class, - KubernetesDiscoveryPropertiesAutoConfiguration.class)) + Fabric8AutoConfiguration.class, Fabric8ReactiveDiscoveryClientAutoConfiguration.class, + KubernetesDiscoveryPropertiesAutoConfiguration.class, + Fabric8DiscoveryClientSpelAutoConfiguration.class)) .withPropertyValues(properties); } @@ -167,8 +156,9 @@ private void setupWithFilteredClassLoader(String name, String... properties) { applicationContextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class, ReactiveCommonsClientAutoConfiguration.class, KubernetesCommonsAutoConfiguration.class, - Fabric8AutoConfiguration.class, KubernetesReactiveDiscoveryClientAutoConfiguration.class, - KubernetesDiscoveryPropertiesAutoConfiguration.class)) + Fabric8AutoConfiguration.class, Fabric8ReactiveDiscoveryClientAutoConfiguration.class, + KubernetesDiscoveryPropertiesAutoConfiguration.class, + Fabric8DiscoveryClientSpelAutoConfiguration.class)) .withClassLoader(new FilteredClassLoader(name)) .withPropertyValues(properties); } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClientAutoConfigurationTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientAutoConfigurationTests.java similarity index 90% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClientAutoConfigurationTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientAutoConfigurationTests.java index 5b9931bed5..f44da7b311 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClientAutoConfigurationTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientAutoConfigurationTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.discovery.reactive; +package org.springframework.cloud.kubernetes.fabric8.discovery; import org.junit.jupiter.api.Test; @@ -28,21 +28,21 @@ import org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryPropertiesAutoConfiguration; import org.springframework.cloud.kubernetes.fabric8.Fabric8AutoConfiguration; -import org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClientAutoConfiguration; import static org.assertj.core.api.Assertions.assertThat; /** * @author Tim Ysewyn */ -class KubernetesReactiveDiscoveryClientAutoConfigurationTests { +class Fabric8ReactiveDiscoveryClientAutoConfigurationTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class, ReactiveCommonsClientAutoConfiguration.class, KubernetesCommonsAutoConfiguration.class, - Fabric8AutoConfiguration.class, KubernetesDiscoveryClientAutoConfiguration.class, - KubernetesReactiveDiscoveryClientAutoConfiguration.class, - KubernetesDiscoveryPropertiesAutoConfiguration.class)); + Fabric8AutoConfiguration.class, Fabric8DiscoveryClientAutoConfiguration.class, + Fabric8ReactiveDiscoveryClientAutoConfiguration.class, + KubernetesDiscoveryPropertiesAutoConfiguration.class, + Fabric8DiscoveryClientSpelAutoConfiguration.class)); @Test void shouldWorkWithDefaults() { diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClientTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientTests.java similarity index 77% rename from spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClientTests.java rename to spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientTests.java index 5f6ef68584..d99c0f529b 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/reactive/KubernetesReactiveDiscoveryClientTests.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8ReactiveDiscoveryClientTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.discovery.reactive; +package org.springframework.cloud.kubernetes.fabric8.discovery; import java.util.ArrayList; import java.util.List; @@ -40,7 +40,11 @@ import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; +import org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver; +import org.springframework.core.env.Environment; +import org.springframework.mock.env.MockEnvironment; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; @@ -50,7 +54,13 @@ * @author Tim Ysewyn */ @EnableKubernetesMockClient(crud = true, https = false) -class KubernetesReactiveDiscoveryClientTests { +class Fabric8ReactiveDiscoveryClientTests { + + private static final ServicePortSecureResolver SERVICE_PORT_SECURE_RESOLVER = new ServicePortSecureResolver( + KubernetesDiscoveryProperties.DEFAULT); + + private static final KubernetesNamespaceProvider NAMESPACE_PROVIDER = new KubernetesNamespaceProvider( + mockEnvironment()); private static KubernetesMockServer kubernetesServer; @@ -74,8 +84,9 @@ void afterEach() { @Test void verifyDefaults() { - ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(kubernetesClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services); + Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(kubernetesClient, + KubernetesDiscoveryProperties.DEFAULT, SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); + ReactiveDiscoveryClient client = new Fabric8ReactiveDiscoveryClient(fabric8DiscoveryClient); assertThat(client.description()).isEqualTo("Fabric8 Kubernetes Reactive Discovery Client"); assertThat(client.getOrder()).isEqualTo(ReactiveDiscoveryClient.DEFAULT_ORDER); } @@ -105,8 +116,11 @@ void shouldReturnFluxOfServices() { .endItem() .build()) .once(); - ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(kubernetesClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services); + + Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(kubernetesClient, + KubernetesDiscoveryProperties.DEFAULT, SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); + ReactiveDiscoveryClient client = new Fabric8ReactiveDiscoveryClient(fabric8DiscoveryClient); + Flux services = client.getServices(); StepVerifier.create(services).expectNext("s1", "s2", "s3").expectComplete().verify(); } @@ -119,8 +133,10 @@ void shouldReturnEmptyFluxOfServicesWhenNoInstancesFound() { .andReturn(200, new ServiceListBuilder().build()) .once(); - ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(kubernetesClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services); + Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(kubernetesClient, + KubernetesDiscoveryProperties.DEFAULT, SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); + ReactiveDiscoveryClient client = new Fabric8ReactiveDiscoveryClient(fabric8DiscoveryClient); + Flux services = client.getServices(); StepVerifier.create(services).expectNextCount(0).expectComplete().verify(); } @@ -133,8 +149,10 @@ void shouldReturnEmptyFluxForNonExistingService() { .andReturn(200, new EndpointsBuilder().build()) .once(); - ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(kubernetesClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services); + Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(kubernetesClient, + KubernetesDiscoveryProperties.DEFAULT, SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); + ReactiveDiscoveryClient client = new Fabric8ReactiveDiscoveryClient(fabric8DiscoveryClient); + Flux instances = client.getInstances("nonexistent-service"); StepVerifier.create(instances).expectNextCount(0).expectComplete().verify(); } @@ -160,8 +178,10 @@ void shouldReturnEmptyFluxWhenServiceHasNoSubsets() { .andReturn(200, new EndpointsBuilder().build()) .once(); - ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(kubernetesClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services); + Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(kubernetesClient, + KubernetesDiscoveryProperties.DEFAULT, SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); + ReactiveDiscoveryClient client = new Fabric8ReactiveDiscoveryClient(fabric8DiscoveryClient); + Flux instances = client.getInstances("existing-service"); StepVerifier.create(instances).expectNextCount(0).expectComplete().verify(); } @@ -214,8 +234,10 @@ void shouldReturnFlux() { kubernetesServer.expect().get().withPath("/api/v1/namespaces/test/services").andReturn(200, services).once(); - ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(kubernetesClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services); + Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(kubernetesClient, + KubernetesDiscoveryProperties.DEFAULT, SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); + ReactiveDiscoveryClient client = new Fabric8ReactiveDiscoveryClient(fabric8DiscoveryClient); + Flux instances = client.getInstances("existing-service"); StepVerifier.create(instances).expectNextCount(1).expectComplete().verify(); } @@ -275,8 +297,10 @@ void shouldReturnFluxWithPrefixedMetadata() { .build()) .once(); - ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(kubernetesClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services); + Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(kubernetesClient, + KubernetesDiscoveryProperties.DEFAULT, SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); + ReactiveDiscoveryClient client = new Fabric8ReactiveDiscoveryClient(fabric8DiscoveryClient); + Flux instances = client.getInstances("existing-service"); StepVerifier.create(instances).expectNextCount(1).expectComplete().verify(); } @@ -337,8 +361,10 @@ void shouldReturnFluxWhenServiceHasMultiplePortsAndPrimaryPortNameIsSet() { .build()) .once(); - ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(kubernetesClient, - KubernetesDiscoveryProperties.DEFAULT, KubernetesClient::services); + Fabric8DiscoveryClient fabric8DiscoveryClient = new Fabric8DiscoveryClient(kubernetesClient, + KubernetesDiscoveryProperties.DEFAULT, SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); + ReactiveDiscoveryClient client = new Fabric8ReactiveDiscoveryClient(fabric8DiscoveryClient); + Flux instances = client.getInstances("existing-service"); StepVerifier.create(instances).expectNextCount(1).expectComplete().verify(); } @@ -398,10 +424,18 @@ void shouldReturnFluxOfServicesAcrossAllNamespaces() { KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60, false, null, Set.of(), Map.of(), "https_tcp", Metadata.DEFAULT, 0, true, false, null); - ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(kubernetesClient, properties, - KubernetesClient::services); + Fabric8DiscoveryClient fabric8KubernetesDiscoveryClient = new Fabric8DiscoveryClient(kubernetesClient, + properties, SERVICE_PORT_SECURE_RESOLVER, NAMESPACE_PROVIDER, x -> true); + + ReactiveDiscoveryClient client = new Fabric8ReactiveDiscoveryClient(fabric8KubernetesDiscoveryClient); Flux instances = client.getInstances("existing-service"); StepVerifier.create(instances).expectNextCount(1).expectComplete().verify(); } + private static Environment mockEnvironment() { + MockEnvironment mockEnvironment = new MockEnvironment(); + mockEnvironment.setProperty("spring.cloud.kubernetes.client.namespace", "test"); + return mockEnvironment; + } + } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientUtilsTests.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientUtilsTests.java deleted file mode 100644 index 9f0bf37483..0000000000 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientUtilsTests.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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.List; -import java.util.Map; -import java.util.Set; - -import io.fabric8.kubernetes.api.model.EndpointAddress; -import io.fabric8.kubernetes.api.model.EndpointAddressBuilder; -import io.fabric8.kubernetes.api.model.EndpointSubset; -import io.fabric8.kubernetes.api.model.EndpointSubsetBuilder; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Test; - -import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; - -/** - * @author wind57 - */ -class KubernetesDiscoveryClientUtilsTests { - - /** - *
-	 *      - ready addresses are empty
-	 *      - not ready addresses are not included
-	 * 
- */ - @Test - void testEmptyAddresses() { - boolean includeNotReadyAddresses = false; - KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60L, - includeNotReadyAddresses, "", Set.of(), Map.of(), "", null, 0, false, false, null); - EndpointSubset endpointSubset = new EndpointSubsetBuilder().build(); - List addresses = Fabric8KubernetesDiscoveryClientUtils.addresses(endpointSubset, properties); - Assertions.assertThat(addresses).isEmpty(); - } - - /** - *
-	 *      - ready addresses has two entries
-	 *      - not ready addresses are not included
-	 * 
- */ - @Test - void testReadyAddressesOnly() { - boolean includeNotReadyAddresses = false; - KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60L, - includeNotReadyAddresses, "", Set.of(), Map.of(), "", null, 0, false, false, null); - EndpointSubset endpointSubset = new EndpointSubsetBuilder() - .withAddresses(new EndpointAddressBuilder().withHostname("one").build(), - new EndpointAddressBuilder().withHostname("two").build()) - .build(); - List addresses = Fabric8KubernetesDiscoveryClientUtils.addresses(endpointSubset, properties); - Assertions.assertThat(addresses.size()).isEqualTo(2); - } - - /** - *
-	 *      - ready addresses has two entries
-	 *      - not ready addresses has a single entry, but we do not take it
-	 * 
- */ - @Test - void testReadyAddressesTakenNotReadyAddressesNotTaken() { - boolean includeNotReadyAddresses = false; - KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60L, - includeNotReadyAddresses, "", Set.of(), Map.of(), "", null, 0, false, false, null); - EndpointSubset endpointSubset = new EndpointSubsetBuilder() - .withAddresses(new EndpointAddressBuilder().withHostname("one").build(), - new EndpointAddressBuilder().withHostname("two").build()) - .withNotReadyAddresses(new EndpointAddressBuilder().withHostname("three").build()) - .build(); - List addresses = Fabric8KubernetesDiscoveryClientUtils.addresses(endpointSubset, properties); - Assertions.assertThat(addresses.size()).isEqualTo(2); - List hostNames = addresses.stream().map(EndpointAddress::getHostname).sorted().toList(); - Assertions.assertThat(hostNames).containsExactlyInAnyOrder("one", "two"); - } - - /** - *
-	 *      - ready addresses has two entries
-	 *      - not ready addresses has a single entry, but we do not take it
-	 * 
- */ - @Test - void testBothAddressesTaken() { - boolean includeNotReadyAddresses = true; - KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60L, - includeNotReadyAddresses, "", Set.of(), Map.of(), "", null, 0, false, false, null); - EndpointSubset endpointSubset = new EndpointSubsetBuilder() - .withAddresses(new EndpointAddressBuilder().withHostname("one").build(), - new EndpointAddressBuilder().withHostname("two").build()) - .withNotReadyAddresses(new EndpointAddressBuilder().withHostname("three").build()) - .build(); - List addresses = Fabric8KubernetesDiscoveryClientUtils.addresses(endpointSubset, properties); - Assertions.assertThat(addresses.size()).isEqualTo(3); - List hostNames = addresses.stream().map(EndpointAddress::getHostname).sorted().toList(); - Assertions.assertThat(hostNames).containsExactlyInAnyOrder("one", "three", "two"); - } - -} diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/resources/META-INF/spring.factories b/spring-cloud-kubernetes-fabric8-discovery/src/test/resources/META-INF/spring.factories index df1469233d..f6fd8ec7e4 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/resources/META-INF/spring.factories +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/resources/META-INF/spring.factories @@ -1,2 +1,2 @@ org.springframework.boot.context.config.ConfigDataLocationResolver=\ -org.springframework.cloud.kubernetes.fabric8.discovery.DummyConfigDataLocationResolver +org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DummyConfigDataLocationResolver diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/Application.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Application.java similarity index 93% rename from spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/Application.java rename to spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Application.java index bf9f641c76..11e4b68ff9 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/Application.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/Application.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.catalog.watch; +package org.springframework.cloud.kubernetes.fabric8.discovery; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/EndpointNameAndNamespaceService.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/EndpointNameAndNamespaceService.java similarity index 94% rename from spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/EndpointNameAndNamespaceService.java rename to spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/EndpointNameAndNamespaceService.java index 9faf5ff4ab..9d1189c278 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/EndpointNameAndNamespaceService.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/EndpointNameAndNamespaceService.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.catalog.watch; +package org.springframework.cloud.kubernetes.fabric8.discovery; import java.util.List; diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/HeartBeatListener.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/HeartBeatListener.java similarity index 88% rename from spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/HeartBeatListener.java rename to spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/HeartBeatListener.java index 8cf753fb90..28168092e9 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/HeartBeatListener.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/HeartBeatListener.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.catalog.watch; +package org.springframework.cloud.kubernetes.fabric8.discovery; import java.util.List; @@ -25,8 +25,7 @@ import org.springframework.stereotype.Component; /** - * Listener that will catch events from - * {@link org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesCatalogWatch} + * Listener that will catch events from Fabric8CatalogWatch * * @author wind57 */ diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/HeartbeatController.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/HeartbeatController.java similarity index 94% rename from spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/HeartbeatController.java rename to spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/HeartbeatController.java index d9746a2131..ae16ab6dbe 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/HeartbeatController.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/HeartbeatController.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.catalog.watch; +package org.springframework.cloud.kubernetes.fabric8.discovery; import java.util.List; diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/Fabric8CatalogWatchBase.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchBase.java similarity index 97% rename from spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/Fabric8CatalogWatchBase.java rename to spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchBase.java index 2a9a80c6c8..1413eed602 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/Fabric8CatalogWatchBase.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchBase.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.catalog.watch; +package org.springframework.cloud.kubernetes.fabric8.discovery; import java.util.Map; import java.util.Set; diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/Fabric8CatalogWatchEndpointSlicesIT.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointSlicesIT.java similarity index 82% rename from spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/Fabric8CatalogWatchEndpointSlicesIT.java rename to spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointSlicesIT.java index 8ba68c3af1..723ff13ed4 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/Fabric8CatalogWatchEndpointSlicesIT.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointSlicesIT.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.catalog.watch; +package org.springframework.cloud.kubernetes.fabric8.discovery; import java.util.Set; @@ -28,20 +28,19 @@ import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.web.server.test.LocalServerPort; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; -import org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesCatalogWatchAutoConfiguration; import org.springframework.cloud.kubernetes.integration.tests.commons.Images; import org.springframework.cloud.kubernetes.integration.tests.commons.Phase; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; -import static org.springframework.cloud.kubernetes.fabric8.catalog.watch.Fabric8CatalogWatchEndpointSlicesIT.TestConfig; -import static org.springframework.cloud.kubernetes.fabric8.catalog.watch.TestAssertions.assertLogStatement; -import static org.springframework.cloud.kubernetes.fabric8.catalog.watch.TestAssertions.invokeAndAssert; +import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8CatalogWatchEndpointSlicesIT.TestConfig; +import static org.springframework.cloud.kubernetes.fabric8.discovery.TestAssertions.assertLogStatement; +import static org.springframework.cloud.kubernetes.fabric8.discovery.TestAssertions.invokeAndAssert; /** * @author wind57 */ -@SpringBootTest(classes = { KubernetesCatalogWatchAutoConfiguration.class, TestConfig.class, Application.class }, +@SpringBootTest(classes = { Fabric8CatalogWatchAutoConfiguration.class, TestConfig.class, Application.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class Fabric8CatalogWatchEndpointSlicesIT extends Fabric8CatalogWatchBase { diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/Fabric8CatalogWatchEndpointsIT.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointsIT.java similarity index 82% rename from spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/Fabric8CatalogWatchEndpointsIT.java rename to spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointsIT.java index a09cbb77cb..4ac8d059eb 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/Fabric8CatalogWatchEndpointsIT.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/Fabric8CatalogWatchEndpointsIT.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.catalog.watch; +package org.springframework.cloud.kubernetes.fabric8.discovery; import java.util.Set; @@ -28,20 +28,19 @@ import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.web.server.test.LocalServerPort; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; -import org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesCatalogWatchAutoConfiguration; import org.springframework.cloud.kubernetes.integration.tests.commons.Images; import org.springframework.cloud.kubernetes.integration.tests.commons.Phase; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; -import static org.springframework.cloud.kubernetes.fabric8.catalog.watch.Fabric8CatalogWatchEndpointsIT.TestConfig; -import static org.springframework.cloud.kubernetes.fabric8.catalog.watch.TestAssertions.assertLogStatement; -import static org.springframework.cloud.kubernetes.fabric8.catalog.watch.TestAssertions.invokeAndAssert; +import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8CatalogWatchEndpointsIT.TestConfig; +import static org.springframework.cloud.kubernetes.fabric8.discovery.TestAssertions.assertLogStatement; +import static org.springframework.cloud.kubernetes.fabric8.discovery.TestAssertions.invokeAndAssert; /** * @author wind57 */ -@SpringBootTest(classes = { KubernetesCatalogWatchAutoConfiguration.class, TestConfig.class, Application.class }, +@SpringBootTest(classes = { Fabric8CatalogWatchAutoConfiguration.class, TestConfig.class, Application.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class Fabric8CatalogWatchEndpointsIT extends Fabric8CatalogWatchBase { diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/TestAssertions.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/TestAssertions.java similarity index 98% rename from spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/TestAssertions.java rename to spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/TestAssertions.java index ea681e679b..03f06b56d0 100644 --- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/catalog/watch/TestAssertions.java +++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-catalog-watcher/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/TestAssertions.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.kubernetes.fabric8.catalog.watch; +package org.springframework.cloud.kubernetes.fabric8.discovery; import java.time.Duration; import java.util.List;