Skip to content

Commit

Permalink
[issue 80] - Adding the EAP 7 legacy s2i template-based build provisi…
Browse files Browse the repository at this point in the history
…oner
  • Loading branch information
fabiobrz committed Nov 8, 2023
1 parent 3b7752f commit 6048d2f
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 21 deletions.
33 changes: 17 additions & 16 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jboss.intersmash.tools.IntersmashConfig;
import org.jboss.intersmash.tools.application.openshift.BootableJarOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.Eap7ImageOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.Eap7LegacyS2iBuildTemplateApplication;
import org.jboss.intersmash.tools.application.openshift.Eap7TemplateOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.KafkaOperatorApplication;
import org.jboss.intersmash.tools.application.openshift.MysqlImageOpenShiftApplication;
Expand All @@ -42,10 +43,12 @@
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.util.openshift.WildflyOpenShiftUtils;
import org.jboss.intersmash.tools.util.wildfly.Eap7CliScriptBuilder;

import cz.xtf.builder.builders.SecretBuilder;
import cz.xtf.builder.builders.secret.SecretType;
import cz.xtf.core.config.OpenShiftConfig;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.kubernetes.api.model.Secret;
Expand Down Expand Up @@ -118,6 +121,51 @@ public String getName() {
};
}

public static Eap7LegacyS2iBuildTemplateApplication getEap7LegacyS2iBuildTemplateApplication() {
return new Eap7LegacyS2iBuildTemplateApplication() {
private String eapImage;
private String eapRuntimeImage;
private final Map<String, String> parameters;
private final List<EnvVar> envVars;

{
eapImage = WildflyOpenShiftUtils.importBuilderImage(IntersmashConfig.eap7ImageURL()).getMetadata().getName();
eapRuntimeImage = WildflyOpenShiftUtils.importRuntimeImage(IntersmashConfig.eap7RuntimeImageUrl()).getMetadata()
.getName();
String deployment = "deployments/intersmash-deployments-shared/intersmash-deployments-shared-eap7/eap7-helloworld";
//params
parameters = new HashMap<>();
parameters.put("APPLICATION_IMAGE", getName());
parameters.put("EAP_IMAGE", eapImage);
parameters.put("EAP_RUNTIME_IMAGE", eapRuntimeImage);
parameters.put("EAP_IMAGESTREAM_NAMESPACE", OpenShiftConfig.namespace());
parameters.put("SOURCE_REPOSITORY_URL", IntersmashConfig.deploymentsRepositoryUrl());
parameters.put("SOURCE_REPOSITORY_REF", IntersmashConfig.deploymentsRepositoryRef());
parameters.put("ARTIFACT_DIR", deployment + "/target");

// envvars
envVars = new ArrayList<>();
// Set to allow cloning from Gitlab - any value here do the job
envVars.add(new EnvVarBuilder().withName("GIT_SSL_NO_VERIFY").withValue("").build());
}

@Override
public List<EnvVar> getEnvVars() {
return envVars;
}

@Override
public Map<String, String> getParameters() {
return parameters;
}

@Override
public String getName() {
return "eap-s2i-build-application";
}
};
}

