Skip to content

Commit

Permalink
Merge pull request #1117 from michalvavrik/feature/fix-ocp-kafka-regi…
Browse files Browse the repository at this point in the history
…stry-test-by-config-props

Fix OpenShiftStrimziKafkaWithRegistryMessagingIT by filtering out invalid environment variables propagated to the OpenShift pod
  • Loading branch information
rsvoboda authored May 7, 2024
2 parents 9500b6b + 3e05420 commit 5090252
Showing 1 changed file with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -741,14 +741,16 @@ private String enrichTemplate(Service service, String template, Map<String, Stri
final Map<String, String> environmentVariables;
final boolean isQuarkusRuntime = "quarkus".equals(templateMetadataLabels.get("app.openshift.io/runtime"));
if (isQuarkusRuntime) {
var propsThatRequireDottedFormat = appPropsThatRequireDottedFormat(enrichProperties);

// Configuration properties are only converted to MP Config format for Quarkus runtime for
// other runtimes may expect different format.
environmentVariables = convertPropertiesToEnvironment(enrichProperties);
environmentVariables = convertPropertiesToEnvironment(enrichProperties, propsThatRequireDottedFormat);

// config properties that contains dashes or other special chars need to be added in dotted
// format in a config source with lower priority than env var config source has
// see https://quarkus.io/version/main/guides/config-reference#environment-variables for more info
var appPropertiesFileContent = createAppPropsForPropsThatRequireDottedFormat(enrichProperties);
var appPropertiesFileContent = createAppPropsForPropsThatRequireDottedFormat(propsThatRequireDottedFormat);
if (!appPropertiesFileContent.isEmpty()) {
mountPropertiesAsQuarkusAppConfigFile(service, enrichProperties, deployment, appPropertiesFileContent);
}
Expand Down Expand Up @@ -785,6 +787,14 @@ private String enrichTemplate(Service service, String template, Map<String, Stri
}

private String createAppPropsForPropsThatRequireDottedFormat(Map<String, String> configProperties) {
return configProperties
.entrySet()
.stream()
.map(e -> e.getKey() + "=" + e.getValue() + System.lineSeparator())
.collect(Collectors.joining());
}

private static Map<String, String> appPropsThatRequireDottedFormat(Map<String, String> configProperties) {
return configProperties
.entrySet()
.stream()
Expand All @@ -794,8 +804,7 @@ private String createAppPropsForPropsThatRequireDottedFormat(Map<String, String>
var configPropertyNameCreatedFromEnvName = StringUtil.toLowerCaseAndDotted(environmentVariableName);
return !configPropertyNameCreatedFromEnvName.equalsIgnoreCase(configPropertyName);
})
.map(e -> e.getKey() + "=" + e.getValue() + System.lineSeparator())
.collect(Collectors.joining());
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

private void mountPropertiesAsQuarkusAppConfigFile(Service service, Map<String, String> enrichProperties,
Expand Down Expand Up @@ -836,11 +845,17 @@ private void mountPropertiesAsQuarkusAppConfigFile(Service service, Map<String,
* <p>
* see https://quarkus.io/guides/config-reference#environment-variables for details
*/
private static Map<String, String> convertPropertiesToEnvironment(Map<String, String> properties) {
private static Map<String, String> convertPropertiesToEnvironment(Map<String, String> properties,
Map<String, String> propsThatRequireDottedFormat) {
HashMap<String, String> environment = new HashMap<>(properties.size());
properties.forEach((property, value) -> {
String variable = StringUtil.replaceNonAlphanumericByUnderscores(property).toUpperCase();
environment.put(variable, value);
// non-Quarkus properties may require different format than MP config format
// they could also consume environment variables directly but the mapping from env vars is not one to one,
// therefore we should use Quarkus config source to propagate the information
if (!propsThatRequireDottedFormat.containsKey(property) || isQuarkusProperty(property)) {
String variable = StringUtil.replaceNonAlphanumericByUnderscores(property).toUpperCase();
environment.put(variable, value);
}
});
return environment;
}
Expand Down Expand Up @@ -1101,4 +1116,8 @@ private String generateRandomProjectName() {
.collect(() -> new StringBuilder("ts-"), StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}

private static boolean isQuarkusProperty(String propertyKey) {
return propertyKey.startsWith("quarkus.");
}
}

0 comments on commit 5090252

Please sign in to comment.