Skip to content

Commit

Permalink
Introduce support for the "jdbc-uri" property in k8s service binding
Browse files Browse the repository at this point in the history
Plus, log a warning when the uri property does not have the expected format
Fix quarkusio#30682
  • Loading branch information
Sgitario committed Feb 7, 2023
1 parent 97ca0fe commit a32c644
Showing 1 changed file with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@

public abstract class DatasourceServiceBindingConfigSourceFactory
implements Function<ServiceBinding, ServiceBindingConfigSource> {

private static final Logger log = Logger.getLogger(DatasourceServiceBindingConfigSourceFactory.class);

private final String configSourceNamePrefix;
private final String urlPropertyName;
private final String prefix;
private final String urlFormat;
protected ServiceBinding serviceBinding;

private DatasourceServiceBindingConfigSourceFactory(String configSourceNamePrefix, String urlPropertyName,
String urlFormat) {
String prefix, String urlFormat) {
this.configSourceNamePrefix = configSourceNamePrefix;
this.urlPropertyName = urlPropertyName;
this.prefix = prefix;
this.urlFormat = urlFormat;
}

Expand Down Expand Up @@ -47,14 +48,13 @@ private Map<String, String> getServiceBindingProperties() {
log.debugf("Property 'password' was not found for datasource of type %s", serviceBinding.getType());
}

String jdbcURL = bindingProperties.get("jdbc-url");
if (jdbcURL != null) {
properties.put(urlPropertyName, jdbcURL);
if (configureUrlPropertyUsingKey(properties, "jdbc-url")) {
return properties;
}
if (configureUrlPropertyUsingKey(properties, "jdbc-uri")) {
return properties;
}
String uri = bindingProperties.get("uri");
if (uri != null) {
properties.put(urlPropertyName, uri);
if (configureUrlPropertyUsingKey(properties, "uri")) {
return properties;
}

Expand All @@ -79,23 +79,38 @@ protected String formatUrl(String urlFormat, String type, String host, String da
return String.format(urlFormat, type, host, portPart, database);
}

private boolean configureUrlPropertyUsingKey(Map<String, String> properties, String key) {
String value = serviceBinding.getProperties().get(key);
if (value != null) {
if (value.startsWith(prefix)) {
properties.put(urlPropertyName, value);
return true;
} else {
log.warnf("The value '%s' from the property '%s' does not start with '%s'. It will be ignored.",
value, key, prefix);
}
}

return false;
}

public static class Jdbc extends DatasourceServiceBindingConfigSourceFactory {
public Jdbc() {
super("jdbc", "quarkus.datasource.jdbc.url", "jdbc:%s://%s%s/%s");
this("jdbc:%s://%s%s/%s");
}

public Jdbc(String urlFormat) {
super("jdbc", "quarkus.datasource.jdbc.url", urlFormat);
super("jdbc", "quarkus.datasource.jdbc.url", "jdbc:", urlFormat);
}
}

public static class Reactive extends DatasourceServiceBindingConfigSourceFactory {
public Reactive() {
super("reactive", "quarkus.datasource.reactive.url", "%s://%s%s/%s");
this("%s://%s%s/%s");
}

public Reactive(String urlFormat) {
super("reactive", "quarkus.datasource.reactive.url", urlFormat);
super("reactive", "quarkus.datasource.reactive.url", "", urlFormat);
}
}
}

0 comments on commit a32c644

Please sign in to comment.