Fixing code and build configuration

This commit is contained in:
Nicola Ferraro
2016-10-11 23:10:00 +02:00
committed by Ioannis Canellos
parent 3d766effa6
commit a740fa69a4
7 changed files with 107 additions and 82 deletions

View File

@@ -90,6 +90,7 @@
<!-- Maven Plugin Versions -->
<maven-compiler-plugin.version>3.5</maven-compiler-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<fabric8.maven.plugin.version>3.1.69</fabric8.maven.plugin.version>
<gmavenplus-plugin.version>1.2</gmavenplus-plugin.version>

View File

@@ -1,17 +1,9 @@
package io.fabric8.spring.cloud.kubernetes.reload;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import javax.annotation.PreDestroy;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.spring.cloud.kubernetes.config.ConfigMapPropertySourceLocator;
import io.fabric8.spring.cloud.kubernetes.config.SecretsPropertySourceLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -23,10 +15,8 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
/**
* Definition of beans needed for the automatic reload of configuration.
@@ -62,12 +52,12 @@ public class ConfigReloadAutoConfiguration {
*/
@Bean
@ConditionalOnMissingBean
public ConfigurationChangeDetector propertyChangeWatcher(ConfigReloadProperties properties, ConfigurationUpdateStrategy strategy, EventWatcher eventWatcher) {
public ConfigurationChangeDetector propertyChangeWatcher(ConfigReloadProperties properties, ConfigurationUpdateStrategy strategy) {
switch (properties.getMode()) {
case POLLING:
return new PollingConfigurationChangeDetector(environment, properties, kubernetesClient, strategy, configMapPropertySourceLocator, secretsPropertySourceLocator);
case EVENT:
return new EventBasedConfigurationChangeDetector(environment, properties, kubernetesClient, strategy, configMapPropertySourceLocator, secretsPropertySourceLocator, eventWatcher);
return new EventBasedConfigurationChangeDetector(environment, properties, kubernetesClient, strategy, configMapPropertySourceLocator, secretsPropertySourceLocator);
}
throw new IllegalStateException("Unsupported configuration reload mode: " + properties.getMode());
}
@@ -89,49 +79,6 @@ public class ConfigReloadAutoConfiguration {
throw new IllegalStateException("Unsupported configuration update strategy: " + properties.getStrategy());
}
/**
* Manages watches asynchronously and clean them up on context close.
*/
@Component
public static class DefaultEventWatcher implements EventWatcher {
private Logger log = LoggerFactory.getLogger(getClass());
private KubernetesClient kubernetesClient;
private Map<String, Watch> watches;
@Autowired
public DefaultEventWatcher(KubernetesClient kubernetesClient) {
this.kubernetesClient = kubernetesClient;
this.watches = new ConcurrentHashMap<>();
}
@Async
public void addWatch(String name, Function<KubernetesClient, Watch> watch) {
if (watches.containsKey(name)) {
throw new IllegalArgumentException("Watch already present: " + name);
}
watches.put(name, watch.apply(kubernetesClient));
log.info("Added new Kubernetes watch: {}", name);
}
@PreDestroy
public void unwatch() {
if (this.watches != null) {
for (Watch watch : this.watches.values()) {
try {
watch.close();
} catch (Exception e) {
log.error("Error while closing the watch connection", e);
}
}
}
}
}
}
}

View File

