simplification in config server (#1614)

This commit is contained in:
erabii
2024-03-28 19:55:43 +02:00
committed by GitHub
parent cb3d7ca781
commit 470ab676d2
3 changed files with 17 additions and 14 deletions

View File

@@ -29,6 +29,7 @@ import org.springframework.cloud.kubernetes.commons.config.SourceData;
*/
public class KubernetesClientSecretsPropertySource extends SecretsPropertySource {
@Deprecated(forRemoval = true)
public KubernetesClientSecretsPropertySource(SourceData sourceData) {
super(sourceData);
}

View File

@@ -16,9 +16,8 @@
package org.springframework.cloud.kubernetes.configserver;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -41,11 +40,11 @@ public class KubernetesEnvironmentRepository implements EnvironmentRepository {
private static final Log LOG = LogFactory.getLog(KubernetesEnvironmentRepository.class);
private CoreV1Api coreApi;
private final CoreV1Api coreApi;
private List<KubernetesPropertySourceSupplier> kubernetesPropertySourceSuppliers;
private final List<KubernetesPropertySourceSupplier> kubernetesPropertySourceSuppliers;
private String namespace;
private final String namespace;
public KubernetesEnvironmentRepository(CoreV1Api coreApi,
List<KubernetesPropertySourceSupplier> kubernetesPropertySourceSuppliers, String namespace) {
@@ -64,8 +63,7 @@ public class KubernetesEnvironmentRepository implements EnvironmentRepository {
if (!StringUtils.hasText(profile)) {
profile = "default";
}
List<String> profiles = new java.util.ArrayList<>(
Arrays.stream(StringUtils.commaDelimitedListToStringArray(profile)).toList());
List<String> profiles = new ArrayList<>(List.of(StringUtils.commaDelimitedListToStringArray(profile)));
Collections.reverse(profiles);
if (!profiles.contains("default")) {
@@ -98,8 +96,7 @@ public class KubernetesEnvironmentRepository implements EnvironmentRepository {
}
private MutablePropertySources createPropertySources(String application) {
Map<String, Object> applicationProperties = new HashMap<>();
applicationProperties.put("spring.application.name", application);
Map<String, Object> applicationProperties = Map.of("spring.application.name", application);
MapPropertySource propertySource = new MapPropertySource("kubernetes-config-server", applicationProperties);
MutablePropertySources mutablePropertySources = new MutablePropertySources();
mutablePropertySources.addFirst(propertySource);
@@ -108,7 +105,7 @@ public class KubernetesEnvironmentRepository implements EnvironmentRepository {
private void addApplicationConfiguration(Environment environment, StandardEnvironment springEnv,
String applicationName) {
kubernetesPropertySourceSuppliers.stream().forEach(supplier -> {
kubernetesPropertySourceSuppliers.forEach(supplier -> {
List<MapPropertySource> propertySources = supplier.get(coreApi, applicationName, namespace, springEnv);
propertySources.forEach(propertySource -> {
if (propertySource.getPropertyNames().length > 0) {

View File

@@ -16,8 +16,6 @@
package org.springframework.cloud.kubernetes.configserver;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import io.kubernetes.client.openapi.apis.CoreV1Api;
@@ -33,11 +31,18 @@ public interface KubernetesPropertySourceSupplier {
List<MapPropertySource> get(CoreV1Api coreV1Api, String name, String namespace, Environment environment);
/*
* return either a List containing 'currentNamespace' (if 'namespacesString' is empty
* or null), or a List of comma delimited tokens (namespaces) from 'namespacesString'.
*
* 'currentNamespace' can be treated logically as the "default namespace" to use, if
* the other argument is not provided.
*/
static List<String> namespaceSplitter(String namespacesString, String currentNamespace) {
List<String> namespaces = Collections.singletonList(currentNamespace);
List<String> namespaces = List.of(currentNamespace);
String[] namespacesArray = StringUtils.commaDelimitedListToStringArray(namespacesString);
if (namespacesArray.length > 0) {
namespaces = Arrays.asList(namespacesArray);
namespaces = List.of(namespacesArray);
}
return namespaces;
}