Skip to content

Commit

Permalink
Merge branch 'main' into simplify-config-data-code-a-bit
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 committed Mar 18, 2024
2 parents 81efd8f + 13ff3b3 commit 5bd3f88
Show file tree
Hide file tree
Showing 30 changed files with 1,144 additions and 73 deletions.
4 changes: 2 additions & 2 deletions spring-cloud-kubernetes-client-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
</dependency>

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
</dependency>

Expand Down
4 changes: 2 additions & 2 deletions spring-cloud-kubernetes-client-discovery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions spring-cloud-kubernetes-client-loadbalancer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2021 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 @@ -14,9 +14,10 @@
* limitations under the License.
*/

package org.springframework.cloud.kubernetes.configserver;
package org.springframework.cloud.kubernetes.configserver.it;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.configserver.KubernetesConfigServerApplication;

/**
* @author Ryan Baxter
Expand All @@ -26,6 +27,6 @@
"spring.profiles.include=kubernetes", "spring.cloud.kubernetes.secrets.enableApi=true",
"spring.cloud.bootstrap.enabled=true" },
classes = { KubernetesConfigServerApplication.class })
public class BootstrapConfigServerIntegrationTest extends ConfigServerIntegrationTest {
class BootstrapConfigServerIntegrationTest extends ConfigServerIntegrationTest {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2021 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 @@ -14,20 +14,19 @@
* limitations under the License.
*/

package org.springframework.cloud.kubernetes.configserver;
package org.springframework.cloud.kubernetes.configserver.it;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import io.kubernetes.client.util.ClientBuilder;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.MockedStatic;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.client.KubernetesClientUtils;
import org.springframework.cloud.kubernetes.configserver.KubernetesConfigServerApplication;
import org.springframework.cloud.kubernetes.configserver.TestBootstrapConfig;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.mockito.Mockito.mockStatic;

/**
Expand All @@ -36,27 +35,20 @@
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.client.namespace=default",
"spring.profiles.include=kubernetes", "spring.cloud.kubernetes.secrets.enableApi=true" },
classes = { KubernetesConfigServerApplication.class })
public class ConfigDataConfigServerIntegrationTest extends ConfigServerIntegrationTest {
classes = { KubernetesConfigServerApplication.class, TestBootstrapConfig.class })
class ConfigDataConfigServerIntegrationTest extends ConfigServerIntegrationTest {

private static WireMockServer wireMockServer;

private static MockedStatic<KubernetesClientUtils> clientUtilsMock;

@BeforeAll
static void setup() {
wireMockServer = new WireMockServer(options().dynamicPort());
wireMockServer.start();
WireMock.configureFor(wireMockServer.port());
private MockedStatic<KubernetesClientUtils> clientUtilsMock;

@BeforeEach
void setup() {
clientUtilsMock = mockStatic(KubernetesClientUtils.class);
clientUtilsMock.when(KubernetesClientUtils::kubernetesApiClient)
.thenReturn(new ClientBuilder().setBasePath(wireMockServer.baseUrl()).build());
}

@AfterAll
static void teardown() {
wireMockServer.stop();
@AfterEach
void teardown() {
clientUtilsMock.close();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2021 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 @@ -14,8 +14,9 @@
* limitations under the License.
*/

package org.springframework.cloud.kubernetes.configserver;
package org.springframework.cloud.kubernetes.configserver.it;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import io.kubernetes.client.openapi.JSON;
import io.kubernetes.client.openapi.models.V1ConfigMapBuilder;
Expand Down Expand Up @@ -45,8 +46,11 @@ abstract class ConfigServerIntegrationTest {
@Autowired
private TestRestTemplate testRestTemplate;

@Autowired
WireMockServer wireMockServer;

@BeforeEach
public void beforeEach() {
void beforeEach() {
V1ConfigMapList TEST_CONFIGMAP = new V1ConfigMapList().addItemsItem(new V1ConfigMapBuilder().withMetadata(
new V1ObjectMetaBuilder().withName("test-cm").withNamespace("default").withResourceVersion("1").build())
.addToData("app.name", "test").build());
Expand All @@ -67,7 +71,7 @@ public void beforeEach() {
}

@Test
public void enabled() {
void enabled() {
Environment env = testRestTemplate.getForObject("/test-cm/default", Environment.class);
assertThat(env.getPropertySources().size()).isEqualTo(2);
assertThat(env.getPropertySources().get(0).getName().equals("configmap.test-cm.default")).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
</exclusions>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
6 changes: 3 additions & 3 deletions spring-cloud-kubernetes-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<hoverfly.version>0.13.0</hoverfly.version>
<kubernetes-fabric8-client.version>6.9.2</kubernetes-fabric8-client.version>
<kubernetes-native-client.version>19.0.0</kubernetes-native-client.version>
<wiremock.version>2.26.3</wiremock.version>
<wiremock.version>3.4.2</wiremock.version>
<commons.collections4.version>4.4</commons.collections4.version>
</properties>
<dependencyManagement>
Expand Down Expand Up @@ -213,8 +213,8 @@
</dependency>

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
Expand Down
4 changes: 2 additions & 2 deletions spring-cloud-kubernetes-discovery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions spring-cloud-kubernetes-fabric8-discovery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<scope>test</scope>
</dependency>
<dependency>
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 @@ -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
Loading

0 comments on commit 5bd3f88

Please sign in to comment.