1919import java .util .List ;
2020import java .util .Map ;
2121
22+ import io .fabric8 .kubernetes .client .KubernetesClient ;
2223import org .junit .jupiter .api .AfterEach ;
2324import org .junit .jupiter .api .Assertions ;
2425import org .junit .jupiter .api .BeforeEach ;
2526import org .junit .jupiter .api .Test ;
2627
2728import org .springframework .boot .test .context .SpringBootTest ;
29+ import org .springframework .boot .test .context .TestConfiguration ;
2830import org .springframework .boot .test .web .server .LocalServerPort ;
2931import org .springframework .cloud .kubernetes .commons .discovery .DefaultKubernetesServiceInstance ;
3032import org .springframework .cloud .kubernetes .fabric8 .client .discovery .Fabric8DiscoveryApp ;
3133import org .springframework .cloud .kubernetes .integration .tests .commons .Images ;
3234import org .springframework .cloud .kubernetes .integration .tests .commons .Phase ;
35+ import org .springframework .context .annotation .Bean ;
36+ import org .springframework .context .annotation .Primary ;
3337import org .springframework .core .ParameterizedTypeReference ;
3438import org .springframework .http .HttpMethod ;
3539import org .springframework .web .reactive .function .client .WebClient ;
3640
3741import static org .springframework .cloud .kubernetes .fabric8 .client .discovery .it .TestAssertions .builder ;
3842import 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 )
4550class 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