move to commons (#1220)

This commit is contained in:
erabii
2023-02-10 15:43:34 +02:00
committed by GitHub
parent fe50304968
commit 4b0aec354b
3 changed files with 47 additions and 22 deletions

View File

@@ -268,6 +268,23 @@ public final class ConfigUtils {
.registerSingleton(name, event.getBootstrapContext().get(cls)));
}
/**
* append prefix to the keys and return a new Map with the new values.
*/
public static Map<String, String> keysWithPrefix(Map<String, String> map, String prefix) {
if (map == null || map.isEmpty()) {
return Map.of();
}
if (!StringUtils.hasText(prefix)) {
return map;
}
Map<String, String> result = CollectionUtils.newHashMap(map.size());
map.forEach((key, value) -> result.put(prefix + key, value));
return result;
}
public static final class Prefix {
/**

View File

@@ -196,4 +196,30 @@ class ConfigUtilsTests {
Assertions.assertEquals(result.data().get("propC"), "C");
}
@Test
void testKeysWithPrefixNullMap() {
Map<String, String> result = ConfigUtils.keysWithPrefix(null, "");
Assertions.assertTrue(result.isEmpty());
}
@Test
void testKeysWithPrefixEmptyMap() {
Map<String, String> result = ConfigUtils.keysWithPrefix(Map.of(), "");
Assertions.assertTrue(result.isEmpty());
}
@Test
void testKeysWithPrefixEmptyPrefix() {
Map<String, String> result = ConfigUtils.keysWithPrefix(Map.of("a", "b"), "");
Assertions.assertFalse(result.isEmpty());
Assertions.assertEquals(Map.of("a", "b"), result);
}
@Test
void testKeysWithPrefixNonEmptyPrefix() {
Map<String, String> result = ConfigUtils.keysWithPrefix(Map.of("a", "b", "c", "d"), "prefix-");
Assertions.assertFalse(result.isEmpty());
Assertions.assertEquals(Map.of("prefix-a", "b", "prefix-c", "d"), result);
}
}

View File

@@ -41,6 +41,7 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import static java.util.stream.Collectors.toMap;
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.keysWithPrefix;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTP;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTPS;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.NAMESPACE_METADATA_KEY;
@@ -160,7 +161,7 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
Map<String, String> ports = s.getPorts().stream()
.filter(port -> StringUtils.hasText(port.getName()))
.collect(toMap(EndpointPort::getName, port -> Integer.toString(port.getPort())));
Map<String, String> portMetadata = getMapWithPrefixedKeys(ports, metadataProps.portsPrefix());
Map<String, String> portMetadata = keysWithPrefix(ports, metadataProps.portsPrefix());
if (log.isDebugEnabled()) {
log.debug("Adding port metadata: " + portMetadata);
}
@@ -202,7 +203,7 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
final Map<String, String> serviceMetadata = new HashMap<>();
KubernetesDiscoveryProperties.Metadata metadataProps = this.properties.metadata();
if (metadataProps.addLabels()) {
Map<String, String> labelMetadata = getMapWithPrefixedKeys(service.getMetadata().getLabels(),
Map<String, String> labelMetadata = keysWithPrefix(service.getMetadata().getLabels(),
metadataProps.labelsPrefix());
if (log.isDebugEnabled()) {
log.debug("Adding label metadata: " + labelMetadata);
@@ -210,7 +211,7 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
serviceMetadata.putAll(labelMetadata);
}
if (metadataProps.addAnnotations()) {
Map<String, String> annotationMetadata = getMapWithPrefixedKeys(service.getMetadata().getAnnotations(),
Map<String, String> annotationMetadata = keysWithPrefix(service.getMetadata().getAnnotations(),
metadataProps.annotationsPrefix());
if (log.isDebugEnabled()) {
log.debug("Adding annotation metadata: " + annotationMetadata);
@@ -266,25 +267,6 @@ public class KubernetesDiscoveryClient implements DiscoveryClient {
return es;
}
// returns a new map that contain all the entries of the original map
// but with the keys prefixed
// if the prefix is null or empty, the map itself is returned (unchanged of course)
private Map<String, String> getMapWithPrefixedKeys(Map<String, String> map, String prefix) {
if (map == null) {
return new HashMap<>();
}
// when the prefix is empty just return an map with the same entries
if (!StringUtils.hasText(prefix)) {
return map;
}
final Map<String, String> result = new HashMap<>();
map.forEach((k, v) -> result.put(prefix + k, v));
return result;
}
@Override
public List<String> getServices() {
return adapter.apply(client).stream().map(s -> s.getMetadata().getName()).toList();