Skip to content

Commit

Permalink
Merge pull request #115 from fabiobrz/add.pgsql-template-prov
Browse files Browse the repository at this point in the history
[issue 114] - Adding the PostgreSql template provisioner (for legacy …
  • Loading branch information
fabiobrz authored Dec 12, 2023
2 parents 4b0d711 + 3316aae commit cf115a2
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
import org.jboss.intersmash.tools.application.openshift.KafkaOperatorApplication;
import org.jboss.intersmash.tools.application.openshift.MysqlImageOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.PostgreSQLImageOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.PostgreSQLTemplateOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.RhSsoTemplateOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.WildflyImageOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.input.BinarySource;
import org.jboss.intersmash.tools.application.openshift.input.BuildInput;
import org.jboss.intersmash.tools.application.openshift.input.BuildInputBuilder;
import org.jboss.intersmash.tools.application.openshift.template.Eap7Template;
import org.jboss.intersmash.tools.application.openshift.template.PostgreSQLTemplate;
import org.jboss.intersmash.tools.application.openshift.template.RhSsoTemplate;
import org.jboss.intersmash.tools.util.ProcessKeystoreGenerator;
import org.jboss.intersmash.tools.util.openshift.WildflyOpenShiftUtils;
Expand Down Expand Up @@ -576,7 +578,7 @@ public String getDbName() {
};
}

public static PostgreSQLImageOpenShiftApplication getPostgreSQLOpenShiftApplication() {
public static PostgreSQLImageOpenShiftApplication getPostgreSQLImageOpenShiftApplication() {
return new PostgreSQLImageOpenShiftApplication() {

@Override
Expand All @@ -596,6 +598,23 @@ public String getDbName() {
};
}

public static PostgreSQLTemplateOpenShiftApplication getPostgreSQLTemplateOpenShiftApplication() {
return new PostgreSQLTemplateOpenShiftApplication() {

String NAME = "postgresql";

@Override
public PostgreSQLTemplate getTemplate() {
return PostgreSQLTemplate.POSTGRESQL_EPHEMERAL;
}

@Override
public String getName() {
return NAME;
}
};
}

/**
* This method serves just for testing purpose. It implements necessary methods of {@link
* KafkaOperatorApplication} interface so we can successfully use it in our tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public class PostgreSQLImageTestCase {
private static final OpenShift openShift = OpenShifts.master();
private static final PostgreSQLImageOpenShiftApplication application = OpenShiftProvisionerTestBase
.getPostgreSQLOpenShiftApplication();
.getPostgreSQLImageOpenShiftApplication();
private static final PostgreSQLImageOpenShiftProvisioner provisioner = new PostgreSQLImageOpenShiftProvisioner(application);

@BeforeAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jboss.intersmash.tools.provision.openshift.MysqlImageOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.OpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.PostgreSQLImageOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.PostgreSQLTemplateOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.RhSsoTemplateOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.WildflyBootableJarImageOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.WildflyImageOpenShiftProvisioner;
Expand All @@ -48,7 +49,10 @@ private static Stream<OpenShiftProvisioner> provisionerProvider() {
new WildflyBootableJarImageOpenShiftProvisioner(
OpenShiftProvisionerTestBase.getWildflyBootableJarJavaxOpenShiftApplication()),
new MysqlImageOpenShiftProvisioner(OpenShiftProvisionerTestBase.getMysqlOpenShiftApplication()),
new PostgreSQLImageOpenShiftProvisioner(OpenShiftProvisionerTestBase.getPostgreSQLOpenShiftApplication()));
new PostgreSQLImageOpenShiftProvisioner(
OpenShiftProvisionerTestBase.getPostgreSQLImageOpenShiftApplication()),
new PostgreSQLTemplateOpenShiftProvisioner(
OpenShiftProvisionerTestBase.getPostgreSQLTemplateOpenShiftApplication()));
} else if (IntersmashTestsuiteProperties.isProductizedTestExecutionProfileEnabled()) {
return Stream.of(
new WildflyImageOpenShiftProvisioner(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jboss.intersmash.tools.application.openshift;

import java.util.Collections;
import java.util.Map;

import org.jboss.intersmash.tools.provision.openshift.template.OpenShiftTemplate;

/**
* This interface is not supposed to be implemented by user Applications. See the "Mapping of implemented provisioners"
* section of Appsint README.md file for the up-to-date list of supported end users Applications.
*/
public interface DBTemplateOpenShiftApplication<TT extends OpenShiftTemplate>
extends TemplateApplication<TT> {

@Override
default Map<String, String> getParameters() {
return Collections.emptyMap();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jboss.intersmash.tools.application.openshift;

import org.jboss.intersmash.tools.application.openshift.template.PostgreSQLTemplate;

/**
* End user Application interface which presents PostgreSQL template application on OpenShift Container Platform.
*
* See {@link PostgreSQLTemplate} for available templates the
* application can represent.
*
* The application will be deployed by:
* <ul>
* <li>{@link org.jboss.intersmash.tools.provision.openshift.PostgreSQLTemplateOpenShiftProvisioner}</li>
* </ul>
*/
public interface PostgreSQLTemplateOpenShiftApplication extends DBTemplateOpenShiftApplication<PostgreSQLTemplate> {

default String getName() {
return "postgresql";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jboss.intersmash.tools.application.openshift.template;

import org.jboss.intersmash.tools.provision.openshift.template.OpenShiftTemplate;

/**
* OpenShift templates for PostgreSQL.
*
* These are build in {@code openshift} namespace by default.
*/
public enum PostgreSQLTemplate implements OpenShiftTemplate {
POSTGRESQL_EPHEMERAL("postgresql-ephemeral"),
POSTGRESQL_PERSISTENT("postgresql-persistent");

private String name;

PostgreSQLTemplate(String name) {
this.name = name;
}

@Override
public String getLabel() {
return name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.jboss.intersmash.tools.provision.openshift;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jboss.intersmash.tools.application.openshift.PostgreSQLTemplateOpenShiftApplication;
import org.jboss.intersmash.tools.provision.openshift.template.OpenShiftTemplate;
import org.slf4j.event.Level;

import cz.xtf.core.event.helpers.EventHelper;
import cz.xtf.core.openshift.OpenShiftWaiters;
import cz.xtf.core.openshift.OpenShifts;
import cz.xtf.core.waiting.failfast.FailFastCheck;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.openshift.api.model.Template;
import lombok.extern.slf4j.Slf4j;

/**
* Provisions PostgreSql on OpenShift via PostgreSql template available on OpenShift and based on a
* {@link PostgreSQLTemplateOpenShiftApplication} concrete implementation.
*
*/
@Slf4j
public class PostgreSQLTemplateOpenShiftProvisioner implements OpenShiftProvisioner<PostgreSQLTemplateOpenShiftApplication> {
private FailFastCheck ffCheck = () -> false;
private final PostgreSQLTemplateOpenShiftApplication postgreSQLApplication;
private final OpenShiftTemplate postgreSQLTemplate;
private KubernetesList kubernetesList;

public PostgreSQLTemplateOpenShiftProvisioner(PostgreSQLTemplateOpenShiftApplication postgreSQLApplication) {
this.postgreSQLApplication = postgreSQLApplication;
this.postgreSQLTemplate = postgreSQLApplication.getTemplate();
}

@Override
public PostgreSQLTemplateOpenShiftApplication getApplication() {
return postgreSQLApplication;
}

@Override
public void deploy() {
ffCheck = FailFastUtils.getFailFastCheck(EventHelper.timeOfLastEventBMOrTestNamespaceOrEpoch(),
postgreSQLApplication.getName());

Map<String, String> parameters = new HashMap<>(postgreSQLApplication.getParameters());
// DATABASE_SERVICE_NAME parameter has to be aligned with the getName() in case that default is not used
if (!postgreSQLApplication.getName().equals("postgresql") || parameters.containsKey("DATABASE_SERVICE_NAME")) {
String dbServiceName = parameters.getOrDefault("DATABASE_SERVICE_NAME", "");
if (!dbServiceName.equals(postgreSQLApplication.getName())) {
log.warn("DATABASE_SERVICE_NAME has to be aligned with the application name, setting it according to name: {}.",
postgreSQLApplication.getName());
parameters.put("DATABASE_SERVICE_NAME", postgreSQLApplication.getName());
}
}
// Get the template from the openshift namespace, recreate it into test namespace and deploy
Template template = OpenShifts.master("openshift")
.getTemplate(postgreSQLApplication.getTemplate().getLabel());
template.getMetadata().setNamespace(openShift.getNamespace());
template.getMetadata().setResourceVersion(null);
openShift.createTemplate(template);
kubernetesList = openShift.processAndDeployTemplate(template.getMetadata().getName(), parameters);
OpenShiftWaiters.get(openShift, ffCheck).isDcReady(postgreSQLApplication.getName()).waitFor();
}

@Override
public void undeploy() {
openShift.deleteResources(kubernetesList);
openShift.deleteTemplate(postgreSQLApplication.getTemplate().getLabel());
}

@Override
public List<Pod> getPods() {
return openShift.getPods(getApplication().getName());
}

@Override
public void scale(int replicas, boolean wait) {
openShift.scale(postgreSQLApplication.getName(), replicas);
if (wait) {
OpenShiftWaiters.get(openShift, ffCheck).areExactlyNPodsReady(replicas, "name", postgreSQLTemplate.getLabel())
.level(Level.DEBUG).waitFor();
}
}

@Override
public String getUrl(String routeName, boolean secure) {
throw new UnsupportedOperationException("Route is not created for DB applications.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.jboss.intersmash.tools.provision.openshift;

import org.jboss.intersmash.tools.application.Application;
import org.jboss.intersmash.tools.application.openshift.PostgreSQLTemplateOpenShiftApplication;
import org.jboss.intersmash.tools.provision.ProvisionerFactory;

import lombok.extern.slf4j.Slf4j;

/**
* Provides logic to obtain a {@link PostgreSQLTemplateOpenShiftProvisioner} instance that is initialized with a
* given {@link PostgreSQLTemplateOpenShiftApplication} instance.
*/
@Slf4j
public class PostgreSQLTemplateOpenShiftProvisionerFactory
implements ProvisionerFactory<PostgreSQLTemplateOpenShiftProvisioner> {

@Override
public PostgreSQLTemplateOpenShiftProvisioner getProvisioner(Application application) {
if (PostgreSQLTemplateOpenShiftApplication.class.isAssignableFrom(application.getClass()))
return new PostgreSQLTemplateOpenShiftProvisioner((PostgreSQLTemplateOpenShiftApplication) application);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ org.jboss.intersmash.tools.provision.openshift.RhSsoOperatorProvisionerFactory
org.jboss.intersmash.tools.provision.helm.WildflyHelmChartOpenShiftProvisionerFactory
org.jboss.intersmash.tools.provision.openshift.Eap7ImageOpenShiftProvisionerFactory
org.jboss.intersmash.tools.provision.openshift.RhSsoTemplateOpenShiftProvisionerFactory
org.jboss.intersmash.tools.provision.openshift.PostgreSQLTemplateOpenShiftProvisionerFactory
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@
import org.jboss.intersmash.tools.application.openshift.KafkaOperatorApplication;
import org.jboss.intersmash.tools.application.openshift.MysqlImageOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.PostgreSQLImageOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.PostgreSQLTemplateOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.RhSsoTemplateOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.WildflyImageOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.WildflyOperatorApplication;
import org.jboss.intersmash.tools.application.openshift.template.PostgreSQLTemplate;
import org.jboss.intersmash.tools.application.openshift.template.RhSsoTemplate;
import org.jboss.intersmash.tools.provision.openshift.ActiveMQOperatorProvisioner;
import org.jboss.intersmash.tools.provision.openshift.Eap7LegacyS2iBuildTemplateProvisioner;
import org.jboss.intersmash.tools.provision.openshift.KafkaOperatorProvisioner;
import org.jboss.intersmash.tools.provision.openshift.MysqlImageOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.PostgreSQLImageOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.PostgreSQLTemplateOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.RhSsoTemplateOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.WildflyBootableJarImageOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.WildflyImageOpenShiftProvisioner;
Expand Down Expand Up @@ -136,7 +139,7 @@ public void kafkaOperatorProvisioner() {
}

/**
* Eap7LegacyS2iBuildTemplateApplication/Eap7LegacyS2iBuildTemplateProvisioner
* Eap7LegacyS2iBuildTemplateApplication / Eap7LegacyS2iBuildTemplateProvisioner
*/
@Test
public void eapS2iBuildTemplateProvisioner() {
Expand All @@ -158,6 +161,19 @@ public void openShiftRhSsoTemplateProvisioner() {
Assertions.assertEquals(RhSsoTemplateOpenShiftProvisioner.class, actual.getClass());
}

/**
* | PostgreSQLTemplateOpenShiftApplication / PostgreSQLTemplateOpenShiftProvisioner |
*/
@Test
public void openShiftPostgreSQLTemplateProvisioner() {
application = mock(PostgreSQLTemplateOpenShiftApplication.class);
when(((PostgreSQLTemplateOpenShiftApplication) application).getTemplate())
.thenReturn(PostgreSQLTemplate.POSTGRESQL_PERSISTENT);

Provisioner actual = ProvisionerManager.getProvisioner(application);
Assertions.assertEquals(PostgreSQLTemplateOpenShiftProvisioner.class, actual.getClass());
}

@Test
public void unsupportedProvisioner() {
Assertions.assertThrows(UnsupportedOperationException.class,
Expand Down

0 comments on commit cf115a2

Please sign in to comment.