Discovery refactor 6 (#1091)

This commit is contained in:
erabii
2022-09-29 02:50:38 +03:00
committed by GitHub
parent cae442fde5
commit 24f768cde1
16 changed files with 248 additions and 350 deletions

View File

@@ -39,8 +39,8 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
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.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
@@ -159,7 +159,7 @@ public class KubernetesInformerDiscoveryClient implements DiscoveryClient, Initi
final int port = findEndpointPort(endpointPorts, primaryPortName, serviceId);
return addresses.stream()
.map(addr -> new KubernetesServiceInstance(
.map(addr -> new DefaultKubernetesServiceInstance(
addr.getTargetRef() != null ? addr.getTargetRef().getUid() : "", serviceId,
addr.getIp(), port, metadata, false, service.getMetadata().getNamespace(),
service.getMetadata().getClusterName()));

View File

@@ -35,8 +35,8 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
@@ -151,8 +151,9 @@ public class KubernetesInformerDiscoveryClientTests {
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
assertThat(discoveryClient.getInstances("test-svc-1").toArray()).isEmpty();
assertThat(discoveryClient.getInstances("test-svc-3").toArray()).containsOnly(new KubernetesServiceInstance("",
"test-svc-3", "2.2.2.2", 8080, new HashMap<>(), false, "namespace1", null));
assertThat(discoveryClient.getInstances("test-svc-3").toArray())
.containsOnly(new DefaultKubernetesServiceInstance("", "test-svc-3", "2.2.2.2", 8080, new HashMap<>(),
false, "namespace1", null));
}
@Test
@@ -179,7 +180,7 @@ public class KubernetesInformerDiscoveryClientTests {
KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new KubernetesServiceInstance("",
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new DefaultKubernetesServiceInstance("",
"test-svc-1", "2.2.2.2", 8080, new HashMap<>(), false, "namespace1", null));
verify(kubernetesDiscoveryProperties, times(2)).isAllNamespaces();
@@ -196,7 +197,7 @@ public class KubernetesInformerDiscoveryClientTests {
KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new KubernetesServiceInstance("",
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new DefaultKubernetesServiceInstance("",
"test-svc-1", "2.2.2.2", 8080, new HashMap<>(), false, "namespace1", null));
verify(kubernetesDiscoveryProperties, times(1)).isAllNamespaces();
verify(kubernetesDiscoveryProperties, times(1)).getPrimaryPortName();
@@ -229,7 +230,7 @@ public class KubernetesInformerDiscoveryClientTests {
KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new KubernetesServiceInstance("",
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new DefaultKubernetesServiceInstance("",
"test-svc-1", "2.2.2.2", 8080, new HashMap<>(), false, "namespace1", null));
verify(kubernetesDiscoveryProperties, times(1)).isAllNamespaces();
verify(kubernetesDiscoveryProperties, times(1)).getPrimaryPortName();
@@ -275,7 +276,7 @@ public class KubernetesInformerDiscoveryClientTests {
KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new KubernetesServiceInstance("",
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new DefaultKubernetesServiceInstance("",
"test-svc-1", "1.1.1.1", 443, new HashMap<>(), false, "namespace1", null));
verify(kubernetesDiscoveryProperties, times(1)).isAllNamespaces();
verify(kubernetesDiscoveryProperties, times(1)).getPrimaryPortName();
@@ -294,7 +295,7 @@ public class KubernetesInformerDiscoveryClientTests {
KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new KubernetesServiceInstance("",
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new DefaultKubernetesServiceInstance("",
"test-svc-1", "1.1.1.1", 80, new HashMap<>(), false, "namespace1", null));
verify(kubernetesDiscoveryProperties, times(1)).isAllNamespaces();
verify(kubernetesDiscoveryProperties, times(1)).getPrimaryPortName();
@@ -311,7 +312,7 @@ public class KubernetesInformerDiscoveryClientTests {
KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new KubernetesServiceInstance("",
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new DefaultKubernetesServiceInstance("",
"test-svc-1", "1.1.1.1", 443, new HashMap<>(), false, "namespace1", null));
verify(kubernetesDiscoveryProperties, times(1)).getPrimaryPortName();
verify(kubernetesDiscoveryProperties, times(1)).isAllNamespaces();
@@ -330,7 +331,7 @@ public class KubernetesInformerDiscoveryClientTests {
KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new KubernetesServiceInstance("",
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new DefaultKubernetesServiceInstance("",
"test-svc-1", "1.1.1.1", 80, new HashMap<>(), false, "namespace1", null));
verify(kubernetesDiscoveryProperties, times(1)).isAllNamespaces();
verify(kubernetesDiscoveryProperties, times(1)).getPrimaryPortName();
@@ -346,7 +347,7 @@ public class KubernetesInformerDiscoveryClientTests {
KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new KubernetesServiceInstance("",
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new DefaultKubernetesServiceInstance("",
"test-svc-1", "1.1.1.1", 443, new HashMap<>(), false, "namespace1", null));
verify(kubernetesDiscoveryProperties, times(1)).isAllNamespaces();
verify(kubernetesDiscoveryProperties, times(1)).getPrimaryPortName();
@@ -362,7 +363,7 @@ public class KubernetesInformerDiscoveryClientTests {
KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new KubernetesServiceInstance("",
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new DefaultKubernetesServiceInstance("",
"test-svc-1", "1.1.1.1", 80, new HashMap<>(), false, "namespace1", null));
verify(kubernetesDiscoveryProperties, times(1)).isAllNamespaces();
verify(kubernetesDiscoveryProperties, times(1)).getPrimaryPortName();
@@ -379,7 +380,7 @@ public class KubernetesInformerDiscoveryClientTests {
KubernetesInformerDiscoveryClient discoveryClient = new KubernetesInformerDiscoveryClient("namespace1",
sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new KubernetesServiceInstance("",
assertThat(discoveryClient.getInstances("test-svc-1")).containsOnly(new DefaultKubernetesServiceInstance("",
"test-svc-1", "1.1.1.1", 80, new HashMap<>(), false, "namespace1", null));
verify(kubernetesDiscoveryProperties, times(1)).isAllNamespaces();
verify(kubernetesDiscoveryProperties, times(1)).getPrimaryPortName();

View File

@@ -1,52 +0,0 @@
/*
* Copyright 2013-2020 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.client.discovery;
import java.util.Collections;
import org.junit.Test;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import static org.assertj.core.api.Assertions.assertThat;
public class KubernetesServiceInstanceTests {
@Test
public void schemeIsHttp() {
assertServiceInstance(false);
}
private KubernetesServiceInstance assertServiceInstance(boolean secure) {
KubernetesServiceInstance instance = new KubernetesServiceInstance("123", "myservice", "1.2.3.4", 8080,
Collections.emptyMap(), secure);
assertThat(instance.getInstanceId()).isEqualTo("123");
assertThat(instance.getServiceId()).isEqualTo("myservice");
assertThat(instance.getHost()).isEqualTo("1.2.3.4");
assertThat(instance.getPort()).isEqualTo(8080);
assertThat(instance.isSecure()).isEqualTo(secure);
assertThat(instance.getScheme()).isEqualTo(secure ? "https" : "http");
return instance;
}
@Test
public void schemeIsHttps() {
assertServiceInstance(true);
}
}

View File

@@ -36,8 +36,8 @@ import org.mockito.junit.MockitoJUnitRunner;
import reactor.test.StepVerifier;
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import org.springframework.mock.env.MockEnvironment;
import static org.mockito.Mockito.mock;
@@ -117,7 +117,7 @@ public class KubernetesInformerReactiveDiscoveryClientTests {
endpointsLister, null, null, kubernetesDiscoveryProperties);
StepVerifier
.create(discoveryClient.getInstances("test-svc-1")).expectNext(new KubernetesServiceInstance("",
.create(discoveryClient.getInstances("test-svc-1")).expectNext(new DefaultKubernetesServiceInstance("",
"test-svc-1", "2.2.2.2", 8080, new HashMap<>(), false, "namespace1", null))
.expectComplete().verify();
@@ -137,7 +137,7 @@ public class KubernetesInformerReactiveDiscoveryClientTests {
kubernetesDiscoveryProperties);
StepVerifier
.create(discoveryClient.getInstances("test-svc-1")).expectNext(new KubernetesServiceInstance("",
.create(discoveryClient.getInstances("test-svc-1")).expectNext(new DefaultKubernetesServiceInstance("",
"test-svc-1", "2.2.2.2", 8080, new HashMap<>(), false, "namespace1", null))
.expectComplete().verify();

View File

@@ -25,6 +25,7 @@ import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1Service;
import io.kubernetes.client.openapi.models.V1ServicePort;
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadBalancerProperties;
@@ -69,7 +70,7 @@ public class KubernetesClientServiceInstanceMapper implements KubernetesServiceI
service.getMetadata().getNamespace(), properties.getClusterDomain());
final boolean secure = KubernetesServiceInstanceMapper.isSecure(service.getMetadata().getLabels(),
service.getMetadata().getAnnotations(), port.getName(), port.getPort());
return new KubernetesServiceInstance(meta.getUid(), meta.getName(), host, port.getPort(),
return new DefaultKubernetesServiceInstance(meta.getUid(), meta.getName(), host, port.getPort(),
getServiceMetadata(service), secure);
}

View File

@@ -26,6 +26,7 @@ import io.kubernetes.client.openapi.models.V1ServicePortBuilder;
import io.kubernetes.client.openapi.models.V1ServiceSpecBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadBalancerProperties;
@@ -56,7 +57,7 @@ class KubernetesClientServiceInstanceMapperTests {
Map<String, String> metadata = new HashMap<>();
metadata.put("org.springframework.cloud", "true");
metadata.put("beta", "true");
KubernetesServiceInstance result = new KubernetesServiceInstance("0", "database",
DefaultKubernetesServiceInstance result = new DefaultKubernetesServiceInstance("0", "database",
"database.default.svc.cluster.local", 80, metadata, false);
assertThat(serviceInstance).isEqualTo(result);
}
@@ -79,7 +80,7 @@ class KubernetesClientServiceInstanceMapperTests {
.build();
KubernetesServiceInstance serviceInstance = mapper.map(service);
KubernetesServiceInstance result = new KubernetesServiceInstance("0", "database",
DefaultKubernetesServiceInstance result = new DefaultKubernetesServiceInstance("0", "database",
"database.default.svc.cluster.local", 443, new HashMap(), true);
assertThat(serviceInstance).isEqualTo(result);
}

View File

@@ -42,8 +42,8 @@ import reactor.test.StepVerifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadBalancerProperties;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.mock.env.MockEnvironment;
@@ -131,7 +131,7 @@ class KubernetesClientServicesListSupplierTests {
Map<String, String> metadata = new HashMap<>();
metadata.put("org.springframework.cloud", "true");
metadata.put("beta", "true");
KubernetesServiceInstance service1 = new KubernetesServiceInstance("0", "service1",
DefaultKubernetesServiceInstance service1 = new DefaultKubernetesServiceInstance("0", "service1",
"service1.default.svc.cluster.local", 80, metadata, false);
List<ServiceInstance> services = new ArrayList<>();
services.add(service1);
@@ -161,9 +161,9 @@ class KubernetesClientServicesListSupplierTests {
Map<String, String> metadata = new HashMap<>();
metadata.put("org.springframework.cloud", "true");
metadata.put("beta", "true");
KubernetesServiceInstance service1 = new KubernetesServiceInstance("0", "service1",
DefaultKubernetesServiceInstance service1 = new DefaultKubernetesServiceInstance("0", "service1",
"service1.default.svc.cluster.local", 80, metadata, false);
KubernetesServiceInstance service2 = new KubernetesServiceInstance("1", "service1",
DefaultKubernetesServiceInstance service2 = new DefaultKubernetesServiceInstance("1", "service1",
"service1.test.svc.cluster.local", 80, new HashMap<>(), false);
List<ServiceInstance> services = new ArrayList<>();
services.add(service1);

View File

@@ -0,0 +1,107 @@
/*
* Copyright 2013-2022 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.commons.discovery;
import java.net.URI;
import java.util.Map;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTP;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTPS;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.NAMESPACE_METADATA_KEY;
/**
* @author wind57
* @param instanceId the id of the instance.
* @param serviceId the id of the service.
* @param host the address where the service instance can be found.
* @param port the port on which the service is running.
* @param metadata a map containing metadata.
* @param secure indicates whether the connection needs to be secure.
* @param namespace the namespace of the service.
* @param cluster the cluster the service resides in.
*/
public record DefaultKubernetesServiceInstance(String instanceId, String serviceId, String host, int port,
Map<String, String> metadata, boolean secure, String namespace,
String cluster) implements KubernetesServiceInstance {
/**
* @param instanceId the id of the instance.
* @param serviceId the id of the service.
* @param host the address where the service instance can be found.
* @param port the port on which the service is running.
* @param metadata a map containing metadata.
* @param secure indicates whether the connection needs to be secure.
*/
public DefaultKubernetesServiceInstance(String instanceId, String serviceId, String host, int port,
Map<String, String> metadata, boolean secure) {
this(instanceId, serviceId, host, port, metadata, secure, null, null);
}
@Override
public String getInstanceId() {
return this.instanceId;
}
@Override
public String getServiceId() {
return serviceId;
}
@Override
public String getHost() {
return host;
}
@Override
public int getPort() {
return port;
}
@Override
public boolean isSecure() {
return secure;
}
@Override
public URI getUri() {
return createUri(secure ? HTTPS : HTTP, host, port);
}
@Override
public Map<String, String> getMetadata() {
return metadata;
}
@Override
public String getScheme() {
return isSecure() ? HTTPS : HTTP;
}
@Override
public String getNamespace() {
return namespace != null ? namespace : this.metadata.get(NAMESPACE_METADATA_KEY);
}
@Override
public String getCluster() {
return this.cluster;
}
private URI createUri(String scheme, String host, int port) {
return URI.create(scheme + "://" + host + ":" + port);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2019-2022 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.
@@ -16,211 +16,17 @@
package org.springframework.cloud.kubernetes.commons.discovery;
import java.net.URI;
import java.util.Map;
import java.util.Objects;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.core.style.ToStringCreator;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTP;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTPS;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.NAMESPACE_METADATA_KEY;
/**
* @author wind57
*
* {@link ServiceInstance} with additional methods, specific to kubernetes.
*/
sealed public interface KubernetesServiceInstance extends ServiceInstance permits DefaultKubernetesServiceInstance {
public final class KubernetesServiceInstance implements ServiceInstance {
String getNamespace();
private String instanceId;
private String serviceId;
private String host;
private int port;
private URI uri;
private Boolean secure;
private Map<String, String> metadata;
private String namespace;
private String cluster;
/**
* @param instanceId the id of the instance.
* @param serviceId the id of the service.
* @param host the address where the service instance can be found.
* @param port the port on which the service is running.
* @param metadata a map containing metadata.
* @param secure indicates whether the connection needs to be secure.
*/
public KubernetesServiceInstance(String instanceId, String serviceId, String host, int port,
Map<String, String> metadata, Boolean secure) {
this.instanceId = instanceId;
this.serviceId = serviceId;
this.host = host;
this.port = port;
this.metadata = metadata;
this.secure = secure;
this.uri = createUri(secure ? HTTPS : HTTP, host, port);
this.namespace = null;
this.cluster = null;
}
/**
* @param instanceId the id of the instance.
* @param serviceId the id of the service.
* @param host the address where the service instance can be found.
* @param port the port on which the service is running.
* @param metadata a map containing metadata.
* @param secure indicates whether the connection needs to be secure.
* @param namespace the namespace of the service.
* @param cluster the cluster the service resides in.
*/
public KubernetesServiceInstance(String instanceId, String serviceId, String host, int port,
Map<String, String> metadata, Boolean secure, String namespace, String cluster) {
this.instanceId = instanceId;
this.serviceId = serviceId;
this.host = host;
this.port = port;
this.metadata = metadata;
this.secure = secure;
this.uri = createUri(secure ? HTTPS : HTTP, host, port);
this.namespace = namespace;
this.cluster = cluster;
}
// Allows for deserialization
public KubernetesServiceInstance() {
}
@Override
public String getInstanceId() {
return this.instanceId;
}
@Override
public String getServiceId() {
return this.serviceId;
}
@Override
public String getHost() {
return this.host;
}
@Override
public int getPort() {
return this.port;
}
@Override
public boolean isSecure() {
return this.secure;
}
@Override
public URI getUri() {
return uri;
}
public Map<String, String> getMetadata() {
return this.metadata;
}
@Override
public String getScheme() {
return isSecure() ? HTTPS : HTTP;
}
private URI createUri(String scheme, String host, int port) {
return URI.create(scheme + "://" + host + ":" + port);
}
public String getNamespace() {
return namespace != null ? namespace : this.metadata.get(NAMESPACE_METADATA_KEY);
}
public String getCluster() {
return this.cluster;
}
public void setInstanceId(String instanceId) {
this.instanceId = instanceId;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
public void setUri(URI uri) {
this.uri = uri;
}
public void setSecure(Boolean secure) {
this.secure = secure;
}
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public void setCluster(String cluster) {
this.cluster = cluster;
}
public Boolean getSecure() {
return secure;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
KubernetesServiceInstance that = (KubernetesServiceInstance) o;
return port == that.port && Objects.equals(instanceId, that.instanceId)
&& Objects.equals(serviceId, that.serviceId) && Objects.equals(host, that.host)
&& Objects.equals(uri, that.uri) && Objects.equals(secure, that.secure)
&& Objects.equals(metadata, that.metadata) && Objects.equals(getNamespace(), that.getNamespace())
&& Objects.equals(cluster, that.cluster);
}
@Override
public String toString() {
ToStringCreator creator = new ToStringCreator(this);
creator.append("instanceId", instanceId);
creator.append("serviceId", serviceId);
creator.append("host", host);
creator.append("port", port);
creator.append("uri", uri);
creator.append("secure", secure);
creator.append("namespace", getNamespace());
creator.append("cluster", cluster);
creator.append("metadata", metadata);
return creator.toString();
}
@Override
public int hashCode() {
return Objects.hash(instanceId, serviceId, host, port, uri, secure, getNamespace(), cluster, metadata);
}
String getCluster();
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2013-2022 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.commons.discovery;
import java.net.URI;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author wind57
*/
class KubernetesServiceInstanceTests {
@Test
void testFirstConstructor() {
DefaultKubernetesServiceInstance instance = new DefaultKubernetesServiceInstance("instanceId", "serviceId",
"host", 8080, Map.of("k8s_namespace", "spring-k8s"), true);
assertThat(instance.getInstanceId()).isEqualTo("instanceId");
assertThat(instance.getServiceId()).isEqualTo("serviceId");
assertThat(instance.getHost()).isEqualTo("host");
assertThat(instance.getPort()).isEqualTo(8080);
assertThat(instance.isSecure()).isTrue();
assertThat(instance.getUri()).isEqualTo(URI.create("https://host:8080"));
assertThat(instance.getMetadata()).isEqualTo(Map.of("k8s_namespace", "spring-k8s"));
assertThat(instance.getScheme()).isEqualTo("https");
assertThat(instance.getNamespace()).isEqualTo("spring-k8s");
assertThat(instance.getCluster()).isNull();
}
@Test
void testSecondConstructor() {
DefaultKubernetesServiceInstance instance = new DefaultKubernetesServiceInstance("instanceId", "serviceId",
"host", 8080, Map.of("a", "b"), true, "spring-k8s", "cluster");
assertThat(instance.getInstanceId()).isEqualTo("instanceId");
assertThat(instance.getServiceId()).isEqualTo("serviceId");
assertThat(instance.getHost()).isEqualTo("host");
assertThat(instance.getPort()).isEqualTo(8080);
assertThat(instance.isSecure()).isTrue();
assertThat(instance.getUri()).isEqualTo(URI.create("https://host:8080"));
assertThat(instance.getMetadata()).isEqualTo(Map.of("a", "b"));
assertThat(instance.getScheme()).isEqualTo("https");
assertThat(instance.getNamespace()).isEqualTo("spring-k8s");
assertThat(instance.getCluster()).isEqualTo("cluster");
}
@Test
void schemeIsHttp() {
assertServiceInstance(false);
}
@Test
void schemeIsHttps() {
assertServiceInstance(true);
}
private DefaultKubernetesServiceInstance assertServiceInstance(boolean secure) {
DefaultKubernetesServiceInstance instance = new DefaultKubernetesServiceInstance("123", "myservice", "1.2.3.4",
8080, Collections.emptyMap(), secure);
assertThat(instance.getInstanceId()).isEqualTo("123");
assertThat(instance.getServiceId()).isEqualTo("myservice");
assertThat(instance.getHost()).isEqualTo("1.2.3.4");
assertThat(instance.getPort()).isEqualTo(8080);
assertThat(instance.isSecure()).isEqualTo(secure);
assertThat(instance.getScheme()).isEqualTo(secure ? "https" : "http");
return instance;
}
}

View File

@@ -48,7 +48,7 @@ import org.springframework.cloud.kubernetes.client.discovery.reactive.Kubernetes
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties;
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationUpdateStrategy;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.reactive.function.client.WebClient;
@@ -162,7 +162,7 @@ class HttpBasedConfigMapWatchChangeDetectorTests {
V1EndpointPort fooEndpointPort = new V1EndpointPort();
fooEndpointPort.setPort(port);
List<ServiceInstance> instances = new ArrayList<>();
KubernetesServiceInstance fooServiceInstance = new KubernetesServiceInstance("foo", "foo",
DefaultKubernetesServiceInstance fooServiceInstance = new DefaultKubernetesServiceInstance("foo", "foo",
fooEndpointAddress.getIp(), fooEndpointPort.getPort(), metadata, false);
instances.add(fooServiceInstance);
when(reactiveDiscoveryClient.getInstances(eq("foo"))).thenReturn(Flux.fromIterable(instances));
@@ -187,7 +187,7 @@ class HttpBasedConfigMapWatchChangeDetectorTests {
fooEndpointPort.setPort(WIRE_MOCK_SERVER.port());
List<ServiceInstance> instances = new ArrayList<>();
KubernetesServiceInstance fooServiceInstance = new KubernetesServiceInstance("foo", "foo",
DefaultKubernetesServiceInstance fooServiceInstance = new DefaultKubernetesServiceInstance("foo", "foo",
fooEndpointAddress.getIp(), fooEndpointPort.getPort(), new HashMap<>(), false);
instances.add(fooServiceInstance);
when(reactiveDiscoveryClient.getInstances(eq("foo"))).thenReturn(Flux.fromIterable(instances));

View File

@@ -48,7 +48,7 @@ import org.springframework.cloud.kubernetes.client.discovery.reactive.Kubernetes
import org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider;
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties;
import org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationUpdateStrategy;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.reactive.function.client.WebClient;
@@ -156,7 +156,7 @@ class HttpBasedSecretsWatchChangeDetectorTests {
V1EndpointPort fooEndpointPort = new V1EndpointPort();
fooEndpointPort.setPort(WIRE_MOCK_SERVER.port());
List<ServiceInstance> instances = new ArrayList<>();
KubernetesServiceInstance fooServiceInstance = new KubernetesServiceInstance("foo", "foo",
DefaultKubernetesServiceInstance fooServiceInstance = new DefaultKubernetesServiceInstance("foo", "foo",
fooEndpointAddress.getIp(), fooEndpointPort.getPort(), metadata, false);
instances.add(fooServiceInstance);
when(reactiveDiscoveryClient.getInstances(eq("foo"))).thenReturn(Flux.fromIterable(instances));
@@ -177,7 +177,7 @@ class HttpBasedSecretsWatchChangeDetectorTests {
V1EndpointPort fooEndpointPort = new V1EndpointPort();
fooEndpointPort.setPort(WIRE_MOCK_SERVER.port());
List<ServiceInstance> instances = new ArrayList<>();
KubernetesServiceInstance fooServiceInstance = new KubernetesServiceInstance("foo", "foo",
DefaultKubernetesServiceInstance fooServiceInstance = new DefaultKubernetesServiceInstance("foo", "foo",
fooEndpointAddress.getIp(), fooEndpointPort.getPort(), new HashMap<>(), false);
instances.add(fooServiceInstance);
when(reactiveDiscoveryClient.getInstances(eq("foo"))).thenReturn(Flux.fromIterable(instances));

View File

@@ -34,8 +34,8 @@ 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.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
@@ -178,7 +178,7 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
if (endpointAddress.getTargetRef() != null) {
instanceId = endpointAddress.getTargetRef().getUid();
}
instances.add(new KubernetesServiceInstance(instanceId, serviceId, endpointAddress.getIp(),
instances.add(new DefaultKubernetesServiceInstance(instanceId, serviceId, endpointAddress.getIp(),
endpointPort, endpointMetadata,
this.servicePortSecureResolver.resolve(new ServicePortSecureResolver.Input(endpointPort,
service.getMetadata().getName(), service.getMetadata().getLabels(),

View File

@@ -1,58 +0,0 @@
/*
* Copyright 2013-2020 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.fabric8.discovery;
import java.util.Collections;
import io.fabric8.kubernetes.api.model.EndpointAddress;
import io.fabric8.kubernetes.api.model.EndpointPort;
import org.junit.Test;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import static org.assertj.core.api.Assertions.assertThat;
public class KubernetesServiceInstanceTests {
@Test
public void schemeIsHttp() {
assertServiceInstance(false);
}
private KubernetesServiceInstance assertServiceInstance(boolean secure) {
EndpointAddress address = new EndpointAddress();
address.setIp("1.2.3.4");
EndpointPort port = new EndpointPort();
port.setPort(8080);
KubernetesServiceInstance instance = new KubernetesServiceInstance("123", "myservice", address.getIp(),
port.getPort(), Collections.emptyMap(), secure);
assertThat(instance.getInstanceId()).isEqualTo("123");
assertThat(instance.getServiceId()).isEqualTo("myservice");
assertThat(instance.getHost()).isEqualTo("1.2.3.4");
assertThat(instance.getPort()).isEqualTo(8080);
assertThat(instance.isSecure()).isEqualTo(secure);
assertThat(instance.getScheme()).isEqualTo(secure ? "https" : "http");
return instance;
}
@Test
public void schemeIsHttps() {
assertServiceInstance(true);
}
}

View File

@@ -26,6 +26,7 @@ import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServicePort;
import io.fabric8.kubernetes.client.utils.Utils;
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadBalancerProperties;
@@ -70,7 +71,7 @@ public class Fabric8ServiceInstanceMapper implements KubernetesServiceInstanceMa
service.getMetadata().getNamespace(), properties.getClusterDomain());
final boolean secure = KubernetesServiceInstanceMapper.isSecure(service.getMetadata().getLabels(),
service.getMetadata().getAnnotations(), port.getName(), port.getPort());
return new KubernetesServiceInstance(meta.getUid(), meta.getName(), host, port.getPort(),
return new DefaultKubernetesServiceInstance(meta.getUid(), meta.getName(), host, port.getPort(),
getServiceMetadata(service), secure);
}

View File

@@ -33,8 +33,8 @@ import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
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.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServicesListSupplier;
import org.springframework.core.env.Environment;
@@ -68,7 +68,8 @@ class KubernetesServiceListSupplierTests {
@Test
void testPositiveMatch() {
when(environment.getProperty("loadbalancer.client.name")).thenReturn("test-service");
when(mapper.map(any(Service.class))).thenReturn(new KubernetesServiceInstance("", "", "", 0, null, false));
when(mapper.map(any(Service.class)))
.thenReturn(new DefaultKubernetesServiceInstance("", "", "", 0, null, false));
when(this.client.getNamespace()).thenReturn("test");
when(this.client.services()).thenReturn(this.serviceOperation);
when(this.serviceOperation.inNamespace("test")).thenReturn(namespaceOperation);
@@ -84,7 +85,8 @@ class KubernetesServiceListSupplierTests {
@Test
void testPositiveMatchAllNamespaces() {
when(environment.getProperty("loadbalancer.client.name")).thenReturn("test-service");
when(mapper.map(any(Service.class))).thenReturn(new KubernetesServiceInstance("", "", "", 0, null, false));
when(mapper.map(any(Service.class)))
.thenReturn(new DefaultKubernetesServiceInstance("", "", "", 0, null, false));
when(this.client.services()).thenReturn(this.serviceOperation);
when(this.serviceOperation.inAnyNamespace()).thenReturn(this.multiDeletable);
when(this.multiDeletable.withField("metadata.name", "test-service")).thenReturn(this.multiDeletable);