Skip to content

Commit

Permalink
Fix checkstyle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Jacobs committed Sep 30, 2020
1 parent e7e3d8d commit 8bdb45c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.cloud.kubernetes.config;

import java.util.Optional;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.client.KubernetesClient;
Expand All @@ -31,8 +33,6 @@
import org.springframework.context.annotation.Import;
import org.springframework.retry.support.RetryTemplate;

import java.util.Optional;

/**
* Auto configuration that reuses Kubernetes config maps as property sources.
*
Expand All @@ -53,8 +53,10 @@ protected static class KubernetesPropertySourceConfiguration {

@Bean
@ConditionalOnProperty(name = "spring.cloud.kubernetes.config.enabled", matchIfMissing = true)
public ConfigMapPropertySourceLocator configMapPropertySourceLocator(ConfigMapConfigProperties properties, Optional<ConfigMapRetryTemplateFactory> configMapRetryTemplateFactory) {
return new ConfigMapPropertySourceLocator(this.client, properties, configMapRetryTemplateFactory.orElse(null));
public ConfigMapPropertySourceLocator configMapPropertySourceLocator(ConfigMapConfigProperties properties,
Optional<ConfigMapRetryTemplateFactory> configMapRetryTemplateFactory) {
return new ConfigMapPropertySourceLocator(this.client, properties,
configMapRetryTemplateFactory.orElse(null));
}

@Bean
Expand All @@ -67,13 +69,11 @@ public SecretsPropertySourceLocator secretsPropertySourceLocator(SecretsConfigPr
@ConditionalOnMissingBean
@ConditionalOnClass(RetryTemplate.class)
public ConfigMapRetryTemplateFactory configMapRetryPolicyFactory(ConfigMapConfigProperties properties) {
final RetryTemplate template = RetryTemplate.builder()
.maxAttempts(properties.getRetry().getMaxAttempts())
.fixedBackoff(properties.getRetry().getBackoff().toMillis())
.retryOn(Exception.class)
.build();
final RetryTemplate template = RetryTemplate.builder().maxAttempts(properties.getRetry().getMaxAttempts())
.fixedBackoff(properties.getRetry().getBackoff().toMillis()).retryOn(Exception.class).build();
return () -> template;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public Duration getBackoff() {
public void setBackoff(Duration backoff) {
this.backoff = backoff;
}

}

static class NormalizedSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ public ConfigMapPropertySource(KubernetesClient client, String name, String name
this(client, name, namespace, environment, null);
}

public ConfigMapPropertySource(KubernetesClient client, String name, String namespace, Environment environment, ConfigMapRetryTemplateFactory configMapRetryTemplateFactory) {
super(getName(client, name, namespace), asObjectMap(getData(client, name, namespace, environment, configMapRetryTemplateFactory)));
public ConfigMapPropertySource(KubernetesClient client, String name, String namespace, Environment environment,
ConfigMapRetryTemplateFactory configMapRetryTemplateFactory) {
super(getName(client, name, namespace),
asObjectMap(getData(client, name, namespace, environment, configMapRetryTemplateFactory)));
}

private static String getName(KubernetesClient client, String name, String namespace) {
Expand All @@ -87,18 +89,20 @@ private static String getName(KubernetesClient client, String name, String names
}

private static Map<String, Object> getData(KubernetesClient client, String name, String namespace,
Environment environment, ConfigMapRetryTemplateFactory configMapRetryTemplateFactory) {
Environment environment, ConfigMapRetryTemplateFactory configMapRetryTemplateFactory) {
try {
if (configMapRetryTemplateFactory == null) {
return tryGetData(client, name, namespace, environment);
} else {
}
else {
final RetryTemplate retryTemplate = configMapRetryTemplateFactory.getRetryTemplate();
return retryTemplate.execute(ctx -> {
try {
return tryGetData(client, name, namespace, environment);
}
catch (Exception e) {
LOG.warn("Can't read configMap with name: [" + name + "] in namespace:[" + namespace + "]. Retrying.", e);
LOG.warn("Can't read configMap with name: [" + name + "] in namespace:[" + namespace
+ "]. Retrying.", e);
throw e;
}
});
Expand All @@ -112,7 +116,7 @@ private static Map<String, Object> getData(KubernetesClient client, String name,
}

private static Map<String, Object> tryGetData(KubernetesClient client, String name, String namespace,
Environment environment) {
Environment environment) {
Map<String, Object> result = new LinkedHashMap<>();
ConfigMap map = StringUtils.isEmpty(namespace) ? client.configMaps().withName(name).get()
: client.configMaps().inNamespace(namespace).withName(name).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public ConfigMapPropertySourceLocator(KubernetesClient client, ConfigMapConfigPr
this(client, properties, null);
}

public ConfigMapPropertySourceLocator(KubernetesClient client, ConfigMapConfigProperties properties, ConfigMapRetryTemplateFactory configMapRetryTemplateFactory) {
public ConfigMapPropertySourceLocator(KubernetesClient client, ConfigMapConfigProperties properties,
ConfigMapRetryTemplateFactory configMapRetryTemplateFactory) {
this.client = client;
this.properties = properties;
this.configMapRetryTemplateFactory = configMapRetryTemplateFactory;
Expand Down Expand Up @@ -95,8 +96,8 @@ private MapPropertySource getMapPropertySourceForSingleConfigMap(ConfigurableEnv
String configurationTarget = this.properties.getConfigurationTarget();
return new ConfigMapPropertySource(this.client,
getApplicationName(environment, normalizedSource.getName(), configurationTarget),
getApplicationNamespace(this.client, normalizedSource.getNamespace(), configurationTarget),
environment, configMapRetryTemplateFactory);
getApplicationNamespace(this.client, normalizedSource.getNamespace(), configurationTarget), environment,
configMapRetryTemplateFactory);
}

private void addPropertySourcesFromPaths(Environment environment, CompositePropertySource composite) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
import org.springframework.retry.support.RetryTemplate;

/**
* This interface determines which retry logic to apply if there are any errors during the loading of config maps
* at application startup.
* This interface determines which retry logic to apply if there are any errors during the
* loading of config maps at application startup.
*
* @author Roy Jacobs
*/
public interface ConfigMapRetryTemplateFactory {

/**
* @return the {@link RetryTemplate} to use when loading config maps.
*/
RetryTemplate getRetryTemplate();

}

0 comments on commit 8bdb45c

Please sign in to comment.