Skip to content

Commit

Permalink
Align fabric8 k8s discovery clients part 6 (#1507)
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 authored Nov 16, 2023
1 parent 13787ee commit 8c055fb
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.config.client.ConfigServerInstanceProvider;
import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties;
import org.springframework.cloud.kubernetes.commons.config.KubernetesConfigServerBootstrapper;
import org.springframework.cloud.kubernetes.commons.config.KubernetesConfigServerInstanceProvider;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
Expand All @@ -53,14 +52,11 @@ public void initialize(BootstrapRegistry registry) {

final static class KubernetesFunction implements ConfigServerInstanceProvider.Function {

private final BootstrapContext context;

private KubernetesFunction(BootstrapContext context) {
this.context = context;
private KubernetesFunction() {
}

static KubernetesFunction create(BootstrapContext context) {
return new KubernetesFunction(context);
return new KubernetesFunction();
}

@Override
Expand All @@ -73,18 +69,12 @@ public List<ServiceInstance> apply(String serviceId, Binder binder, BindHandler
// Kubernetes DiscoveryClient
return Collections.emptyList();
}
KubernetesDiscoveryProperties discoveryProperties = createKubernetesDiscoveryProperties(binder,
bindHandler);
KubernetesClientProperties clientProperties = createKubernetesClientProperties(binder, bindHandler);
return getInstanceProvider(discoveryProperties, clientProperties, context, binder, bindHandler, log)
.getInstances(serviceId);
return getInstanceProvider(binder, bindHandler).getInstances(serviceId);
}

private KubernetesConfigServerInstanceProvider getInstanceProvider(
KubernetesDiscoveryProperties discoveryProperties, KubernetesClientProperties clientProperties,
BootstrapContext context, Binder binder, BindHandler bindHandler, Log log) {
private KubernetesConfigServerInstanceProvider getInstanceProvider(Binder binder, BindHandler bindHandler) {
KubernetesDiscoveryClientProperties kubernetesDiscoveryClientProperties = binder
.bind("spring.cloud.kubernetes.discovery", Bindable.of(KubernetesDiscoveryClientProperties.class),
.bind(KubernetesDiscoveryProperties.PREFIX, Bindable.of(KubernetesDiscoveryClientProperties.class),
bindHandler)
.orElseGet(KubernetesDiscoveryClientProperties::new);
KubernetesDiscoveryClientAutoConfiguration.Servlet autoConfiguration = new KubernetesDiscoveryClientAutoConfiguration.Servlet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,23 @@ void beforeAll() throws JsonProcessingException {
wireMockServer = new WireMockServer(options().dynamicPort());
wireMockServer.start();
WireMock.configureFor(wireMockServer.port());
String APPS_NAME = "[{\"instanceId\":\"uid2\",\"serviceId\":\"spring-cloud-kubernetes-configserver\",\"host\":\"localhost\",\"port\":"
+ wireMockServer.port() + ",\"uri\":\"" + wireMockServer.baseUrl()
+ "\",\"secure\":false,\"metadata\":{\"spring\":\"true\",\"http\":\"8080\",\"k8s\":\"true\"},\"namespace\":\"namespace1\",\"cluster\":null,\"scheme\":\"http\"}]";
String APPS_NAME = """
[{
"instanceId": "uid2",
"serviceId": "spring-cloud-kubernetes-configserver",
"host": "localhost",
"port": "%s",
"uri": "%s",
"secure":false,
"metadata":{"spring": "true", "http": "8080", "k8s": "true"},
"namespace": "namespace1",
"cluster": null,
"scheme":"http"
}]
""".formatted(wireMockServer.port(), wireMockServer.baseUrl());

System.out.println(APPS_NAME);

stubFor(get("/apps/spring-cloud-kubernetes-configserver").willReturn(
aResponse().withStatus(200).withBody(APPS_NAME).withHeader("content-type", "application/json")));
Environment environment = new Environment("test", "default");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
*/
class KubernetesDiscoveryClientAutoConfigurationTests {

private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class,
ReactiveCommonsClientAutoConfiguration.class, KubernetesDiscoveryClientAutoConfiguration.class));

@Test
public void shouldWorkWithDefaults() {
void shouldWorkWithDefaults() {
contextRunner
.withPropertyValues("spring.main.cloud-platform=KUBERNETES",
"spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver")
Expand All @@ -51,7 +51,7 @@ public void shouldWorkWithDefaults() {
}

@Test
public void shouldNotHaveDiscoveryClientWhenDiscoveryDisabled() {
void shouldNotHaveDiscoveryClientWhenDiscoveryDisabled() {
contextRunner
.withPropertyValues("spring.cloud.discovery.enabled=false",
"spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver")
Expand All @@ -63,7 +63,7 @@ public void shouldNotHaveDiscoveryClientWhenDiscoveryDisabled() {
}

@Test
public void shouldNotHaveDiscoveryClientWhenKubernetesDiscoveryDisabled() {
void shouldNotHaveDiscoveryClientWhenKubernetesDiscoveryDisabled() {
contextRunner
.withPropertyValues("spring.cloud.kubernetes.discovery.enabled=false",
"spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver")
Expand All @@ -75,7 +75,7 @@ public void shouldNotHaveDiscoveryClientWhenKubernetesDiscoveryDisabled() {
}

@Test
public void shouldHaveReactiveDiscoveryClient() {
void shouldHaveReactiveDiscoveryClient() {
contextRunner
.withPropertyValues("spring.main.cloud-platform=KUBERNETES",
"spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver")
Expand All @@ -87,23 +87,23 @@ public void shouldHaveReactiveDiscoveryClient() {
}

@Test
public void shouldNotHaveDiscoveryClientWhenReactiveDiscoveryDisabled() {
void shouldNotHaveDiscoveryClientWhenReactiveDiscoveryDisabled() {
contextRunner.withPropertyValues("spring.cloud.discovery.reactive.enabled=false").run(context -> {
assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class);
});
}

@Test
public void shouldNotHaveDiscoveryClientWhenKubernetesDisabled() {
void shouldNotHaveDiscoveryClientWhenKubernetesDisabled() {
contextRunner.run(context -> {
assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class);
});
}

@Test
public void worksWithoutActuator() {
void worksWithoutActuator() {
contextRunner
.withPropertyValues("spring.main.cloud-platform=KUBERNETES",
"spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,55 @@
*/
class KubernetesDiscoveryClientTests {

private static final String APPS = "[{\"name\":\"test-svc-1\",\"serviceInstances\":[{\"instanceId\":\"uid1\",\"serviceId\":\"test-svc-1\",\"host\":\"2.2.2.2\",\"port\":8080,\"uri\":\"http://2.2.2.2:8080\",\"secure\":false,\"metadata\":{\"http\":\"8080\"},\"namespace\":\"namespace1\",\"cluster\":null,\"scheme\":\"http\"}]},{\"name\":\"test-svc-3\",\"serviceInstances\":[{\"instanceId\":\"uid2\",\"serviceId\":\"test-svc-3\",\"host\":\"2.2.2.2\",\"port\":8080,\"uri\":\"http://2.2.2.2:8080\",\"secure\":false,\"metadata\":{\"spring\":\"true\",\"http\":\"8080\",\"k8s\":\"true\"},\"namespace\":\"namespace2\",\"cluster\":null,\"scheme\":\"http\"}]}]";

private static final String APPS_NAME = "[{\"instanceId\":\"uid2\",\"serviceId\":\"test-svc-3\",\"host\":\"2.2.2.2\",\"port\":8080,\"uri\":\"http://2.2.2.2:8080\",\"secure\":false,\"metadata\":{\"spring\":\"true\",\"http\":\"8080\",\"k8s\":\"true\"},\"namespace\":\"namespace2\",\"cluster\":null,\"scheme\":\"http\"}]";
private static final String APPS = """
[{
"name": "test-svc-1",
"serviceInstances":
[{
"instanceId": "uid1",
"serviceId": "test-svc-1",
"host": "2.2.2.2",
"port": 8080,
"uri": "http://2.2.2.2:8080",
"secure": false,
"metadata":{"http": "8080"},
"namespace": "namespace1",
"cluster": null,
"scheme": "http"
}]
},
{
"name": "test-svc-3",
"serviceInstances":
[{
"instanceId": "uid2",
"serviceId": "test-svc-3",
"host": "2.2.2.2",
"port": 8080,
"uri": "http://2.2.2.2:8080",
"secure": false,
"metadata": {"spring": "true", "http": "8080", "k8s": "true"},
"namespace": "namespace2",
"cluster":null,
"scheme":"http"
}]
}]
""";

private static final String APPS_NAME = """
[{
"instanceId": "uid2",
"serviceId": "test-svc-3",
"host": "2.2.2.2",
"port": 8080,
"uri": "http://2.2.2.2:8080",
"secure": false,
"metadata": {"spring": "true", "http": "8080", "k8s": "true"},
"namespace": "namespace2",
"cluster": null,
"scheme": "http"
}]
""";

private static WireMockServer wireMockServer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,55 @@
*/
class KubernetesReactiveDiscoveryClientTests {

private static final String APPS = "[{\"name\":\"test-svc-1\",\"serviceInstances\":[{\"instanceId\":\"uid1\",\"serviceId\":\"test-svc-1\",\"host\":\"2.2.2.2\",\"port\":8080,\"uri\":\"http://2.2.2.2:8080\",\"secure\":false,\"metadata\":{\"http\":\"8080\"},\"namespace\":\"namespace1\",\"cluster\":null,\"scheme\":\"http\"}]},{\"name\":\"test-svc-3\",\"serviceInstances\":[{\"instanceId\":\"uid2\",\"serviceId\":\"test-svc-3\",\"host\":\"2.2.2.2\",\"port\":8080,\"uri\":\"http://2.2.2.2:8080\",\"secure\":false,\"metadata\":{\"spring\":\"true\",\"http\":\"8080\",\"k8s\":\"true\"},\"namespace\":\"namespace1\",\"cluster\":null,\"scheme\":\"http\"}]}]";
private static final String APPS = """
[{
"name": "test-svc-1",
"serviceInstances":
[{
"instanceId": "uid1",
"serviceId": "test-svc-1",
"host": "2.2.2.2",
"port": 8080,
"uri": "http://2.2.2.2:8080",
"secure": false,
"metadata": {"http":"8080"},
"namespace": "namespace1",
"cluster": null,
"scheme": "http"
}]
},
{
"name": "test-svc-3",
"serviceInstances":
[{
"instanceId": "uid2",
"serviceId": "test-svc-3",
"host": "2.2.2.2",
"port": 8080,
"uri": "http://2.2.2.2:8080",
"secure": false,
"metadata": {"spring": "true", "http": "8080", "k8s": "true"},
"namespace": "namespace1",
"cluster": null,
"scheme": "http"
}]
}]
""";

private static final String APPS_NAME = "[{\"instanceId\":\"uid2\",\"serviceId\":\"test-svc-3\",\"host\":\"2.2.2.2\",\"port\":8080,\"uri\":\"http://2.2.2.2:8080\",\"secure\":false,\"metadata\":{\"spring\":\"true\",\"http\":\"8080\",\"k8s\":\"true\"},\"namespace\":\"namespace1\",\"cluster\":null,\"scheme\":\"http\"}]";
private static final String APPS_NAME = """
[{
"instanceId": "uid2",
"serviceId": "test-svc-3",
"host": "2.2.2.2",
"port": 8080,
"uri": "http://2.2.2.2:8080",
"secure": false,
"metadata": {"spring": "true", "http": "8080", "k8s": "true"},
"namespace": "namespace1",
"cluster": null,
"scheme": "http"
}]
""";

private static WireMockServer wireMockServer;

Expand Down

0 comments on commit 8c055fb

Please sign in to comment.