diff --git a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/EndpointSubsetNS.java b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/EndpointSubsetNS.java new file mode 100644 index 00000000..e9799622 --- /dev/null +++ b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/EndpointSubsetNS.java @@ -0,0 +1,61 @@ +/* + * Copyright 2012-2019 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.discovery; + +import java.util.ArrayList; +import java.util.List; + +import io.fabric8.kubernetes.api.model.EndpointSubset; + +/** + * @author Haytham Mohamed + **/ +public class EndpointSubsetNS { + + private String namespace; + + private List endpointSubset; + + public EndpointSubsetNS() { + endpointSubset = new ArrayList<>(); + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + public List getEndpointSubset() { + return endpointSubset; + } + + public void setEndpointSubset(List endpointSubset) { + this.endpointSubset = endpointSubset; + } + + public boolean equals(Object o) { + return this.endpointSubset.equals(o); + } + + public int hashCode() { + return this.endpointSubset.hashCode(); + } + +} diff --git a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java index 19db10a6..3aae79ff 100644 --- a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java +++ b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClient.java @@ -101,40 +101,39 @@ public class KubernetesDiscoveryClient implements DiscoveryClient { public List getInstances(String serviceId) { Assert.notNull(serviceId, "[Assertion failed] - the object argument must not be null"); + List endpointsList = this.properties.isAllNamespaces() ? this.client.endpoints().inAnyNamespace() .withField("metadata.name", serviceId).list().getItems() : Collections .singletonList(this.client.endpoints().withName(serviceId).get()); - List subsets = endpointsList.stream() - .flatMap(endpoints -> getSubsetsFromEndpoints(endpoints).stream()) + + List subsetsNS = endpointsList.stream() + .map(endpoints -> getSubsetsFromEndpoints(endpoints)) .collect(Collectors.toList()); + + List instances = new ArrayList<>(); + if (!subsetsNS.isEmpty()) { + for (EndpointSubsetNS es : subsetsNS) { + instances.addAll(this.getNamespaceServiceInstances(es, serviceId)); + } + } + + return instances; + } + + private List getNamespaceServiceInstances(EndpointSubsetNS es, + String serviceId) { + String namespace = es.getNamespace(); + List subsets = es.getEndpointSubset(); List instances = new ArrayList<>(); if (!subsets.isEmpty()) { - - final Service service = this.client.services().withName(serviceId).get(); - - final Map serviceMetadata = new HashMap<>(); + final Service service = this.client.services().inNamespace(namespace) + .withName(serviceId).get(); + final Map serviceMetadata = this.getServiceMetadata(namespace, + serviceId); KubernetesDiscoveryProperties.Metadata metadataProps = this.properties .getMetadata(); - if (metadataProps.isAddLabels()) { - Map labelMetadata = getMapWithPrefixedKeys( - service.getMetadata().getLabels(), - metadataProps.getLabelsPrefix()); - if (log.isDebugEnabled()) { - log.debug("Adding label metadata: " + labelMetadata); - } - serviceMetadata.putAll(labelMetadata); - } - if (metadataProps.isAddAnnotations()) { - Map annotationMetadata = getMapWithPrefixedKeys( - service.getMetadata().getAnnotations(), - metadataProps.getAnnotationsPrefix()); - if (log.isDebugEnabled()) { - log.debug("Adding annotation metadata: " + annotationMetadata); - } - serviceMetadata.putAll(annotationMetadata); - } for (EndpointSubset s : subsets) { // Extend the service metadata map with per-endpoint port information (if @@ -176,6 +175,34 @@ public class KubernetesDiscoveryClient implements DiscoveryClient { return instances; } + private Map getServiceMetadata(String namespace, String serviceId) { + final Service service = this.client.services().inNamespace(namespace) + .withName(serviceId).get(); + + final Map serviceMetadata = new HashMap<>(); + KubernetesDiscoveryProperties.Metadata metadataProps = this.properties + .getMetadata(); + if (metadataProps.isAddLabels()) { + Map labelMetadata = getMapWithPrefixedKeys( + service.getMetadata().getLabels(), metadataProps.getLabelsPrefix()); + if (log.isDebugEnabled()) { + log.debug("Adding label metadata: " + labelMetadata); + } + serviceMetadata.putAll(labelMetadata); + } + if (metadataProps.isAddAnnotations()) { + Map annotationMetadata = getMapWithPrefixedKeys( + service.getMetadata().getAnnotations(), + metadataProps.getAnnotationsPrefix()); + if (log.isDebugEnabled()) { + log.debug("Adding annotation metadata: " + annotationMetadata); + } + serviceMetadata.putAll(annotationMetadata); + } + + return serviceMetadata; + } + private EndpointPort findEndpointPort(EndpointSubset s) { List ports = s.getPorts(); EndpointPort endpointPort; @@ -197,15 +224,17 @@ public class KubernetesDiscoveryClient implements DiscoveryClient { return endpointPort; } - private List getSubsetsFromEndpoints(Endpoints endpoints) { - if (endpoints == null) { - return new ArrayList<>(); - } - if (endpoints.getSubsets() == null) { - return new ArrayList<>(); + private EndpointSubsetNS getSubsetsFromEndpoints(Endpoints endpoints) { + EndpointSubsetNS es = new EndpointSubsetNS(); + es.setNamespace(this.client.getNamespace()); // start with the default that comes + // with the client + + if (endpoints != null && endpoints.getSubsets() != null) { + es.setNamespace(endpoints.getMetadata().getNamespace()); + es.setEndpointSubset(endpoints.getSubsets()); } - return endpoints.getSubsets(); + return es; } // returns a new map that contain all the entries of the original map