static BootableJarOpenShiftApplication getWildflyBootableJarOpenShiftApplication() {
return new BootableJarOpenShiftApplication() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import java.util.stream.Stream;

import org.jboss.intersmash.testsuite.IntersmashTestsuiteProperties;
import org.jboss.intersmash.tools.provision.openshift.Eap7LegacyS2iBuildTemplateProvisioner;
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.WildflyBootableJarImageOpenShiftProvisioner;
import org.jboss.intersmash.tools.provision.openshift.WildflyImageOpenShiftProvisioner;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

Expand Down Expand Up @@ -80,4 +82,30 @@ public void testProvisioningWorkflowCleanup(OpenShiftProvisioner provisioner) {
openShift.configMaps().withName("no-delete").delete();
openShift.waiters().isProjectClean().waitFor();
}

/**
* EapS2iBuild application requires additional image streams to be created. CleanBeforeEach would delete it if the
* provisioner is initialized in {@link #provisionerProvider()}, so we need a separate test method.
*/
@Test
public void eapS2iBuild() {
Eap7LegacyS2iBuildTemplateProvisioner provisioner = new Eap7LegacyS2iBuildTemplateProvisioner(
OpenShiftProvisionerTestBase.getEap7LegacyS2iBuildTemplateApplication());
provisioner.preDeploy();
provisioner.deploy();
openShift.configMaps().create(new ConfigMapBuilder().withNewMetadata().withName("no-delete").endMetadata().build());
provisioner.undeploy();
provisioner.postUndeploy();
Assertions.assertNotNull(openShift.configMaps().withName("no-delete").get());
openShift.configMaps().withName("no-delete").delete();
// delete the images streams created by EapS2iBuildTemplateApplication
openShift.imageStreams()
.withName(((String) provisioner.getApplication().getParameters().get("EAP_IMAGE")).split(":")[0])
.delete();
openShift.imageStreams()
.withName(((String) provisioner.getApplication().getParameters().get("EAP_RUNTIME_IMAGE")).split(":")[0])
.delete();

openShift.waiters().isProjectClean().waitFor();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collections;
import java.util.List;

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

import io.fabric8.kubernetes.api.model.EnvVar;
Expand All @@ -26,15 +27,18 @@
* End user Application interface which presents resources on OpenShift Container Platform created by deploying the
* eap-s2i-build build template.
*
* The template is supposed to be used in conjunction with the WILDFLY Operator - the later can be used to deploy the
* The template is supposed to be used in conjunction with the WILDFLY Operator - the latter can be used to deploy the
* application image (can be referenced by an actual image link to local image registry, or by an image stream produced
* by the eap-s2i-build build).
*
* See https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.3/html-single/getting_started_with_jboss_eap_for_openshift_container_platform/index#the-eap-s2i-build-template-for-creating-application-images_default
*
* See https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.4/html-single/getting_started_with_jboss_eap_for_openshift_container_platform/index#the-eap-s2i-build-template-for-creating-application-images_default
*
* The application will be deployed by:
* <ul>
* <li>{@link Eap7LegacyS2iBuildTemplateProvisioner}</li>
* </ul>
*/
public interface WildflyLegacyS2iBuildTemplateApplication extends TemplateApplication, HasEnvVars {
public interface Eap7LegacyS2iBuildTemplateApplication extends TemplateApplication, HasEnvVars {

/**
* Defaults to https://raw.githubusercontent.com/jboss-container-images/jboss-eap-openshift-templates/master/eap-s2i-build.yaml template
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.jboss.intersmash.tools.provision.openshift;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import org.jboss.intersmash.tools.application.openshift.Eap7LegacyS2iBuildTemplateApplication;
import org.slf4j.event.Level;

import cz.xtf.core.event.helpers.EventHelper;
import cz.xtf.core.openshift.OpenShiftWaiters;
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.BuildConfig;
import io.fabric8.openshift.api.model.Template;
import lombok.extern.slf4j.Slf4j;

/**
* Provisioner used process and deploy eap-s2i-build template to OpenShift Container Platform cluster.
*
* @see Eap7LegacyS2iBuildTemplateApplication
*/
@Slf4j
public class Eap7LegacyS2iBuildTemplateProvisioner implements OpenShiftProvisioner<Eap7LegacyS2iBuildTemplateApplication> {
// we can parametrize this one once required, but its location should be static
private String EAP_S2I_BUILD = "https://raw.githubusercontent.com/jboss-container-images/jboss-eap-openshift-templates/master/eap-s2i-build.yaml";
private KubernetesList deployedResources;
private Eap7LegacyS2iBuildTemplateApplication application;
private FailFastCheck ffCheck = () -> false;
private Template template;

public Eap7LegacyS2iBuildTemplateProvisioner(Eap7LegacyS2iBuildTemplateApplication application) {
this.application = application;
}

@Override
public Eap7LegacyS2iBuildTemplateApplication getApplication() {
return application;
}

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

if (application.getParameters().getOrDefault("APPLICATION_IMAGE", "null") != application.getName()) {
throw new IllegalArgumentException("APPLICATION_IMAGE template parameters has to match the application name!");
}
try {
template = openShift.templates().load(new URL(EAP_S2I_BUILD)).get();
} catch (IOException e) {
throw new RuntimeException("Failed to load eap-s2i-build template from " + EAP_S2I_BUILD, e);
}
template.setApiVersion("template.openshift.io/v1");
openShift.createTemplate(template);
deployedResources = openShift.processTemplate(template.getMetadata().getName(),
application.getParameters());
// add additional environment variables to the build config resources
deployedResources.getItems().stream()
.filter(hasMetadata -> hasMetadata.getKind().equals(BuildConfig.class.getSimpleName()))
.forEach(hasMetadata -> setEnvToBuildConfig((BuildConfig) hasMetadata));
openShift.createResources(deployedResources);

// two build configs has to be built (builder/runtime) in order to proceed
waitForBuilds();
}

private void setEnvToBuildConfig(BuildConfig buildConfig) {
// Update Git based build config
if (buildConfig.getSpec().getStrategy().getSourceStrategy() != null) {
if (buildConfig.getSpec().getStrategy().getSourceStrategy().getEnv() == null) {
buildConfig.getSpec().getStrategy().getSourceStrategy().setEnv(application.getEnvVars());
} else {
buildConfig.getSpec().getStrategy().getSourceStrategy().getEnv().addAll(application.getEnvVars());
}
}
// Update dockerile based build config
if (buildConfig.getSpec().getStrategy().getDockerStrategy() != null) {
if (buildConfig.getSpec().getStrategy().getDockerStrategy().getEnv() == null) {
buildConfig.getSpec().getStrategy().getDockerStrategy().setEnv(application.getEnvVars());
} else {
buildConfig.getSpec().getStrategy().getDockerStrategy().getEnv().addAll(application.getEnvVars());
}
}
}

private void waitForBuilds() {
OpenShiftWaiters.get(openShift, ffCheck).hasBuildCompleted(application.getName() + "-build-artifacts")
.level(Level.DEBUG)
.waitFor();
OpenShiftWaiters.get(openShift, ffCheck).hasBuildCompleted(application.getName())
.level(Level.DEBUG)
.waitFor();
}

@Override
public void undeploy() {
openShift.deleteResources(deployedResources);
openShift.deleteTemplate(template);
}

@Override
public URL getURL() {
throw new UnsupportedOperationException("No route for eap-s2i-build template instance.");
}

@Override
public List<Pod> getPods() {
throw new UnsupportedOperationException(
"eap-s2i-build does not provide any running pods - all pods are completed once builds are done.");
}

@Override
public void scale(int replicas, boolean wait) {
throw new UnsupportedOperationException("eap-s2i-build template is not suppose to be scaled - no running pods.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jboss.intersmash.tools.provision.openshift;

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

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Eap7LegacyS2iBuildTemplateProvisionerFactory implements ProvisionerFactory<Eap7LegacyS2iBuildTemplateProvisioner> {

@Override
public Eap7LegacyS2iBuildTemplateProvisioner getProvisioner(Application application) {
if (Eap7LegacyS2iBuildTemplateApplication.class.isAssignableFrom(application.getClass()))
return new Eap7LegacyS2iBuildTemplateProvisioner((Eap7LegacyS2iBuildTemplateApplication) application);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import org.jboss.intersmash.tools.application.Application;
import org.jboss.intersmash.tools.application.openshift.ActiveMQOperatorApplication;
import org.jboss.intersmash.tools.application.openshift.BootableJarOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.Eap7LegacyS2iBuildTemplateApplication;
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.WildflyImageOpenShiftApplication;
import org.jboss.intersmash.tools.application.openshift.WildflyOperatorApplication;
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;
Expand All @@ -42,7 +44,7 @@
* | InfinispanImageOpenShiftApplication | IMAGE | InfinispanImageOpenShiftProvisioner |
* | MysqlImageOpenShiftApplication | IMAGE | MysqlImageOpenShiftProvisioner |
* | PostgreSQLImageOpenShiftApplication | IMAGE | PostgreSQLImageOpenShiftProvisioner |
* | EapS2iBuildTemplateApplication | TEMPLATE | EapS2iBuildTemplateProvisioner |
* | Eap7LegacyS2iBuildTemplateApplication | TEMPLATE | Eap7LegacyS2iBuildTemplateProvisioner |
* | WildflyOperatorApplication | OPERATOR | WildflyOperatorProvisioner |
* | ActiveMQOperatorApplication | OPERATOR | ActiveMQOperatorProvisioner |
* | KafkaOperatorApplication | OPERATOR | KafkaOperatorProvisioner |
Expand Down Expand Up @@ -128,6 +130,17 @@ public void kafkaOperatorProvisioner() {
Assertions.assertEquals(KafkaOperatorProvisioner.class, actual.getClass());
}

/**
* Eap7LegacyS2iBuildTemplateApplication/Eap7LegacyS2iBuildTemplateProvisioner
*/
@Test
public void eapS2iBuildTemplateProvisioner() {
application = mock(Eap7LegacyS2iBuildTemplateApplication.class);

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

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

0 comments on commit 6048d2f

Please sign in to comment.