Minor nitpicks k8s client config maps functionality (#852)

* slightly refactored utils

* add final modifier

* final + remove isDebugEnabled

* more nitpicks

* checkstyle fix

* one more change

* fix so that logs are present
This commit is contained in:
erabii
2021-08-16 16:04:23 -04:00
committed by GitHub
parent 888031daf7
commit c5cdebbf6b
4 changed files with 22 additions and 43 deletions

View File

@@ -16,11 +16,12 @@
package org.springframework.cloud.kubernetes.client.config;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.CoreV1Api;
@@ -46,17 +47,18 @@ public class KubernetesClientConfigMapPropertySource extends ConfigMapPropertySo
Environment environment) {
try {
List<String> names = new ArrayList<>();
Set<String> names = new HashSet<>();
names.add(name);
if (environment != null) {
for (String activeProfile : environment.getActiveProfiles()) {
names.add(name + "-" + activeProfile);
}
}
Map<String, Object> result = new LinkedHashMap<>();
Map<String, Object> result = new HashMap<>();
coreV1Api.listNamespacedConfigMap(namespace, null, null, null, null, null, null, null, null, null, null)
.getItems().stream().filter(cm -> names.contains(cm.getMetadata().getName()))
.forEach(map -> result.putAll(processAllEntries(map.getData(), environment)));
.map(map -> processAllEntries(map.getData(), environment)).collect(Collectors.toList())
.forEach(result::putAll);
return result;
}

View File

@@ -32,7 +32,7 @@ import static org.springframework.cloud.kubernetes.client.config.KubernetesClien
*/
public class KubernetesClientConfigMapPropertySourceLocator extends ConfigMapPropertySourceLocator {
private CoreV1Api coreV1Api;
private final CoreV1Api coreV1Api;
private KubernetesClientProperties kubernetesClientProperties;

View File

@@ -53,22 +53,14 @@ public final class KubernetesClientConfigUtils {
public static String getNamespace(ConfigMapConfigProperties.NormalizedSource normalizedSource,
String fallbackNamespace) {
if (!StringUtils.hasText(normalizedSource.getNamespace())) {
return fallbackNamespace;
}
else {
return normalizedSource.getNamespace();
}
String normalizedNamespace = normalizedSource.getNamespace();
return StringUtils.hasText(normalizedNamespace) ? normalizedNamespace : fallbackNamespace;
}
public static String getNamespace(SecretsConfigProperties.NormalizedSource normalizedSource,
String fallbackNamespace) {
if (!StringUtils.hasText(normalizedSource.getNamespace())) {
return fallbackNamespace;
}
else {
return normalizedSource.getNamespace();
}
String normalizedNamespace = normalizedSource.getNamespace();
return StringUtils.hasText(normalizedNamespace) ? normalizedNamespace : fallbackNamespace;
}
}

View File

@@ -22,8 +22,6 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.boot.logging.DeferredLog;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import static org.springframework.cloud.kubernetes.commons.KubernetesClientProperties.SERVICE_ACCOUNT_NAMESPACE_PATH;
@@ -31,7 +29,9 @@ import static org.springframework.cloud.kubernetes.commons.KubernetesClientPrope
/**
* @author Ryan Baxter
*/
public class KubernetesNamespaceProvider implements ApplicationListener<ApplicationEvent> {
public class KubernetesNamespaceProvider {
private static final DeferredLog LOG = new DeferredLog();
/**
* Property name for namespace.
@@ -43,22 +43,18 @@ public class KubernetesNamespaceProvider implements ApplicationListener<Applicat
*/
public static final String NAMESPACE_PATH_PROPERTY = "spring.cloud.kubernetes.client.serviceAccountNamespacePath";
private static final DeferredLog LOG = new DeferredLog();
private String serviceAccountNamespace;
private Environment environment;
private final Environment environment;
public KubernetesNamespaceProvider(Environment env) {
this.environment = env;
LOG.replayTo(KubernetesNamespaceProvider.class);
}
public String getNamespace() {
String namespace = environment.getProperty(NAMESPACE_PROPERTY);
if (namespace == null) {
namespace = getServiceAccountNamespace();
}
return namespace;
return namespace != null ? namespace : getServiceAccountNamespace();
}
private String getServiceAccountNamespace() {
@@ -70,28 +66,17 @@ public class KubernetesNamespaceProvider implements ApplicationListener<Applicat
return serviceAccountNamespace;
}
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
LOG.replayTo(KubernetesNamespaceProvider.class);
}
public static String getNamespaceFromServiceAccountFile(String path) {
String namespace = null;
if (LOG.isDebugEnabled()) {
LOG.debug("Looking for service account namespace at: [" + path + "].");
}
LOG.debug("Looking for service account namespace at: [" + path + "].");
Path serviceAccountNamespacePath = Paths.get(path);
boolean serviceAccountNamespaceExists = Files.isRegularFile(serviceAccountNamespacePath);
if (serviceAccountNamespaceExists) {
if (LOG.isDebugEnabled()) {
LOG.debug("Found service account namespace at: [" + serviceAccountNamespacePath + "].");
}
LOG.debug("Found service account namespace at: [" + serviceAccountNamespacePath + "].");
try {
namespace = new String(Files.readAllBytes((serviceAccountNamespacePath)));
if (LOG.isDebugEnabled()) {
LOG.debug("Service account namespace value: " + serviceAccountNamespacePath);
}
LOG.debug("Service account namespace value: " + serviceAccountNamespacePath);
}
catch (IOException ioe) {
LOG.error("Error reading service account namespace from: [" + serviceAccountNamespacePath + "].", ioe);