-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ENV VAR kv parsing to handle json values
- JOB_KUBE_ANNOTATIONS can have JSON as value - This fixes the parsin of such values
- Loading branch information
Showing
4 changed files
with
129 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...e-commons-with-dependencies/src/main/java/io/airbyte/commons/workers/config/EnvUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.airbyte.commons.workers.config; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
@Slf4j | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
class EnvUtils { | ||
|
||
private static final Pattern KEY_JSON_VALUE_PATTERN = Pattern.compile("(?<key>[^=]+)\\s*?=\\s*?(?<value>\\{[^=]+})\\s*,?\\s*"); | ||
private static final Pattern KEY_VALUE_PATTERN = Pattern.compile("(?<key>[^=]+)=(?<value>[^=,]+),?"); | ||
private static final String KEY_GROUP_ALIAS = "key"; | ||
private static final String VALUE_GROUP_ALIAS = "value"; | ||
|
||
/** | ||
* Splits key value pairs from the input string into a map. Each kv-pair is separated by a ','. | ||
* </br> | ||
* The key and the value are separated by '='. | ||
* <p> | ||
* For example:- The following represents two map entries | ||
* </p> | ||
* - key1=value1,key2=value2 | ||
* </br> | ||
* - key1={key11: value1},key2={key22: value2} | ||
* </br> | ||
* - key1={key11: value11, key12: value12},key2={key21: value21, key22: value22} | ||
* | ||
* @param input string | ||
* @return map containing kv pairs | ||
*/ | ||
public static Map<String, String> splitKVPairsFromEnvString(final String input) { | ||
if (input == null || input.isBlank()) { | ||
return Map.of(); | ||
} | ||
final Map<String, String> jsonValuesMatchResult = match(input, KEY_JSON_VALUE_PATTERN); | ||
return jsonValuesMatchResult.isEmpty() | ||
? getKVPairsMatchedWithSimplePattern(input) | ||
: jsonValuesMatchResult; | ||
} | ||
|
||
private static Map<String, String> match(final String input, final Pattern pattern) { | ||
final Matcher matcher = pattern.matcher(input); | ||
final Map<String, String> kvResult = new HashMap<>(); | ||
while (matcher.find()) { | ||
kvResult.put(matcher.group(KEY_GROUP_ALIAS).trim(), matcher.group(VALUE_GROUP_ALIAS).trim()); | ||
} | ||
return kvResult; | ||
} | ||
|
||
private static Map<String, String> getKVPairsMatchedWithSimplePattern(final String input) { | ||
final Map<String, String> stringMatchResult = match(input, KEY_VALUE_PATTERN); | ||
if (stringMatchResult.isEmpty()) { | ||
log.warn("No valid key value pairs found in the input string: {}", input); | ||
return Collections.emptyMap(); | ||
} | ||
return stringMatchResult; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...mmons-with-dependencies/src/test/java/io/airbyte/commons/workers/config/EnvUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.airbyte.commons.workers.config; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class EnvUtilsTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource("splitKVPairsFromEnvString") | ||
void splitKVPairsFromEnvString(String input, Map<String, String> expected) { | ||
final Map<String, String> result = EnvUtils.splitKVPairsFromEnvString(input); | ||
assertThat(result).isEqualTo(expected); | ||
} | ||
|
||
private static Stream<Arguments> splitKVPairsFromEnvString() { | ||
return Stream.of( | ||
// unmatched | ||
Arguments.of("key1", Collections.emptyMap()), | ||
Arguments.of("key1,value", Collections.emptyMap()), | ||
Arguments.of("key1-value", Collections.emptyMap()), | ||
Arguments.of("key1:value", Collections.emptyMap()), | ||
// matched k:v pairs | ||
Arguments.of("key1=value1", Map.of("key1", "value1")), | ||
Arguments.of("key1 = value1", Map.of("key1", "value1")), | ||
Arguments.of("key1=value1,key2=value2", Map.of("key1", "value1", "key2", "value2")), | ||
Arguments.of("key1 = value1, key2 = value2", Map.of("key1", "value1", "key2", "value2")), | ||
// matched k:jsonV pairs | ||
Arguments.of("key1={value1}", Map.of("key1", "{value1}")), | ||
Arguments.of("key1={ value1 }", Map.of("key1", "{ value1 }")), | ||
Arguments.of("key1 = { value1 }", Map.of("key1", "{ value1 }")), | ||
Arguments.of("key1={value1},key2={value2}", Map.of("key1", "{value1}", "key2", "{value2}")), | ||
Arguments.of("key1= {value1} , key2={value2}", Map.of("key1", "{value1}", "key2", "{value2}")), | ||
Arguments.of("key1= {value1 } , key2= { value2}", Map.of("key1", "{value1 }", "key2", "{ value2}")), | ||
Arguments.of("key1={key11: value11},key2={key22: value22}", Map.of( | ||
"key1", "{key11: value11}", | ||
"key2", "{key22: value22}" | ||
)), | ||
Arguments.of("key1={key11: value11, key12: value12},key2={key21: value21, key22: value22}", Map.of( | ||
"key1", "{key11: value11, key12: value12}", | ||
"key2", "{key21: value21, key22: value22}" | ||
)), | ||
Arguments.of("key1={key11: value11, key12: value12, key13: {key131: value131}}," | ||
+ "key2={key21: value21, key22: value22, key23: {key231: value231}}", Map.of( | ||
"key1", "{key11: value11, key12: value12, key13: {key131: value131}}", | ||
"key2", "{key21: value21, key22: value22, key23: {key231: value231}}" | ||
)) | ||
); | ||
} | ||
|
||
} |