Skip to content

Commit

Permalink
Add namespace provider to fabric8 loadbalancer (#1597)
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 authored Mar 18, 2024
1 parent 19fa6ab commit 0bd7bc5
Show file tree
Hide file tree
Showing 12 changed files with 1,052 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2023 the original author or authors.
* Copyright 2013-2024 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.
Expand Down Expand Up @@ -46,6 +46,7 @@ public static KubernetesClientServicesFunction servicesFunction(KubernetesDiscov

}

@Deprecated(forRemoval = true)
public static KubernetesClientServicesFunction servicesFunction(KubernetesDiscoveryProperties properties,
Binder binder, BindHandler bindHandler) {
return servicesFunction(properties, new KubernetesNamespaceProvider(binder, bindHandler));
Expand Down
23 changes: 23 additions & 0 deletions spring-cloud-kubernetes-fabric8-loadbalancer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,35 @@
<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>io.fabric8</groupId>
<artifactId>kubernetes-server-mock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</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
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-2024 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.
Expand All @@ -21,14 +21,17 @@

import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Flux;

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.loadbalancer.KubernetesServicesListSupplier;
import org.springframework.cloud.kubernetes.fabric8.Fabric8Utils;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import org.springframework.core.log.LogAccessor;

/**
* Implementation of {@link ServiceInstanceListSupplier} for load balancer in SERVICE
Expand All @@ -38,30 +41,45 @@
*/
public class Fabric8ServicesListSupplier extends KubernetesServicesListSupplier<Service> {

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

private final KubernetesClient kubernetesClient;

private final KubernetesNamespaceProvider namespaceProvider;

Fabric8ServicesListSupplier(Environment environment, KubernetesClient kubernetesClient,
Fabric8ServiceInstanceMapper mapper, KubernetesDiscoveryProperties discoveryProperties) {
super(environment, mapper, discoveryProperties);
this.kubernetesClient = kubernetesClient;
namespaceProvider = new KubernetesNamespaceProvider(environment);
}

@Override
public Flux<List<ServiceInstance>> get() {
List<ServiceInstance> result = new ArrayList<>();
String serviceName = getServiceId();
LOG.debug(() -> "serviceID : " + serviceName);

if (discoveryProperties.allNamespaces()) {
LOG.debug(() -> "discovering services in all namespaces");
List<Service> services = kubernetesClient.services().inAnyNamespace()
.withField("metadata.name", getServiceId()).list().getItems();
.withField("metadata.name", serviceName).list().getItems();
services.forEach(service -> result.add(mapper.map(service)));
}
else {
Service service = StringUtils.hasText(kubernetesClient.getNamespace()) ? kubernetesClient.services()
.inNamespace(kubernetesClient.getNamespace()).withName(getServiceId()).get()
: kubernetesClient.services().withName(getServiceId()).get();
String namespace = Fabric8Utils.getApplicationNamespace(kubernetesClient, null, "loadbalancer-service",
namespaceProvider);
LOG.debug(() -> "discovering services in namespace : " + namespace);
Service service = kubernetesClient.services().inNamespace(namespace).withName(serviceName).get();
if (service != null) {
result.add(mapper.map(service));
}
else {
LOG.debug(() -> "did not find service with name : " + serviceName + " in namespace : " + namespace);
}
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2023 the original author or authors.
* Copyright 2013-2024 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.
Expand Down Expand Up @@ -78,7 +78,6 @@ void testPositiveMatch() {
KubernetesServicesListSupplier<Service> supplier = new Fabric8ServicesListSupplier(environment, client, mapper,
KubernetesDiscoveryProperties.DEFAULT);
List<ServiceInstance> instances = supplier.get().blockFirst();
assert instances != null;
Assertions.assertEquals(1, instances.size());
}

Expand All @@ -98,7 +97,6 @@ void testPositiveMatchAllNamespaces() {
KubernetesServicesListSupplier<Service> supplier = new Fabric8ServicesListSupplier(environment, client, mapper,
discoveryProperties);
List<ServiceInstance> instances = supplier.get().blockFirst();
assert instances != null;
Assertions.assertEquals(1, instances.size());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright 2013-2024 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.loadbalancer;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;
import io.fabric8.kubernetes.api.model.ServicePortBuilder;
import io.fabric8.kubernetes.api.model.ServiceSpecBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

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.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadBalancerProperties;
import org.springframework.core.env.Environment;
import org.springframework.mock.env.MockEnvironment;

/**
* @author wind57
*/
@EnableKubernetesMockClient(crud = true, https = false)
@ExtendWith(OutputCaptureExtension.class)
class Fabric8ServicesListSupplierMockClientTests {

private static KubernetesClient mockClient;

@BeforeAll
static void setUpBeforeClass() {
// Configure the kubernetes master url to point to the mock server
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl());
System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test");
System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true");
}

@AfterEach
void afterEach() {
mockClient.services().inAnyNamespace().delete();
}

@Test
void testAllNamespaces(CapturedOutput output) {

createService("a", "service-a", 8887);
createService("b", "service-b", 8888);
createService("c", "service-a", 8889);

Environment environment = new MockEnvironment().withProperty("loadbalancer.client.name", "service-a");
boolean allNamespaces = true;
Set<String> selectiveNamespaces = Set.of();

KubernetesLoadBalancerProperties loadBalancerProperties = new KubernetesLoadBalancerProperties();
KubernetesDiscoveryProperties discoveryProperties = new KubernetesDiscoveryProperties(true, allNamespaces,
selectiveNamespaces, true, 60, false, null, Set.of(), Map.of(), null,
KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false, false, null);

Fabric8ServicesListSupplier supplier = new Fabric8ServicesListSupplier(environment, mockClient,
new Fabric8ServiceInstanceMapper(loadBalancerProperties, discoveryProperties), discoveryProperties);

List<List<ServiceInstance>> serviceInstances = supplier.get().collectList().block();
Assertions.assertEquals(serviceInstances.size(), 1);
List<ServiceInstance> inner = serviceInstances.get(0);

List<ServiceInstance> serviceInstancesSorted = serviceInstances.get(0).stream()
.sorted(Comparator.comparing(ServiceInstance::getServiceId)).toList();
Assertions.assertEquals(serviceInstancesSorted.size(), 2);
Assertions.assertEquals(inner.get(0).getServiceId(), "service-a");
Assertions.assertEquals(inner.get(0).getHost(), "service-a.a.svc.cluster.local");
Assertions.assertEquals(inner.get(0).getPort(), 8887);

Assertions.assertEquals(inner.get(1).getServiceId(), "service-a");
Assertions.assertEquals(inner.get(1).getHost(), "service-a.c.svc.cluster.local");
Assertions.assertEquals(inner.get(1).getPort(), 8889);

Assertions.assertTrue(output.getOut().contains("discovering services in all namespaces"));
}

@Test
void testOneNamespace(CapturedOutput output) {

createService("a", "service-c", 8887);
createService("b", "service-b", 8888);
createService("c", "service-c", 8889);

Environment environment = new MockEnvironment().withProperty("spring.cloud.kubernetes.client.namespace", "c")
.withProperty("loadbalancer.client.name", "service-c");
boolean allNamespaces = false;
Set<String> selectiveNamespaces = Set.of();

KubernetesLoadBalancerProperties loadBalancerProperties = new KubernetesLoadBalancerProperties();
KubernetesDiscoveryProperties discoveryProperties = new KubernetesDiscoveryProperties(true, allNamespaces,
selectiveNamespaces, true, 60, false, null, Set.of(), Map.of(), null,
KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false, false, null);

Fabric8ServicesListSupplier supplier = new Fabric8ServicesListSupplier(environment, mockClient,
new Fabric8ServiceInstanceMapper(loadBalancerProperties, discoveryProperties), discoveryProperties);

List<List<ServiceInstance>> serviceInstances = supplier.get().collectList().block();
Assertions.assertEquals(serviceInstances.size(), 1);
List<ServiceInstance> inner = serviceInstances.get(0);

List<ServiceInstance> serviceInstancesSorted = serviceInstances.get(0).stream()
.sorted(Comparator.comparing(ServiceInstance::getServiceId)).toList();
Assertions.assertEquals(serviceInstancesSorted.size(), 1);
Assertions.assertEquals(inner.get(0).getServiceId(), "service-c");
Assertions.assertEquals(inner.get(0).getHost(), "service-c.c.svc.cluster.local");
Assertions.assertEquals(inner.get(0).getPort(), 8889);

Assertions.assertTrue(output.getOut().contains("discovering services in namespace : c"));
}

private void createService(String namespace, String name, int port) {
Service service = new ServiceBuilder().withNewMetadata().withNamespace(namespace).withName(name).endMetadata()
.withSpec(new ServiceSpecBuilder()
.withPorts(new ServicePortBuilder().withName("http").withPort(port).build()).build())
.build();
mockClient.services().resource(service).create();
}

}
Loading

0 comments on commit 0bd7bc5

Please sign in to comment.