fix issue 811 and 16 (#812)

This commit is contained in:
erabii
2021-11-10 16:58:06 -05:00
committed by GitHub
parent b41dcc146e
commit b73b9528b2
5 changed files with 7 additions and 320 deletions

View File

@@ -20,8 +20,9 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.style.ToStringCreator;
@@ -33,10 +34,6 @@ public class KubernetesDiscoveryProperties {
/** If Kubernetes Discovery is enabled. */
private boolean enabled = true;
/** The service name of the local instance. */
@Value("${spring.application.name:unknown}")
private String serviceName = "unknown";
/** If discovering all namespaces. */
private boolean allNamespaces = false;
@@ -64,12 +61,7 @@ public class KubernetesDiscoveryProperties {
private String filter;
/** Set the port numbers that are considered secure and use HTTPS. */
private Set<Integer> knownSecurePorts = new HashSet<Integer>() {
{
add(443);
add(8443);
}
};
private Set<Integer> knownSecurePorts = Stream.of(443, 8443).collect(Collectors.toCollection(HashSet::new));
/**
* If set, then only the services matching these labels will be fetched from the
@@ -95,14 +87,6 @@ public class KubernetesDiscoveryProperties {
this.enabled = enabled;
}
public String getServiceName() {
return this.serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public String getFilter() {
return this.filter;
}
@@ -185,15 +169,15 @@ public class KubernetesDiscoveryProperties {
@Override
public String toString() {
return new ToStringCreator(this).append("enabled", this.enabled).append("serviceName", this.serviceName)
.append("filter", this.filter).append("knownSecurePorts", this.knownSecurePorts)
.append("serviceLabels", this.serviceLabels).append("metadata", this.metadata).toString();
return new ToStringCreator(this).append("enabled", this.enabled).append("filter", this.filter)
.append("knownSecurePorts", this.knownSecurePorts).append("serviceLabels", this.serviceLabels)
.append("metadata", this.metadata).toString();
}
/**
* Metadata properties.
*/
public class Metadata {
public static class Metadata {
/**
* When set, the Kubernetes labels of the services will be included as metadata of

View File

@@ -33,8 +33,6 @@ import org.springframework.cloud.kubernetes.commons.PodUtils;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryClientHealthIndicatorInitializer;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.fabric8.Fabric8AutoConfiguration;
import org.springframework.cloud.kubernetes.fabric8.registry.KubernetesRegistration;
import org.springframework.cloud.kubernetes.fabric8.registry.KubernetesServiceRegistry;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -72,16 +70,6 @@ public class KubernetesDiscoveryClientAutoConfiguration {
}
}
@Bean
public KubernetesServiceRegistry getServiceRegistry() {
return new KubernetesServiceRegistry();
}
@Bean
public KubernetesRegistration getRegistration(KubernetesClient client, KubernetesDiscoveryProperties properties) {
return new KubernetesRegistration(client, properties);
}
@Bean
public KubernetesDiscoveryProperties getKubernetesDiscoveryProperties() {
return new KubernetesDiscoveryProperties();

View File

@@ -1,122 +0,0 @@
/*
* Copyright 2013-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.fabric8.registry;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
/**
* Auto service registration for Kubernetes.
*
* @author Mauricio Salatino
*/
@Deprecated
// TODO Remove this class in 2.x as it is not used or necessary in Kubernetes
public class KubernetesAutoServiceRegistration implements AutoServiceRegistration, SmartLifecycle, Ordered {
private static final Log log = LogFactory.getLog(KubernetesAutoServiceRegistration.class);
private AtomicBoolean running = new AtomicBoolean(false);
private int order = 0;
private AtomicInteger port = new AtomicInteger(0);
private ApplicationContext context;
private KubernetesServiceRegistry serviceRegistry;
private KubernetesRegistration registration;
public KubernetesAutoServiceRegistration(ApplicationContext context, KubernetesServiceRegistry serviceRegistry,
KubernetesRegistration registration) {
this.context = context;
this.serviceRegistry = serviceRegistry;
this.registration = registration;
}
@Override
public boolean isAutoStartup() {
return true;
}
@Override
public void stop(Runnable callback) {
stop();
callback.run();
}
@Override
public void start() {
this.serviceRegistry.register(this.registration);
this.context.publishEvent(new InstanceRegisteredEvent<>(this, this.registration.getProperties()));
this.running.set(true);
}
@Override
public void stop() {
this.serviceRegistry.deregister(this.registration);
this.running.set(false);
}
@Override
public boolean isRunning() {
return this.running.get();
}
@Override
public int getPhase() {
return 0;
}
@Override
public int getOrder() {
return 0;
}
@EventListener(ServletWebServerInitializedEvent.class)
public void onApplicationEvent(ServletWebServerInitializedEvent event) {
// TODO: take SSL into account
int localPort = event.getWebServer().getPort();
if (this.port.get() == 0) {
log.info("Updating port to " + localPort);
this.port.compareAndSet(0, localPort);
start();
}
}
@EventListener(ContextClosedEvent.class)
public void onApplicationEvent(ContextClosedEvent event) {
if (event.getApplicationContext() == this.context) {
stop();
}
}
}

View File

@@ -1,100 +0,0 @@
/*
* Copyright 2013-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.fabric8.registry;
import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
/**
* Kubernetes implementation of a {@link Registration}.
*
* @author Mauricio Salatino
*/
public class KubernetesRegistration implements Registration, Closeable {
private final KubernetesClient client;
private KubernetesDiscoveryProperties properties;
private AtomicBoolean running = new AtomicBoolean(false);
public KubernetesRegistration(KubernetesClient client, KubernetesDiscoveryProperties properties) {
this.client = client;
this.properties = properties;
}
@Override
public void close() throws IOException {
this.client.close();
}
@Override
public String getServiceId() {
return this.properties.getServiceName();
}
@Override
public String getHost() {
return this.client.getMasterUrl().getHost();
}
@Override
public int getPort() {
return 0;
}
@Override
public boolean isSecure() {
return false;
}
@Override
public URI getUri() {
try {
return this.client.getMasterUrl().toURI();
}
catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
public KubernetesDiscoveryProperties getProperties() {
return this.properties;
}
@Override
public Map<String, String> getMetadata() {
return null;
}
@Override
public String toString() {
return "KubernetesRegistration{" + "client=" + this.client + ", properties=" + this.properties + ", running="
+ this.running + '}';
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright 2013-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.fabric8.registry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
/**
* Kubernetes {@link ServiceRegistry}.
*
* @author Mauricio Salatino
*/
public class KubernetesServiceRegistry implements ServiceRegistry<KubernetesRegistration> {
private static final Log log = LogFactory.getLog(KubernetesServiceRegistry.class);
public KubernetesServiceRegistry() {
}
@Override
public void register(KubernetesRegistration registration) {
log.info("Registering : " + registration);
}
@Override
public void deregister(KubernetesRegistration registration) {
log.info("DeRegistering : " + registration);
}
@Override
public void close() {
}
@Override
public void setStatus(KubernetesRegistration registration, String status) {
log.info("Set Status for : " + registration + " Status: " + status);
}
@Override
public <T> T getStatus(KubernetesRegistration registration) {
log.info("Get Status for : " + registration);
return null;
}
}