From b281bd2d28aa4723ec984bd16f60b33b1a60436f Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 3 Aug 2018 13:45:46 -0400 Subject: [PATCH] Updates formatting and license header --- pom.xml | 27 +- .../config/AbstractConfigProperties.java | 49 ++- .../config/BootstrapConfiguration.java | 42 +-- .../config/ConfigMapConfigProperties.java | 43 +-- .../config/ConfigMapPropertySource.java | 180 ++++++----- .../ConfigMapPropertySourceLocator.java | 66 ++-- .../cloud/kubernetes/config/ConfigUtils.java | 75 +++-- .../cloud/kubernetes/config/Constants.java | 14 +- .../config/KubernetesPropertySource.java | 4 +- .../config/SecretsConfigProperties.java | 57 ++-- .../config/SecretsPropertySource.java | 145 ++++----- .../config/SecretsPropertySourceLocator.java | 30 +- .../reload/ConfigReloadAutoConfiguration.java | 126 ++++---- .../config/reload/ConfigReloadProperties.java | 185 +++++------ .../reload/ConfigurationChangeDetector.java | 201 ++++++------ .../reload/ConfigurationUpdateStrategy.java | 49 +-- ...EventBasedConfigurationChangeDetector.java | 204 ++++++------ .../PollingConfigurationChangeDetector.java | 93 +++--- .../cloud/kubernetes/config/CoreTest.groovy | 4 +- .../kubernetes/config/TestApplication.groovy | 4 +- .../kubernetes/config/ConfigMapTestUtil.java | 17 + .../config/ConfigMapsSpringBootTest.java | 52 ++- .../kubernetes/config/ConfigMapsTest.java | 213 ++++++------- ...ProfilesNoActiveProfileSpringBootTest.java | 53 ++- .../ConfigMapsWithProfilesSpringBootTest.java | 53 ++- ...nfigMapsWithoutProfilesSpringBootTest.java | 17 + .../MultipleConfigMapsSpringBootTest.java | 4 +- .../cloud/kubernetes/config/example/App.java | 11 +- .../kubernetes/config/example/Greeting.java | 4 +- .../config/example/GreetingController.java | 4 +- .../config/example/GreetingProperties.java | 4 +- .../config/example2/ExampleApp.java | 17 + .../config/example2/ExampleAppProps.java | 17 + spring-cloud-kubernetes-core/pom.xml | 27 +- .../KubernetesAutoConfiguration.java | 167 +++++----- .../KubernetesClientProperties.java | 301 +++++++++--------- .../kubernetes/KubernetesHealthIndicator.java | 76 ++--- .../cloud/kubernetes/LazilyInstantiate.java | 56 ++-- .../cloud/kubernetes/PodUtils.java | 43 +-- .../cloud/kubernetes/StandardPodUtils.java | 120 +++---- ...bernetesApplicationContextInitializer.java | 54 ++-- .../KubernetesProfileApplicationListener.java | 117 +++---- .../kubernetes/LazilyInstantiateTest.java | 21 +- .../kubernetes/StandardPodUtilsTest.java | 33 +- ...ernetesProfileApplicationListenerTest.java | 17 + spring-cloud-kubernetes-dependencies/pom.xml | 17 + spring-cloud-kubernetes-discovery/pom.xml | 27 +- .../discovery/KubernetesCatalogWatch.java | 21 +- .../discovery/KubernetesDiscoveryClient.java | 21 +- ...netesDiscoveryClientAutoConfiguration.java | 17 + .../KubernetesDiscoveryProperties.java | 21 +- .../discovery/KubernetesServiceInstance.java | 21 +- .../KubernetesAutoServiceRegistration.java | 17 + .../registry/KubernetesRegistration.java | 17 + .../registry/KubernetesServiceRegistry.java | 17 + .../KubernetesDiscoveryClientTest.groovy | 17 + ...CatalogServicesWatchConfigurationTest.java | 17 + .../discovery/KubernetesCatalogWatchTest.java | 17 + .../greeting-service/pom.xml | 17 + .../name-service/pom.xml | 17 + .../pom.xml | 17 + .../cloud/kubernetes/examples/App.java | 4 +- .../kubernetes/examples/HelloController.java | 18 +- .../examples/ApplicationTestIT.java | 37 ++- .../kubernetes-reload-example/pom.xml | 17 + .../cloud/kubernetes/examples/App.java | 11 +- .../kubernetes/examples/DummyConfig.java | 18 +- .../cloud/kubernetes/examples/MyBean.java | 22 +- .../cloud/kubernetes/examples/MyConfig.java | 18 +- .../kubernetes-zipkin-example/pom.xml | 17 + spring-cloud-kubernetes-examples/pom.xml | 17 + .../cloud/kubernetes/leader/Leader.java | 5 +- .../leader/LeaderAutoConfiguration.java | 5 +- .../kubernetes/leader/LeaderContext.java | 5 +- .../kubernetes/leader/LeaderInitiator.java | 5 +- .../leader/LeaderKubernetesHelper.java | 5 +- .../leader/LeadershipController.java | 5 +- .../kubernetes/leader/LeaderContextTest.java | 17 + .../leader/LeaderInitiatorTest.java | 17 + .../leader/LeaderKubernetesHelperTest.java | 17 + .../cloud/kubernetes/leader/LeaderTest.java | 17 + .../leader/LeadershipControllerTest.java | 17 + spring-cloud-kubernetes-ribbon/pom.xml | 28 +- .../ribbon/KubernetesConfigKey.java | 144 ++++----- .../KubernetesRibbonClientConfiguration.java | 23 +- .../ribbon/KubernetesServerList.java | 127 ++++---- .../RibbonKubernetesAutoConfiguration.java | 4 +- .../cloud/kubernetes/ribbon/Types.java | 67 ++-- .../kubernetes/ribbon/test/RibbonTest.groovy | 4 +- .../ribbon/test/TestApplication.groovy | 4 +- .../ribbon/KubernetesConfigKeyTest.java | 50 ++- .../kubernetes/ribbon/RibbonFallbackTest.java | 114 ++++--- .../cloud/kubernetes/ribbon/RibbonTest.java | 65 ++-- .../kubernetes/ribbon/TestApplication.java | 17 + spring-cloud-starter-kubernetes-all/pom.xml | 28 +- .../pom.xml | 28 +- .../pom.xml | 28 +- spring-cloud-starter-kubernetes/pom.xml | 27 +- 98 files changed, 2545 insertions(+), 1901 deletions(-) diff --git a/pom.xml b/pom.xml index 32b23de4..51b9600f 100644 --- a/pom.xml +++ b/pom.xml @@ -1,18 +1,19 @@ paths = new LinkedList<>(); @@ -58,28 +59,32 @@ public class ConfigMapConfigProperties extends AbstractConfigProperties { } /** - * @return A list of Source to use - * If the user has not specified any Source properties, then a single Source - * is constructed based on the supplied name and namespace + * @return A list of Source to use If the user has not specified any Source + * properties, then a single Source is constructed based on the supplied name and + * namespace * - * These are the actual name/namespace pairs that are used to create a ConfigMapPropertySource + * These are the actual name/namespace pairs that are used to create a + * ConfigMapPropertySource */ public List determineSources() { if (sources.isEmpty()) { - return new ArrayList() {{ - add(new NormalizedSource(name, namespace)); - }}; + return new ArrayList() { + { + add(new NormalizedSource(name, namespace)); + } + }; } - return sources.stream().map(s -> s.normalize(name, namespace)).collect(Collectors.toList()); + return sources.stream().map(s -> s.normalize(name, namespace)) + .collect(Collectors.toList()); } @Override - public String getConfigurationTarget() { - return TARGET; - } + public String getConfigurationTarget() { + return TARGET; + } - public static class Source { + public static class Source { /** * The name of the ConfigMap @@ -120,10 +125,10 @@ public class ConfigMapConfigProperties extends AbstractConfigProperties { } public NormalizedSource normalize(String defaultName, String defaultNamespace) { - final String normalizedName = - StringUtils.isEmpty(this.name) ? defaultName : this.name; - final String normalizedNamespace = - StringUtils.isEmpty(this.namespace) ? defaultNamespace : this.namespace; + final String normalizedName = StringUtils.isEmpty(this.name) ? defaultName + : this.name; + final String normalizedNamespace = StringUtils.isEmpty(this.namespace) + ? defaultNamespace : this.namespace; return new NormalizedSource(normalizedName, normalizedNamespace); } diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySource.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySource.java index 5d69a38e..9ca05252 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySource.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySource.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -17,13 +17,6 @@ package org.springframework.cloud.kubernetes.config; -import static java.util.Arrays.asList; -import static org.springframework.beans.factory.config.YamlProcessor.MatchStatus.ABSTAIN; -import static org.springframework.beans.factory.config.YamlProcessor.MatchStatus.FOUND; -import static org.springframework.beans.factory.config.YamlProcessor.MatchStatus.NOT_FOUND; - -import io.fabric8.kubernetes.api.model.ConfigMap; -import io.fabric8.kubernetes.client.KubernetesClient; import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.HashMap; @@ -33,58 +26,72 @@ import java.util.Properties; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; + +import io.fabric8.kubernetes.api.model.ConfigMap; +import io.fabric8.kubernetes.client.KubernetesClient; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.config.YamlProcessor.DocumentMatcher; + import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.core.io.ByteArrayResource; import org.springframework.util.StringUtils; +import static java.util.Arrays.asList; +import static org.springframework.beans.factory.config.YamlProcessor.MatchStatus.ABSTAIN; +import static org.springframework.beans.factory.config.YamlProcessor.MatchStatus.FOUND; +import static org.springframework.beans.factory.config.YamlProcessor.MatchStatus.NOT_FOUND; + public class ConfigMapPropertySource extends KubernetesPropertySource { - private static final Log LOG = LogFactory.getLog(ConfigMapPropertySource.class); + private static final Log LOG = LogFactory.getLog(ConfigMapPropertySource.class); - private static final String APPLICATION_YML = "application.yml"; - private static final String APPLICATION_YAML = "application.yaml"; - private static final String APPLICATION_PROPERTIES = "application.properties"; + private static final String APPLICATION_YML = "application.yml"; + private static final String APPLICATION_YAML = "application.yaml"; + private static final String APPLICATION_PROPERTIES = "application.properties"; - private static final String PREFIX = "configmap"; + private static final String PREFIX = "configmap"; - public ConfigMapPropertySource(KubernetesClient client, String name, ConfigMapConfigProperties config) { + public ConfigMapPropertySource(KubernetesClient client, String name, + ConfigMapConfigProperties config) { this(client, name, null, config); } - public ConfigMapPropertySource(KubernetesClient client, String name, String[] profiles, ConfigMapConfigProperties config) { - this(client, name, null, profiles, config); - } + public ConfigMapPropertySource(KubernetesClient client, String name, + String[] profiles, ConfigMapConfigProperties config) { + this(client, name, null, profiles, config); + } - public ConfigMapPropertySource(KubernetesClient client, String name, String namespace, String[] profiles, ConfigMapConfigProperties config) { - super(getName(client, name, namespace), asObjectMap(getData(client, name, namespace, profiles, config))); - } + public ConfigMapPropertySource(KubernetesClient client, String name, String namespace, + String[] profiles, ConfigMapConfigProperties config) { + super(getName(client, name, namespace), + asObjectMap(getData(client, name, namespace, profiles, config))); + } - private static String getName(KubernetesClient client, String name, String namespace) { - return new StringBuilder() - .append(PREFIX) - .append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR) - .append(name) - .append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR) - .append(namespace == null || namespace.isEmpty() ? client.getNamespace() : namespace) - .toString(); - } + private static String getName(KubernetesClient client, String name, + String namespace) { + return new StringBuilder().append(PREFIX) + .append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR).append(name) + .append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR) + .append(namespace == null || namespace.isEmpty() ? client.getNamespace() + : namespace) + .toString(); + } - private static Map getData(KubernetesClient client, String name, String namespace, - String[] profiles, ConfigMapConfigProperties config) { - Map result = new HashMap<>(); + private static Map getData(KubernetesClient client, String name, + String namespace, String[] profiles, ConfigMapConfigProperties config) { + Map result = new HashMap<>(); if (config.isEnableApi()) { try { ConfigMap map = StringUtils.isEmpty(namespace) - ? client.configMaps().withName(name).get() - : client.configMaps().inNamespace(namespace).withName(name).get(); + ? client.configMaps().withName(name).get() + : client.configMaps().inNamespace(namespace).withName(name).get(); if (map != null) { result.putAll(processAllEntries(map.getData(), profiles)); } - } catch (Exception e) { - LOG.warn("Can't read configMap with name: [" + name + "] in namespace:[" + namespace + "]. Ignoring", e); + } + catch (Exception e) { + LOG.warn("Can't read configMap with name: [" + name + "] in namespace:[" + + namespace + "]. Ignoring", e); } } @@ -92,29 +99,35 @@ public class ConfigMapPropertySource extends KubernetesPropertySource { putPathConfig(configsFromPaths, config.getPaths()); result.putAll(processAllEntries(configsFromPaths, profiles)); return result; - } + } private static Map processAllEntries(Map input, - String[] profiles) { + String[] profiles) { Set> entrySet = input.entrySet(); - if(entrySet.size() == 1) { + if (entrySet.size() == 1) { Entry singleEntry = entrySet.iterator().next(); String propertyName = singleEntry.getKey(); String propertyValue = singleEntry.getValue(); if (propertyName.endsWith(".yml") || propertyName.endsWith(".yaml")) { if (LOG.isDebugEnabled()) { - LOG.debug("The single property with name: [" + propertyName + "] will be treated as a yaml file"); + LOG.debug("The single property with name: [" + propertyName + + "] will be treated as a yaml file"); } - return yamlParserGenerator(profiles).andThen(PROPERTIES_TO_MAP).apply(propertyValue); - } else if (propertyName.endsWith(".properties")) { + return yamlParserGenerator(profiles).andThen(PROPERTIES_TO_MAP) + .apply(propertyValue); + } + else if (propertyName.endsWith(".properties")) { if (LOG.isDebugEnabled()) { - LOG.debug("The single property with name: [" + propertyName + "] will be treated as a properties file"); + LOG.debug("The single property with name: [" + propertyName + + "] will be treated as a properties file"); } - return KEY_VALUE_TO_PROPERTIES.andThen(PROPERTIES_TO_MAP).apply(propertyValue); - } else { + return KEY_VALUE_TO_PROPERTIES.andThen(PROPERTIES_TO_MAP) + .apply(propertyValue); + } + else { return defaultProcessAllEntries(input, profiles); } } @@ -123,42 +136,48 @@ public class ConfigMapPropertySource extends KubernetesPropertySource { } private static Map defaultProcessAllEntries(Map input, - String[] profiles) { + String[] profiles) { return input.entrySet().stream() - .map(e -> extractProperties(e.getKey(), e.getValue(), profiles)) - .filter(m -> !m.isEmpty()) - .flatMap(m -> m.entrySet().stream()) - .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); + .map(e -> extractProperties(e.getKey(), e.getValue(), profiles)) + .filter(m -> !m.isEmpty()).flatMap(m -> m.entrySet().stream()) + .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); } - private static Map extractProperties(String resourceName, String content, String[] profiles) { + private static Map extractProperties(String resourceName, + String content, String[] profiles) { Map result = new HashMap<>(); - if (resourceName.equals(APPLICATION_YAML) || resourceName.equals(APPLICATION_YML)) { - result.putAll(yamlParserGenerator(profiles).andThen(PROPERTIES_TO_MAP).apply(content)); - } else if (resourceName.equals(APPLICATION_PROPERTIES)) { - result.putAll(KEY_VALUE_TO_PROPERTIES.andThen(PROPERTIES_TO_MAP).apply(content)); - } else { + if (resourceName.equals(APPLICATION_YAML) + || resourceName.equals(APPLICATION_YML)) { + result.putAll(yamlParserGenerator(profiles).andThen(PROPERTIES_TO_MAP) + .apply(content)); + } + else if (resourceName.equals(APPLICATION_PROPERTIES)) { + result.putAll( + KEY_VALUE_TO_PROPERTIES.andThen(PROPERTIES_TO_MAP).apply(content)); + } + else { result.put(resourceName, content); } - return result; - } + return result; + } - private static Map asObjectMap(Map source) { - return source.entrySet() - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); - } + private static Map asObjectMap(Map source) { + return source.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } - private static Function yamlParserGenerator(final String[] profiles) { + private static Function yamlParserGenerator( + final String[] profiles) { return s -> { YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); yamlFactory.setDocumentMatchers(properties -> { String profileProperty = properties.getProperty("spring.profiles"); if (profileProperty != null && profileProperty.length() > 0) { return asList(profiles).contains(profileProperty) ? FOUND : NOT_FOUND; - } else { + } + else { return ABSTAIN; } }); @@ -167,20 +186,19 @@ public class ConfigMapPropertySource extends KubernetesPropertySource { }; } - private static final Function KEY_VALUE_TO_PROPERTIES = s -> { - Properties properties = new Properties(); - try { - properties.load(new ByteArrayInputStream(s.getBytes())); - return properties; - } catch (IOException e) { - throw new IllegalArgumentException(); - } - }; - - private static final Function> PROPERTIES_TO_MAP = p -> p.entrySet().stream() - .collect(Collectors.toMap( - e -> String.valueOf(e.getKey()), - e -> String.valueOf(e.getValue()))); + private static final Function KEY_VALUE_TO_PROPERTIES = s -> { + Properties properties = new Properties(); + try { + properties.load(new ByteArrayInputStream(s.getBytes())); + return properties; + } + catch (IOException e) { + throw new IllegalArgumentException(); + } + }; + private static final Function> PROPERTIES_TO_MAP = p -> p + .entrySet().stream().collect(Collectors.toMap(e -> String.valueOf(e.getKey()), + e -> String.valueOf(e.getValue()))); } diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySourceLocator.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySourceLocator.java index 38c1e952..d16e892d 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySourceLocator.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigMapPropertySourceLocator.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -17,11 +17,10 @@ package org.springframework.cloud.kubernetes.config; -import static org.springframework.cloud.kubernetes.config.ConfigUtils.getApplicationName; -import static org.springframework.cloud.kubernetes.config.ConfigUtils.getApplicationNamespace; +import java.util.List; import io.fabric8.kubernetes.client.KubernetesClient; -import java.util.List; + import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.cloud.kubernetes.config.ConfigMapConfigProperties.NormalizedSource; import org.springframework.core.annotation.Order; @@ -31,47 +30,50 @@ import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; +import static org.springframework.cloud.kubernetes.config.ConfigUtils.getApplicationName; +import static org.springframework.cloud.kubernetes.config.ConfigUtils.getApplicationNamespace; + @Order(0) public class ConfigMapPropertySourceLocator implements PropertySourceLocator { - private final KubernetesClient client; - private final ConfigMapConfigProperties properties; + private final KubernetesClient client; + private final ConfigMapConfigProperties properties; - public ConfigMapPropertySourceLocator(KubernetesClient client, ConfigMapConfigProperties properties) { - this.client = client; - this.properties = properties; - } + public ConfigMapPropertySourceLocator(KubernetesClient client, + ConfigMapConfigProperties properties) { + this.client = client; + this.properties = properties; + } - @Override - public PropertySource locate(Environment environment) { - if (environment instanceof ConfigurableEnvironment) { - ConfigurableEnvironment env = (ConfigurableEnvironment) environment; + @Override + public PropertySource locate(Environment environment) { + if (environment instanceof ConfigurableEnvironment) { + ConfigurableEnvironment env = (ConfigurableEnvironment) environment; - List sources = - properties.determineSources(); + List sources = properties + .determineSources(); if (sources.size() == 1) { return getMapPropertySourceForSingleConfigMap(env, sources.get(0)); } - CompositePropertySource composite = new CompositePropertySource("composite-configmap"); - sources.forEach(s -> - composite.addFirstPropertySource(getMapPropertySourceForSingleConfigMap(env, s)) - ); + CompositePropertySource composite = new CompositePropertySource( + "composite-configmap"); + sources.forEach(s -> composite.addFirstPropertySource( + getMapPropertySourceForSingleConfigMap(env, s))); return composite; } - return null; - } + return null; + } private MapPropertySource getMapPropertySourceForSingleConfigMap( - ConfigurableEnvironment environment, NormalizedSource normalizedSource) { + ConfigurableEnvironment environment, NormalizedSource normalizedSource) { - String configurationTarget = properties.getConfigurationTarget(); - return new ConfigMapPropertySource( - client, - getApplicationName(environment, normalizedSource.getName(), configurationTarget), - getApplicationNamespace(client, normalizedSource.getNamespace(), configurationTarget), - environment.getActiveProfiles(), - properties - ); + String configurationTarget = properties.getConfigurationTarget(); + return new ConfigMapPropertySource(client, + getApplicationName(environment, normalizedSource.getName(), + configurationTarget), + getApplicationNamespace(client, normalizedSource.getNamespace(), + configurationTarget), + environment.getActiveProfiles(), properties); } } diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigUtils.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigUtils.java index 1271b537..cfb15ef9 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigUtils.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/ConfigUtils.java @@ -1,12 +1,29 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.config; +import io.fabric8.kubernetes.client.KubernetesClient; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; -import io.fabric8.kubernetes.client.KubernetesClient; - import static org.springframework.cloud.kubernetes.config.Constants.FALLBACK_APPLICATION_NAME; import static org.springframework.cloud.kubernetes.config.Constants.SPRING_APPLICATION_NAME; @@ -14,36 +31,38 @@ public class ConfigUtils { private static final Log LOG = LogFactory.getLog(ConfigUtils.class); - public static String getApplicationName(Environment env, - String configName, String configurationTarget) { - String name = configName; - if (StringUtils.isEmpty(name)) { - //TODO: use relaxed binding - if (LOG.isDebugEnabled()) { - LOG.debug(configurationTarget + - " name has not been set, taking it from property/env " + - SPRING_APPLICATION_NAME + " (default=" + FALLBACK_APPLICATION_NAME + ")"); - } + public static String getApplicationName( + Environment env, String configName, String configurationTarget) { + String name = configName; + if (StringUtils.isEmpty(name)) { + // TODO: use relaxed binding + if (LOG.isDebugEnabled()) { + LOG.debug(configurationTarget + + " name has not been set, taking it from property/env " + + SPRING_APPLICATION_NAME + " (default=" + + FALLBACK_APPLICATION_NAME + ")"); + } - name = env.getProperty(SPRING_APPLICATION_NAME, FALLBACK_APPLICATION_NAME); - } + name = env.getProperty(SPRING_APPLICATION_NAME, FALLBACK_APPLICATION_NAME); + } - return name; - } + return name; + } - public static String getApplicationNamespace( - KubernetesClient client, String configNamespace, String configurationTarget) { - String namespace = configNamespace; - if (StringUtils.isEmpty(namespace)) { - if (LOG.isDebugEnabled()) { - LOG.debug( - configurationTarget + " namespace has not been set, taking it from client (ns="+client.getNamespace()+")"); - } + public static String getApplicationNamespace( + KubernetesClient client, String configNamespace, String configurationTarget) { + String namespace = configNamespace; + if (StringUtils.isEmpty(namespace)) { + if (LOG.isDebugEnabled()) { + LOG.debug(configurationTarget + + " namespace has not been set, taking it from client (ns=" + + client.getNamespace() + ")"); + } - namespace = client.getNamespace(); - } + namespace = client.getNamespace(); + } - return namespace; - } + return namespace; + } } diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/Constants.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/Constants.java index 97695ffc..fe4cd2b4 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/Constants.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/Constants.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -17,10 +17,10 @@ package org.springframework.cloud.kubernetes.config; final class Constants { - static final String SPRING_APPLICATION_NAME = "spring.application.name"; - static final String FALLBACK_APPLICATION_NAME = "application"; - static final String PROPERTY_SOURCE_NAME_SEPARATOR = "."; + static final String SPRING_APPLICATION_NAME = "spring.application.name"; + static final String FALLBACK_APPLICATION_NAME = "application"; + static final String PROPERTY_SOURCE_NAME_SEPARATOR = "."; - private Constants() { - } + private Constants() { + } } diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/KubernetesPropertySource.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/KubernetesPropertySource.java index b4695185..8ee235a9 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/KubernetesPropertySource.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/KubernetesPropertySource.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2017 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsConfigProperties.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsConfigProperties.java index 3ab63a00..0655b551 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsConfigProperties.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsConfigProperties.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -26,39 +26,38 @@ import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties("spring.cloud.kubernetes.secrets") public class SecretsConfigProperties extends AbstractConfigProperties { - private static final String TARGET = "Secret"; + private static final String TARGET = "Secret"; - private boolean enableApi = false; - private Map labels = new HashMap<>(); - private List paths = new LinkedList<>(); + private boolean enableApi = false; + private Map labels = new HashMap<>(); + private List paths = new LinkedList<>(); + public boolean isEnableApi() { + return enableApi; + } - public boolean isEnableApi() { - return enableApi; - } + public void setEnableApi(boolean enableApi) { + this.enableApi = enableApi; + } - public void setEnableApi(boolean enableApi) { - this.enableApi = enableApi; - } + public void setLabels(Map labels) { + this.labels = labels; + } - public void setLabels(Map labels) { - this.labels = labels; - } + public Map getLabels() { + return labels; + } - public Map getLabels() { - return labels; - } + public void setPaths(List paths) { + this.paths = paths; + } - public void setPaths(List paths) { - this.paths = paths; - } + public List getPaths() { + return paths; + } - public List getPaths() { - return paths; - } - - @Override - public String getConfigurationTarget() { - return TARGET; - } + @Override + public String getConfigurationTarget() { + return TARGET; + } } diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsPropertySource.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsPropertySource.java index 73ea52c8..44556004 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsPropertySource.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsPropertySource.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -22,99 +22,90 @@ import java.util.Map; import io.fabric8.kubernetes.api.model.Secret; import io.fabric8.kubernetes.client.KubernetesClient; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; -import static org.springframework.cloud.kubernetes.config.ConfigUtils.*; +import static org.springframework.cloud.kubernetes.config.ConfigUtils.getApplicationName; +import static org.springframework.cloud.kubernetes.config.ConfigUtils.getApplicationNamespace; public class SecretsPropertySource extends KubernetesPropertySource { - private static final Log LOG = LogFactory.getLog(SecretsPropertySource.class); + private static final Log LOG = LogFactory.getLog(SecretsPropertySource.class); - private static final String PREFIX = "secrets"; + private static final String PREFIX = "secrets"; - public SecretsPropertySource(KubernetesClient client, Environment env, SecretsConfigProperties config) { - super( - getSourceName(client, env, config), - getSourceData(client, env, config) - ); - } + public SecretsPropertySource(KubernetesClient client, Environment env, + SecretsConfigProperties config) { + super(getSourceName(client, env, config), getSourceData(client, env, config)); + } - private static String getSourceName(KubernetesClient client, Environment env, SecretsConfigProperties config) { - return new StringBuilder() - .append(PREFIX) - .append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR) - .append(getApplicationName(env, config.getName(), config.getConfigurationTarget())) - .append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR) - .append(getApplicationNamespace(client, config.getNamespace(), - config.getConfigurationTarget())) - .toString(); - } + private static String getSourceName(KubernetesClient client, Environment env, + SecretsConfigProperties config) { + return new StringBuilder().append(PREFIX) + .append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR) + .append(getApplicationName(env, config.getName(), + config.getConfigurationTarget())) + .append(Constants.PROPERTY_SOURCE_NAME_SEPARATOR) + .append(getApplicationNamespace(client, config.getNamespace(), + config.getConfigurationTarget())) + .toString(); + } - private static Map getSourceData(KubernetesClient client, Environment env, SecretsConfigProperties config) { - String name = getApplicationName(env, config.getName(), config.getConfigurationTarget()); - String namespace = getApplicationNamespace(client, config.getNamespace(), - config.getConfigurationTarget()); - Map result = new HashMap<>(); + private static Map getSourceData(KubernetesClient client, + Environment env, SecretsConfigProperties config) { + String name = getApplicationName(env, config.getName(), + config.getConfigurationTarget()); + String namespace = getApplicationNamespace(client, config.getNamespace(), + config.getConfigurationTarget()); + Map result = new HashMap<>(); - if (config.isEnableApi()) { - try { - // Read for secrets api (named) + if (config.isEnableApi()) { + try { + // Read for secrets api (named) Secret secret; - if (StringUtils.isEmpty(namespace)) { - secret = client.secrets() - .withName(name) - .get(); - } else { - secret = client.secrets() - .inNamespace(namespace) - .withName(name) - .get(); - } - putAll(secret, result); + if (StringUtils.isEmpty(namespace)) { + secret = client.secrets().withName(name).get(); + } + else { + secret = client.secrets().inNamespace(namespace).withName(name).get(); + } + putAll(secret, result); - // Read for secrets api (label) - if (!config.getLabels().isEmpty()) { - if (StringUtils.isEmpty(namespace)) { - client.secrets() - .withLabels(config.getLabels()) - .list() - .getItems() - .forEach(s -> putAll(s, result)); - } else { - client.secrets() - .inNamespace(namespace) - .withLabels(config.getLabels()) - .list() - .getItems() - .forEach(s -> putAll(s, result)); - } - } - } catch (Exception e) { - LOG.warn("Can't read secret with name: [" + name + - "] or labels [" + config.getLabels() + - "] in namespace:[" + namespace + - "] (cause: " + e.getMessage() + "). Ignoring"); - } - } + // Read for secrets api (label) + if (!config.getLabels().isEmpty()) { + if (StringUtils.isEmpty(namespace)) { + client.secrets().withLabels(config.getLabels()).list().getItems() + .forEach(s -> putAll(s, result)); + } + else { + client.secrets().inNamespace(namespace) + .withLabels(config.getLabels()).list().getItems() + .forEach(s -> putAll(s, result)); + } + } + } + catch (Exception e) { + LOG.warn("Can't read secret with name: [" + name + "] or labels [" + + config.getLabels() + "] in namespace:[" + namespace + + "] (cause: " + e.getMessage() + "). Ignoring"); + } + } - // read for secrets mount + // read for secrets mount putPathConfig(result, config.getPaths()); return result; - } + } // ***************************** - // Helpers - // ***************************** - private static void putAll(Secret secret, Map result) { - if (secret != null && secret.getData() != null) { - secret.getData().forEach((k, v) -> result.put( - k, - new String(Base64.getDecoder().decode(v)).trim()) - ); - } - } + // Helpers + // ***************************** + private static void putAll(Secret secret, Map result) { + if (secret != null && secret.getData() != null) { + secret.getData().forEach((k, v) -> result.put(k, + new String(Base64.getDecoder().decode(v)).trim())); + } + } } diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsPropertySourceLocator.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsPropertySourceLocator.java index 6674e920..fec0020e 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsPropertySourceLocator.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/SecretsPropertySourceLocator.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -18,27 +18,27 @@ package org.springframework.cloud.kubernetes.config; import io.fabric8.kubernetes.client.KubernetesClient; + import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.annotation.Order; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; -import org.springframework.core.env.PropertySource; @Order(1) public class SecretsPropertySourceLocator implements PropertySourceLocator { - private final KubernetesClient client; - private final SecretsConfigProperties properties; + private final KubernetesClient client; + private final SecretsConfigProperties properties; - public SecretsPropertySourceLocator(KubernetesClient client, SecretsConfigProperties properties) { - this.client = client; - this.properties = properties; - } + public SecretsPropertySourceLocator(KubernetesClient client, + SecretsConfigProperties properties) { + this.client = client; + this.properties = properties; + } - @Override - public MapPropertySource locate(Environment environment) { - return environment instanceof ConfigurableEnvironment - ? new SecretsPropertySource(client, environment, properties) - : null; - } + @Override + public MapPropertySource locate(Environment environment) { + return environment instanceof ConfigurableEnvironment + ? new SecretsPropertySource(client, environment, properties) : null; + } } diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfiguration.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfiguration.java index 2258ba52..198dadf3 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfiguration.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfiguration.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -17,21 +17,20 @@ package org.springframework.cloud.kubernetes.config.reload; import io.fabric8.kubernetes.client.KubernetesClient; -import org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration; -import org.springframework.boot.actuate.info.InfoEndpoint; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; -import org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration; -import org.springframework.cloud.kubernetes.config.ConfigMapPropertySourceLocator; -import org.springframework.cloud.kubernetes.config.SecretsPropertySourceLocator; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration; import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.cloud.context.restart.RestartEndpoint; +import org.springframework.cloud.kubernetes.config.ConfigMapPropertySourceLocator; +import org.springframework.cloud.kubernetes.config.SecretsPropertySourceLocator; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -44,64 +43,77 @@ import org.springframework.scheduling.annotation.EnableScheduling; */ @Configuration @ConditionalOnProperty(value = "spring.cloud.kubernetes.enabled", matchIfMissing = true) -@AutoConfigureAfter({InfoEndpointAutoConfiguration.class, RefreshEndpointAutoConfiguration.class, RefreshAutoConfiguration.class}) +@AutoConfigureAfter({ InfoEndpointAutoConfiguration.class, + RefreshEndpointAutoConfiguration.class, RefreshAutoConfiguration.class }) @EnableConfigurationProperties(ConfigReloadProperties.class) public class ConfigReloadAutoConfiguration { - /** - * Configuration reload must be enabled explicitly. - */ - @ConditionalOnProperty(value = "spring.cloud.kubernetes.reload.enabled") - @ConditionalOnClass({RestartEndpoint.class, ContextRefresher.class}) - @EnableScheduling - @EnableAsync - protected static class ConfigReloadAutoConfigurationBeans { + /** + * Configuration reload must be enabled explicitly. + */ + @ConditionalOnProperty(value = "spring.cloud.kubernetes.reload.enabled") + @ConditionalOnClass({ RestartEndpoint.class, ContextRefresher.class }) + @EnableScheduling + @EnableAsync + protected static class ConfigReloadAutoConfigurationBeans { - @Autowired - private AbstractEnvironment environment; + @Autowired + private AbstractEnvironment environment; - @Autowired - private KubernetesClient kubernetesClient; + @Autowired + private KubernetesClient kubernetesClient; - @Autowired - private ConfigMapPropertySourceLocator configMapPropertySourceLocator; + @Autowired + private ConfigMapPropertySourceLocator configMapPropertySourceLocator; - @Autowired - private SecretsPropertySourceLocator secretsPropertySourceLocator; + @Autowired + private SecretsPropertySourceLocator secretsPropertySourceLocator; - /** - * Provides a bean that listen to configuration changes and fire a reload. - */ - @Bean - @ConditionalOnMissingBean - 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); - } - throw new IllegalStateException("Unsupported configuration reload mode: " + properties.getMode()); - } + /** + * Provides a bean that listen to configuration changes and fire a reload. + */ + @Bean + @ConditionalOnMissingBean + 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); + } + throw new IllegalStateException( + "Unsupported configuration reload mode: " + properties.getMode()); + } - /** - * Provides the action to execute when the configuration changes. - */ - @Bean - @ConditionalOnMissingBean - public ConfigurationUpdateStrategy configurationUpdateStrategy(ConfigReloadProperties properties, ConfigurableApplicationContext ctx, RestartEndpoint restarter, ContextRefresher refresher) { - switch (properties.getStrategy()) { - case RESTART_CONTEXT: - return new ConfigurationUpdateStrategy(properties.getStrategy().name(), restarter::restart); - case REFRESH: - return new ConfigurationUpdateStrategy(properties.getStrategy().name(), refresher::refresh); - case SHUTDOWN: - return new ConfigurationUpdateStrategy(properties.getStrategy().name(), ctx::close); - } - throw new IllegalStateException("Unsupported configuration update strategy: " + properties.getStrategy()); - } + /** + * Provides the action to execute when the configuration changes. + */ + @Bean + @ConditionalOnMissingBean + public ConfigurationUpdateStrategy configurationUpdateStrategy( + ConfigReloadProperties properties, ConfigurableApplicationContext ctx, + RestartEndpoint restarter, ContextRefresher refresher) { + switch (properties.getStrategy()) { + case RESTART_CONTEXT: + return new ConfigurationUpdateStrategy(properties.getStrategy().name(), + restarter::restart); + case REFRESH: + return new ConfigurationUpdateStrategy(properties.getStrategy().name(), + refresher::refresh); + case SHUTDOWN: + return new ConfigurationUpdateStrategy(properties.getStrategy().name(), + ctx::close); + } + throw new IllegalStateException("Unsupported configuration update strategy: " + + properties.getStrategy()); + } - } + } } diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadProperties.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadProperties.java index 7320d65e..f55321a4 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadProperties.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadProperties.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -24,117 +24,120 @@ import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "spring.cloud.kubernetes.reload") public class ConfigReloadProperties { - /** - * Enables the Kubernetes configuration reload on change. - */ - private boolean enabled = false; + /** + * Enables the Kubernetes configuration reload on change. + */ + private boolean enabled = false; - /** - * Enables monitoring on config maps to detect changes. - */ - private boolean monitoringConfigMaps = true; + /** + * Enables monitoring on config maps to detect changes. + */ + private boolean monitoringConfigMaps = true; - /** - * Enables monitoring on secrets to detect changes. - */ - private boolean monitoringSecrets = false; + /** + * Enables monitoring on secrets to detect changes. + */ + private boolean monitoringSecrets = false; - /** - * Sets the reload strategy for Kubernetes configuration reload on change. - */ - private ReloadStrategy strategy = ReloadStrategy.REFRESH; + /** + * Sets the reload strategy for Kubernetes configuration reload on change. + */ + private ReloadStrategy strategy = ReloadStrategy.REFRESH; - /** - * Sets the detection mode for Kubernetes configuration reload. - */ - private ReloadDetectionMode mode = ReloadDetectionMode.EVENT; + /** + * Sets the detection mode for Kubernetes configuration reload. + */ + private ReloadDetectionMode mode = ReloadDetectionMode.EVENT; - /** - * Sets the polling period in milliseconds to use when the detection mode is POLLING. - */ - private Long period = 15000L; + /** + * Sets the polling period in milliseconds to use when the detection mode is POLLING. + */ + private Long period = 15000L; - public ConfigReloadProperties() { - } + public ConfigReloadProperties() { + } - public boolean isEnabled() { - return enabled; - } + public boolean isEnabled() { + return enabled; + } - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } - public boolean isMonitoringConfigMaps() { - return monitoringConfigMaps; - } + public boolean isMonitoringConfigMaps() { + return monitoringConfigMaps; + } - public void setMonitoringConfigMaps(boolean monitoringConfigMaps) { - this.monitoringConfigMaps = monitoringConfigMaps; - } + public void setMonitoringConfigMaps(boolean monitoringConfigMaps) { + this.monitoringConfigMaps = monitoringConfigMaps; + } - public boolean isMonitoringSecrets() { - return monitoringSecrets; - } + public boolean isMonitoringSecrets() { + return monitoringSecrets; + } - public void setMonitoringSecrets(boolean monitoringSecrets) { - this.monitoringSecrets = monitoringSecrets; - } + public void setMonitoringSecrets(boolean monitoringSecrets) { + this.monitoringSecrets = monitoringSecrets; + } - public ReloadStrategy getStrategy() { - return strategy; - } + public ReloadStrategy getStrategy() { + return strategy; + } - public void setStrategy(ReloadStrategy strategy) { - this.strategy = strategy; - } + public void setStrategy(ReloadStrategy strategy) { + this.strategy = strategy; + } - public ReloadDetectionMode getMode() { - return mode; - } + public ReloadDetectionMode getMode() { + return mode; + } - public Long getPeriod() { - return period; - } + public Long getPeriod() { + return period; + } - public void setPeriod(Long period) { - this.period = period; - } + public void setPeriod(Long period) { + this.period = period; + } - public void setMode(ReloadDetectionMode mode) { - this.mode = mode; - } + public void setMode(ReloadDetectionMode mode) { + this.mode = mode; + } - public enum ReloadStrategy { - /** - * Fire a refresh of beans annotated with @ConfigurationProperties or @RefreshScope. - */ - REFRESH, + public enum ReloadStrategy { + /** + * Fire a refresh of beans annotated with @ConfigurationProperties + * or @RefreshScope. + */ + REFRESH, - /** - * Restarts the Spring ApplicationContext to apply the new configuration. - */ - RESTART_CONTEXT, + /** + * Restarts the Spring ApplicationContext to apply the new configuration. + */ + RESTART_CONTEXT, - /** - * Shuts down the Spring ApplicationContext to activate a restart of the container. - * Make sure that the lifecycle of all non-daemon threads is bound to the ApplicationContext and that - * a replication controller or replica set is configured to restart the pod. - */ - SHUTDOWN - } + /** + * Shuts down the Spring ApplicationContext to activate a restart of the + * container. Make sure that the lifecycle of all non-daemon threads is bound to + * the ApplicationContext and that a replication controller or replica set is + * configured to restart the pod. + */ + SHUTDOWN + } - public enum ReloadDetectionMode { - /** - * Enables a polling task that retrieves periodically all external properties and - * fire a reload when they change. - */ - POLLING, + public enum ReloadDetectionMode { + /** + * Enables a polling task that retrieves periodically all external properties and + * fire a reload when they change. + */ + POLLING, - /** - * Listens to Kubernetes events and checks if a reload is needed when configmaps or secrets change. - */ - EVENT - } + /** + * Listens to Kubernetes events and checks if a reload is needed when configmaps + * or secrets change. + */ + EVENT + } } diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigurationChangeDetector.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigurationChangeDetector.java index fa55b9bc..b03085c0 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigurationChangeDetector.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigurationChangeDetector.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -16,15 +16,18 @@ */ package org.springframework.cloud.kubernetes.config.reload; -import io.fabric8.kubernetes.client.KubernetesClient; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; + import javax.annotation.PreDestroy; + +import io.fabric8.kubernetes.client.KubernetesClient; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.ConfigurableEnvironment; @@ -33,61 +36,67 @@ import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; /** - * This is the superclass of all beans that can listen to changes in the configuration and fire a reload. + * This is the superclass of all beans that can listen to changes in the configuration and + * fire a reload. */ public abstract class ConfigurationChangeDetector { - protected Log log = LogFactory.getLog(getClass()); + protected Log log = LogFactory.getLog(getClass()); - protected ConfigurableEnvironment environment; + protected ConfigurableEnvironment environment; - protected ConfigReloadProperties properties; + protected ConfigReloadProperties properties; - protected KubernetesClient kubernetesClient; + protected KubernetesClient kubernetesClient; - protected ConfigurationUpdateStrategy strategy; + protected ConfigurationUpdateStrategy strategy; - public ConfigurationChangeDetector(ConfigurableEnvironment environment, ConfigReloadProperties properties, KubernetesClient kubernetesClient, ConfigurationUpdateStrategy strategy) { - this.environment = environment; - this.properties = properties; - this.kubernetesClient = kubernetesClient; - this.strategy = strategy; - } + public ConfigurationChangeDetector(ConfigurableEnvironment environment, + ConfigReloadProperties properties, KubernetesClient kubernetesClient, + ConfigurationUpdateStrategy strategy) { + this.environment = environment; + this.properties = properties; + this.kubernetesClient = kubernetesClient; + this.strategy = strategy; + } - @PreDestroy - public void shutdown() { - // Ensure the kubernetes client is cleaned up from spare threads when shutting down - kubernetesClient.close(); - } + @PreDestroy + public void shutdown() { + // Ensure the kubernetes client is cleaned up from spare threads when shutting + // down + kubernetesClient.close(); + } - public void reloadProperties() { - log.info("Reloading using strategy: " + strategy.getName()); - strategy.reload(); - } + public void reloadProperties() { + log.info("Reloading using strategy: " + strategy.getName()); + strategy.reload(); + } - /** - * Determines if two property sources are different. - */ - protected boolean changed(MapPropertySource mp1, MapPropertySource mp2) { - if (mp1 == mp2) return false; - if (mp1 == null && mp2 != null || mp1 != null && mp2 == null) return true; + /** + * Determines if two property sources are different. + */ + protected boolean changed(MapPropertySource mp1, MapPropertySource mp2) { + if (mp1 == mp2) + return false; + if (mp1 == null && mp2 != null || mp1 != null && mp2 == null) + return true; - Map s1 = mp1.getSource(); - Map s2 = mp2.getSource(); + Map s1 = mp1.getSource(); + Map s2 = mp2.getSource(); - return s1 == null ? s2 != null : !s1.equals(s2); - } + return s1 == null ? s2 != null : !s1.equals(s2); + } - protected boolean changed(List l1, - List l2) { + protected boolean changed(List l1, + List l2) { - if(l1.size() != l2.size()) { + if (l1.size() != l2.size()) { log.debug("The current number of Confimap PropertySources does not match " - + "the ones loaded from the Kubernetes - No reload will take place"); + + "the ones loaded from the Kubernetes - No reload will take place"); return false; } - for(int i=0; i> S findPropertySource(Class sourceClass) { - List sources = findPropertySources(sourceClass); - if (sources.size() == 0) { - return null; - } - if (sources.size() > 1) { - log.warn("Found more than one property source of type " + sourceClass); - } - return sources.get(0); - } - - /** - * Finds all registered property sources of the given type. - */ - protected > List findPropertySources(Class sourceClass) { - List managedSources = new LinkedList<>(); - - LinkedList> sources = toLinkedList(environment.getPropertySources()); - while (!sources.isEmpty()) { - PropertySource source = sources.pop(); - if (source instanceof CompositePropertySource) { - CompositePropertySource comp = (CompositePropertySource) source; - sources.addAll(comp.getPropertySources()); - } else if (sourceClass.isInstance(source)) { - managedSources.add(sourceClass.cast(source)); - } - } - - return managedSources; - } - - private LinkedList toLinkedList(Iterable it) { - LinkedList list = new LinkedList(); - for (E e : it) { - list.add(e); - } - return list; - } + /** + * Finds one registered property source of the given type, logging a warning if + * multiple property sources of that type are available. + */ + protected > S findPropertySource(Class sourceClass) { + List sources = findPropertySources(sourceClass); + if (sources.size() == 0) { + return null; + } + if (sources.size() > 1) { + log.warn("Found more than one property source of type " + sourceClass); + } + return sources.get(0); + } /** - * Returns a list of MapPropertySource that correspond to the current state of the system - * This only handles the PropertySource objects that are returned + * Finds all registered property sources of the given type. + */ + protected > List findPropertySources( + Class sourceClass) { + List managedSources = new LinkedList<>(); + + LinkedList> sources = toLinkedList( + environment.getPropertySources()); + while (!sources.isEmpty()) { + PropertySource source = sources.pop(); + if (source instanceof CompositePropertySource) { + CompositePropertySource comp = (CompositePropertySource) source; + sources.addAll(comp.getPropertySources()); + } + else if (sourceClass.isInstance(source)) { + managedSources.add(sourceClass.cast(source)); + } + } + + return managedSources; + } + + private LinkedList toLinkedList(Iterable it) { + LinkedList list = new LinkedList(); + for (E e : it) { + list.add(e); + } + return list; + } + + /** + * Returns a list of MapPropertySource that correspond to the current state of the + * system This only handles the PropertySource objects that are returned */ protected List locateMapPropertySources( - PropertySourceLocator propertySourceLocator, Environment environment) { + PropertySourceLocator propertySourceLocator, Environment environment) { List result = new ArrayList<>(); - PropertySource propertySource= propertySourceLocator.locate(environment); - if(propertySource instanceof MapPropertySource) { + PropertySource propertySource = propertySourceLocator.locate(environment); + if (propertySource instanceof MapPropertySource) { result.add((MapPropertySource) propertySource); - } else if(propertySource instanceof CompositePropertySource) { - result.addAll(((CompositePropertySource) propertySource) - .getPropertySources() - .stream() - .filter(p -> p instanceof MapPropertySource) - .map(p -> (MapPropertySource) p) - .collect(Collectors.toList())); - } else { + } + else if (propertySource instanceof CompositePropertySource) { + result.addAll(((CompositePropertySource) propertySource).getPropertySources() + .stream().filter(p -> p instanceof MapPropertySource) + .map(p -> (MapPropertySource) p).collect(Collectors.toList())); + } + else { log.debug("Found property source that cannot be handled: " - + propertySource.getClass()); + + propertySource.getClass()); } return result; diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigurationUpdateStrategy.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigurationUpdateStrategy.java index 62ef7386..7862f59b 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigurationUpdateStrategy.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigurationUpdateStrategy.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -19,34 +19,35 @@ package org.springframework.cloud.kubernetes.config.reload; import java.util.Objects; /** - * This is the superclass of all named strategies that can be fired when the configuration changes. + * This is the superclass of all named strategies that can be fired when the configuration + * changes. */ public class ConfigurationUpdateStrategy { - private String name; + private String name; - private Runnable reloadProcedure; + private Runnable reloadProcedure; - public ConfigurationUpdateStrategy(String name, Runnable reloadProcedure) { - Objects.requireNonNull(name, "name cannot be null"); - Objects.requireNonNull(reloadProcedure, "reloadProcedure cannot be null"); - this.name = name; - this.reloadProcedure = reloadProcedure; - } + public ConfigurationUpdateStrategy(String name, Runnable reloadProcedure) { + Objects.requireNonNull(name, "name cannot be null"); + Objects.requireNonNull(reloadProcedure, "reloadProcedure cannot be null"); + this.name = name; + this.reloadProcedure = reloadProcedure; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public void reload() { - this.reloadProcedure.run(); - } + public void reload() { + this.reloadProcedure.run(); + } - @Override - public String toString() { - final StringBuilder sb = new StringBuilder("ConfigurationUpdateStrategy{"); - sb.append("name='").append(name).append('\''); - sb.append('}'); - return sb.toString(); - } + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("ConfigurationUpdateStrategy{"); + sb.append("name='").append(name).append('\''); + sb.append('}'); + return sb.toString(); + } } diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/EventBasedConfigurationChangeDetector.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/EventBasedConfigurationChangeDetector.java index 431dbe61..ca90103c 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/EventBasedConfigurationChangeDetector.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/EventBasedConfigurationChangeDetector.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -16,16 +16,19 @@ */ package org.springframework.cloud.kubernetes.config.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 java.util.HashMap; -import java.util.Map; -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; + import org.springframework.cloud.kubernetes.config.ConfigMapPropertySource; import org.springframework.cloud.kubernetes.config.ConfigMapPropertySourceLocator; import org.springframework.cloud.kubernetes.config.SecretsPropertySource; @@ -34,117 +37,124 @@ import org.springframework.core.env.AbstractEnvironment; import org.springframework.core.env.MapPropertySource; /** - * A change detector that subscribes to changes in secrets and configmaps and fire a reload when something changes. + * A change detector that subscribes to changes in secrets and configmaps and fire a + * reload when something changes. */ public class EventBasedConfigurationChangeDetector extends ConfigurationChangeDetector { - private ConfigMapPropertySourceLocator configMapPropertySourceLocator; + private ConfigMapPropertySourceLocator configMapPropertySourceLocator; - private SecretsPropertySourceLocator secretsPropertySourceLocator; + private SecretsPropertySourceLocator secretsPropertySourceLocator; - private Map watches; + private Map watches; - public EventBasedConfigurationChangeDetector(AbstractEnvironment environment, - ConfigReloadProperties properties, - KubernetesClient kubernetesClient, - ConfigurationUpdateStrategy strategy, - ConfigMapPropertySourceLocator configMapPropertySourceLocator, - SecretsPropertySourceLocator secretsPropertySourceLocator) { - super(environment, properties, kubernetesClient, strategy); + public EventBasedConfigurationChangeDetector(AbstractEnvironment environment, + ConfigReloadProperties properties, KubernetesClient kubernetesClient, + ConfigurationUpdateStrategy strategy, + ConfigMapPropertySourceLocator configMapPropertySourceLocator, + SecretsPropertySourceLocator secretsPropertySourceLocator) { + super(environment, properties, kubernetesClient, strategy); - this.configMapPropertySourceLocator = configMapPropertySourceLocator; - this.secretsPropertySourceLocator = secretsPropertySourceLocator; - this.watches = new HashMap<>(); - } + this.configMapPropertySourceLocator = configMapPropertySourceLocator; + this.secretsPropertySourceLocator = secretsPropertySourceLocator; + this.watches = new HashMap<>(); + } - @PostConstruct - public void watch() { - boolean activated = false; + @PostConstruct + public void watch() { + boolean activated = false; - if (properties.isMonitoringConfigMaps()) { - try { - String name = "config-maps-watch"; - watches.put(name, kubernetesClient.configMaps() - .watch(new Watcher() { - @Override - public void eventReceived(Action action, ConfigMap configMap) { - onEvent(configMap); - } + if (properties.isMonitoringConfigMaps()) { + try { + String name = "config-maps-watch"; + watches.put(name, + kubernetesClient.configMaps().watch(new Watcher() { + @Override + public void eventReceived(Action action, + ConfigMap configMap) { + onEvent(configMap); + } - @Override - public void onClose(KubernetesClientException e) { - } - })); - activated = true; - log.info("Added new Kubernetes watch: "+name); - } catch (Exception e) { - log.error("Error while establishing a connection to watch config maps: configuration may remain stale", e); - } - } + @Override + public void onClose(KubernetesClientException e) { + } + })); + activated = true; + log.info("Added new Kubernetes watch: " + name); + } + catch (Exception e) { + log.error( + "Error while establishing a connection to watch config maps: configuration may remain stale", + e); + } + } - if (properties.isMonitoringSecrets()) { - try { - activated = false; - String name = "secrets-watch"; - watches.put(name, kubernetesClient.secrets() - .watch(new Watcher() { - @Override - public void eventReceived(Action action, Secret secret) { - onEvent(secret); - } + if (properties.isMonitoringSecrets()) { + try { + activated = false; + String name = "secrets-watch"; + watches.put(name, kubernetesClient.secrets().watch(new Watcher() { + @Override + public void eventReceived(Action action, Secret secret) { + onEvent(secret); + } - @Override - public void onClose(KubernetesClientException e) { - } - })); - activated = true; - log.info("Added new Kubernetes watch: " + name); - } catch (Exception e) { - log.error("Error while establishing a connection to watch secrets: configuration may remain stale", e); - } - } + @Override + public void onClose(KubernetesClientException e) { + } + })); + activated = true; + log.info("Added new Kubernetes watch: " + name); + } + catch (Exception e) { + log.error( + "Error while establishing a connection to watch secrets: configuration may remain stale", + e); + } + } - if (activated) { - log.info("Kubernetes event-based configuration change detector activated"); - } - } + if (activated) { + log.info("Kubernetes event-based configuration change detector activated"); + } + } - @PreDestroy - public void unwatch() { - if (this.watches != null) { - for (Map.Entry entry : this.watches.entrySet()) { - try { - log.debug("Closing the watch "+ entry.getKey()); - entry.getValue().close(); + @PreDestroy + public void unwatch() { + if (this.watches != null) { + for (Map.Entry 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); - } - } - } - } + } + catch (Exception e) { + log.error("Error while closing the watch connection", e); + } + } + } + } - private void onEvent(ConfigMap configMap) { + private void onEvent(ConfigMap configMap) { boolean changed = changed( - locateMapPropertySources(configMapPropertySourceLocator, environment), - findPropertySources(ConfigMapPropertySource.class) - ); - if(changed) { + locateMapPropertySources(configMapPropertySourceLocator, environment), + findPropertySources(ConfigMapPropertySource.class)); + if (changed) { log.info("Detected change in config maps"); reloadProperties(); } - } - - private void onEvent(Secret secret) { - MapPropertySource currentSecretSource = findPropertySource(SecretsPropertySource.class); - if (currentSecretSource != null) { - MapPropertySource newSecretSource = secretsPropertySourceLocator.locate(environment); - if (changed(currentSecretSource, newSecretSource)) { - log.info("Detected change in secrets"); - reloadProperties(); - } - } - } + } + private void onEvent(Secret secret) { + MapPropertySource currentSecretSource = findPropertySource( + SecretsPropertySource.class); + if (currentSecretSource != null) { + MapPropertySource newSecretSource = secretsPropertySourceLocator + .locate(environment); + if (changed(currentSecretSource, newSecretSource)) { + log.info("Detected change in secrets"); + reloadProperties(); + } + } + } } diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/PollingConfigurationChangeDetector.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/PollingConfigurationChangeDetector.java index 27e13032..08812ee8 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/PollingConfigurationChangeDetector.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/PollingConfigurationChangeDetector.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -16,11 +16,14 @@ */ package org.springframework.cloud.kubernetes.config.reload; -import io.fabric8.kubernetes.client.KubernetesClient; import java.util.List; + import javax.annotation.PostConstruct; + +import io.fabric8.kubernetes.client.KubernetesClient; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.cloud.kubernetes.config.ConfigMapPropertySource; import org.springframework.cloud.kubernetes.config.ConfigMapPropertySourceLocator; import org.springframework.cloud.kubernetes.config.SecretsPropertySource; @@ -30,61 +33,63 @@ import org.springframework.core.env.MapPropertySource; import org.springframework.scheduling.annotation.Scheduled; /** - * A change detector that periodically retrieves secrets and configmaps and fire a reload when something changes. + * A change detector that periodically retrieves secrets and configmaps and fire a reload + * when something changes. */ public class PollingConfigurationChangeDetector extends ConfigurationChangeDetector { protected Log log = LogFactory.getLog(getClass()); - private ConfigMapPropertySourceLocator configMapPropertySourceLocator; + private ConfigMapPropertySourceLocator configMapPropertySourceLocator; - private SecretsPropertySourceLocator secretsPropertySourceLocator; + private SecretsPropertySourceLocator secretsPropertySourceLocator; - public PollingConfigurationChangeDetector(AbstractEnvironment environment, - ConfigReloadProperties properties, - KubernetesClient kubernetesClient, - ConfigurationUpdateStrategy strategy, - ConfigMapPropertySourceLocator configMapPropertySourceLocator, - SecretsPropertySourceLocator secretsPropertySourceLocator) { - super(environment, properties, kubernetesClient, strategy); + public PollingConfigurationChangeDetector(AbstractEnvironment environment, + ConfigReloadProperties properties, KubernetesClient kubernetesClient, + ConfigurationUpdateStrategy strategy, + ConfigMapPropertySourceLocator configMapPropertySourceLocator, + SecretsPropertySourceLocator secretsPropertySourceLocator) { + super(environment, properties, kubernetesClient, strategy); - this.configMapPropertySourceLocator = configMapPropertySourceLocator; - this.secretsPropertySourceLocator = secretsPropertySourceLocator; - } + this.configMapPropertySourceLocator = configMapPropertySourceLocator; + this.secretsPropertySourceLocator = secretsPropertySourceLocator; + } - @PostConstruct - public void init() { - log.info("Kubernetes polling configuration change detector activated"); - } + @PostConstruct + public void init() { + log.info("Kubernetes polling configuration change detector activated"); + } - @Scheduled(initialDelayString = "${spring.cloud.kubernetes.reload.period:15000}", fixedDelayString = "${spring.cloud.kubernetes.reload.period:15000}") - public void executeCycle() { + @Scheduled(initialDelayString = "${spring.cloud.kubernetes.reload.period:15000}", fixedDelayString = "${spring.cloud.kubernetes.reload.period:15000}") + public void executeCycle() { - boolean changedConfigMap = false; - if (properties.isMonitoringConfigMaps()) { - List currentConfigMapSources - = findPropertySources(ConfigMapPropertySource.class); + boolean changedConfigMap = false; + if (properties.isMonitoringConfigMaps()) { + List currentConfigMapSources = findPropertySources( + ConfigMapPropertySource.class); - if (!currentConfigMapSources.isEmpty()) { + if (!currentConfigMapSources.isEmpty()) { changedConfigMap = changed( - locateMapPropertySources(configMapPropertySourceLocator, environment), - currentConfigMapSources - ); - } - } + locateMapPropertySources(configMapPropertySourceLocator, + environment), + currentConfigMapSources); + } + } - boolean changedSecrets = false; - if (properties.isMonitoringSecrets()) { - MapPropertySource currentSecretSource = findPropertySource(SecretsPropertySource.class); - if (currentSecretSource != null) { - MapPropertySource newSecretSource = secretsPropertySourceLocator.locate(environment); - changedSecrets = changed(currentSecretSource, newSecretSource); - } - } + boolean changedSecrets = false; + if (properties.isMonitoringSecrets()) { + MapPropertySource currentSecretSource = findPropertySource( + SecretsPropertySource.class); + if (currentSecretSource != null) { + MapPropertySource newSecretSource = secretsPropertySourceLocator + .locate(environment); + changedSecrets = changed(currentSecretSource, newSecretSource); + } + } - if (changedConfigMap || changedSecrets) { - reloadProperties(); - } - } + if (changedConfigMap || changedSecrets) { + reloadProperties(); + } + } } diff --git a/spring-cloud-kubernetes-config/src/test/groovy/org/springframework/cloud/kubernetes/config/CoreTest.groovy b/spring-cloud-kubernetes-config/src/test/groovy/org/springframework/cloud/kubernetes/config/CoreTest.groovy index fbf99c18..3991be73 100644 --- a/spring-cloud-kubernetes-config/src/test/groovy/org/springframework/cloud/kubernetes/config/CoreTest.groovy +++ b/spring-cloud-kubernetes-config/src/test/groovy/org/springframework/cloud/kubernetes/config/CoreTest.groovy @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, diff --git a/spring-cloud-kubernetes-config/src/test/groovy/org/springframework/cloud/kubernetes/config/TestApplication.groovy b/spring-cloud-kubernetes-config/src/test/groovy/org/springframework/cloud/kubernetes/config/TestApplication.groovy index 25fa666d..b4e96855 100644 --- a/spring-cloud-kubernetes-config/src/test/groovy/org/springframework/cloud/kubernetes/config/TestApplication.groovy +++ b/spring-cloud-kubernetes-config/src/test/groovy/org/springframework/cloud/kubernetes/config/TestApplication.groovy @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapTestUtil.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapTestUtil.java index c2c2450d..b2d0f0d6 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapTestUtil.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapTestUtil.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.config; import io.fabric8.kubernetes.client.utils.IOHelpers; diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsSpringBootTest.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsSpringBootTest.java index d805a634..3b8fdd9a 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsSpringBootTest.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsSpringBootTest.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -30,6 +30,7 @@ import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; @@ -44,11 +45,9 @@ import static org.junit.Assert.assertEquals; * @author Charles Moulliard */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - classes = App.class, - properties = { "spring.application.name=configmap-example", - "spring.cloud.kubernetes.reload.enabled=false"} - ) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, properties = { + "spring.application.name=configmap-example", + "spring.cloud.kubernetes.reload.enabled=false" }) public class ConfigMapsSpringBootTest { @ClassRule @@ -68,23 +67,23 @@ public class ConfigMapsSpringBootTest { public static void setUpBeforeClass() { mockClient = server.getClient(); - //Configure the kubernetes master url to point to the mock server - System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); + // Configure the kubernetes master url to point to the mock server + System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, + mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false"); - System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false"); + System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, + "false"); System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test"); - HashMap data = new HashMap<>(); - data.put("bean.greeting","Hello ConfigMap, %s!"); - server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME).andReturn(200, new ConfigMapBuilder() - .withNewMetadata().withName(APPLICATION_NAME).endMetadata() - .addToData(data) - .build()) - .always(); + HashMap data = new HashMap<>(); + data.put("bean.greeting", "Hello ConfigMap, %s!"); + server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME) + .andReturn(200, new ConfigMapBuilder().withNewMetadata() + .withName(APPLICATION_NAME).endMetadata().addToData(data).build()) + .always(); } - @Before public void setUp() { RestAssured.baseURI = String.format("http://localhost:%d/api/greeting", port); @@ -92,23 +91,22 @@ public class ConfigMapsSpringBootTest { @Test public void testConfig() { - assertEquals(config.getMasterUrl(),mockClient.getConfiguration().getMasterUrl()); - assertEquals(config.getNamespace(),mockClient.getNamespace()); + assertEquals(config.getMasterUrl(), mockClient.getConfiguration().getMasterUrl()); + assertEquals(config.getNamespace(), mockClient.getNamespace()); } @Test public void testGreetingEndpoint() { - when().get() - .then() - .statusCode(200) - .body("content", is("Hello ConfigMap, World!")); + when().get().then().statusCode(200).body("content", + is("Hello ConfigMap, World!")); } @Test public void testConfigMap() { - ConfigMap configmap = mockClient.configMaps().inNamespace("test").withName(APPLICATION_NAME).get(); - HashMap keys = (HashMap) configmap.getData(); - assertEquals(keys.get("bean.greeting"),"Hello ConfigMap, %s!"); + ConfigMap configmap = mockClient.configMaps().inNamespace("test") + .withName(APPLICATION_NAME).get(); + HashMap keys = (HashMap) configmap.getData(); + assertEquals(keys.get("bean.greeting"), "Hello ConfigMap, %s!"); } } diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsTest.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsTest.java index cd8b6624..f3a64430 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsTest.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsTest.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -17,17 +17,6 @@ package org.springframework.cloud.kubernetes.config; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -import io.fabric8.kubernetes.api.model.ConfigMapBuilder; -import io.fabric8.kubernetes.api.model.ConfigMapList; -import io.fabric8.kubernetes.api.model.ConfigMapListBuilder; -import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.KubernetesServer; -import io.fabric8.kubernetes.client.utils.IOHelpers; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -35,14 +24,22 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; import java.util.Map; + +import io.fabric8.kubernetes.api.model.ConfigMapBuilder; +import io.fabric8.kubernetes.api.model.ConfigMapList; +import io.fabric8.kubernetes.api.model.ConfigMapListBuilder; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.server.mock.KubernetesServer; import org.junit.Rule; import org.junit.Test; + import org.springframework.util.FileSystemUtils; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.*; +import static org.junit.Assert.assertNull; +import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.readResourceFile; /** * @author Charles Moulliard @@ -54,7 +51,8 @@ public class ConfigMapsTest { @Test public void testConfigMapList() { - server.expect().withPath("/api/v1/namespaces/ns1/configmaps").andReturn(200, new ConfigMapListBuilder().build()).once(); + server.expect().withPath("/api/v1/namespaces/ns1/configmaps") + .andReturn(200, new ConfigMapListBuilder().build()).once(); KubernetesClient client = server.getClient(); @@ -65,19 +63,21 @@ public class ConfigMapsTest { @Test public void testConfigMapGet() { - server.expect().withPath("/api/v1/namespaces/ns2/configmaps").andReturn(200, new ConfigMapBuilder() - .withNewMetadata().withName("reload-example").endMetadata() - .addToData("KEY","123") - .build()) - .once(); + server.expect().withPath("/api/v1/namespaces/ns2/configmaps") + .andReturn(200, + new ConfigMapBuilder().withNewMetadata() + .withName("reload-example").endMetadata() + .addToData("KEY", "123").build()) + .once(); KubernetesClient client = server.getClient(); ConfigMapList configMapList = client.configMaps().inNamespace("ns2").list(); assertNotNull(configMapList); assertEquals(1, configMapList.getAdditionalProperties().size()); @SuppressWarnings("unchecked") - Map data = (Map) configMapList.getAdditionalProperties().get("data"); - assertEquals("123",data.get("KEY")); + Map data = (Map) configMapList + .getAdditionalProperties().get("data"); + assertEquals("123", data.get("KEY")); } @Test @@ -85,20 +85,18 @@ public class ConfigMapsTest { String configMapName = "app-properties-test"; String namespace = "app-props"; server.expect() - .withPath( - String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName) - ) - .andReturn(200, new ConfigMapBuilder() - .withNewMetadata().withName(configMapName).endMetadata() - .addToData("application.properties",readResourceFile("application.properties")) - .build() - ) - .once(); + .withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, + configMapName)) + .andReturn(200, new ConfigMapBuilder().withNewMetadata() + .withName(configMapName).endMetadata() + .addToData("application.properties", + readResourceFile("application.properties")) + .build()) + .once(); ConfigMapPropertySource cmps = new ConfigMapPropertySource( - server.getClient().inNamespace(namespace), configMapName, - new ConfigMapConfigProperties() - ); + server.getClient().inNamespace(namespace), configMapName, + new ConfigMapConfigProperties()); assertEquals("a", cmps.getProperty("dummy.property.string1")); assertEquals("1", cmps.getProperty("dummy.property.int1")); @@ -110,20 +108,19 @@ public class ConfigMapsTest { String configMapName = "app-yaml-test"; String namespace = "app-props"; server.expect() - .withPath( - String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName) - ) - .andReturn(200, new ConfigMapBuilder() - .withNewMetadata().withName(configMapName).endMetadata() - .addToData("application.yaml",readResourceFile("application.yaml")) - .build() - ) - .once(); + .withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, + configMapName)) + .andReturn(200, + new ConfigMapBuilder().withNewMetadata().withName(configMapName) + .endMetadata() + .addToData("application.yaml", + readResourceFile("application.yaml")) + .build()) + .once(); ConfigMapPropertySource cmps = new ConfigMapPropertySource( - server.getClient().inNamespace(namespace), configMapName, - new ConfigMapConfigProperties() - ); + server.getClient().inNamespace(namespace), configMapName, + new ConfigMapConfigProperties()); assertEquals("a", cmps.getProperty("dummy.property.string2")); assertEquals("1", cmps.getProperty("dummy.property.int2")); @@ -135,20 +132,16 @@ public class ConfigMapsTest { String configMapName = "single-non-standard-test"; String namespace = "app-props"; server.expect() - .withPath( - String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName) - ) - .andReturn(200, new ConfigMapBuilder() - .withNewMetadata().withName(configMapName).endMetadata() - .addToData("adhoc.yml",readResourceFile("adhoc.yml")) - .build() - ) - .once(); + .withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, + configMapName)) + .andReturn(200, new ConfigMapBuilder().withNewMetadata() + .withName(configMapName).endMetadata() + .addToData("adhoc.yml", readResourceFile("adhoc.yml")).build()) + .once(); ConfigMapPropertySource cmps = new ConfigMapPropertySource( - server.getClient().inNamespace(namespace), configMapName, - new ConfigMapConfigProperties() - ); + server.getClient().inNamespace(namespace), configMapName, + new ConfigMapConfigProperties()); assertEquals("a", cmps.getProperty("dummy.property.string3")); assertEquals("1", cmps.getProperty("dummy.property.int3")); @@ -160,22 +153,18 @@ public class ConfigMapsTest { String configMapName = "single-unparseable-properties-test"; String namespace = "app-props"; server.expect() - .withPath( - String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName) - ) - .andReturn(200, new ConfigMapBuilder() - .withNewMetadata().withName(configMapName).endMetadata() - .addToData("application.properties","somevalue") - .build() - ) - .once(); + .withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, + configMapName)) + .andReturn(200, + new ConfigMapBuilder().withNewMetadata().withName(configMapName) + .endMetadata() + .addToData("application.properties", "somevalue").build()) + .once(); - new ConfigMapPropertySource( - server.getClient().inNamespace(namespace), configMapName, - new ConfigMapConfigProperties() - ); + new ConfigMapPropertySource(server.getClient().inNamespace(namespace), + configMapName, new ConfigMapConfigProperties()); - //no exception is thrown for unparseable content + // no exception is thrown for unparseable content } @Test @@ -183,22 +172,18 @@ public class ConfigMapsTest { String configMapName = "single-unparseable-yaml-test"; String namespace = "app-props"; server.expect() - .withPath( - String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName) - ) - .andReturn(200, new ConfigMapBuilder() - .withNewMetadata().withName(configMapName).endMetadata() - .addToData("application.yaml","somevalue") - .build() - ) - .once(); + .withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, + configMapName)) + .andReturn(200, + new ConfigMapBuilder().withNewMetadata().withName(configMapName) + .endMetadata().addToData("application.yaml", "somevalue") + .build()) + .once(); - new ConfigMapPropertySource( - server.getClient().inNamespace(namespace), configMapName, - new ConfigMapConfigProperties() - ); + new ConfigMapPropertySource(server.getClient().inNamespace(namespace), + configMapName, new ConfigMapConfigProperties()); - //no exception is thrown for unparseable content + // no exception is thrown for unparseable content } @Test @@ -206,28 +191,28 @@ public class ConfigMapsTest { String configMapName = "app-multiple-properties-test"; String namespace = "app-props"; server.expect() - .withPath( - String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, configMapName) - ) - .andReturn(200, new ConfigMapBuilder() - .withNewMetadata().withName(configMapName).endMetadata() - .addToData("application.properties",readResourceFile("application.properties")) - .addToData("adhoc.properties",readResourceFile("adhoc.properties")) - .build() - ) - .once(); + .withPath(String.format("/api/v1/namespaces/%s/configmaps/%s", namespace, + configMapName)) + .andReturn(200, + new ConfigMapBuilder().withNewMetadata().withName(configMapName) + .endMetadata() + .addToData("application.properties", + readResourceFile("application.properties")) + .addToData("adhoc.properties", + readResourceFile("adhoc.properties")) + .build()) + .once(); ConfigMapPropertySource cmps = new ConfigMapPropertySource( - server.getClient().inNamespace(namespace), configMapName, - new ConfigMapConfigProperties() - ); + server.getClient().inNamespace(namespace), configMapName, + new ConfigMapConfigProperties()); - //application.properties should be read correctly + // application.properties should be read correctly assertEquals("a", cmps.getProperty("dummy.property.string1")); assertEquals("1", cmps.getProperty("dummy.property.int1")); assertEquals("true", cmps.getProperty("dummy.property.bool1")); - //the adhoc.properties file should not be parsed + // the adhoc.properties file should not be parsed assertNull(cmps.getProperty("dummy.property.bool2")); assertNull(cmps.getProperty("dummy.property.bool2")); assertNull(cmps.getProperty("dummy.property.bool2")); @@ -250,12 +235,16 @@ public class ConfigMapsTest { final Path filesPath = tmp.resolve("cm/files"); - createConfigMapFile(filesPath, "application.yaml", readResourceFile("application.yaml")); - createConfigMapFile(filesPath, "application.properties", readResourceFile("application.properties")); + createConfigMapFile(filesPath, "application.yaml", + readResourceFile("application.yaml")); + createConfigMapFile(filesPath, "application.properties", + readResourceFile("application.properties")); // parse ConfigMaps - cmConfProperties.setPaths(Arrays.asList(dbPath.toString(), apiPath.toString(), filesPath.toString())); - ConfigMapPropertySource cmps = new ConfigMapPropertySource(client, "testapp", cmConfProperties); + cmConfProperties.setPaths(Arrays.asList(dbPath.toString(), apiPath.toString(), + filesPath.toString())); + ConfigMapPropertySource cmps = new ConfigMapPropertySource(client, "testapp", + cmConfProperties); // assert as expected assertEquals("42", cmps.getProperty("foo.bar")); @@ -271,11 +260,12 @@ public class ConfigMapsTest { assertEquals("1", cmps.getProperty("dummy.property.int2")); assertEquals("true", cmps.getProperty("dummy.property.bool2")); - FileSystemUtils.deleteRecursively(tmp.toFile()); + FileSystemUtils.deleteRecursively(tmp.toFile()); } @Test - public void testConfigMapGetSingleApplicationPropertiesFromVolume() throws IOException { + public void testConfigMapGetSingleApplicationPropertiesFromVolume() + throws IOException { KubernetesClient client = server.getClient(); ConfigMapConfigProperties cmConfProperties = new ConfigMapConfigProperties(); cmConfProperties.setEnableApi(false); @@ -286,11 +276,13 @@ public class ConfigMapsTest { final Path filesPath = tmp.resolve("cm/files"); - createConfigMapFile(filesPath, "adhoc.properties", readResourceFile("adhoc.properties")); + createConfigMapFile(filesPath, "adhoc.properties", + readResourceFile("adhoc.properties")); // parse ConfigMaps cmConfProperties.setPaths(Collections.singletonList(filesPath.toString())); - ConfigMapPropertySource cmps = new ConfigMapPropertySource(client, "testapp", cmConfProperties); + ConfigMapPropertySource cmps = new ConfigMapPropertySource(client, "testapp", + cmConfProperties); // assert as expected assertEquals("a", cmps.getProperty("dummy.property.string4")); @@ -300,7 +292,8 @@ public class ConfigMapsTest { FileSystemUtils.deleteRecursively(tmp.toFile()); } - private void createConfigMapFile(Path basePath, String key, String value) throws IOException { + private void createConfigMapFile(Path basePath, String key, String value) + throws IOException { Files.createDirectories(basePath); final Path apiUrlFile = Files.createFile(basePath.resolve(key)); Files.write(apiUrlFile, value.getBytes(StandardCharsets.UTF_8)); diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithProfilesNoActiveProfileSpringBootTest.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithProfilesNoActiveProfileSpringBootTest.java index 71902d5a..6844f2da 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithProfilesNoActiveProfileSpringBootTest.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithProfilesNoActiveProfileSpringBootTest.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -17,35 +17,35 @@ package org.springframework.cloud.kubernetes.config; -import static io.restassured.RestAssured.when; -import static org.hamcrest.core.Is.is; -import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.readResourceFile; +import java.util.HashMap; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.server.mock.KubernetesServer; import io.restassured.RestAssured; -import java.util.HashMap; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.kubernetes.config.example.App; import org.springframework.test.context.junit4.SpringRunner; +import static io.restassured.RestAssured.when; +import static org.hamcrest.core.Is.is; +import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.readResourceFile; + /** * @author Charles Moulliard */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - classes = App.class, - properties = { "spring.application.name=configmap-with-profile-no-active-profiles-example", - "spring.cloud.kubernetes.reload.enabled=false"} - ) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, properties = { + "spring.application.name=configmap-with-profile-no-active-profiles-example", + "spring.cloud.kubernetes.reload.enabled=false" }) public class ConfigMapsWithProfilesNoActiveProfileSpringBootTest { @ClassRule @@ -65,38 +65,35 @@ public class ConfigMapsWithProfilesNoActiveProfileSpringBootTest { public static void setUpBeforeClass() { mockClient = server.getClient(); - //Configure the kubernetes master url to point to the mock server - System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); + // Configure the kubernetes master url to point to the mock server + System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, + mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false"); - System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false"); + System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, + "false"); System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test"); - HashMap data = new HashMap<>(); + HashMap data = new HashMap<>(); data.put("application.yml", readResourceFile("application-with-profiles.yaml")); - server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME).andReturn(200, new ConfigMapBuilder() - .withNewMetadata().withName(APPLICATION_NAME).endMetadata() - .addToData(data) - .build()) - .always(); + server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME) + .andReturn(200, new ConfigMapBuilder().withNewMetadata() + .withName(APPLICATION_NAME).endMetadata().addToData(data).build()) + .always(); } @Test public void testGreetingEndpoint() { RestAssured.baseURI = String.format("http://localhost:%d/api/greeting", port); - when().get() - .then() - .statusCode(200) - .body("content", is("Hello ConfigMap default, World!")); + when().get().then().statusCode(200).body("content", + is("Hello ConfigMap default, World!")); } @Test public void testFarewellEndpoint() { RestAssured.baseURI = String.format("http://localhost:%d/api/farewell", port); - when().get() - .then() - .statusCode(200) - .body("content", is("Goodbye ConfigMap default, World!")); + when().get().then().statusCode(200).body("content", + is("Goodbye ConfigMap default, World!")); } } diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithProfilesSpringBootTest.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithProfilesSpringBootTest.java index 85aa4cbf..53a5ac83 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithProfilesSpringBootTest.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithProfilesSpringBootTest.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -17,20 +17,18 @@ package org.springframework.cloud.kubernetes.config; -import static io.restassured.RestAssured.when; -import static org.hamcrest.core.Is.is; -import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.*; +import java.util.HashMap; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.server.mock.KubernetesServer; import io.restassured.RestAssured; -import java.util.HashMap; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; @@ -38,15 +36,17 @@ import org.springframework.cloud.kubernetes.config.example.App; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; +import static io.restassured.RestAssured.when; +import static org.hamcrest.core.Is.is; +import static org.springframework.cloud.kubernetes.config.ConfigMapTestUtil.readResourceFile; + /** * @author Charles Moulliard */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, - classes = App.class, - properties = { "spring.application.name=configmap-with-profile-example", - "spring.cloud.kubernetes.reload.enabled=false"} - ) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, properties = { + "spring.application.name=configmap-with-profile-example", + "spring.cloud.kubernetes.reload.enabled=false" }) @ActiveProfiles("development") public class ConfigMapsWithProfilesSpringBootTest { @@ -67,37 +67,34 @@ public class ConfigMapsWithProfilesSpringBootTest { public static void setUpBeforeClass() { mockClient = server.getClient(); - //Configure the kubernetes master url to point to the mock server - System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); + // Configure the kubernetes master url to point to the mock server + System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, + mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false"); - System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false"); + System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, + "false"); System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test"); - HashMap data = new HashMap<>(); + HashMap data = new HashMap<>(); data.put("application.yml", readResourceFile("application-with-profiles.yaml")); - server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME).andReturn(200, new ConfigMapBuilder() - .withNewMetadata().withName(APPLICATION_NAME).endMetadata() - .addToData(data) - .build()) - .always(); + server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME) + .andReturn(200, new ConfigMapBuilder().withNewMetadata() + .withName(APPLICATION_NAME).endMetadata().addToData(data).build()) + .always(); } @Test public void testGreetingEndpoint() { RestAssured.baseURI = String.format("http://localhost:%d/api/greeting", port); - when().get() - .then() - .statusCode(200) - .body("content", is("Hello ConfigMap dev, World!")); + when().get().then().statusCode(200).body("content", + is("Hello ConfigMap dev, World!")); } @Test public void testFarewellEndpoint() { RestAssured.baseURI = String.format("http://localhost:%d/api/farewell", port); - when().get() - .then() - .statusCode(200) - .body("content", is("Goodbye ConfigMap default, World!")); + when().get().then().statusCode(200).body("content", + is("Goodbye ConfigMap default, World!")); } } diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithoutProfilesSpringBootTest.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithoutProfilesSpringBootTest.java index 7a981f14..98b73afd 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithoutProfilesSpringBootTest.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/ConfigMapsWithoutProfilesSpringBootTest.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.config; import static io.restassured.RestAssured.when; diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/MultipleConfigMapsSpringBootTest.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/MultipleConfigMapsSpringBootTest.java index d88d2533..68d5b6f0 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/MultipleConfigMapsSpringBootTest.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/MultipleConfigMapsSpringBootTest.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/App.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/App.java index 078a68df..287fbad8 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/App.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/App.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -19,14 +19,13 @@ package org.springframework.cloud.kubernetes.config.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.scheduling.annotation.EnableScheduling; /** * */ @SpringBootApplication public class App { - public static void main(String[] args) { - SpringApplication.run(App.class, args); - } + public static void main(String[] args) { + SpringApplication.run(App.class, args); + } } diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/Greeting.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/Greeting.java index b9047a13..1cf849e1 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/Greeting.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/Greeting.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingController.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingController.java index bc49d70d..c36222e8 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingController.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingController.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingProperties.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingProperties.java index a5f72acb..381bb893 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingProperties.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example/GreetingProperties.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example2/ExampleApp.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example2/ExampleApp.java index df77a4ba..c61fd757 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example2/ExampleApp.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example2/ExampleApp.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.config.example2; import org.springframework.boot.SpringApplication; diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example2/ExampleAppProps.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example2/ExampleAppProps.java index f6630b3e..482d719f 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example2/ExampleAppProps.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/example2/ExampleAppProps.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.config.example2; import org.springframework.boot.context.properties.ConfigurationProperties; diff --git a/spring-cloud-kubernetes-core/pom.xml b/spring-cloud-kubernetes-core/pom.xml index 1aa6b971..10e956f3 100644 --- a/spring-cloud-kubernetes-core/pom.xml +++ b/spring-cloud-kubernetes-core/pom.xml @@ -1,18 +1,19 @@ D or(D dis, D dat) { - if (dis != null) { - return dis; - } else { - return dat; - } - } + private static D or(D dis, D dat) { + if (dis != null) { + return dis; + } + else { + return dat; + } + } } diff --git a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/KubernetesClientProperties.java b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/KubernetesClientProperties.java index f7a3ca8b..d50d7393 100644 --- a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/KubernetesClientProperties.java +++ b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/KubernetesClientProperties.java @@ -1,17 +1,18 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 + * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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. * - * 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; @@ -21,184 +22,184 @@ import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties("spring.cloud.kubernetes.client") public class KubernetesClientProperties { - private Boolean trustCerts; - private String masterUrl; - private String apiVersion; - private String namespace; - private String caCertFile; - private String caCertData; - private String clientCertFile; - private String clientCertData; - private String clientKeyFile; - private String clientKeyData; - private String clientKeyAlgo; - private String clientKeyPassphrase; - private String username; - private String password; - private Integer watchReconnectInterval; - private Integer watchReconnectLimit; - private Integer connectionTimeout; - private Integer requestTimeout; - private Long rollingTimeout; - private Integer loggingInterval; + private Boolean trustCerts; + private String masterUrl; + private String apiVersion; + private String namespace; + private String caCertFile; + private String caCertData; + private String clientCertFile; + private String clientCertData; + private String clientKeyFile; + private String clientKeyData; + private String clientKeyAlgo; + private String clientKeyPassphrase; + private String username; + private String password; + private Integer watchReconnectInterval; + private Integer watchReconnectLimit; + private Integer connectionTimeout; + private Integer requestTimeout; + private Long rollingTimeout; + private Integer loggingInterval; - public String getClientCertData() { - return clientCertData; - } + public String getClientCertData() { + return clientCertData; + } - public void setClientCertData(String clientCertData) { - this.clientCertData = clientCertData; - } + public void setClientCertData(String clientCertData) { + this.clientCertData = clientCertData; + } - public Boolean isTrustCerts() { - return trustCerts; - } + public Boolean isTrustCerts() { + return trustCerts; + } - public void setTrustCerts(Boolean trustCerts) { - this.trustCerts = trustCerts; - } + public void setTrustCerts(Boolean trustCerts) { + this.trustCerts = trustCerts; + } - public String getMasterUrl() { - return masterUrl; - } + public String getMasterUrl() { + return masterUrl; + } - public void setMasterUrl(String masterUrl) { - this.masterUrl = masterUrl; - } + public void setMasterUrl(String masterUrl) { + this.masterUrl = masterUrl; + } - public String getApiVersion() { - return apiVersion; - } + public String getApiVersion() { + return apiVersion; + } - public void setApiVersion(String apiVersion) { - this.apiVersion = apiVersion; - } + public void setApiVersion(String apiVersion) { + this.apiVersion = apiVersion; + } - public String getNamespace() { - return namespace; - } + public String getNamespace() { + return namespace; + } - public void setNamespace(String namespace) { - this.namespace = namespace; - } + public void setNamespace(String namespace) { + this.namespace = namespace; + } - public String getCaCertFile() { - return caCertFile; - } + public String getCaCertFile() { + return caCertFile; + } - public void setCaCertFile(String caCertFile) { - this.caCertFile = caCertFile; - } + public void setCaCertFile(String caCertFile) { + this.caCertFile = caCertFile; + } - public String getCaCertData() { - return caCertData; - } + public String getCaCertData() { + return caCertData; + } - public void setCaCertData(String caCertData) { - this.caCertData = caCertData; - } + public void setCaCertData(String caCertData) { + this.caCertData = caCertData; + } - public String getClientCertFile() { - return clientCertFile; - } + public String getClientCertFile() { + return clientCertFile; + } - public void setClientCertFile(String clientCertFile) { - this.clientCertFile = clientCertFile; - } + public void setClientCertFile(String clientCertFile) { + this.clientCertFile = clientCertFile; + } - public String getClientKeyFile() { - return clientKeyFile; - } + public String getClientKeyFile() { + return clientKeyFile; + } - public void setClientKeyFile(String clientKeyFile) { - this.clientKeyFile = clientKeyFile; - } + public void setClientKeyFile(String clientKeyFile) { + this.clientKeyFile = clientKeyFile; + } - public String getClientKeyData() { - return clientKeyData; - } + public String getClientKeyData() { + return clientKeyData; + } - public void setClientKeyData(String clientKeyData) { - this.clientKeyData = clientKeyData; - } + public void setClientKeyData(String clientKeyData) { + this.clientKeyData = clientKeyData; + } - public String getClientKeyAlgo() { - return clientKeyAlgo; - } + public String getClientKeyAlgo() { + return clientKeyAlgo; + } - public void setClientKeyAlgo(String clientKeyAlgo) { - this.clientKeyAlgo = clientKeyAlgo; - } + public void setClientKeyAlgo(String clientKeyAlgo) { + this.clientKeyAlgo = clientKeyAlgo; + } - public String getClientKeyPassphrase() { - return clientKeyPassphrase; - } + public String getClientKeyPassphrase() { + return clientKeyPassphrase; + } - public void setClientKeyPassphrase(String clientKeyPassphrase) { - this.clientKeyPassphrase = clientKeyPassphrase; - } + public void setClientKeyPassphrase(String clientKeyPassphrase) { + this.clientKeyPassphrase = clientKeyPassphrase; + } - public String getUsername() { - return username; - } + public String getUsername() { + return username; + } - public void setUsername(String username) { - this.username = username; - } + public void setUsername(String username) { + this.username = username; + } - public String getPassword() { - return password; - } + public String getPassword() { + return password; + } - public void setPassword(String password) { - this.password = password; - } + public void setPassword(String password) { + this.password = password; + } - public Integer getWatchReconnectInterval() { - return watchReconnectInterval; - } + public Integer getWatchReconnectInterval() { + return watchReconnectInterval; + } - public void setWatchReconnectInterval(Integer watchReconnectInterval) { - this.watchReconnectInterval = watchReconnectInterval; - } + public void setWatchReconnectInterval(Integer watchReconnectInterval) { + this.watchReconnectInterval = watchReconnectInterval; + } - public Integer getWatchReconnectLimit() { - return watchReconnectLimit; - } + public Integer getWatchReconnectLimit() { + return watchReconnectLimit; + } - public void setWatchReconnectLimit(Integer watchReconnectLimit) { - this.watchReconnectLimit = watchReconnectLimit; - } + public void setWatchReconnectLimit(Integer watchReconnectLimit) { + this.watchReconnectLimit = watchReconnectLimit; + } - public Integer getConnectionTimeout() { - return connectionTimeout; - } + public Integer getConnectionTimeout() { + return connectionTimeout; + } - public void setConnectionTimeout(Integer connectionTimeout) { - this.connectionTimeout = connectionTimeout; - } + public void setConnectionTimeout(Integer connectionTimeout) { + this.connectionTimeout = connectionTimeout; + } - public Integer getRequestTimeout() { - return requestTimeout; - } + public Integer getRequestTimeout() { + return requestTimeout; + } - public void setRequestTimeout(Integer requestTimeout) { - this.requestTimeout = requestTimeout; - } + public void setRequestTimeout(Integer requestTimeout) { + this.requestTimeout = requestTimeout; + } - public Long getRollingTimeout() { - return rollingTimeout; - } + public Long getRollingTimeout() { + return rollingTimeout; + } - public void setRollingTimeout(Long rollingTimeout) { - this.rollingTimeout = rollingTimeout; - } + public void setRollingTimeout(Long rollingTimeout) { + this.rollingTimeout = rollingTimeout; + } - public Integer getLoggingInterval() { - return loggingInterval; - } + public Integer getLoggingInterval() { + return loggingInterval; + } - public void setLoggingInterval(Integer loggingInterval) { - this.loggingInterval = loggingInterval; - } + public void setLoggingInterval(Integer loggingInterval) { + this.loggingInterval = loggingInterval; + } } diff --git a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/KubernetesHealthIndicator.java b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/KubernetesHealthIndicator.java index 67fc1beb..513e7f24 100644 --- a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/KubernetesHealthIndicator.java +++ b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/KubernetesHealthIndicator.java @@ -1,53 +1,55 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 + * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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. * - * 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; +import io.fabric8.kubernetes.api.model.Pod; + import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.Health; -import io.fabric8.kubernetes.api.model.Pod; - public class KubernetesHealthIndicator extends AbstractHealthIndicator { - private PodUtils utils; + private PodUtils utils; - public KubernetesHealthIndicator(PodUtils utils) { - this.utils = utils; - } + public KubernetesHealthIndicator(PodUtils utils) { + this.utils = utils; + } - @Override - protected void doHealthCheck(Health.Builder builder) throws Exception { - try { - Pod current = utils.currentPod().get(); - if (current != null) { - builder.up() - .withDetail("inside", true) - .withDetail("namespace", current.getMetadata().getNamespace()) - .withDetail("podName", current.getMetadata().getName()) - .withDetail("podIp", current.getStatus().getPodIP()) - .withDetail("serviceAccount", current.getSpec().getServiceAccountName()) - .withDetail("nodeName", current.getSpec().getNodeName()) - .withDetail("hostIp", current.getStatus().getHostIP()); - } else { - builder.up() - .withDetail("inside", false); - } - } catch (Exception e) { - builder.down(e); - } - } + @Override + protected void doHealthCheck(Health.Builder builder) throws Exception { + try { + Pod current = utils.currentPod().get(); + if (current != null) { + builder.up().withDetail("inside", true) + .withDetail("namespace", current.getMetadata().getNamespace()) + .withDetail("podName", current.getMetadata().getName()) + .withDetail("podIp", current.getStatus().getPodIP()) + .withDetail("serviceAccount", + current.getSpec().getServiceAccountName()) + .withDetail("nodeName", current.getSpec().getNodeName()) + .withDetail("hostIp", current.getStatus().getHostIP()); + } + else { + builder.up().withDetail("inside", false); + } + } + catch (Exception e) { + builder.down(e); + } + } } diff --git a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/LazilyInstantiate.java b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/LazilyInstantiate.java index 6bd9e49d..74129c19 100644 --- a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/LazilyInstantiate.java +++ b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/LazilyInstantiate.java @@ -1,45 +1,45 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 + * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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. * - * 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; - import java.util.function.Supplier; public class LazilyInstantiate implements Supplier { - public static LazilyInstantiate using(Supplier supplier) { - return new LazilyInstantiate(supplier); - } + public static LazilyInstantiate using(Supplier supplier) { + return new LazilyInstantiate(supplier); + } - public synchronized T get() { - return current.get(); - } + public synchronized T get() { + return current.get(); + } - private LazilyInstantiate(Supplier supplier) { - this.supplier = supplier; - this.current = () -> swapper(); - } + private LazilyInstantiate(Supplier supplier) { + this.supplier = supplier; + this.current = () -> swapper(); + } - private final Supplier supplier; - private Supplier current; + private final Supplier supplier; + private Supplier current; - private T swapper() { - T obj = supplier.get(); - current = () -> obj; - return obj; - } + private T swapper() { + T obj = supplier.get(); + current = () -> obj; + return obj; + } } diff --git a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/PodUtils.java b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/PodUtils.java index 856c06dd..86b792c2 100644 --- a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/PodUtils.java +++ b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/PodUtils.java @@ -1,35 +1,36 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 + * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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. * - * 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; -import io.fabric8.kubernetes.api.model.Pod; - import java.util.function.Supplier; +import io.fabric8.kubernetes.api.model.Pod; + public interface PodUtils { - /** - * @return A supplier of the currentPod {@link Pod}. The supplier will hold the currentPod {@Pod} if inside Kubernetes - * or false, otherwise. - */ - Supplier currentPod(); + /** + * @return A supplier of the currentPod {@link Pod}. The supplier will hold the + * currentPod {@Pod} if inside Kubernetes or false, otherwise. + */ + Supplier currentPod(); - /** - * @return true if called from within Kubernetes, false otherwise. - */ - Boolean isInsideKubernetes(); + /** + * @return true if called from within Kubernetes, false otherwise. + */ + Boolean isInsideKubernetes(); } diff --git a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/StandardPodUtils.java b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/StandardPodUtils.java index 0cbe7567..e3fcbab2 100644 --- a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/StandardPodUtils.java +++ b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/StandardPodUtils.java @@ -1,79 +1,85 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 + * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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. * - * 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; -import io.fabric8.kubernetes.api.model.Pod; -import io.fabric8.kubernetes.client.Config; -import io.fabric8.kubernetes.client.KubernetesClient; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - import java.nio.file.Paths; import java.util.function.Supplier; +import io.fabric8.kubernetes.api.model.Pod; +import io.fabric8.kubernetes.client.Config; +import io.fabric8.kubernetes.client.KubernetesClient; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + public class StandardPodUtils implements PodUtils { - private static final Log LOG = LogFactory.getLog(StandardPodUtils.class); - public static final String HOSTNAME = "HOSTNAME"; + private static final Log LOG = LogFactory.getLog(StandardPodUtils.class); + public static final String HOSTNAME = "HOSTNAME"; - private final KubernetesClient client; - private final String hostName; - private Supplier current; + private final KubernetesClient client; + private final String hostName; + private Supplier current; - public StandardPodUtils(KubernetesClient client) { - if (client == null) { - throw new IllegalArgumentException("Must provide an instance of KubernetesClient"); - } + public StandardPodUtils(KubernetesClient client) { + if (client == null) { + throw new IllegalArgumentException( + "Must provide an instance of KubernetesClient"); + } - this.client = client; - this.hostName = System.getenv(HOSTNAME); - this.current = LazilyInstantiate.using(() -> internalGetPod()); - } + this.client = client; + this.hostName = System.getenv(HOSTNAME); + this.current = LazilyInstantiate.using(() -> internalGetPod()); + } - @Override - public Supplier currentPod() { - return current; - } + @Override + public Supplier currentPod() { + return current; + } - @Override - public Boolean isInsideKubernetes() { - return currentPod().get() != null; - } + @Override + public Boolean isInsideKubernetes() { + return currentPod().get() != null; + } - private synchronized Pod internalGetPod() { - try { - if (isServiceAccountFound() && isHostNameEnvVarPresent()) { - return client.pods().withName(hostName).get(); - } else { - return null; - } - } catch (Throwable t) { - LOG.warn("Failed to get pod with name:[" + hostName + "]. You should look into this if things aren't working as you expect. Are you missing serviceaccount permissions?", t); - return null; - } - } + private synchronized Pod internalGetPod() { + try { + if (isServiceAccountFound() && isHostNameEnvVarPresent()) { + return client.pods().withName(hostName).get(); + } + else { + return null; + } + } + catch (Throwable t) { + LOG.warn("Failed to get pod with name:[" + hostName + + "]. You should look into this if things aren't working as you expect. Are you missing serviceaccount permissions?", + t); + return null; + } + } - private boolean isHostNameEnvVarPresent() { - return hostName != null && !hostName.isEmpty(); - } + private boolean isHostNameEnvVarPresent() { + return hostName != null && !hostName.isEmpty(); + } - private boolean isServiceAccountFound() { - return Paths.get(Config.KUBERNETES_SERVICE_ACCOUNT_TOKEN_PATH).toFile().exists() && - Paths.get(Config.KUBERNETES_SERVICE_ACCOUNT_CA_CRT_PATH).toFile().exists(); - } + private boolean isServiceAccountFound() { + return Paths.get(Config.KUBERNETES_SERVICE_ACCOUNT_TOKEN_PATH).toFile().exists() + && Paths.get(Config.KUBERNETES_SERVICE_ACCOUNT_CA_CRT_PATH).toFile() + .exists(); + } } diff --git a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/profile/KubernetesApplicationContextInitializer.java b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/profile/KubernetesApplicationContextInitializer.java index 22396a03..72f60c0c 100644 --- a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/profile/KubernetesApplicationContextInitializer.java +++ b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/profile/KubernetesApplicationContextInitializer.java @@ -1,23 +1,26 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 + * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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. * - * 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.profile; -import io.fabric8.kubernetes.client.DefaultKubernetesClient; import java.util.function.Supplier; + +import io.fabric8.kubernetes.client.DefaultKubernetesClient; + import org.springframework.cloud.kubernetes.LazilyInstantiate; import org.springframework.cloud.kubernetes.StandardPodUtils; import org.springframework.context.ApplicationContextInitializer; @@ -25,34 +28,33 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.Ordered; public class KubernetesApplicationContextInitializer implements - ApplicationContextInitializer, Ordered { + ApplicationContextInitializer, Ordered { - private static final int ORDER = 100; + private static final int ORDER = 100; - private final Supplier listenerSupplier; + private final Supplier listenerSupplier; public KubernetesApplicationContextInitializer() { this(LazilyInstantiate.using(() -> - //If we are inside Kubernetes this should be perfectly valid. - //If not then we won't add the Kubernetes profile anyway. - new KubernetesProfileApplicationListener( - new StandardPodUtils(new DefaultKubernetesClient())) - )); + // If we are inside Kubernetes this should be perfectly valid. + // If not then we won't add the Kubernetes profile anyway. + new KubernetesProfileApplicationListener( + new StandardPodUtils(new DefaultKubernetesClient())))); } public KubernetesApplicationContextInitializer( - Supplier listenerSupplier) { + Supplier listenerSupplier) { this.listenerSupplier = listenerSupplier; } @Override - public int getOrder() { - return ORDER; - } + public int getOrder() { + return ORDER; + } - @Override - public void initialize(ConfigurableApplicationContext applicationContext) { + @Override + public void initialize(ConfigurableApplicationContext applicationContext) { listenerSupplier.get().addKubernetesProfile(applicationContext.getEnvironment()); - } + } } diff --git a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/profile/KubernetesProfileApplicationListener.java b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/profile/KubernetesProfileApplicationListener.java index 997537b6..a369b40b 100644 --- a/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/profile/KubernetesProfileApplicationListener.java +++ b/spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/profile/KubernetesProfileApplicationListener.java @@ -1,79 +1,86 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 + * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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. * - * 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.profile; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.cloud.kubernetes.PodUtils; + import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.cloud.kubernetes.PodUtils; import org.springframework.context.ApplicationListener; import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; -public class KubernetesProfileApplicationListener implements ApplicationListener, Ordered { +public class KubernetesProfileApplicationListener + implements ApplicationListener, Ordered { - private static final Log LOG = LogFactory.getLog(KubernetesProfileApplicationListener.class); + private static final Log LOG = LogFactory + .getLog(KubernetesProfileApplicationListener.class); - private static final String KUBERNETES_PROFILE = "kubernetes"; - private static final int OFFSET = 1; - private static final int ORDER = Ordered.HIGHEST_PRECEDENCE + OFFSET; - private final PodUtils utils; + private static final String KUBERNETES_PROFILE = "kubernetes"; + private static final int OFFSET = 1; + private static final int ORDER = Ordered.HIGHEST_PRECEDENCE + OFFSET; + private final PodUtils utils; - public KubernetesProfileApplicationListener(PodUtils utils) { - this.utils = utils; - } + public KubernetesProfileApplicationListener(PodUtils utils) { + this.utils = utils; + } - @Override - public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { - ConfigurableEnvironment environment = event.getEnvironment(); - addKubernetesProfile(environment); - } + @Override + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { + ConfigurableEnvironment environment = event.getEnvironment(); + addKubernetesProfile(environment); + } - void addKubernetesProfile(ConfigurableEnvironment environment) { - if (utils.isInsideKubernetes()) { - if (hasKubernetesProfile(environment)) { - if (LOG.isDebugEnabled()) { - LOG.debug("'kubernetes' already in list of active profiles"); - } - } else { - if (LOG.isDebugEnabled()) { - LOG.debug("Adding 'kubernetes' to list of active profiles"); - } - environment.addActiveProfile(KUBERNETES_PROFILE); - } - } else { - if (LOG.isDebugEnabled()) { - LOG.warn("Not running inside kubernetes. Skipping 'kubernetes' profile activation."); - } - } - } + void addKubernetesProfile(ConfigurableEnvironment environment) { + if (utils.isInsideKubernetes()) { + if (hasKubernetesProfile(environment)) { + if (LOG.isDebugEnabled()) { + LOG.debug("'kubernetes' already in list of active profiles"); + } + } + else { + if (LOG.isDebugEnabled()) { + LOG.debug("Adding 'kubernetes' to list of active profiles"); + } + environment.addActiveProfile(KUBERNETES_PROFILE); + } + } + else { + if (LOG.isDebugEnabled()) { + LOG.warn( + "Not running inside kubernetes. Skipping 'kubernetes' profile activation."); + } + } + } - private boolean hasKubernetesProfile(Environment environment) { - for (String activeProfile : environment.getActiveProfiles()) { - if (KUBERNETES_PROFILE.equalsIgnoreCase(activeProfile)) { - return true; - } - } - return false; - } + private boolean hasKubernetesProfile(Environment environment) { + for (String activeProfile : environment.getActiveProfiles()) { + if (KUBERNETES_PROFILE.equalsIgnoreCase(activeProfile)) { + return true; + } + } + return false; + } - @Override - public int getOrder() { - return ORDER; - } + @Override + public int getOrder() { + return ORDER; + } } diff --git a/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/LazilyInstantiateTest.java b/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/LazilyInstantiateTest.java index 2f46c568..225e1631 100644 --- a/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/LazilyInstantiateTest.java +++ b/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/LazilyInstantiateTest.java @@ -1,17 +1,18 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 + * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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. * - * 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; diff --git a/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/StandardPodUtilsTest.java b/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/StandardPodUtilsTest.java index 3c79b793..3e0f85c8 100644 --- a/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/StandardPodUtilsTest.java +++ b/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/StandardPodUtilsTest.java @@ -1,17 +1,18 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 + * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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. * - * 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; @@ -19,11 +20,11 @@ import org.junit.Test; public class StandardPodUtilsTest { - @Test(expected = IllegalArgumentException.class) - public void constructorThrowsIllegalArgumentExceptionWhenKubeClientNull() { - // expect an IllegalArgumentException if KubernetesClient argument is - // null - new StandardPodUtils(null); - } + @Test(expected = IllegalArgumentException.class) + public void constructorThrowsIllegalArgumentExceptionWhenKubeClientNull() { + // expect an IllegalArgumentException if KubernetesClient argument is + // null + new StandardPodUtils(null); + } } diff --git a/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/profile/KubernetesProfileApplicationListenerTest.java b/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/profile/KubernetesProfileApplicationListenerTest.java index f8af73f7..ef7d1138 100644 --- a/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/profile/KubernetesProfileApplicationListenerTest.java +++ b/spring-cloud-kubernetes-core/src/test/java/org/springframework/cloud/kubernetes/profile/KubernetesProfileApplicationListenerTest.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.profile; import org.junit.Before; diff --git a/spring-cloud-kubernetes-dependencies/pom.xml b/spring-cloud-kubernetes-dependencies/pom.xml index d67561ab..87494708 100644 --- a/spring-cloud-kubernetes-dependencies/pom.xml +++ b/spring-cloud-kubernetes-dependencies/pom.xml @@ -1,4 +1,21 @@ + + 4.0.0 diff --git a/spring-cloud-kubernetes-discovery/pom.xml b/spring-cloud-kubernetes-discovery/pom.xml index 29d52fc6..fad015b3 100644 --- a/spring-cloud-kubernetes-discovery/pom.xml +++ b/spring-cloud-kubernetes-discovery/pom.xml @@ -1,18 +1,19 @@ + + diff --git a/spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example/name-service/pom.xml b/spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example/name-service/pom.xml index 420ef347..01ae687a 100644 --- a/spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example/name-service/pom.xml +++ b/spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example/name-service/pom.xml @@ -1,4 +1,21 @@ + + diff --git a/spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example/pom.xml b/spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example/pom.xml index 77174673..87b12fe9 100644 --- a/spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example/pom.xml +++ b/spring-cloud-kubernetes-examples/kubernetes-circuitbreaker-ribbon-example/pom.xml @@ -1,4 +1,21 @@ + + diff --git a/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/main/java/org/springframework/cloud/kubernetes/examples/App.java b/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/main/java/org/springframework/cloud/kubernetes/examples/App.java index 70d399d9..c359877c 100644 --- a/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/main/java/org/springframework/cloud/kubernetes/examples/App.java +++ b/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/main/java/org/springframework/cloud/kubernetes/examples/App.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, diff --git a/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/main/java/org/springframework/cloud/kubernetes/examples/HelloController.java b/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/main/java/org/springframework/cloud/kubernetes/examples/HelloController.java index 5f6c3f99..0a032f37 100644 --- a/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/main/java/org/springframework/cloud/kubernetes/examples/HelloController.java +++ b/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/main/java/org/springframework/cloud/kubernetes/examples/HelloController.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.examples; import java.util.List; @@ -5,7 +22,6 @@ import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; diff --git a/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/test/java/org/springframework/cloud/kubernetes/examples/ApplicationTestIT.java b/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/test/java/org/springframework/cloud/kubernetes/examples/ApplicationTestIT.java index b69ea538..dbc33d79 100644 --- a/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/test/java/org/springframework/cloud/kubernetes/examples/ApplicationTestIT.java +++ b/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/test/java/org/springframework/cloud/kubernetes/examples/ApplicationTestIT.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.examples; import org.junit.Test; @@ -5,7 +22,6 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; -import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; import static org.assertj.core.api.Assertions.*; @@ -14,14 +30,15 @@ import static org.assertj.core.api.Assertions.*; @SpringBootTest(classes = App.class) public class ApplicationTestIT { - @Autowired - private ApplicationContext context; + @Autowired + private ApplicationContext context; - /* - * This test proves that the application can be loaded successful and that all @configurations and dependencies are there - */ - @Test - public void contextLoads() throws Exception { - assertThat(context).isNotNull(); - } + /* + * This test proves that the application can be loaded successful and that + * all @configurations and dependencies are there + */ + @Test + public void contextLoads() throws Exception { + assertThat(context).isNotNull(); + } } diff --git a/spring-cloud-kubernetes-examples/kubernetes-reload-example/pom.xml b/spring-cloud-kubernetes-examples/kubernetes-reload-example/pom.xml index 6c046aac..39651ede 100644 --- a/spring-cloud-kubernetes-examples/kubernetes-reload-example/pom.xml +++ b/spring-cloud-kubernetes-examples/kubernetes-reload-example/pom.xml @@ -1,4 +1,21 @@ + + diff --git a/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/App.java b/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/App.java index 580c8435..72cbf228 100644 --- a/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/App.java +++ b/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/App.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -18,7 +18,6 @@ package org.springframework.cloud.kubernetes.examples; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableScheduling; /** @@ -28,8 +27,8 @@ import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling public class App { - public static void main(String[] args) { - SpringApplication.run(App.class, args); - } + public static void main(String[] args) { + SpringApplication.run(App.class, args); + } } diff --git a/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/DummyConfig.java b/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/DummyConfig.java index 2f890410..f3965172 100644 --- a/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/DummyConfig.java +++ b/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/DummyConfig.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -23,14 +23,14 @@ import org.springframework.context.annotation.Configuration; @ConfigurationProperties(prefix = "dummy") public class DummyConfig { - private String message = "this is a dummy message"; + private String message = "this is a dummy message"; - public String getMessage() { - return message; - } + public String getMessage() { + return message; + } - public void setMessage(String message) { - this.message = message; - } + public void setMessage(String message) { + this.message = message; + } } diff --git a/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/MyBean.java b/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/MyBean.java index ee60deb1..8271f140 100644 --- a/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/MyBean.java +++ b/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/MyBean.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -23,17 +23,17 @@ import org.springframework.stereotype.Component; @Component public class MyBean { - @Autowired - private MyConfig myConfig; + @Autowired + private MyConfig myConfig; - @Autowired - private DummyConfig dummyConfig; + @Autowired + private DummyConfig dummyConfig; - @Scheduled(fixedDelay = 5000) - public void hello() { - System.out.println("The first message is: " + myConfig.getMessage()); - System.out.println("The other message is: " + dummyConfig.getMessage()); - } + @Scheduled(fixedDelay = 5000) + public void hello() { + System.out.println("The first message is: " + myConfig.getMessage()); + System.out.println("The other message is: " + dummyConfig.getMessage()); + } } diff --git a/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/MyConfig.java b/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/MyConfig.java index b898906c..eb219ba7 100644 --- a/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/MyConfig.java +++ b/spring-cloud-kubernetes-examples/kubernetes-reload-example/src/main/java/org/springframework/cloud/kubernetes/examples/MyConfig.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -23,14 +23,14 @@ import org.springframework.context.annotation.Configuration; @ConfigurationProperties(prefix = "bean") public class MyConfig { - private String message = "a message that can be changed live"; + private String message = "a message that can be changed live"; - public String getMessage() { - return message; - } + public String getMessage() { + return message; + } - public void setMessage(String message) { - this.message = message; - } + public void setMessage(String message) { + this.message = message; + } } diff --git a/spring-cloud-kubernetes-examples/kubernetes-zipkin-example/pom.xml b/spring-cloud-kubernetes-examples/kubernetes-zipkin-example/pom.xml index b045cea5..b235e027 100644 --- a/spring-cloud-kubernetes-examples/kubernetes-zipkin-example/pom.xml +++ b/spring-cloud-kubernetes-examples/kubernetes-zipkin-example/pom.xml @@ -1,4 +1,21 @@ + + diff --git a/spring-cloud-kubernetes-examples/pom.xml b/spring-cloud-kubernetes-examples/pom.xml index 2600ed29..73f0d60e 100644 --- a/spring-cloud-kubernetes-examples/pom.xml +++ b/spring-cloud-kubernetes-examples/pom.xml @@ -1,4 +1,21 @@ + + diff --git a/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/Leader.java b/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/Leader.java index 8a7c3d3a..520a136b 100644 --- a/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/Leader.java +++ b/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/Leader.java @@ -1,17 +1,18 @@ /* - * Copyright (C) 2018 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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.leader; diff --git a/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderAutoConfiguration.java b/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderAutoConfiguration.java index 78956aa0..7312d3d4 100644 --- a/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderAutoConfiguration.java +++ b/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderAutoConfiguration.java @@ -1,17 +1,18 @@ /* - * Copyright (C) 2018 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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.leader; diff --git a/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderContext.java b/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderContext.java index 423a00a5..5a52e992 100644 --- a/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderContext.java +++ b/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderContext.java @@ -1,17 +1,18 @@ /* - * Copyright (C) 2018 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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.leader; diff --git a/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderInitiator.java b/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderInitiator.java index ee39c465..13827055 100644 --- a/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderInitiator.java +++ b/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderInitiator.java @@ -1,17 +1,18 @@ /* - * Copyright (C) 2018 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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.leader; diff --git a/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderKubernetesHelper.java b/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderKubernetesHelper.java index d3694c60..a8d2fcdf 100644 --- a/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderKubernetesHelper.java +++ b/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeaderKubernetesHelper.java @@ -1,17 +1,18 @@ /* - * Copyright (C) 2018 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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.leader; diff --git a/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeadershipController.java b/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeadershipController.java index 134c7f1f..0f86ac4e 100644 --- a/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeadershipController.java +++ b/spring-cloud-kubernetes-leader/src/main/java/org/springframework/cloud/kubernetes/leader/LeadershipController.java @@ -1,17 +1,18 @@ /* - * Copyright (C) 2018 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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.leader; diff --git a/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderContextTest.java b/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderContextTest.java index bf70f326..e3d91b89 100644 --- a/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderContextTest.java +++ b/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderContextTest.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.leader; import org.junit.Before; diff --git a/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderInitiatorTest.java b/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderInitiatorTest.java index 1588ed6c..8652261a 100644 --- a/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderInitiatorTest.java +++ b/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderInitiatorTest.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.leader; import java.util.concurrent.ScheduledExecutorService; diff --git a/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderKubernetesHelperTest.java b/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderKubernetesHelperTest.java index 687f99d1..9b7824c0 100644 --- a/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderKubernetesHelperTest.java +++ b/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderKubernetesHelperTest.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.leader; import java.util.Collections; diff --git a/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderTest.java b/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderTest.java index ef5b1e50..abee0d3a 100644 --- a/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderTest.java +++ b/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeaderTest.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.leader; import org.junit.Before; diff --git a/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeadershipControllerTest.java b/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeadershipControllerTest.java index d0a667bb..113733e0 100644 --- a/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeadershipControllerTest.java +++ b/spring-cloud-kubernetes-leader/src/test/java/org/springframework/cloud/kubernetes/leader/LeadershipControllerTest.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.leader; import java.util.Collections; diff --git a/spring-cloud-kubernetes-ribbon/pom.xml b/spring-cloud-kubernetes-ribbon/pom.xml index 2b4adf85..eea9d451 100644 --- a/spring-cloud-kubernetes-ribbon/pom.xml +++ b/spring-cloud-kubernetes-ribbon/pom.xml @@ -1,19 +1,19 @@ implements IClientConfigKey { - public static final IClientConfigKey Namespace = new KubernetesConfigKey("KubernetesNamespace"){}; - public static final IClientConfigKey PortName = new KubernetesConfigKey("PortName"){}; + public static final IClientConfigKey Namespace = new KubernetesConfigKey("KubernetesNamespace") { + }; + public static final IClientConfigKey PortName = new KubernetesConfigKey("PortName") { + }; - private static final Set keys = new HashSet(); + private static final Set keys = new HashSet(); - static { - for (Field f: KubernetesConfigKey.class.getDeclaredFields()) { - if (Modifier.isStatic(f.getModifiers()) //&& Modifier.isPublic(f.getModifiers()) - && IClientConfigKey.class.isAssignableFrom(f.getType())) { - try { - keys.add((IClientConfigKey) f.get(null)); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - } - } + static { + for (Field f : KubernetesConfigKey.class.getDeclaredFields()) { + if (Modifier.isStatic(f.getModifiers()) //&& Modifier.isPublic(f.getModifiers()) + && IClientConfigKey.class.isAssignableFrom(f.getType())) { + try { + keys.add((IClientConfigKey) f.get(null)); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } + } - /** - * @deprecated see {@link #keys()} - */ - @Deprecated - public static IClientConfigKey[] values() { - return keys().toArray(new IClientConfigKey[0]); - } + /** + * @deprecated see {@link #keys()} + */ + @Deprecated + public static IClientConfigKey[] values() { + return keys().toArray(new IClientConfigKey[0]); + } - /** - * return all the public static keys defined in this class - */ - public static Set keys() { - return keys; - } + /** + * return all the public static keys defined in this class + */ + public static Set keys() { + return keys; + } - public static IClientConfigKey valueOf(final String name) { - for (IClientConfigKey key: keys()) { - if (key.key().equals(name)) { - return key; - } - } - return new IClientConfigKey() { - @Override - public String key() { - return name; - } + public static IClientConfigKey valueOf(final String name) { + for (IClientConfigKey key : keys()) { + if (key.key().equals(name)) { + return key; + } + } + return new IClientConfigKey() { + @Override + public String key() { + return name; + } - @Override - public Class type() { - return String.class; - } - }; - } + @Override + public Class type() { + return String.class; + } + }; + } - private final String configKey; - private final Class type; + private final String configKey; + private final Class type; - @SuppressWarnings("unchecked") - protected KubernetesConfigKey(String configKey) { - this.configKey = configKey; - Type superclass = getClass().getGenericSuperclass(); - Assert.isTrue(superclass instanceof ParameterizedType, - superclass + " isn't parameterized"); - Type runtimeType = ((ParameterizedType) superclass).getActualTypeArguments()[0]; - type = (Class) Types.rawType(runtimeType); - } + @SuppressWarnings("unchecked") + protected KubernetesConfigKey(String configKey) { + this.configKey = configKey; + Type superclass = getClass().getGenericSuperclass(); + Assert.isTrue(superclass instanceof ParameterizedType, + superclass + " isn't parameterized"); + Type runtimeType = ((ParameterizedType) superclass).getActualTypeArguments()[0]; + type = (Class) Types.rawType(runtimeType); + } - @Override - public Class type() { - return type; - } + @Override + public Class type() { + return type; + } - /* (non-Javadoc) + /* (non-Javadoc) * @see com.netflix.niws.client.ClientConfig#key() */ - @Override - public String key() { - return configKey; - } + @Override + public String key() { + return configKey; + } - @Override - public String toString() { - return configKey; - } + @Override + public String toString() { + return configKey; + } } diff --git a/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/KubernetesRibbonClientConfiguration.java b/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/KubernetesRibbonClientConfiguration.java index 15357197..be19033b 100644 --- a/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/KubernetesRibbonClientConfiguration.java +++ b/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/KubernetesRibbonClientConfiguration.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -20,6 +20,7 @@ package org.springframework.cloud.kubernetes.ribbon; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.ServerList; import io.fabric8.kubernetes.client.KubernetesClient; + import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -27,14 +28,14 @@ import org.springframework.context.annotation.Configuration; @Configuration public class KubernetesRibbonClientConfiguration { - public KubernetesRibbonClientConfiguration() { - } + public KubernetesRibbonClientConfiguration() { + } - @Bean - @ConditionalOnMissingBean - public ServerList ribbonServerList(KubernetesClient client, IClientConfig config) { - KubernetesServerList serverList = new KubernetesServerList(client); - serverList.initWithNiwsConfig(config); - return serverList; - } + @Bean + @ConditionalOnMissingBean + public ServerList ribbonServerList(KubernetesClient client, IClientConfig config) { + KubernetesServerList serverList = new KubernetesServerList(client); + serverList.initWithNiwsConfig(config); + return serverList; + } } diff --git a/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/KubernetesServerList.java b/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/KubernetesServerList.java index 9c494915..75b25d65 100644 --- a/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/KubernetesServerList.java +++ b/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/KubernetesServerList.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -17,86 +17,89 @@ package org.springframework.cloud.kubernetes.ribbon; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.AbstractServerList; import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.ServerList; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - import io.fabric8.kubernetes.api.model.EndpointAddress; import io.fabric8.kubernetes.api.model.EndpointPort; import io.fabric8.kubernetes.api.model.EndpointSubset; import io.fabric8.kubernetes.api.model.Endpoints; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.utils.Utils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +public class KubernetesServerList extends AbstractServerList + implements ServerList { -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; + private static final int FIRST = 0; + private static final Log LOG = LogFactory.getLog(KubernetesServerList.class); -public class KubernetesServerList extends AbstractServerList implements ServerList { + private final KubernetesClient client; - private static final int FIRST = 0; - private static final Log LOG = LogFactory.getLog(KubernetesServerList.class); + private String serviceId; - private final KubernetesClient client; + private String namespace; + private String portName; - private String serviceId; + public KubernetesServerList(KubernetesClient client) { + this.client = client; + } - private String namespace; - private String portName; + public void initWithNiwsConfig(IClientConfig clientConfig) { + this.serviceId = clientConfig.getClientName(); + this.namespace = clientConfig.getPropertyAsString(KubernetesConfigKey.Namespace, + client.getNamespace()); + this.portName = clientConfig.getPropertyAsString(KubernetesConfigKey.PortName, + null); + } + public List getInitialListOfServers() { + return Collections.emptyList(); + } - public KubernetesServerList(KubernetesClient client) { - this.client = client; - } + public List getUpdatedListOfServers() { + Endpoints endpoints = namespace != null + ? client.endpoints().inNamespace(namespace).withName(serviceId).get() + : client.endpoints().withName(serviceId).get(); - public void initWithNiwsConfig(IClientConfig clientConfig) { - this.serviceId = clientConfig.getClientName(); - this.namespace = clientConfig.getPropertyAsString(KubernetesConfigKey.Namespace, client.getNamespace()); - this.portName = clientConfig.getPropertyAsString(KubernetesConfigKey.PortName, null); - } + List result = new ArrayList(); + if (endpoints != null) { - public List getInitialListOfServers() { - return Collections.emptyList(); - } + if (LOG.isDebugEnabled()) { + LOG.debug("Found [" + endpoints.getSubsets().size() + + "] endpoints in namespace [" + namespace + "] for name [" + + serviceId + "] and portName [" + portName + "]"); + } + for (EndpointSubset subset : endpoints.getSubsets()) { - public List getUpdatedListOfServers() { - Endpoints endpoints = namespace != null - ? client.endpoints().inNamespace(namespace).withName(serviceId).get() - : client.endpoints().withName(serviceId).get(); - - List result = new ArrayList(); - if (endpoints != null) { - - if (LOG.isDebugEnabled()) { - LOG.debug("Found [" + endpoints.getSubsets().size() + "] endpoints in namespace [" + - namespace + "] for name [" + serviceId + "] and portName [" + portName + "]"); - } - for (EndpointSubset subset : endpoints.getSubsets()) { - - if (subset.getPorts().size() == 1) { - EndpointPort port = subset.getPorts().get(FIRST); - for (EndpointAddress address : subset.getAddresses()) { - result.add(new Server(address.getIp(), port.getPort())); - } - } else { - for (EndpointPort port : subset.getPorts()) { - if (Utils.isNullOrEmpty(portName) || portName.endsWith(port.getName())) { - for (EndpointAddress address : subset.getAddresses()) { - result.add(new Server(address.getIp(), port.getPort())); - } - } - } - } - } - } else { - LOG.warn("Did not find any endpoints in ribbon in namespace [" + namespace + "] for name [" + - serviceId + "] and portName [" + portName + "]"); - } - return result; - } + if (subset.getPorts().size() == 1) { + EndpointPort port = subset.getPorts().get(FIRST); + for (EndpointAddress address : subset.getAddresses()) { + result.add(new Server(address.getIp(), port.getPort())); + } + } + else { + for (EndpointPort port : subset.getPorts()) { + if (Utils.isNullOrEmpty(portName) + || portName.endsWith(port.getName())) { + for (EndpointAddress address : subset.getAddresses()) { + result.add(new Server(address.getIp(), port.getPort())); + } + } + } + } + } + } + else { + LOG.warn("Did not find any endpoints in ribbon in namespace [" + namespace + + "] for name [" + serviceId + "] and portName [" + portName + "]"); + } + return result; + } } diff --git a/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/RibbonKubernetesAutoConfiguration.java b/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/RibbonKubernetesAutoConfiguration.java index 9e2ed300..0a8772a5 100644 --- a/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/RibbonKubernetesAutoConfiguration.java +++ b/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/RibbonKubernetesAutoConfiguration.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, diff --git a/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/Types.java b/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/Types.java index 89eedf0a..d8766e77 100644 --- a/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/Types.java +++ b/spring-cloud-kubernetes-ribbon/src/main/java/org/springframework/cloud/kubernetes/ribbon/Types.java @@ -1,36 +1,55 @@ -package org.springframework.cloud.kubernetes.ribbon; +/* + * Copyright 2013-2018 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 + * + * http://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.ribbon; import java.lang.reflect.GenericArrayType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; -import java.util.Set; final class Types { - private Types() { - //Utlity - } + private Types() { + // Utlity + } - static Class rawType(Type type) { - if (type instanceof Class) { - return (Class) type; - } else if (type instanceof TypeVariable) { - return rawType(firstOrObject(((TypeVariable) type).getBounds())); - } else if (type instanceof WildcardType) { - return rawType(firstOrObject(((WildcardType) type).getUpperBounds())); - } else if (type instanceof GenericArrayType) { - return rawType(((GenericArrayType) type).getGenericComponentType()); - } - return Object.class; - } + static Class rawType(Type type) { + if (type instanceof Class) { + return (Class) type; + } + else if (type instanceof TypeVariable) { + return rawType(firstOrObject(((TypeVariable) type).getBounds())); + } + else if (type instanceof WildcardType) { + return rawType(firstOrObject(((WildcardType) type).getUpperBounds())); + } + else if (type instanceof GenericArrayType) { + return rawType(((GenericArrayType) type).getGenericComponentType()); + } + return Object.class; + } - private static Type firstOrObject(Type[] types) { - if (types.length > 0) { - return rawType(types[0]); - } else { - return Void.class; - } - } + private static Type firstOrObject(Type[] types) { + if (types.length > 0) { + return rawType(types[0]); + } + else { + return Void.class; + } + } } diff --git a/spring-cloud-kubernetes-ribbon/src/test/groovy/io/fabric8/spring/cloud/kubernetes/ribbon/test/RibbonTest.groovy b/spring-cloud-kubernetes-ribbon/src/test/groovy/io/fabric8/spring/cloud/kubernetes/ribbon/test/RibbonTest.groovy index 43e0a313..91e4d147 100644 --- a/spring-cloud-kubernetes-ribbon/src/test/groovy/io/fabric8/spring/cloud/kubernetes/ribbon/test/RibbonTest.groovy +++ b/spring-cloud-kubernetes-ribbon/src/test/groovy/io/fabric8/spring/cloud/kubernetes/ribbon/test/RibbonTest.groovy @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, diff --git a/spring-cloud-kubernetes-ribbon/src/test/groovy/io/fabric8/spring/cloud/kubernetes/ribbon/test/TestApplication.groovy b/spring-cloud-kubernetes-ribbon/src/test/groovy/io/fabric8/spring/cloud/kubernetes/ribbon/test/TestApplication.groovy index 04e5a630..ccb9df65 100644 --- a/spring-cloud-kubernetes-ribbon/src/test/groovy/io/fabric8/spring/cloud/kubernetes/ribbon/test/TestApplication.groovy +++ b/spring-cloud-kubernetes-ribbon/src/test/groovy/io/fabric8/spring/cloud/kubernetes/ribbon/test/TestApplication.groovy @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, diff --git a/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/KubernetesConfigKeyTest.java b/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/KubernetesConfigKeyTest.java index 549635b7..0af6418e 100644 --- a/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/KubernetesConfigKeyTest.java +++ b/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/KubernetesConfigKeyTest.java @@ -1,27 +1,47 @@ -package org.springframework.cloud.kubernetes.ribbon; +/* + * Copyright 2013-2018 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 + * + * http://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.ribbon; import org.junit.Assert; import org.junit.Test; public class KubernetesConfigKeyTest { - private class TypeOne {} + private class TypeOne { + } - @Test - public void testTypes() { - //with class - KubernetesConfigKey key1 = new KubernetesConfigKey("key1"){}; + @Test + public void testTypes() { + // with class + KubernetesConfigKey key1 = new KubernetesConfigKey("key1") { + }; - //with type variable - KubernetesConfigKey key2 = new KubernetesConfigKey("key2"){}; + // with type variable + KubernetesConfigKey key2 = new KubernetesConfigKey("key2") { + }; - //with type variable with no bounds - KubernetesConfigKey key3 = new KubernetesConfigKey("key3"){}; + // with type variable with no bounds + KubernetesConfigKey key3 = new KubernetesConfigKey("key3") { + }; - Assert.assertEquals(String.class, key1.type()); - Assert.assertEquals(TypeOne.class, key2.type()); - Assert.assertEquals(Object.class, key3.type()); - } + Assert.assertEquals(String.class, key1.type()); + Assert.assertEquals(TypeOne.class, key2.type()); + Assert.assertEquals(Object.class, key3.type()); + } -} \ No newline at end of file +} diff --git a/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/RibbonFallbackTest.java b/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/RibbonFallbackTest.java index a10a58bf..69643495 100644 --- a/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/RibbonFallbackTest.java +++ b/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/RibbonFallbackTest.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -30,6 +30,7 @@ import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -44,12 +45,11 @@ import static org.junit.Assert.fail; * @author Charles Moulliard */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = TestApplication.class, - properties = { - "spring.application.name=testapp", - "spring.cloud.kubernetes.client.namespace=testns", - "spring.cloud.kubernetes.client.trustCerts=true", - "spring.cloud.kubernetes.config.namespace=testns"}) +@SpringBootTest(classes = TestApplication.class, properties = { + "spring.application.name=testapp", + "spring.cloud.kubernetes.client.namespace=testns", + "spring.cloud.kubernetes.client.trustCerts=true", + "spring.cloud.kubernetes.config.namespace=testns" }) @EnableAutoConfiguration @EnableDiscoveryClient public class RibbonFallbackTest { @@ -76,11 +76,13 @@ public class RibbonFallbackTest { public static void setUpBefore() throws Exception { mockClient = mockServer.getClient(); - //Configure the kubernetes master url to point to the mock server - System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); + // Configure the kubernetes master url to point to the mock server + System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, + mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false"); - System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false"); + System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, + "false"); mockEndpoint = new DefaultMockServer(false); mockEndpoint.start(); @@ -89,28 +91,27 @@ public class RibbonFallbackTest { @Test public void testFallBackGreetingEndpoint() { /** - * Scenario tested - * 1. Register the mock endpoint of the service into KubeMockServer and call /greeting service - * 2. Unregister the mock endpoint and verify that Ribbon doesn't have any instances anymore in its list - * 3. Re register the mock endpoint and play step 1) + * Scenario tested 1. Register the mock endpoint of the service into + * KubeMockServer and call /greeting service 2. Unregister the mock endpoint and + * verify that Ribbon doesn't have any instances anymore in its list 3. Re + * register the mock endpoint and play step 1) **/ LOG.info(">>>>>>>>>> BEGIN PART 1 <<<<<<<<<<<<<"); // As Ribbon refreshes its list every serverListRefreshInterval ms, - // we configure the API Server endpoint to reply to exactly serviceOccurrence attempts + // we configure the API Server endpoint to reply to exactly serviceOccurrence + // attempts // to be sure that Ribbon will get the mockendpoint to access it for the call - mockServer.expect().get() - .withPath("/api/v1/namespaces/testns/endpoints/testapp") - .andReturn(200, newEndpoint("testapp-a","testns", mockEndpoint)) - .times(serviceOccurrence); + mockServer.expect().get().withPath("/api/v1/namespaces/testns/endpoints/testapp") + .andReturn(200, newEndpoint("testapp-a", "testns", mockEndpoint)) + .times(serviceOccurrence); - mockEndpoint.expect().get() - .withPath("/greeting") - .andReturn(200, "Hello from A") - .once(); + mockEndpoint.expect().get().withPath("/greeting").andReturn(200, "Hello from A") + .once(); - String response = restTemplate.getForObject("http://testapp/greeting", String.class); + String response = restTemplate.getForObject("http://testapp/greeting", + String.class); Assert.assertEquals("Hello from A", response); LOG.info(">>>>>>>>>> END PART 1 <<<<<<<<<<<<<"); @@ -118,53 +119,66 @@ public class RibbonFallbackTest { try { ensureEndpointsNoLongerReturnedByAPIServer(); restTemplate.getForObject("http://testapp/greeting", String.class); - fail("Ribbon was supposed to throw an Exception due to not knowing of any endpoints to route the request to"); - } catch (Exception e) { + fail("Ribbon was supposed to throw an Exception due to not knowing of any endpoints to route the request to"); + } + catch (Exception e) { // No endpoint is available anymore and Ribbon list is empty Assert.assertEquals("No instances available for testapp", e.getMessage()); } LOG.info(">>>>>>>>>> END PART 2 <<<<<<<<<<<<<"); LOG.info(">>>>>>>>>> BEGIN PART 3 <<<<<<<<<<<<<"); - mockServer.expect().get() - .withPath("/api/v1/namespaces/testns/endpoints/testapp") - .andReturn(200, newEndpoint("testapp-a","testns", mockEndpoint)) - .always(); + mockServer.expect().get().withPath("/api/v1/namespaces/testns/endpoints/testapp") + .andReturn(200, newEndpoint("testapp-a", "testns", mockEndpoint)) + .always(); - // the purpose of sleeping here is to make sure that even after some refreshes to it's list + // the purpose of sleeping here is to make sure that even after some refreshes to + // it's list // Ribbon still has endpoints to route to - // This is different than the first part of the test because the API server has now been - // configured to always respond with some endpoints as opposed to only a certain amount of + // This is different than the first part of the test because the API server has + // now been + // configured to always respond with some endpoints as opposed to only a certain + // amount of // requests which was the case in part 1 try { Thread.sleep(2000); - } catch(InterruptedException ex) { + } + catch (InterruptedException ex) { Thread.currentThread().interrupt(); } - mockEndpoint.expect().get().withPath("/greeting").andReturn(200, "Hello from A").once(); + mockEndpoint.expect().get().withPath("/greeting").andReturn(200, "Hello from A") + .once(); response = restTemplate.getForObject("http://testapp/greeting", String.class); - Assert.assertEquals("Hello from A",response); + Assert.assertEquals("Hello from A", response); LOG.info(">>>>>>>>>> END PART 3 <<<<<<<<<<<<<"); } - // This works because the (mock) API server is configured to return the endpoints exactly - // serviceOccurrence times while Ribbon refreshes it's list every serverListRefreshInterval milliseconds - private void ensureEndpointsNoLongerReturnedByAPIServer() throws InterruptedException { + // This works because the (mock) API server is configured to return the endpoints + // exactly + // serviceOccurrence times while Ribbon refreshes it's list every + // serverListRefreshInterval milliseconds + private void ensureEndpointsNoLongerReturnedByAPIServer() + throws InterruptedException { Thread.sleep((serviceOccurrence + 1) * serverListRefreshInterval); } - public static Endpoints newEndpoint(String name, String namespace, DefaultMockServer mockServer) { + public static Endpoints newEndpoint(String name, String namespace, + DefaultMockServer mockServer) { + // @formatter:off return new EndpointsBuilder() - .withNewMetadata() - .withName(name) - .withNamespace(namespace) - .endMetadata() - .addNewSubset() - .addNewAddress().withIp(mockServer.getHostName()).endAddress() - .addNewPort("http",mockServer.getPort(),"http") - .endSubset() - .build(); + .withNewMetadata() + .withName(name) + .withNamespace(namespace) + .endMetadata() + .addNewSubset() + .addNewAddress() + .withIp(mockServer.getHostName()) + .endAddress() + .addNewPort("http", mockServer.getPort(), "http") + .endSubset() + .build(); + // @formatter:on } } diff --git a/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/RibbonTest.java b/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/RibbonTest.java index ee90c9a1..a0bfada2 100644 --- a/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/RibbonTest.java +++ b/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/RibbonTest.java @@ -1,11 +1,11 @@ /* - * Copyright (C) 2016 to the original authors. + * Copyright 2013-2018 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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, @@ -28,6 +28,7 @@ import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; @@ -39,12 +40,11 @@ import org.springframework.web.client.RestTemplate; * @author Charles Moulliard */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = TestApplication.class, - properties = { - "spring.application.name=testapp", - "spring.cloud.kubernetes.client.namespace=testns", - "spring.cloud.kubernetes.client.trustCerts=true", - "spring.cloud.kubernetes.config.namespace=testns"}) +@SpringBootTest(classes = TestApplication.class, properties = { + "spring.application.name=testapp", + "spring.cloud.kubernetes.client.namespace=testns", + "spring.cloud.kubernetes.client.trustCerts=true", + "spring.cloud.kubernetes.config.namespace=testns" }) @EnableAutoConfiguration @EnableDiscoveryClient public class RibbonTest { @@ -67,36 +67,43 @@ public class RibbonTest { public static void setUpBefore() throws Exception { mockClient = server.getClient(); - //Configure the kubernetes master url to point to the mock server - System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, mockClient.getConfiguration().getMasterUrl()); + // Configure the kubernetes master url to point to the mock server + System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, + mockClient.getConfiguration().getMasterUrl()); System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false"); - System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false"); + System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, + "false"); - //Configured - server.expect().get().withPath("/api/v1/namespaces/testns/endpoints/testapp").andReturn(200, new EndpointsBuilder() - .withNewMetadata() - .withName("testapp-a") - .endMetadata() - .addNewSubset() - .addNewAddress().withIp(mockEndpointA.getMockServer().getHostName()).endAddress() - .addNewPort("http", mockEndpointA.getMockServer().getPort(), "http") - .endSubset() - .addNewSubset() - .addNewAddress().withIp(mockEndpointB.getMockServer().getHostName()).endAddress() - .addNewPort("http", mockEndpointB.getMockServer().getPort(), "http") - .endSubset() - .build()).always(); + // Configured + server.expect().get().withPath("/api/v1/namespaces/testns/endpoints/testapp") + .andReturn(200, + new EndpointsBuilder().withNewMetadata().withName("testapp-a") + .endMetadata().addNewSubset().addNewAddress() + .withIp(mockEndpointA.getMockServer().getHostName()) + .endAddress() + .addNewPort("http", + mockEndpointA.getMockServer().getPort(), "http") + .endSubset().addNewSubset().addNewAddress() + .withIp(mockEndpointB.getMockServer().getHostName()) + .endAddress() + .addNewPort("http", + mockEndpointB.getMockServer().getPort(), "http") + .endSubset().build()) + .always(); - mockEndpointA.expect().get().withPath("/greeting").andReturn(200, "Hello from A").always(); - mockEndpointB.expect().get().withPath("/greeting").andReturn(200, "Hello from B").always(); + mockEndpointA.expect().get().withPath("/greeting").andReturn(200, "Hello from A") + .always(); + mockEndpointB.expect().get().withPath("/greeting").andReturn(200, "Hello from B") + .always(); } @Test public void testGreetingEndpoint() { List greetings = new ArrayList<>(); - for (int i = 0; i < 2 ; i++) { - greetings.add(restTemplate.getForObject("http://testapp/greeting", String.class)); + for (int i = 0; i < 2; i++) { + greetings.add( + restTemplate.getForObject("http://testapp/greeting", String.class)); } greetings.contains("Hello from A"); greetings.contains("Hello from B"); diff --git a/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/TestApplication.java b/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/TestApplication.java index 08ed1e60..bbf631d8 100644 --- a/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/TestApplication.java +++ b/spring-cloud-kubernetes-ribbon/src/test/java/org/springframework/cloud/kubernetes/ribbon/TestApplication.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013-2018 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 + * + * http://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.ribbon; import org.springframework.boot.SpringApplication; diff --git a/spring-cloud-starter-kubernetes-all/pom.xml b/spring-cloud-starter-kubernetes-all/pom.xml index 35fe3aec..577cd6b0 100644 --- a/spring-cloud-starter-kubernetes-all/pom.xml +++ b/spring-cloud-starter-kubernetes-all/pom.xml @@ -1,19 +1,19 @@