diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClient.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClient.java index 2e2b7482..490c3ce7 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClient.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClient.java @@ -31,7 +31,11 @@ import org.apache.commons.logging.LogFactory; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; +import org.springframework.cloud.kubernetes.fabric8.Fabric8Utils; +import org.springframework.context.EnvironmentAware; +import org.springframework.core.env.Environment; import org.springframework.core.log.LogAccessor; import static org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClientUtils.addresses; @@ -46,7 +50,7 @@ import static org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesD * @author Ioannis Canellos * @author Tim Ysewyn */ -public class KubernetesDiscoveryClient implements DiscoveryClient { +public class KubernetesDiscoveryClient implements DiscoveryClient, EnvironmentAware { private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(KubernetesDiscoveryClient.class)); @@ -60,6 +64,8 @@ public class KubernetesDiscoveryClient implements DiscoveryClient { private KubernetesClient client; + private KubernetesNamespaceProvider namespaceProvider; + public KubernetesDiscoveryClient(KubernetesClient client, KubernetesDiscoveryProperties kubernetesDiscoveryProperties, KubernetesClientServicesFunction kubernetesClientServicesFunction) { @@ -114,8 +120,9 @@ public class KubernetesDiscoveryClient implements DiscoveryClient { return endpoints(client.endpoints().inAnyNamespace().withNewFilter(), properties, serviceId); } else if (properties.namespaces().isEmpty()) { - LOG.debug(() -> "searching for endpoints in namespace : " + client.getNamespace()); - return endpoints(client.endpoints().withNewFilter(), properties, serviceId); + String namespace = Fabric8Utils.getApplicationNamespace(client, null, "discovery", namespaceProvider); + LOG.debug(() -> "searching for endpoints in namespace : " + namespace); + return endpoints(client.endpoints().inNamespace(namespace).withNewFilter(), properties, serviceId); } else { LOG.debug(() -> "searching for endpoints in namespaces : " + properties.namespaces()); @@ -171,4 +178,9 @@ public class KubernetesDiscoveryClient implements DiscoveryClient { return properties.order(); } + @Deprecated(forRemoval = true) + @Override + public final void setEnvironment(Environment environment) { + namespaceProvider = new KubernetesNamespaceProvider(environment); + } } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientFilterMetadataTest.java b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientFilterMetadataTest.java index a18f6858..0e1390e6 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientFilterMetadataTest.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/test/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClientFilterMetadataTest.java @@ -44,6 +44,8 @@ import org.mockito.Mockito; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; +import org.springframework.core.env.Environment; +import org.springframework.mock.env.MockEnvironment; import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; @@ -82,6 +84,7 @@ class KubernetesDiscoveryClientFilterMetadataTest { false, null, Set.of(), Map.of(), null, metadata, 0, true); KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); + discoveryClient.setEnvironment(withClientNamespace()); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "lab"), Map.of("l1", "lab"), Map.of(80, "http", 5555, "")); @@ -100,6 +103,7 @@ class KubernetesDiscoveryClientFilterMetadataTest { false, null, Set.of(), Map.of(), null, metadata, 0, true); KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); + discoveryClient.setEnvironment(withClientNamespace()); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1", "l2", "v2"), Map.of("l1", "lab"), Map.of(80, "http", 5555, "")); @@ -119,6 +123,7 @@ class KubernetesDiscoveryClientFilterMetadataTest { false, null, Set.of(), Map.of(), null, metadata, 0, true); KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); + discoveryClient.setEnvironment(withClientNamespace()); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1", "l2", "v2"), Map.of("l1", "lab"), Map.of(80, "http", 5555, "")); @@ -138,6 +143,7 @@ class KubernetesDiscoveryClientFilterMetadataTest { false, null, Set.of(), Map.of(), null, metadata, 0, true); KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); + discoveryClient.setEnvironment(withClientNamespace()); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1"), Map.of("a1", "v1", "a2", "v2"), Map.of(80, "http", 5555, "")); @@ -157,6 +163,7 @@ class KubernetesDiscoveryClientFilterMetadataTest { false, null, Set.of(), Map.of(), null, metadata, 0, true); KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); + discoveryClient.setEnvironment(withClientNamespace()); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1"), Map.of("a1", "v1", "a2", "v2"), Map.of(80, "http", 5555, "")); @@ -176,13 +183,14 @@ class KubernetesDiscoveryClientFilterMetadataTest { false, null, Set.of(), Map.of(), null, metadata, 0, true); KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); + discoveryClient.setEnvironment(withClientNamespace()); - setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1"), + setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "test", Map.of("l1", "v1"), Map.of("a1", "v1", "a2", "v2"), Map.of(80, "http", 5555, "")); List instances = discoveryClient.getInstances(serviceId); assertThat(instances).hasSize(1); - assertThat(instances.get(0).getMetadata()).containsOnly(entry("http", "80"), entry("k8s_namespace", "ns")); + assertThat(instances.get(0).getMetadata()).containsOnly(entry("http", "80"), entry("k8s_namespace", "test")); } @Test @@ -194,6 +202,7 @@ class KubernetesDiscoveryClientFilterMetadataTest { false, null, Set.of(), Map.of(), null, metadata, 0, true); KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); + discoveryClient.setEnvironment(withClientNamespace()); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "v1"), Map.of("a1", "v1", "a2", "v2"), Map.of(80, "http", 5555, "")); @@ -212,6 +221,7 @@ class KubernetesDiscoveryClientFilterMetadataTest { false, null, Set.of(), Map.of(), null, metadata, 0, true); KubernetesDiscoveryClient discoveryClient = new KubernetesDiscoveryClient(CLIENT, properties, a -> null); + discoveryClient.setEnvironment(withClientNamespace()); setupServiceWithLabelsAndAnnotationsAndPorts(serviceId, "ns", Map.of("l1", "la1"), Map.of("a1", "an1", "a2", "an2"), Map.of(80, "http", 5555, "")); @@ -239,6 +249,7 @@ class KubernetesDiscoveryClientFilterMetadataTest { .addAllToPorts(getEndpointPorts(ports)).addNewAddress().endAddress().endSubset().build(); when(CLIENT.endpoints()).thenReturn(endpointsOperation); + when(endpointsOperation.inNamespace(Mockito.anyString())).thenReturn(endpointsOperation); when(endpointsOperation.withNewFilter()).thenReturn(filterNested); EndpointsList endpointsList = new EndpointsList(null, Collections.singletonList(endpoints), null, null); @@ -272,4 +283,10 @@ class KubernetesDiscoveryClientFilterMetadataTest { }).collect(toList()); } + private static Environment withClientNamespace() { + MockEnvironment mockEnvironment = new MockEnvironment(); + mockEnvironment.setProperty("spring.cloud.kubernetes.client.namespace", "test"); + return mockEnvironment; + } + }