Add namespace to service instance result (#1238)
This commit is contained in:
@@ -31,13 +31,13 @@ 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.discovery.DefaultKubernetesServiceInstance;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
|
||||
import org.springframework.core.log.LogAccessor;
|
||||
|
||||
import static org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClientUtils.addresses;
|
||||
import static org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClientUtils.endpoints;
|
||||
import static org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClientUtils.endpointsPort;
|
||||
import static org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClientUtils.serviceInstance;
|
||||
import static org.springframework.cloud.kubernetes.fabric8.discovery.KubernetesDiscoveryClientUtils.serviceMetadata;
|
||||
|
||||
/**
|
||||
@@ -146,17 +146,9 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
int endpointPort = endpointsPort(endpointSubset, serviceId, properties, service);
|
||||
List<EndpointAddress> addresses = addresses(endpointSubset, properties);
|
||||
for (EndpointAddress endpointAddress : addresses) {
|
||||
|
||||
String instanceId = null;
|
||||
if (endpointAddress.getTargetRef() != null) {
|
||||
instanceId = endpointAddress.getTargetRef().getUid();
|
||||
}
|
||||
instances
|
||||
.add(new DefaultKubernetesServiceInstance(instanceId, serviceId, endpointAddress.getIp(),
|
||||
endpointPort, serviceMetadata,
|
||||
servicePortSecureResolver.resolve(new ServicePortSecureResolver.Input(endpointPort,
|
||||
service.getMetadata().getName(), service.getMetadata().getLabels(),
|
||||
service.getMetadata().getAnnotations()))));
|
||||
ServiceInstance serviceInstance = serviceInstance(servicePortSecureResolver, service, endpointAddress,
|
||||
endpointPort, serviceId, serviceMetadata, namespace);
|
||||
instances.add(serviceInstance);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,12 +28,15 @@ import io.fabric8.kubernetes.api.model.EndpointPort;
|
||||
import io.fabric8.kubernetes.api.model.EndpointSubset;
|
||||
import io.fabric8.kubernetes.api.model.Endpoints;
|
||||
import io.fabric8.kubernetes.api.model.EndpointsList;
|
||||
import io.fabric8.kubernetes.api.model.ObjectReference;
|
||||
import io.fabric8.kubernetes.api.model.Service;
|
||||
import io.fabric8.kubernetes.client.dsl.FilterNested;
|
||||
import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable;
|
||||
import io.fabric8.kubernetes.client.dsl.Resource;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
|
||||
import org.springframework.core.log.LogAccessor;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -182,6 +185,21 @@ final class KubernetesDiscoveryClientUtils {
|
||||
return addresses;
|
||||
}
|
||||
|
||||
static ServiceInstance serviceInstance(ServicePortSecureResolver servicePortSecureResolver, Service service,
|
||||
EndpointAddress endpointAddress, int endpointPort, String serviceId, Map<String, String> serviceMetadata,
|
||||
String namespace) {
|
||||
// instanceId is usually the pod-uid as seen in the .metadata.uid
|
||||
String instanceId = Optional.ofNullable(endpointAddress.getTargetRef()).map(ObjectReference::getUid)
|
||||
.orElse(null);
|
||||
|
||||
boolean secured = servicePortSecureResolver
|
||||
.resolve(new ServicePortSecureResolver.Input(endpointPort, service.getMetadata().getName(),
|
||||
service.getMetadata().getLabels(), service.getMetadata().getAnnotations()));
|
||||
|
||||
return new DefaultKubernetesServiceInstance(instanceId, serviceId, endpointAddress.getIp(), endpointPort,
|
||||
serviceMetadata, secured, namespace, null);
|
||||
}
|
||||
|
||||
private static Optional<Integer> fromMap(Map<String, Integer> existingPorts, String key, String message) {
|
||||
Integer fromPrimaryPortName = existingPorts.get(key);
|
||||
if (fromPrimaryPortName == null) {
|
||||
|
||||
@@ -38,6 +38,8 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.boot.test.system.CapturedOutput;
|
||||
import org.springframework.boot.test.system.OutputCaptureExtension;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
|
||||
|
||||
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.PRIMARY_PORT_NAME_LABEL_KEY;
|
||||
@@ -679,6 +681,31 @@ class KubernetesDiscoveryClientUtilsTests {
|
||||
Assertions.assertEquals(hostNames, List.of("one", "three", "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testServiceInstance() {
|
||||
KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60L,
|
||||
false, "", Set.of(), Map.of(), "", null, 0, false);
|
||||
ServicePortSecureResolver resolver = new ServicePortSecureResolver(properties);
|
||||
Service service = new ServiceBuilder().withMetadata(new ObjectMeta()).build();
|
||||
EndpointAddress address = new EndpointAddressBuilder().withNewTargetRef().withUid("123").endTargetRef()
|
||||
.withIp("127.0.0.1").build();
|
||||
|
||||
ServiceInstance serviceInstance = KubernetesDiscoveryClientUtils.serviceInstance(resolver, service, address,
|
||||
8080, "my-service", Map.of("a", "b"), "k8s");
|
||||
Assertions.assertTrue(serviceInstance instanceof DefaultKubernetesServiceInstance);
|
||||
DefaultKubernetesServiceInstance defaultInstance = (DefaultKubernetesServiceInstance) serviceInstance;
|
||||
Assertions.assertEquals(defaultInstance.getInstanceId(), "123");
|
||||
Assertions.assertEquals(defaultInstance.getServiceId(), "my-service");
|
||||
Assertions.assertEquals(defaultInstance.getHost(), "127.0.0.1");
|
||||
Assertions.assertEquals(defaultInstance.getPort(), 8080);
|
||||
Assertions.assertFalse(defaultInstance.isSecure());
|
||||
Assertions.assertEquals(defaultInstance.getUri().toASCIIString(), "http://127.0.0.1:8080");
|
||||
Assertions.assertEquals(defaultInstance.getMetadata(), Map.of("a", "b"));
|
||||
Assertions.assertEquals(defaultInstance.getScheme(), "http");
|
||||
Assertions.assertEquals(defaultInstance.getNamespace(), "k8s");
|
||||
Assertions.assertNull(defaultInstance.getCluster());
|
||||
}
|
||||
|
||||
private String filterOnK8sNamespace(Map<String, String> result) {
|
||||
return result.entrySet().stream().filter(en -> !en.getKey().contains("k8s_namespace"))
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)).toString();
|
||||
|
||||
Reference in New Issue
Block a user