Skip to content

Commit

Permalink
Merge branch 'main' into move-to-a-common-configuration-for-health
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 committed Apr 3, 2024
2 parents ce72c22 + 02737d4 commit 5eb1612
Show file tree
Hide file tree
Showing 29 changed files with 2,403 additions and 535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ private V1Pod internalGetPod() {
}
catch (Throwable t) {
if (failFast) {
if (t instanceof ApiException apiException) {
LOG.error("error reading pod : " + apiException.getResponseBody());
}
throw new RuntimeException(t);
}
if (t instanceof ApiException apiException) {
Expand Down
18 changes: 18 additions & 0 deletions spring-cloud-kubernetes-client-loadbalancer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,20 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.mokito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
Expand All @@ -51,6 +64,11 @@
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-test-support</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Flux;

Expand All @@ -32,47 +31,88 @@
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServiceInstanceMapper;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServicesListSupplier;
import org.springframework.core.env.Environment;
import org.springframework.core.log.LogAccessor;

import static org.springframework.cloud.kubernetes.client.KubernetesClientUtils.getApplicationNamespace;

/**
* @author Ryan Baxter
*/
public class KubernetesClientServicesListSupplier extends KubernetesServicesListSupplier<V1Service> {

private static final Log LOG = LogFactory.getLog(KubernetesClientServicesListSupplier.class);
private static final LogAccessor LOG = new LogAccessor(
LogFactory.getLog(KubernetesClientServicesListSupplier.class));

private final CoreV1Api coreV1Api;

private final String namespace;
private final KubernetesNamespaceProvider kubernetesNamespaceProvider;

public KubernetesClientServicesListSupplier(Environment environment,
KubernetesServiceInstanceMapper<V1Service> mapper, KubernetesDiscoveryProperties discoveryProperties,
CoreV1Api coreV1Api, KubernetesNamespaceProvider kubernetesNamespaceProvider) {
super(environment, mapper, discoveryProperties);
this.coreV1Api = coreV1Api;
this.namespace = kubernetesNamespaceProvider.getNamespace();
this.kubernetesNamespaceProvider = kubernetesNamespaceProvider;
}

@Override
public Flux<List<ServiceInstance>> get() {
LOG.info("Getting services with id " + this.getServiceId());
List<ServiceInstance> result = new ArrayList<>();
List<V1Service> services;
try {
if (discoveryProperties.allNamespaces()) {
services = coreV1Api.listServiceForAllNamespaces(null, null, "metadata.name=" + this.getServiceId(),
null, null, null, null, null, null, null, null).getItems();
String serviceName = getServiceId();
LOG.debug(() -> "serviceID : " + serviceName);

if (discoveryProperties.allNamespaces()) {
LOG.debug(() -> "discovering services in all namespaces");
List<V1Service> services = services(null, serviceName);
services.forEach(service -> addMappedService(mapper, result, service));
}
else if (!discoveryProperties.namespaces().isEmpty()) {
List<String> selectiveNamespaces = discoveryProperties.namespaces().stream().sorted().toList();
LOG.debug(() -> "discovering services in selective namespaces : " + selectiveNamespaces);
selectiveNamespaces.forEach(selectiveNamespace -> {
List<V1Service> services = services(selectiveNamespace, serviceName);
services.forEach(service -> addMappedService(mapper, result, service));
});
}
else {
String namespace = getApplicationNamespace(null, "loadbalancer-service", kubernetesNamespaceProvider);
LOG.debug(() -> "discovering services in namespace : " + namespace);
List<V1Service> services = services(namespace, serviceName);
services.forEach(service -> addMappedService(mapper, result, service));
}

LOG.debug(() -> "found services : " + result);
return Flux.defer(() -> Flux.just(result));
}

private void addMappedService(KubernetesServiceInstanceMapper<V1Service> mapper, List<ServiceInstance> services,
V1Service service) {
services.add(mapper.map(service));
}

private List<V1Service> services(String namespace, String serviceName) {
if (namespace == null) {
try {
return coreV1Api.listServiceForAllNamespaces(null, null, "metadata.name=" + serviceName, null, null,
null, null, null, null, null, null).getItems();
}
else {
services = coreV1Api.listNamespacedService(namespace, null, null, null,
"metadata.name=" + this.getServiceId(), null, null, null, null, null, null, null).getItems();
catch (ApiException apiException) {
LOG.warn(apiException, "Error retrieving services (in all namespaces) with name " + serviceName);
return List.of();
}
services.forEach(service -> result.add(mapper.map(service)));
}
catch (ApiException e) {
LOG.warn("Error retrieving service with name " + this.getServiceId(), e);
else {
try {
// there is going to be a single service here, if found
return coreV1Api.listNamespacedService(namespace, null, null, null, "metadata.name=" + serviceName,
null, null, null, null, null, null, null).getItems();
}
catch (ApiException apiException) {
LOG.warn(apiException,
"Error retrieving service with name " + serviceName + " in namespace : " + namespace);
return List.of();
}
}
LOG.info("Returning services: " + result);
return Flux.defer(() -> Flux.just(result));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void multiportMap() {

KubernetesServiceInstance serviceInstance = mapper.map(service);
DefaultKubernetesServiceInstance result = new DefaultKubernetesServiceInstance("0", "database",
"database.default.svc.cluster.local", 443, new HashMap(), true);
"database.default.svc.cluster.local", 443, Map.of(), true);
assertThat(serviceInstance).isEqualTo(result);
}

Expand Down
Loading

0 comments on commit 5eb1612

Please sign in to comment.