|
| 1 | +/* |
| 2 | + * Copyright 2013-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.kubernetes.fabric8.loadbalancer; |
| 18 | + |
| 19 | +import java.util.Comparator; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import io.fabric8.kubernetes.api.model.Service; |
| 25 | +import io.fabric8.kubernetes.api.model.ServiceBuilder; |
| 26 | +import io.fabric8.kubernetes.api.model.ServicePortBuilder; |
| 27 | +import io.fabric8.kubernetes.api.model.ServiceSpecBuilder; |
| 28 | +import io.fabric8.kubernetes.client.Config; |
| 29 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 30 | +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; |
| 31 | +import org.junit.jupiter.api.AfterEach; |
| 32 | +import org.junit.jupiter.api.Assertions; |
| 33 | +import org.junit.jupiter.api.BeforeAll; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 36 | + |
| 37 | +import org.springframework.boot.test.system.CapturedOutput; |
| 38 | +import org.springframework.boot.test.system.OutputCaptureExtension; |
| 39 | +import org.springframework.cloud.client.ServiceInstance; |
| 40 | +import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; |
| 41 | +import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadBalancerProperties; |
| 42 | +import org.springframework.core.env.Environment; |
| 43 | +import org.springframework.mock.env.MockEnvironment; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author wind57 |
| 47 | + */ |
| 48 | +@EnableKubernetesMockClient(crud = true, https = false) |
| 49 | +@ExtendWith(OutputCaptureExtension.class) |
| 50 | +class Fabric8ServicesListSupplierMockClientTests { |
| 51 | + |
| 52 | + private static KubernetesClient mockClient; |
| 53 | + |
| 54 | + @BeforeAll |
| 55 | + static void setUpBeforeClass() { |
| 56 | + // Configure the kubernetes master url to point to the mock server |
| 57 | + System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); |
| 58 | + System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); |
| 59 | + System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false"); |
| 60 | + System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false"); |
| 61 | + System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test"); |
| 62 | + System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true"); |
| 63 | + } |
| 64 | + |
| 65 | + @AfterEach |
| 66 | + void afterEach() { |
| 67 | + mockClient.services().inAnyNamespace().delete(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testAllNamespaces(CapturedOutput output) { |
| 72 | + |
| 73 | + createService("a", "service-a", 8887); |
| 74 | + createService("b", "service-b", 8888); |
| 75 | + createService("c", "service-a", 8889); |
| 76 | + |
| 77 | + Environment environment = new MockEnvironment().withProperty("loadbalancer.client.name", "service-a"); |
| 78 | + boolean allNamespaces = true; |
| 79 | + Set<String> selectiveNamespaces = Set.of(); |
| 80 | + |
| 81 | + KubernetesLoadBalancerProperties loadBalancerProperties = new KubernetesLoadBalancerProperties(); |
| 82 | + KubernetesDiscoveryProperties discoveryProperties = new KubernetesDiscoveryProperties(true, allNamespaces, |
| 83 | + selectiveNamespaces, true, 60, false, null, Set.of(), Map.of(), null, |
| 84 | + KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false, false, null); |
| 85 | + |
| 86 | + Fabric8ServicesListSupplier supplier = new Fabric8ServicesListSupplier(environment, mockClient, |
| 87 | + new Fabric8ServiceInstanceMapper(loadBalancerProperties, discoveryProperties), discoveryProperties); |
| 88 | + |
| 89 | + List<List<ServiceInstance>> serviceInstances = supplier.get().collectList().block(); |
| 90 | + Assertions.assertEquals(serviceInstances.size(), 1); |
| 91 | + List<ServiceInstance> inner = serviceInstances.get(0); |
| 92 | + |
| 93 | + List<ServiceInstance> serviceInstancesSorted = serviceInstances.get(0).stream() |
| 94 | + .sorted(Comparator.comparing(ServiceInstance::getServiceId)).toList(); |
| 95 | + Assertions.assertEquals(serviceInstancesSorted.size(), 2); |
| 96 | + Assertions.assertEquals(inner.get(0).getServiceId(), "service-a"); |
| 97 | + Assertions.assertEquals(inner.get(0).getHost(), "service-a.a.svc.cluster.local"); |
| 98 | + Assertions.assertEquals(inner.get(0).getPort(), 8887); |
| 99 | + |
| 100 | + Assertions.assertEquals(inner.get(1).getServiceId(), "service-a"); |
| 101 | + Assertions.assertEquals(inner.get(1).getHost(), "service-a.c.svc.cluster.local"); |
| 102 | + Assertions.assertEquals(inner.get(1).getPort(), 8889); |
| 103 | + |
| 104 | + Assertions.assertTrue(output.getOut().contains("discovering services in all namespaces")); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void testOneNamespace(CapturedOutput output) { |
| 109 | + |
| 110 | + createService("a", "service-c", 8887); |
| 111 | + createService("b", "service-b", 8888); |
| 112 | + createService("c", "service-c", 8889); |
| 113 | + |
| 114 | + Environment environment = new MockEnvironment().withProperty("spring.cloud.kubernetes.client.namespace", "c") |
| 115 | + .withProperty("loadbalancer.client.name", "service-c"); |
| 116 | + boolean allNamespaces = false; |
| 117 | + Set<String> selectiveNamespaces = Set.of(); |
| 118 | + |
| 119 | + KubernetesLoadBalancerProperties loadBalancerProperties = new KubernetesLoadBalancerProperties(); |
| 120 | + KubernetesDiscoveryProperties discoveryProperties = new KubernetesDiscoveryProperties(true, allNamespaces, |
| 121 | + selectiveNamespaces, true, 60, false, null, Set.of(), Map.of(), null, |
| 122 | + KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false, false, null); |
| 123 | + |
| 124 | + Fabric8ServicesListSupplier supplier = new Fabric8ServicesListSupplier(environment, mockClient, |
| 125 | + new Fabric8ServiceInstanceMapper(loadBalancerProperties, discoveryProperties), discoveryProperties); |
| 126 | + |
| 127 | + List<List<ServiceInstance>> serviceInstances = supplier.get().collectList().block(); |
| 128 | + Assertions.assertEquals(serviceInstances.size(), 1); |
| 129 | + List<ServiceInstance> inner = serviceInstances.get(0); |
| 130 | + |
| 131 | + List<ServiceInstance> serviceInstancesSorted = serviceInstances.get(0).stream() |
| 132 | + .sorted(Comparator.comparing(ServiceInstance::getServiceId)).toList(); |
| 133 | + Assertions.assertEquals(serviceInstancesSorted.size(), 1); |
| 134 | + Assertions.assertEquals(inner.get(0).getServiceId(), "service-c"); |
| 135 | + Assertions.assertEquals(inner.get(0).getHost(), "service-c.c.svc.cluster.local"); |
| 136 | + Assertions.assertEquals(inner.get(0).getPort(), 8889); |
| 137 | + |
| 138 | + Assertions.assertTrue(output.getOut().contains("discovering services in namespace : c")); |
| 139 | + } |
| 140 | + |
| 141 | + private void createService(String namespace, String name, int port) { |
| 142 | + Service service = new ServiceBuilder().withNewMetadata().withNamespace(namespace).withName(name).endMetadata() |
| 143 | + .withSpec(new ServiceSpecBuilder() |
| 144 | + .withPorts(new ServicePortBuilder().withName("http").withPort(port).build()).build()) |
| 145 | + .build(); |
| 146 | + mockClient.services().resource(service).create(); |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments