Skip to content

Commit

Permalink
Add PostgreSQLBuilder and configuration options to PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
tommaso-borgato committed Nov 7, 2024
1 parent fd595bc commit f4f10cb
Show file tree
Hide file tree
Showing 3 changed files with 546 additions and 25 deletions.
78 changes: 72 additions & 6 deletions builder/src/main/java/cz/xtf/builder/db/AbstractDatabase.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cz.xtf.builder.db;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import cz.xtf.builder.builders.ApplicationBuilder;
import cz.xtf.builder.builders.DeploymentConfigBuilder;
Expand All @@ -11,6 +14,9 @@
import cz.xtf.builder.builders.pod.PersistentVolumeClaim;

public abstract class AbstractDatabase extends DefaultStatefulAuxiliary {
private static final String DEFAULT_USERNAME = "testuser";
private static final String DEFAULT_PASSWORD = "testpwd";
private static final String DEFAULT_DATABASE_NAME = "testdb";
protected final String username;
protected final String password;
protected final String dbName;
Expand All @@ -26,12 +32,49 @@ public abstract class AbstractDatabase extends DefaultStatefulAuxiliary {
protected boolean withReadinessProbe;
protected boolean withStartupProbe;

private Supplier<String> deploymentConfigName;
private Supplier<String> envVarPrefix;

public void setDeploymentConfigName(Supplier<String> deploymentConfigName) {
this.deploymentConfigName = deploymentConfigName;
}

public void setEnvVarPrefix(Supplier<String> envVarPrefix) {
this.envVarPrefix = envVarPrefix;
}

public AbstractDatabase(
String symbolicName,
String dataDir,
PersistentVolumeClaim pvc,
String username,
String password,
String dbName,
boolean configureEnvironment,
boolean withLivenessProbe,
boolean withReadinessProbe,
boolean withStartupProbe,
Supplier<String> deploymentConfigName,
Supplier<String> envVarPrefix) {
super(symbolicName, dataDir, pvc);
this.symbolicName = symbolicName;
this.username = (username == null || username.isEmpty()) ? DEFAULT_USERNAME : username;
this.password = (password == null || password.isEmpty()) ? DEFAULT_PASSWORD : password;
this.dbName = (dbName == null || dbName.isEmpty()) ? DEFAULT_DATABASE_NAME : dbName;
this.withLivenessProbe = withLivenessProbe;
this.withReadinessProbe = withReadinessProbe;
this.withStartupProbe = withStartupProbe;
this.configureEnvironment = configureEnvironment;
this.deploymentConfigName = deploymentConfigName;
this.envVarPrefix = envVarPrefix;
}

public AbstractDatabase(String symbolicName, String dataDir) {
this("testuser", "testpwd", "testdb", symbolicName, dataDir);
this(DEFAULT_USERNAME, DEFAULT_PASSWORD, DEFAULT_DATABASE_NAME, symbolicName, dataDir);
}

public AbstractDatabase(String symbolicName, String dataDir, boolean withLivenessProbe, boolean withReadinessProbe) {
this("testuser", "testpwd", "testdb", symbolicName, dataDir);
this(DEFAULT_USERNAME, DEFAULT_PASSWORD, DEFAULT_DATABASE_NAME, symbolicName, dataDir);

this.withLivenessProbe = withLivenessProbe;
this.withReadinessProbe = withReadinessProbe;
Expand All @@ -54,12 +97,13 @@ public AbstractDatabase(String symbolicName, String dataDir, boolean withLivenes
}

public AbstractDatabase(String symbolicName, String dataDir, PersistentVolumeClaim pvc) {
this("testuser", "testpwd", "testdb", symbolicName, dataDir, pvc);
this(DEFAULT_USERNAME, DEFAULT_PASSWORD, DEFAULT_DATABASE_NAME, symbolicName, dataDir, pvc);
}

public AbstractDatabase(String symbolicName, String dataDir, PersistentVolumeClaim pvc, boolean withLivenessProbe,
boolean withReadinessProbe) {
this("testuser", "testpwd", "testdb", symbolicName, dataDir, pvc, withLivenessProbe, withReadinessProbe);
this(DEFAULT_USERNAME, DEFAULT_PASSWORD, DEFAULT_DATABASE_NAME, symbolicName, dataDir, pvc, withLivenessProbe,
withReadinessProbe);
}

public AbstractDatabase(String username, String password, String dbName, String symbolicName, String dataDir,
Expand All @@ -73,7 +117,7 @@ public AbstractDatabase(String username, String password, String dbName, String

public AbstractDatabase(String symbolicName, String dataDir, PersistentVolumeClaim pvc, boolean withLivenessProbe,
boolean withReadinessProbe, boolean withStartupProbe) {
this("testuser", "testpwd", "testdb", symbolicName, dataDir, pvc);
this(DEFAULT_USERNAME, DEFAULT_PASSWORD, DEFAULT_DATABASE_NAME, symbolicName, dataDir, pvc);

this.withLivenessProbe = withLivenessProbe;
this.withReadinessProbe = withReadinessProbe;
Expand Down Expand Up @@ -148,14 +192,28 @@ public Map<String, String> getImageVariables() {
return vars;
}

public List<String> getImageArgs() {
return Collections.emptyList();
}

public String getServiceAccount() {
return null;
}

public String getDeploymentConfigName() {
if (deploymentConfigName != null) {
return deploymentConfigName.get();
}
if (openShiftName == null) {
openShiftName = dbName.toLowerCase() + "-" + getSymbolicName().toLowerCase();
}
return openShiftName;
}

public String getEnvVarPrefix() {
if (envVarPrefix != null) {
return envVarPrefix.get();
}
return dbName.toUpperCase() + "_" + getSymbolicName().toUpperCase();
}

Expand Down Expand Up @@ -183,11 +241,19 @@ public DeploymentConfigBuilder configureDeployment(ApplicationBuilder appBuilder

public DeploymentConfigBuilder configureDeployment(ApplicationBuilder appBuilder, boolean synchronous) {
final DeploymentConfigBuilder builder = appBuilder.deploymentConfig(getDeploymentConfigName());
builder.podTemplate().container().fromImage(getImageName()).envVars(getImageVariables()).port(getPort());
ContainerBuilder containerBuilder = builder.podTemplate().container().fromImage(getImageName())
.envVars(getImageVariables())
.port(getPort());
if (getImageArgs() != null) {
getImageArgs().forEach(containerBuilder::args);
}
if (synchronous) {
builder.onConfigurationChange();
builder.synchronousDeployment();
}
if (getServiceAccount() != null) {
builder.podTemplate().addServiceAccount(getServiceAccount());
}

configureContainer(builder.podTemplate().container());

Expand Down
29 changes: 29 additions & 0 deletions builder/src/main/java/cz/xtf/builder/db/AbstractSQLDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.apache.commons.io.IOUtils;

Expand All @@ -12,6 +13,34 @@

public abstract class AbstractSQLDatabase extends AbstractDatabase implements SQLExecutor {

public AbstractSQLDatabase(
String symbolicName,
String dataDir,
PersistentVolumeClaim pvc,
String username,
String password,
String dbName,
boolean configureEnvironment,
boolean withLivenessProbe,
boolean withReadinessProbe,
boolean withStartupProbe,
Supplier<String> deploymentConfigName,
Supplier<String> envVarPrefix) {
super(
symbolicName,
dataDir,
pvc,
username,
password,
dbName,
configureEnvironment,
withLivenessProbe,
withReadinessProbe,
withStartupProbe,
deploymentConfigName,
envVarPrefix);
}

public AbstractSQLDatabase(String symbolicName, String dataDir, boolean withLivenessProbe, boolean withReadinessProbe) {
super(symbolicName, dataDir, withLivenessProbe, withReadinessProbe);
}
Expand Down
Loading

0 comments on commit f4f10cb

Please sign in to comment.