From 4b0aec354b285a5e620161bbc4891b416443e1f9 Mon Sep 17 00:00:00 2001 From: erabii Date: Fri, 10 Feb 2023 15:43:34 +0200 Subject: [PATCH] move to commons (#1220) --- .../commons/config/ConfigUtils.java | 17 ++++++++++++ .../commons/config/ConfigUtilsTests.java | 26 +++++++++++++++++++ .../discovery/KubernetesDiscoveryClient.java | 26 +++---------------- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java index fc814318..8f0cca6a 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java @@ -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 keysWithPrefix(Map map, String prefix) { + if (map == null || map.isEmpty()) { + return Map.of(); + } + + if (!StringUtils.hasText(prefix)) { + return map; + } + + Map result = CollectionUtils.newHashMap(map.size()); + map.forEach((key, value) -> result.put(prefix + key, value)); + return result; + } + public static final class Prefix { /** diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java index 274a99f2..54f30c5c 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java @@ -196,4 +196,30 @@ class ConfigUtilsTests { Assertions.assertEquals(result.data().get("propC"), "C"); } + @Test + void testKeysWithPrefixNullMap() { + Map result = ConfigUtils.keysWithPrefix(null, ""); + Assertions.assertTrue(result.isEmpty()); + } + + @Test + void testKeysWithPrefixEmptyMap() { + Map result = ConfigUtils.keysWithPrefix(Map.of(), ""); + Assertions.assertTrue(result.isEmpty()); + } + + @Test + void testKeysWithPrefixEmptyPrefix() { + Map result = ConfigUtils.keysWithPrefix(Map.of("a", "b"), ""); + Assertions.assertFalse(result.isEmpty()); + Assertions.assertEquals(Map.of("a", "b"), result); + } + + @Test + void testKeysWithPrefixNonEmptyPrefix() { + Map 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); + } + } diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClient.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClient.java index 9f643758..f006d660 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClient.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/KubernetesDiscoveryClient.java @@ -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 ports = s.getPorts().stream() .filter(port -> StringUtils.hasText(port.getName())) .collect(toMap(EndpointPort::getName, port -> Integer.toString(port.getPort()))); - Map portMetadata = getMapWithPrefixedKeys(ports, metadataProps.portsPrefix()); + Map 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 serviceMetadata = new HashMap<>(); KubernetesDiscoveryProperties.Metadata metadataProps = this.properties.metadata(); if (metadataProps.addLabels()) { - Map labelMetadata = getMapWithPrefixedKeys(service.getMetadata().getLabels(), + Map 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 annotationMetadata = getMapWithPrefixedKeys(service.getMetadata().getAnnotations(), + Map 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 getMapWithPrefixedKeys(Map 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 result = new HashMap<>(); - map.forEach((k, v) -> result.put(prefix + k, v)); - - return result; - } - @Override public List getServices() { return adapter.apply(client).stream().map(s -> s.getMetadata().getName()).toList();