Skip to content

Commit b87b82b

Browse files
committed
dirty
1 parent e38293f commit b87b82b

File tree

3 files changed

+29
-43
lines changed

3 files changed

+29
-43
lines changed

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/client/discovery/Fabric8DiscoveryController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ List<Endpoints> getEndPointsList(@PathVariable("serviceId") String serviceId) {
5353

5454
@GetMapping("/service-instances/{serviceId}")
5555
List<ServiceInstance> serviceInstances(@PathVariable("serviceId") String serviceId) {
56+
discoveryClient.getServices();
5657
return discoveryClient.getInstances(serviceId);
5758
}
5859

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/discovery/it/Fabric8DiscoveryBase.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,20 @@
2828
import org.springframework.cloud.kubernetes.integration.tests.commons.fabric8_client.Util;
2929
import org.springframework.test.context.TestPropertySource;
3030

31+
/**
32+
* @author wind57
33+
*/
3134
@TestPropertySource(
3235
properties = { "spring.main.cloud-platform=kubernetes", "spring.cloud.config.import-check.enabled=false",
3336
"spring.cloud.kubernetes.client.namespace=default",
34-
"logging.level.org.springframework.cloud.kubernetes.fabric8.discovery=DEBUG" })
37+
"spring.cloud.kubernetes.discovery.metadata.add-pod-labels=true",
38+
"spring.cloud.kubernetes.discovery.metadata.add-pod-annotations=true" ,
39+
"logging.level.org.springframework.cloud.kubernetes.fabric8.discovery=debug" })
3540
@ExtendWith(OutputCaptureExtension.class)
3641
abstract class Fabric8DiscoveryBase {
3742

3843
protected static final String NAMESPACE = "default";
3944

40-
protected static final String NAMESPACE_A = "a";
41-
42-
protected static final String NAMESPACE_B = "b";
43-
4445
protected static final K3sContainer K3S = Commons.container();
4546

4647
protected static Util util;

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-client-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/client/discovery/it/Fabric8DiscoveryPodMetadataIT.java

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,33 @@
1919
import java.util.List;
2020
import java.util.Map;
2121

22+
import io.fabric8.kubernetes.client.KubernetesClient;
2223
import org.junit.jupiter.api.AfterEach;
2324
import org.junit.jupiter.api.Assertions;
2425
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Test;
2627

2728
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.boot.test.context.TestConfiguration;
2830
import org.springframework.boot.test.web.server.LocalServerPort;
2931
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
3032
import org.springframework.cloud.kubernetes.fabric8.client.discovery.Fabric8DiscoveryApp;
3133
import org.springframework.cloud.kubernetes.integration.tests.commons.Images;
3234
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
35+
import org.springframework.context.annotation.Bean;
36+
import org.springframework.context.annotation.Primary;
3337
import org.springframework.core.ParameterizedTypeReference;
3438
import org.springframework.http.HttpMethod;
3539
import org.springframework.web.reactive.function.client.WebClient;
3640

3741
import static org.springframework.cloud.kubernetes.fabric8.client.discovery.it.TestAssertions.builder;
3842
import static org.springframework.cloud.kubernetes.fabric8.client.discovery.it.TestAssertions.retrySpec;
43+
import static org.springframework.cloud.kubernetes.fabric8.client.discovery.it.Fabric8DiscoveryPodMetadataIT.TestConfig;
3944

4045
/**
4146
* @author wind57
4247
*/
43-
@SpringBootTest(classes = { Fabric8DiscoveryApp.class },
48+
@SpringBootTest(classes = { Fabric8DiscoveryApp.class, TestConfig.class },
4449
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
4550
class Fabric8DiscoveryPodMetadataIT extends Fabric8DiscoveryBase {
4651

@@ -58,9 +63,16 @@ void afterEach() {
5863
util.busybox(NAMESPACE, Phase.DELETE);
5964
}
6065

66+
/**
67+
* <pre>
68+
* - there is a 'busybox-service' service deployed with two pods
69+
* - find each of the pod, add annotation to one and labels to another
70+
* - call
71+
* </pre>
72+
*/
6173
@Test
6274
void test() throws Exception {
63-
// find both pods
75+
// find both pods
6476
String[] both = K3S.execInContainer("sh", "-c", "kubectl get pods -l app=busybox -o=name --no-headers")
6577
.getStdout()
6678
.split("\n");
@@ -70,45 +82,17 @@ void test() throws Exception {
7082
// add annotation to the second pod
7183
K3S.execInContainer("sh", "-c",
7284
"kubectl annotate pods " + both[1].split("/")[1] + " custom-annotation=custom-annotation-value");
85+
}
7386

74-
WebClient client = builder().baseUrl("http://localhost:" + port + "/busybox-service").build();
75-
List<DefaultKubernetesServiceInstance> serviceInstances = client.method(HttpMethod.GET)
76-
.retrieve()
77-
.bodyToMono(new ParameterizedTypeReference<List<DefaultKubernetesServiceInstance>>() {
78-
79-
})
80-
.retryWhen(retrySpec())
81-
.block();
87+
@TestConfiguration
88+
static class TestConfig {
8289

83-
DefaultKubernetesServiceInstance withCustomLabel = serviceInstances.stream()
84-
.filter(x -> x.podMetadata().getOrDefault("annotations", Map.of()).isEmpty())
85-
.toList()
86-
.get(0);
87-
Assertions.assertEquals(withCustomLabel.getServiceId(), "busybox-service");
88-
Assertions.assertNotNull(withCustomLabel.getInstanceId());
89-
Assertions.assertNotNull(withCustomLabel.getHost());
90-
Assertions.assertEquals(withCustomLabel.getMetadata(),
91-
Map.of("k8s_namespace", "default", "type", "ClusterIP", "port.busybox-port", "80"));
92-
Assertions.assertTrue(withCustomLabel.podMetadata()
93-
.get("labels")
94-
.entrySet()
95-
.stream()
96-
.anyMatch(x -> x.getKey().equals("custom-label") && x.getValue().equals("custom-label-value")));
90+
@Bean
91+
@Primary
92+
KubernetesClient kubernetesClient() {
93+
return client();
94+
}
9795

98-
DefaultKubernetesServiceInstance withCustomAnnotation = serviceInstances.stream()
99-
.filter(x -> !x.podMetadata().getOrDefault("annotations", Map.of()).isEmpty())
100-
.toList()
101-
.get(0);
102-
Assertions.assertEquals(withCustomAnnotation.getServiceId(), "busybox-service");
103-
Assertions.assertNotNull(withCustomAnnotation.getInstanceId());
104-
Assertions.assertNotNull(withCustomAnnotation.getHost());
105-
Assertions.assertEquals(withCustomAnnotation.getMetadata(),
106-
Map.of("k8s_namespace", "default", "type", "ClusterIP", "port.busybox-port", "80"));
107-
Assertions.assertTrue(withCustomAnnotation.podMetadata()
108-
.get("annotations")
109-
.entrySet()
110-
.stream()
111-
.anyMatch(x -> x.getKey().equals("custom-annotation") && x.getValue().equals("custom-annotation-value")));
11296
}
11397

11498
}

0 commit comments

Comments
 (0)