#562 Removing unnecessary @Bean + changing KubernetesServiceInstance implementation
This commit is contained in:
@@ -160,7 +160,8 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
EndpointPort endpointPort = findEndpointPort(s);
|
||||
instances.add(new KubernetesServiceInstance(instanceId, serviceId,
|
||||
endpointAddress, endpointPort, endpointMetadata,
|
||||
endpointAddress.getIp(), endpointPort.getPort(),
|
||||
endpointMetadata,
|
||||
this.isServicePortSecureResolver
|
||||
.resolve(new DefaultIsServicePortSecureResolver.Input(
|
||||
endpointPort.getPort(),
|
||||
|
||||
@@ -19,9 +19,6 @@ package org.springframework.cloud.kubernetes.discovery;
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import io.fabric8.kubernetes.api.model.EndpointAddress;
|
||||
import io.fabric8.kubernetes.api.model.EndpointPort;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
|
||||
/**
|
||||
@@ -37,43 +34,49 @@ public class KubernetesServiceInstance implements ServiceInstance {
|
||||
|
||||
private static final String DSL = "//";
|
||||
|
||||
private static final String COLN = ":";
|
||||
private static final String COLON = ":";
|
||||
|
||||
private final String instanceId;
|
||||
private String instanceId;
|
||||
|
||||
private final String serviceId;
|
||||
private String serviceId;
|
||||
|
||||
private final EndpointAddress endpointAddress;
|
||||
private String host;
|
||||
|
||||
private final EndpointPort endpointPort;
|
||||
private int port;
|
||||
|
||||
private final Boolean secure;
|
||||
private URI uri;
|
||||
|
||||
private final Map<String, String> metadata;
|
||||
private Boolean secure;
|
||||
|
||||
private Map<String, String> metadata;
|
||||
|
||||
/**
|
||||
* @param serviceId the id of the service.
|
||||
* @param endpointAddress the address where the service instance can be found.
|
||||
* @param endpointPort the port on which the service is running.
|
||||
* @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 or not the connection needs to be secure.
|
||||
* @deprecated - use other constructor
|
||||
*/
|
||||
@Deprecated
|
||||
public KubernetesServiceInstance(String serviceId, EndpointAddress endpointAddress,
|
||||
EndpointPort endpointPort, Map<String, String> metadata, Boolean secure) {
|
||||
this(null, serviceId, endpointAddress, endpointPort, metadata, secure);
|
||||
public KubernetesServiceInstance(String serviceId, String host, int port,
|
||||
Map<String, String> metadata, Boolean secure) {
|
||||
this(null, serviceId, host, port, metadata, secure);
|
||||
}
|
||||
|
||||
public KubernetesServiceInstance(String instanceId, String serviceId,
|
||||
EndpointAddress endpointAddress, EndpointPort endpointPort,
|
||||
Map<String, String> metadata, Boolean secure) {
|
||||
public KubernetesServiceInstance() {
|
||||
|
||||
}
|
||||
|
||||
public KubernetesServiceInstance(String instanceId, String serviceId, String host,
|
||||
int port, Map<String, String> metadata, Boolean secure) {
|
||||
this.instanceId = instanceId;
|
||||
this.serviceId = serviceId;
|
||||
this.endpointAddress = endpointAddress;
|
||||
this.endpointPort = endpointPort;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.metadata = metadata;
|
||||
this.secure = secure;
|
||||
this.uri = createUri(secure ? HTTPS_PREFIX : HTTP_PREFIX, host, port);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -88,12 +91,12 @@ public class KubernetesServiceInstance implements ServiceInstance {
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
return this.endpointAddress.getIp();
|
||||
return this.host;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return this.endpointPort.getPort();
|
||||
return this.port;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -103,10 +106,7 @@ public class KubernetesServiceInstance implements ServiceInstance {
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(getScheme()).append(COLN).append(DSL).append(getHost()).append(COLN)
|
||||
.append(getPort());
|
||||
return URI.create(sb.toString());
|
||||
return uri;
|
||||
}
|
||||
|
||||
public Map<String, String> getMetadata() {
|
||||
@@ -118,4 +118,11 @@ public class KubernetesServiceInstance implements ServiceInstance {
|
||||
return isSecure() ? HTTPS_PREFIX : HTTP_PREFIX;
|
||||
}
|
||||
|
||||
private URI createUri(String scheme, String host, int port) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(scheme).append(COLON).append(DSL).append(host).append(COLON)
|
||||
.append(port);
|
||||
return URI.create(sb.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,7 +37,8 @@ public class KubernetesServiceInstanceTests {
|
||||
EndpointPort port = new EndpointPort();
|
||||
port.setPort(8080);
|
||||
KubernetesServiceInstance instance = new KubernetesServiceInstance("123",
|
||||
"myservice", address, port, Collections.emptyMap(), secure);
|
||||
"myservice", address.getIp(), port.getPort(), Collections.emptyMap(),
|
||||
secure);
|
||||
|
||||
assertThat(instance.getInstanceId()).isEqualTo("123");
|
||||
assertThat(instance.getServiceId()).isEqualTo("myservice");
|
||||
|
||||
@@ -30,11 +30,6 @@ import org.springframework.context.annotation.Configuration;
|
||||
@LoadBalancerClients(defaultConfiguration = KubernetesClientConfiguration.class)
|
||||
public class KubernetesLoadBalancerAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
KubernetesLoadBalancerProperties properties() {
|
||||
return new KubernetesLoadBalancerProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
KubernetesServiceInstanceMapper mapper(KubernetesLoadBalancerProperties properties,
|
||||
KubernetesDiscoveryProperties discoveryProperties) {
|
||||
|
||||
@@ -1,92 +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.loadbalancer;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
|
||||
/**
|
||||
* @author Piotr Minkowski
|
||||
*/
|
||||
public class KubernetesServiceInstance implements ServiceInstance {
|
||||
|
||||
private String serviceId;
|
||||
|
||||
private String instanceId;
|
||||
|
||||
private int port;
|
||||
|
||||
private boolean secure;
|
||||
|
||||
private String host;
|
||||
|
||||
private URI uri;
|
||||
|
||||
private Map<String, String> metadata;
|
||||
|
||||
KubernetesServiceInstance() {
|
||||
}
|
||||
|
||||
KubernetesServiceInstance(String serviceId, String instanceId, int port,
|
||||
boolean secure, String host, URI uri, Map<String, String> metadata) {
|
||||
this.serviceId = serviceId;
|
||||
this.instanceId = instanceId;
|
||||
this.port = port;
|
||||
this.secure = secure;
|
||||
this.host = host;
|
||||
this.uri = uri;
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return serviceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return instanceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSecure() {
|
||||
return secure;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getMetadata() {
|
||||
return metadata;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import io.fabric8.kubernetes.client.utils.Utils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryProperties;
|
||||
import org.springframework.cloud.kubernetes.discovery.KubernetesServiceInstance;
|
||||
|
||||
/**
|
||||
* @author Piotr Minkowski
|
||||
@@ -70,10 +71,8 @@ public class KubernetesServiceInstanceMapper {
|
||||
}
|
||||
final String host = createHost(service);
|
||||
final boolean secure = isSecure(service, port);
|
||||
return new KubernetesServiceInstance(meta.getName(), meta.getUid(),
|
||||
port.getPort(), secure, host,
|
||||
createUri(secure ? "https" : "http", host, port.getPort()),
|
||||
getServiceMetadata(service));
|
||||
return new KubernetesServiceInstance(meta.getUid(), meta.getName(), host,
|
||||
port.getPort(), getServiceMetadata(service), secure);
|
||||
}
|
||||
|
||||
private Map<String, String> getServiceMetadata(Service service) {
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryProperties;
|
||||
import org.springframework.cloud.kubernetes.discovery.KubernetesServiceInstance;
|
||||
|
||||
public class KubernetesServiceInstanceMapperTest {
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryProperties;
|
||||
import org.springframework.cloud.kubernetes.discovery.KubernetesServiceInstance;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
|
||||
Reference in New Issue
Block a user