From 87ba285df400cf4f9eb2147c6176305dd5b6f183 Mon Sep 17 00:00:00 2001 From: Helber Belmiro Date: Tue, 29 Aug 2023 15:29:50 -0300 Subject: [PATCH] Fixed interpolation Signed-off-by: Helber Belmiro --- .../k8s/config/ConfigValueExpander.java | 30 ++++++------------- .../k8s/config/ConfigValueExpanderTest.java | 17 +++++++++++ 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/quarkus/addons/kubernetes/runtime/src/main/java/org/kie/kogito/addons/quarkus/k8s/config/ConfigValueExpander.java b/quarkus/addons/kubernetes/runtime/src/main/java/org/kie/kogito/addons/quarkus/k8s/config/ConfigValueExpander.java index bf08f172047..eae96b81cc9 100644 --- a/quarkus/addons/kubernetes/runtime/src/main/java/org/kie/kogito/addons/quarkus/k8s/config/ConfigValueExpander.java +++ b/quarkus/addons/kubernetes/runtime/src/main/java/org/kie/kogito/addons/quarkus/k8s/config/ConfigValueExpander.java @@ -15,26 +15,10 @@ */ package org.kie.kogito.addons.quarkus.k8s.config; -import java.util.Arrays; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -import org.kie.kogito.addons.k8s.resource.catalog.KubernetesProtocol; - import io.smallrye.config.ConfigValue; class ConfigValueExpander { - private static final Pattern placeholderPattern = createPlaceholderPattern(); - - private static Pattern createPlaceholderPattern() { - String protocols = Arrays.stream(KubernetesProtocol.values()) - .map(KubernetesProtocol::getValue) - .collect(Collectors.joining("|")); - return Pattern.compile("\\$\\{(" + protocols + "):.+}"); - } - private final KubeDiscoveryConfigCache kubeDiscoveryConfigCache; ConfigValueExpander(KubeDiscoveryConfigCache kubeDiscoveryConfigCache) { @@ -56,14 +40,18 @@ ConfigValue expand(ConfigValue configValue) { } public static String interpolate(String input, String replacement) { - return placeholderPattern.matcher(input).replaceAll(replacement); + int startIndex = input.indexOf("${"); + int endIndex = input.indexOf("}", startIndex); + + return input.substring(0, startIndex) + replacement + input.substring(endIndex + 1); } - private static String extractServiceCoordinates(String rawValue) { - Matcher matcher = placeholderPattern.matcher(rawValue); + static String extractServiceCoordinates(String rawValue) { + int startIndex = rawValue.indexOf("${"); + int endIndex = rawValue.indexOf("}", startIndex); - if (matcher.find()) { - return matcher.group(1); + if (startIndex != -1 && endIndex != -1) { + return rawValue.substring(startIndex + 2, endIndex); } else { return null; } diff --git a/quarkus/addons/kubernetes/runtime/src/test/java/org/kie/kogito/addons/quarkus/k8s/config/ConfigValueExpanderTest.java b/quarkus/addons/kubernetes/runtime/src/test/java/org/kie/kogito/addons/quarkus/k8s/config/ConfigValueExpanderTest.java index 6721437030d..a5de2db6f3e 100644 --- a/quarkus/addons/kubernetes/runtime/src/test/java/org/kie/kogito/addons/quarkus/k8s/config/ConfigValueExpanderTest.java +++ b/quarkus/addons/kubernetes/runtime/src/test/java/org/kie/kogito/addons/quarkus/k8s/config/ConfigValueExpanderTest.java @@ -16,9 +16,12 @@ package org.kie.kogito.addons.quarkus.k8s.config; import java.util.Optional; +import java.util.stream.Stream; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.ValueSource; import io.smallrye.config.ConfigValue; @@ -27,6 +30,20 @@ class ConfigValueExpanderTest { + static Stream extractServiceCoordinatesSource() { + return Stream.of( + Arguments.of("${kubernetes:pods.v1/kie/kogito}/path", "kubernetes:pods.v1/kie/kogito"), + Arguments.of("${knative:services.v1.serving.knative.dev/default/serverless-workflow-greeting-quarkus}/path", + "knative:services.v1.serving.knative.dev/default/serverless-workflow-greeting-quarkus")); + } + + @ParameterizedTest + @MethodSource("extractServiceCoordinatesSource") + void extractServiceCoordinates(String expandableValue, String expectedCoordinate) { + assertThat(ConfigValueExpander.extractServiceCoordinates(expandableValue)) + .isEqualTo(expectedCoordinate); + } + @ParameterizedTest @ValueSource(strings = { "${kubernetes:pods.v1/kie/kogito}/path", "${openshift:pods.v1/kie/kogito}/path", "${knative:kie/kogito}/path" }) void expandable(String expandableValues) {