Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/discovery-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> getMetadata() {
return metadata;
}

public String getInstanceId() {
return instanceId;
}

public String type() {
return "ExternalName";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -43,32 +42,28 @@
/**
* @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;

private Function<Fabric8CatalogWatchContext, List<EndpointNameAndNamespace>> stateGenerator;

private volatile List<EndpointNameAndNamespace> 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<EndpointNameAndNamespace> currentState = stateGenerator.apply(context);
Expand Down Expand Up @@ -97,7 +92,7 @@ Function<Fabric8CatalogWatchContext, List<EndpointNameAndNamespace>> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,7 +38,7 @@
class Fabric8ConfigServerBootstrapper extends KubernetesConfigServerBootstrapper {

@Override
public void initialize(BootstrapRegistry registry) {
public void initialize(@Nonnull BootstrapRegistry registry) {
if (hasConfigServerInstanceProvider()) {
return;
}
Expand All @@ -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 {
Expand All @@ -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;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,19 @@
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;
import static org.springframework.cloud.kubernetes.commons.discovery.DiscoveryClientUtils.serviceInstance;
import static org.springframework.cloud.kubernetes.commons.discovery.DiscoveryClientUtils.serviceInstanceMetadata;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.EXTERNAL_NAME;
import static org.springframework.cloud.kubernetes.fabric8.Fabric8Utils.serviceMetadata;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.addresses;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.endpointSubsetsPortData;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.endpoints;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8DiscoveryClientUtils.services;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8InstanceIdHostPodNameSupplier.externalName;
import static org.springframework.cloud.kubernetes.fabric8.discovery.Fabric8InstanceIdHostPodNameSupplier.nonExternalName;
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;

Expand All @@ -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<Service> predicate;

this(client, kubernetesDiscoveryProperties, kubernetesClientServicesFunction, null,
new ServicePortSecureResolver(kubernetesDiscoveryProperties));
}

KubernetesDiscoveryClient(KubernetesClient client, KubernetesDiscoveryProperties kubernetesDiscoveryProperties,
KubernetesClientServicesFunction kubernetesClientServicesFunction, Predicate<Service> filter,
ServicePortSecureResolver servicePortSecureResolver) {
Fabric8DiscoveryClient(KubernetesClient client, KubernetesDiscoveryProperties kubernetesDiscoveryProperties,
ServicePortSecureResolver servicePortSecureResolver, KubernetesNamespaceProvider namespaceProvider,
Predicate<Service> 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
Expand All @@ -105,7 +92,8 @@ public String description() {
public List<ServiceInstance> getInstances(String serviceId) {
Objects.requireNonNull(serviceId);

List<Endpoints> allEndpoints = getEndPointsList(serviceId).stream().toList();
List<Endpoints> allEndpoints = endpoints(properties, client, namespaceProvider, "fabric8-discovery", serviceId,
predicate);

List<ServiceInstance> instances = new ArrayList<>();
for (Endpoints endpoints : allEndpoints) {
Expand Down Expand Up @@ -135,8 +123,20 @@ public List<ServiceInstance> getInstances(String serviceId) {
return instances;
}

public List<Endpoints> getEndPointsList(String serviceId) {
return endpoints(properties, client, namespaceProvider, "fabric8-discovery", serviceId, adapter.filter());
@Override
public List<String> getServices() {
List<String> services = services(properties, client, namespaceProvider, predicate, null, "fabric8 discovery")
.stream()
.map(service -> service.getMetadata().getName())
.distinct()
.toList();
LOG.debug(() -> "will return services : " + services);
return services;
}

@Override
public int getOrder() {
return properties.order();
}

private List<ServiceInstance> serviceInstances(Endpoints endpoints, String serviceId) {
Expand Down Expand Up @@ -176,31 +176,4 @@ private List<ServiceInstance> serviceInstances(Endpoints endpoints, String servi
return instances;
}

@Override
public List<String> getServices() {
List<String> 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<String> getServices(Predicate<Service> 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);
}

}
Loading
Loading