Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add namespace provider to fabric8 loadbalancer #1597

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
143 commits
Select commit Hold shift + click to select a range
032014e
test
wind57 Dec 4, 2021
c3d0ad2
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Dec 13, 2021
10889fd
fix @Nested tests not running
wind57 Dec 16, 2021
8f0375a
merged main
wind57 Dec 17, 2021
96ebf42
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jan 5, 2022
db8403e
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jan 12, 2022
90a5345
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jan 12, 2022
b041c00
trigger again
wind57 Jan 13, 2022
9580444
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 15, 2022
a34ac47
Add renovate.json
renovate[bot] Jul 13, 2022
6613f78
Merge branch 'spring-cloud:main' into main
wind57 Jul 13, 2022
1b3eae9
Merge pull request #1 from wind57/renovate/configure
wind57 Jul 13, 2022
4275382
Merge branch 'spring-cloud:main' into main
wind57 Mar 13, 2023
618f25a
Delete renovate.json
wind57 Mar 14, 2023
100a9cd
Delete delme.sh
wind57 Mar 14, 2023
4b9056b
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 14, 2023
315a85b
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 15, 2023
ed3c264
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 15, 2023
446d630
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 16, 2023
6821a28
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 16, 2023
9e95a8a
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 16, 2023
02840b1
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 16, 2023
7e31d65
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 20, 2023
926f4d7
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 20, 2023
344e1d4
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 21, 2023
e91ac12
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 23, 2023
ba6e088
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 28, 2023
49025e8
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 1, 2023
a647bb3
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 4, 2023
16b09ab
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 4, 2023
c8d8a72
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 5, 2023
591f340
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 6, 2023
c331bd0
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 6, 2023
f48b22b
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 7, 2023
900843d
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 8, 2023
76ecdd5
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 8, 2023
b69d4a6
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 11, 2023
df2ddcc
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 14, 2023
3ff1cdd
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 18, 2023
bd89cc8
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 18, 2023
39ef510
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 19, 2023
d389953
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 19, 2023
b676bac
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 21, 2023
b1473c9
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 24, 2023
83ff113
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 24, 2023
33dfbb7
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 24, 2023
316af58
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 25, 2023
425d30b
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 26, 2023
763280a
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 26, 2023
3afbaef
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 27, 2023
cb97ba1
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Apr 28, 2023
b33edc8
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 1, 2023
7a1fdf1
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 2, 2023
a8beed9
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 2, 2023
2e445d9
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 9, 2023
5b51db1
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 12, 2023
b0ce76c
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 12, 2023
918bb7f
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 12, 2023
c629951
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 15, 2023
ccfc0bf
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 22, 2023
0f3fa24
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 23, 2023
b60a283
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 25, 2023
901cb66
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 26, 2023
251002b
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 27, 2023
fedeb17
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 May 30, 2023
004f95b
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jun 3, 2023
10168be
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jun 5, 2023
dc811cf
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jun 6, 2023
3dfe77d
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jun 8, 2023
619c8d0
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jun 16, 2023
12ca211
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jun 17, 2023
a91c1e1
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jun 22, 2023
be2e809
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jun 24, 2023
1c84e33
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jul 17, 2023
1319fe7
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jul 18, 2023
9424c2e
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jul 25, 2023
cddd550
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Aug 2, 2023
f8d34f2
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Aug 3, 2023
31d2fb8
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Aug 5, 2023
fa9fd09
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Aug 8, 2023
22f3f51
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Aug 16, 2023
bb44f4b
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Aug 21, 2023
9714f2c
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Aug 22, 2023
0f9cb3d
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 2, 2023
6e60174
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 11, 2023
78c5575
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 12, 2023
5eb5c51
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 16, 2023
48f7cb0
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 18, 2023
8a18085
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 20, 2023
e4cab39
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 21, 2023
ba4cab0
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 21, 2023
bbc6e30
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 23, 2023
0e9f1ad
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 26, 2023
8a5243c
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 28, 2023
cbf83c4
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 29, 2023
3dd0260
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Sep 30, 2023
1408d44
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Oct 2, 2023
2925ee1
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Oct 2, 2023
af3a12b
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Oct 5, 2023
f5a27dc
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Oct 7, 2023
494454a
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Oct 7, 2023
add0f67
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Oct 13, 2023
b6a4b45
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Oct 20, 2023
00a559d
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Oct 27, 2023
882f9c1
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Oct 31, 2023
6c9071e
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Nov 3, 2023
09c10bb
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Nov 8, 2023
3615396
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Nov 14, 2023
116c199
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Nov 16, 2023
f1447cd
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Nov 18, 2023
14b165b
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Nov 21, 2023
3ce8fac
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Nov 30, 2023
41eaa8d
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Dec 4, 2023
4f89298
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Dec 7, 2023
ee1031d
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Dec 19, 2023
f6707d5
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Dec 22, 2023
9930c7d
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Dec 27, 2023
0043006
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Jan 16, 2024
4e292ec
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Feb 2, 2024
3129276
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Feb 16, 2024
1c90fc2
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Feb 16, 2024
fa0888a
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Feb 22, 2024
ab680d1
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Feb 25, 2024
e896ec4
dirty
wind57 Mar 9, 2024
4023ca3
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 9, 2024
7ed7f5d
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 9, 2024
3be81b7
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 10, 2024
dd710ec
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 10, 2024
cd93c3d
merge main, still dirty
wind57 Mar 11, 2024
faa1d85
still dirty
wind57 Mar 12, 2024
4fdd06a
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 12, 2024
e8ba51d
still dirty
wind57 Mar 13, 2024
14a93c0
ready
wind57 Mar 13, 2024
bf61054
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 13, 2024
5151acd
Merge branch 'main' into add-namespace-provider-to-fabric8-loadbalancer
wind57 Mar 13, 2024
97028d4
merge main
wind57 Mar 13, 2024
f059697
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 13, 2024
0ec1230
Merge branch 'main' into add-namespace-provider-to-fabric8-loadbalancer
wind57 Mar 13, 2024
013cc6a
Merge branch 'main' of https://github.com/spring-cloud/spring-cloud-k…
wind57 Mar 14, 2024
44a35a5
Merge branch 'main' into add-namespace-provider-to-fabric8-loadbalancer
wind57 Mar 14, 2024
02fffce
refactor tests
wind57 Mar 16, 2024
beb0c02
cleanup
wind57 Mar 16, 2024
b48edd4
checkstyle
wind57 Mar 16, 2024
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
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

