Skip to content

Commit

Permalink
Allow configuration YAML property values to reference other configs
Browse files Browse the repository at this point in the history
Enables references to ENVs set from secrets

Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Jun 11, 2024
1 parent 157690f commit 032f6c5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ Map<String, Object> buildConfig(Set<String> configNames,
.stream()
.map(configName -> Optional.ofNullable(clientProperties.get().get(configName))
.or(() -> Optional.ofNullable(config.getProperties().get(configName)))
.map(this::resolveValue)
.or(() -> getDefaultConfig(clientType, configName))
.map(configValue -> Map.entry(configName, configValue)))
.filter(Optional::isPresent)
Expand Down Expand Up @@ -396,6 +397,24 @@ Map<String, Object> buildConfig(Set<String> configNames,
return cfg;
}

/**
* If the given value is an expression referencing a configuration value,
* replace it with the target property value.
*
* @param value configuration value that may be a reference to another
* configuration property
* @return replacement property or the same value if the given string is not a
* reference.
*/
private String resolveValue(String value) {
if (value.startsWith("${") && value.endsWith("}")) {
String replacement = value.substring(2, value.length() - 1);
return config.getOptionalValue(replacement, String.class).orElse(value);
}

return value;
}

Optional<String> getDefaultConfig(String clientType, String configName) {
String clientSpecificKey = "console.kafka.%s.%s".formatted(clientType, configName);
String generalKey = "console.kafka.%s".formatted(configName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,28 @@ public Map<String, String> start() {
- name: test-kafka1
namespace: default
properties:
bootstrap.servers: %s
bootstrap.servers: ${console.test.external-bootstrap}
- name: test-kafka2
namespace: default
properties:
bootstrap.servers: %s
bootstrap.servers: ${console.test.random-bootstrap}
- name: test-kafka3
namespace: default
# listener is named and bootstrap.servers not set (will be retrieved from Kafka CR)
listener: listener0
properties:
security.protocol: SSL
""".formatted(
externalBootstrap,
randomBootstrapServers.toString(),
externalBootstrap),
""",
StandardOpenOption.WRITE);
} catch (IOException e) {
throw new UncheckedIOException(e);
}

return Map.ofEntries(
Map.entry(profile + CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, externalBootstrap),
Map.entry(profile + "console.config-path", configFile.getAbsolutePath()));
Map.entry(profile + "console.config-path", configFile.getAbsolutePath()),
Map.entry(profile + "console.test.external-bootstrap", externalBootstrap),
Map.entry(profile + "console.test.random-bootstrap", randomBootstrapServers.toString()));
}

@Override
Expand Down

0 comments on commit 032f6c5

Please sign in to comment.