@@ -5,7 +5,6 @@ import java.util.List;
import java.util.Map;
import javax.annotation.PreDestroy;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.slf4j.Logger;
@@ -40,9 +39,7 @@ public abstract class ConfigurationChangeDetector {
@PreDestroy
public void shutdown() {
// Ensure the kubernetes client is cleaned up from spare threads when shutting down
if (kubernetesClient instanceof Client) {
((Client) kubernetesClient).close();
}
kubernetesClient.close();
}
public void reloadProperties() {

View File

@@ -1,11 +1,15 @@
package io.fabric8.spring.cloud.kubernetes.reload;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.spring.cloud.kubernetes.config.ConfigMapPropertySource;
import io.fabric8.spring.cloud.kubernetes.config.ConfigMapPropertySourceLocator;
@@ -24,27 +28,27 @@ public class EventBasedConfigurationChangeDetector extends ConfigurationChangeDe
private SecretsPropertySourceLocator secretsPropertySourceLocator;
private EventWatcher eventWatcher;
private Map<String, Watch> watches;
public EventBasedConfigurationChangeDetector(AbstractEnvironment environment,
ConfigReloadProperties properties,
KubernetesClient kubernetesClient,
ConfigurationUpdateStrategy strategy,
ConfigMapPropertySourceLocator configMapPropertySourceLocator,
SecretsPropertySourceLocator secretsPropertySourceLocator,
EventWatcher eventWatcher) {
SecretsPropertySourceLocator secretsPropertySourceLocator) {
super(environment, properties, kubernetesClient, strategy);
this.eventWatcher = eventWatcher;
this.configMapPropertySourceLocator = configMapPropertySourceLocator;
this.secretsPropertySourceLocator = secretsPropertySourceLocator;
this.watches = new HashMap<>();
}
@PostConstruct
public void watch() {
if (properties.isMonitoringConfigMaps()) {
eventWatcher.addWatch("config-maps-watch", k -> k.configMaps()
String name = "config-maps-watch";
watches.put(name, kubernetesClient.configMaps()
.watch(new Watcher<ConfigMap>() {
@Override
public void eventReceived(Action action, ConfigMap configMap) {
@@ -55,10 +59,12 @@ public class EventBasedConfigurationChangeDetector extends ConfigurationChangeDe
public void onClose(KubernetesClientException e) {
}
}));
log.info("Added new Kubernetes watch: {}", name);
}
if (properties.isMonitoringSecrets()) {
eventWatcher.addWatch("secrets-watch", k -> k.secrets()
String name = "secrets-watch";
watches.put(name, kubernetesClient.secrets()
.watch(new Watcher<Secret>() {
@Override
public void eventReceived(Action action, Secret secret) {
@@ -69,11 +75,27 @@ public class EventBasedConfigurationChangeDetector extends ConfigurationChangeDe
public void onClose(KubernetesClientException e) {
}
}));
log.info("Added new Kubernetes watch: {}", name);
}
log.info("Kubernetes polling configuration change detector activated");
}
@PreDestroy
public void unwatch() {
if (this.watches != null) {
for (Map.Entry<String, Watch> entry : this.watches.entrySet()) {
try {
log.debug("Closing the watch {}", entry.getKey());
entry.getValue().close();
} catch (Exception e) {
log.error("Error while closing the watch connection", e);
}
}
}
}
private void onEvent(ConfigMap configMap) {
MapPropertySource currentConfigMapSource = findPropertySource(ConfigMapPropertySource.class);
if (currentConfigMapSource != null) {

View File

@@ -1,15 +0,0 @@
package io.fabric8.spring.cloud.kubernetes.reload;
import java.util.function.Function;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.Watch;
/**
* Provides a way to start Kubernetes watches and bind their lifecycle to the application context.
*/
public interface EventWatcher {
void addWatch(String name, Function<KubernetesClient, Watch> watch);
}

View File

@@ -62,15 +62,39 @@
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<!--skip deploy -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>${fabric8.maven.plugin.version}</version>
<executions>
<execution>
<id>fmp</id>
<goals>
<goal>resource</goal>
<goal>build</goal>
<goal>helm</goal>
</goals>
</execution>
</executions>
@@ -78,4 +102,53 @@
</plugins>
</build>
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>${fabric8.maven.plugin.version}</version>
<executions>
<execution>
<id>fmp</id>
<goals>
<goal>resource</goal>
<goal>helm</goal>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>docker</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>${fabric8.maven.plugin.version}</version>
<executions>
<execution>
<id>fmp</id>
<goals>
<goal>resource</goal>
<goal>helm</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@@ -16,7 +16,7 @@ oc policy add-role-to-user view --serviceaccount=default
You can deploy the application using the fabric8 maven plugin:
```
mvn clean install fabric8:deploy
mvn clean install fabric8:build fabric8:deploy
```
### Changing the configuration