un-related to the current PR, just saw that this one is un-used, so deprecated it. Later, when its allowed, I will be searching for these for clean-up, so it will useful to easy find them

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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the important bit right here : String namespace = Fabric8Utils.getApplicationNamespace .... This adds support for namespace resolution that we have in various other places too. Besides that, everything else are just log additions. Of course, when I am done with both clients, everything will be documented properly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this change the functionality a bit? Right now if we don't find the service in the namespace we look for it across all namespaces. With this change we will just return an empty Flux if we don't find it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean this piece of code, right?

Service service = StringUtils.hasText(kubernetesClient.getNamespace()) ? kubernetesClient.services()
					.inNamespace(kubernetesClient.getNamespace()).withName(getServiceId()).get()
					: kubernetesClient.services().withName(getServiceId()).get();

If so, this code is miss-leading, at best. It's actually exactly the same. It's counter intuitive, but its the same. The change was done a lot time ago in fabric8 client: fabric8io/kubernetes-client#123

So the fact that you can omit the namespace, creates this confusion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying it is exactly the same because before the KubernetesClient that was passed into the class already had the namespace set so when calling .inNamespace it didn't make a difference in the services returned?

Copy link
Contributor Author

@wind57 wind57 Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really, Im saying that:

kubernetesClient.services().inNamespace(kubernetesClient.getNamespace()).get()

is a longer version of :

kubernetesClient.services().get()

according this this comment from the issue I linked and my testing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand now, when you specify the namespace and it does not find the service it will also return the service if it exists in the default namespace?

Copy link
Contributor Author

@wind57 wind57 Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, but "default" namespace is not the literal default. meaning if this client is running in namespace a, this will be the "default namespace" : a, not default. Or, in other words, kubernetesClient.getNamespace() will return a.

So that code we currently have, kind of makes no sense.

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
Loading