clean-up in discovery implementation - part 1 (#1082)
This commit is contained in:
@@ -45,6 +45,10 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
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.PRIMARY_PORT_NAME_LABEL_KEY;
|
||||
|
||||
/**
|
||||
* @author Min Kim
|
||||
* @author Ryan Baxter
|
||||
@@ -54,12 +58,6 @@ public class KubernetesInformerDiscoveryClient implements DiscoveryClient, Initi
|
||||
|
||||
private static final Log log = LogFactory.getLog(KubernetesInformerDiscoveryClient.class);
|
||||
|
||||
private static final String PRIMARY_PORT_NAME_LABEL_KEY = "primary-port-name";
|
||||
|
||||
private static final String HTTPS_PORT_NAME = "https";
|
||||
|
||||
private static final String HTTP_PORT_NAME = "http";
|
||||
|
||||
private final SharedInformerFactory sharedInformerFactory;
|
||||
|
||||
private final Lister<V1Service> serviceLister;
|
||||
@@ -182,7 +180,7 @@ public class KubernetesInformerDiscoveryClient implements DiscoveryClient, Initi
|
||||
// In case no port has been found return -1 to log a warning and fall back to
|
||||
// the first port in the list.
|
||||
int discoveredPort = ports.getOrDefault(primaryPortName,
|
||||
ports.getOrDefault(HTTPS_PORT_NAME, ports.getOrDefault(HTTP_PORT_NAME, -1)));
|
||||
ports.getOrDefault(HTTPS, ports.getOrDefault(HTTP, -1)));
|
||||
|
||||
if (discoveredPort == -1) {
|
||||
if (StringUtils.hasText(primaryPortName)) {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Constants that are to be used across discovery implementations.
|
||||
*
|
||||
* @author wind57
|
||||
*/
|
||||
public final class KubernetesDiscoveryConstants {
|
||||
|
||||
private KubernetesDiscoveryConstants() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Primary port label.
|
||||
*/
|
||||
public static final String PRIMARY_PORT_NAME_LABEL_KEY = "primary-port-name";
|
||||
|
||||
/**
|
||||
* Https scheme.
|
||||
*/
|
||||
public static final String HTTPS = "https";
|
||||
|
||||
/**
|
||||
* Http scheme.
|
||||
*/
|
||||
public static final String HTTP = "http";
|
||||
|
||||
/**
|
||||
* Key of the namespace metadata.
|
||||
*/
|
||||
public static final String NAMESPACE_METADATA_KEY = "k8s_namespace";
|
||||
|
||||
}
|
||||
@@ -23,17 +23,12 @@ 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;
|
||||
|
||||
public final class KubernetesServiceInstance implements ServiceInstance {
|
||||
|
||||
/**
|
||||
* Key of the namespace metadata.
|
||||
*/
|
||||
public static final String NAMESPACE_METADATA_KEY = "k8s_namespace";
|
||||
|
||||
private static final String HTTP_PREFIX = "http";
|
||||
|
||||
private static final String HTTPS_PREFIX = "https";
|
||||
|
||||
private String instanceId;
|
||||
|
||||
private String serviceId;
|
||||
@@ -68,7 +63,7 @@ public final class KubernetesServiceInstance implements ServiceInstance {
|
||||
this.port = port;
|
||||
this.metadata = metadata;
|
||||
this.secure = secure;
|
||||
this.uri = createUri(secure ? HTTPS_PREFIX : HTTP_PREFIX, host, port);
|
||||
this.uri = createUri(secure ? HTTPS : HTTP, host, port);
|
||||
this.namespace = null;
|
||||
this.cluster = null;
|
||||
}
|
||||
@@ -91,7 +86,7 @@ public final class KubernetesServiceInstance implements ServiceInstance {
|
||||
this.port = port;
|
||||
this.metadata = metadata;
|
||||
this.secure = secure;
|
||||
this.uri = createUri(secure ? HTTPS_PREFIX : HTTP_PREFIX, host, port);
|
||||
this.uri = createUri(secure ? HTTPS : HTTP, host, port);
|
||||
this.namespace = namespace;
|
||||
this.cluster = cluster;
|
||||
}
|
||||
@@ -136,7 +131,7 @@ public final class KubernetesServiceInstance implements ServiceInstance {
|
||||
|
||||
@Override
|
||||
public String getScheme() {
|
||||
return isSecure() ? HTTPS_PREFIX : HTTP_PREFIX;
|
||||
return isSecure() ? HTTPS : HTTP;
|
||||
}
|
||||
|
||||
private URI createUri(String scheme, String host, int port) {
|
||||
|
||||
@@ -44,7 +44,10 @@ import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance.NAMESPACE_METADATA_KEY;
|
||||
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;
|
||||
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.PRIMARY_PORT_NAME_LABEL_KEY;
|
||||
|
||||
/**
|
||||
* Kubernetes implementation of {@link DiscoveryClient}.
|
||||
@@ -56,12 +59,6 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
private static final Log log = LogFactory.getLog(KubernetesDiscoveryClient.class);
|
||||
|
||||
private static final String PRIMARY_PORT_NAME_LABEL_KEY = "primary-port-name";
|
||||
|
||||
private static final String HTTPS_PORT_NAME = "https";
|
||||
|
||||
private static final String HTTP_PORT_NAME = "http";
|
||||
|
||||
private final KubernetesDiscoveryProperties properties;
|
||||
|
||||
private final ServicePortSecureResolver servicePortSecureResolver;
|
||||
@@ -231,7 +228,7 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
|
||||
// In case no port has been found return -1 to log a warning and fall back to
|
||||
// the first port in the list.
|
||||
int discoveredPort = ports.getOrDefault(primaryPortName,
|
||||
ports.getOrDefault(HTTPS_PORT_NAME, ports.getOrDefault(HTTP_PORT_NAME, -1)));
|
||||
ports.getOrDefault(HTTPS, ports.getOrDefault(HTTP, -1)));
|
||||
|
||||
if (discoveredPort == -1) {
|
||||
if (StringUtils.hasText(primaryPortName)) {
|
||||
|
||||
Reference in New Issue
Block a user