obtain service and metadata per namespace fixes gh-465

This commit is contained in:
Haytham Mohamed
2019-09-12 11:19:07 -05:00
parent 03f6a61374
commit bfd6772705
2 changed files with 121 additions and 31 deletions

View File

@@ -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> endpointSubset;
public EndpointSubsetNS() {
endpointSubset = new ArrayList<>();
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public List<EndpointSubset> getEndpointSubset() {
return endpointSubset;
}
public void setEndpointSubset(List<EndpointSubset> endpointSubset) {
this.endpointSubset = endpointSubset;
}
public boolean equals(Object o) {
return this.endpointSubset.equals(o);
}
public int hashCode() {
return this.endpointSubset.hashCode();
}
}

View File

@@ -101,40 +101,39 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
public List<ServiceInstance> getInstances(String serviceId) {
Assert.notNull(serviceId,
"[Assertion failed] - the object argument must not be null");
List<Endpoints> endpointsList = this.properties.isAllNamespaces()
? this.client.endpoints().inAnyNamespace()
.withField("metadata.name", serviceId).list().getItems()
: Collections
.singletonList(this.client.endpoints().withName(serviceId).get());
List<EndpointSubset> subsets = endpointsList.stream()
.flatMap(endpoints -> getSubsetsFromEndpoints(endpoints).stream())
List<EndpointSubsetNS> subsetsNS = endpointsList.stream()
.map(endpoints -> getSubsetsFromEndpoints(endpoints))
.collect(Collectors.toList());
List<ServiceInstance> instances = new ArrayList<>();
if (!subsetsNS.isEmpty()) {
for (EndpointSubsetNS es : subsetsNS) {
instances.addAll(this.getNamespaceServiceInstances(es, serviceId));
}
}
return instances;
}
private List<ServiceInstance> getNamespaceServiceInstances(EndpointSubsetNS es,
String serviceId) {
String namespace = es.getNamespace();
List<EndpointSubset> subsets = es.getEndpointSubset();
List<ServiceInstance> instances = new ArrayList<>();
if (!subsets.isEmpty()) {
final Service service = this.client.services().withName(serviceId).get();
final Map<String, String> serviceMetadata = new HashMap<>();
final Service service = this.client.services().inNamespace(namespace)
.withName(serviceId).get();
final Map<String, String> serviceMetadata = this.getServiceMetadata(namespace,
serviceId);
KubernetesDiscoveryProperties.Metadata metadataProps = this.properties
.getMetadata();
if (metadataProps.isAddLabels()) {
Map<String, String> labelMetadata = getMapWithPrefixedKeys(
service.getMetadata().getLabels(),
metadataProps.getLabelsPrefix());
if (log.isDebugEnabled()) {
log.debug("Adding label metadata: " + labelMetadata);
}
serviceMetadata.putAll(labelMetadata);
}
if (metadataProps.isAddAnnotations()) {
Map<String, String> 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<String, String> getServiceMetadata(String namespace, String serviceId) {
final Service service = this.client.services().inNamespace(namespace)
.withName(serviceId).get();
final Map<String, String> serviceMetadata = new HashMap<>();
KubernetesDiscoveryProperties.Metadata metadataProps = this.properties
.getMetadata();
if (metadataProps.isAddLabels()) {
Map<String, String> labelMetadata = getMapWithPrefixedKeys(
service.getMetadata().getLabels(), metadataProps.getLabelsPrefix());
if (log.isDebugEnabled()) {
log.debug("Adding label metadata: " + labelMetadata);
}
serviceMetadata.putAll(labelMetadata);
}
if (metadataProps.isAddAnnotations()) {
Map<String, String> 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<EndpointPort> ports = s.getPorts();
EndpointPort endpointPort;
@@ -197,15 +224,17 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
return endpointPort;
}
private List<EndpointSubset> 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