Skip to content

Commit

Permalink
Merge pull request #51 from jfdenise/openshift-prov
Browse files Browse the repository at this point in the history
Add MySQL and MariaDB deployers
  • Loading branch information
jfdenise authored Mar 8, 2024
2 parents 02faacd + da9c289 commit bd20e4a
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 119 deletions.
8 changes: 8 additions & 0 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
<groupId>${project.groupId}</groupId>
<artifactId>wildfly-glow-openshift-deployment-postgresql</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>wildfly-glow-openshift-deployment-mysql</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>wildfly-glow-openshift-deployment-mariadb</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>wildfly-glow-openshift-deployment-artemis</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/intro/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ provisioning and create your application deployment.
At the end of the build, the application is deployed and the route to your application inside the cluster is printed.
Use it to interact with your application.

###### Automatic deployment of PostGreSQL, Artemis JMS Broker and Keycloak
###### Automatic deployment of PostGreSQL, MySQL, MariaDB, Artemis JMS Broker and Keycloak

If WildFly Glow detects the need for these technologies, it will automatically deploy the required servers and will bound the application to them.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2024 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.glow.deployment.openshift.api;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerPort;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;
import io.fabric8.kubernetes.api.model.ServicePort;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.client.dsl.NonDeletingOperation;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.openshift.client.OpenShiftClient;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.wildfly.glow.GlowMessageWriter;

/**
*
* @author jdenise
*/
public class AbstractDatabaseDeployer implements Deployer {

private static final String SAMPLEDB = "sampledb";
private static final String PASSWORD = "admin";
private static final String USER = "admin";
private static final String SERVICE_PORT_ENV_SUFFIX = "_SERVICE_PORT";
private static final String SERVICE_HOST_ENV_SUFFIX = "_SERVICE_HOST";

private final String dbName;
private final String image;
private final String envRadical;
private final int port;

private final Map<String, String> CONNECTION_MAP = new HashMap<>();
private final Map<String, String> APP_MAP = new HashMap<>();

protected AbstractDatabaseDeployer(String dbName,
String image,
String dbEnvRadical,
String envRadical,
String host,
int port) {
this.dbName = dbName;
this.image = image;
this.envRadical = envRadical;
this.port = port;
CONNECTION_MAP.put(dbEnvRadical + "_DATABASE", SAMPLEDB);
CONNECTION_MAP.put(dbEnvRadical + "_PASSWORD", PASSWORD);
CONNECTION_MAP.put(dbEnvRadical + "_USER", USER);

APP_MAP.put(envRadical + "_DATABASE", SAMPLEDB);
APP_MAP.put(envRadical + "_PASSWORD", PASSWORD);
APP_MAP.put(envRadical + "_USER", USER);
APP_MAP.put(envRadical + SERVICE_PORT_ENV_SUFFIX, "" + port);
APP_MAP.put(envRadical + SERVICE_HOST_ENV_SUFFIX, host);
}

@Override
public Map<String, String> disabledDeploy(String appHost, String appName, String matching, Map<String, String> env) {
Map<String, String> ret = new HashMap<>();
ret.put(envRadical + SERVICE_HOST_ENV_SUFFIX, dbName + " server host name.");
ret.put(envRadical + SERVICE_PORT_ENV_SUFFIX, dbName + "server port.");
ret.putAll(getExistingEnv(env));
return ret;
}

private Map<String, String> getExistingEnv(Map<String, String> env) {
Map<String, String> ret = new HashMap<>();
for (Entry<String, String> entry : env.entrySet()) {
if (entry.getKey().startsWith(envRadical + "_")) {
ret.put(entry.getKey(), entry.getValue());
}
}
return ret;
}

@Override
public Map<String, String> deploy(GlowMessageWriter writer, Path target, OpenShiftClient osClient,
Map<String, String> env, String appHost, String appName, String matching) throws Exception {
writer.info("\nDeploying " + dbName + " server");
Map<String, String> labels = new HashMap<>();
labels.put(LABEL, dbName);
ContainerPort port = new ContainerPort();
port.setContainerPort(this.port);
port.setProtocol("TCP");
List<ContainerPort> ports = new ArrayList<>();
ports.add(port);
List<EnvVar> vars = new ArrayList<>();
for (Map.Entry<String, String> entry : CONNECTION_MAP.entrySet()) {
vars.add(new EnvVar().toBuilder().withName(entry.getKey()).withValue(entry.getValue()).build());
}
Container container = new Container();
container.setName(dbName);
container.setImage(image);
container.setPorts(ports);
container.setEnv(vars);
container.setImagePullPolicy("IfNotPresent");

Deployment deployment = new DeploymentBuilder().withNewMetadata().withName(dbName).endMetadata().
withNewSpec().withReplicas(1).
withNewSelector().withMatchLabels(labels).endSelector().
withNewTemplate().withNewMetadata().withLabels(labels).endMetadata().withNewSpec().
withContainers(container).withRestartPolicy("Always").
endSpec().endTemplate().withNewStrategy().withType("RollingUpdate").endStrategy().endSpec().build();
osClient.resources(Deployment.class).resource(deployment).createOr(NonDeletingOperation::update);
Files.write(target.resolve(dbName + "-deployment.yaml"), Serialization.asYaml(deployment).getBytes());
IntOrString v = new IntOrString();
v.setValue(this.port);
Service service = new ServiceBuilder().withNewMetadata().withName(dbName).endMetadata().
withNewSpec().withPorts(new ServicePort().toBuilder().withName(this.port + "-tcp").withProtocol("TCP").
withPort(this.port).
withTargetPort(v).build()).withType("ClusterIP").withSessionAffinity("None").withSelector(labels).endSpec().build();
osClient.services().resource(service).createOr(NonDeletingOperation::update);
Files.write(target.resolve(dbName + "-service.yaml"), Serialization.asYaml(service).getBytes());
Map<String, String> ret = new HashMap<>();
ret.putAll(getExistingEnv(env));
ret.putAll(APP_MAP);
return ret;
}

@Override
public Set<String> getSupportedLayers() {
Set<String> ret = new HashSet<>();
ret.add(dbName + "-datasource");
ret.add(dbName + "-driver");
return ret;
}

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

}
19 changes: 19 additions & 0 deletions openshift-deployment/mariadb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.wildfly.glow</groupId>
<artifactId>wildfly-glow-openshift-deployment</artifactId>
<version>1.0.0.Beta10-SNAPSHOT</version>
</parent>

<artifactId>wildfly-glow-openshift-deployment-mariadb</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>wildfly-glow-openshift-deployment-api</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2024 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.glow.deployment.openshift.mariadb;

import org.wildfly.glow.deployment.openshift.api.AbstractDatabaseDeployer;

/**
*
* @author jdenise
*/
public class MariaDBDeployer extends AbstractDatabaseDeployer {

private static final String MARIADB_NAME = "mariadb";
private static final int MARIADB_SERVICE_PORT = 3306;
private static final String MARIADB_SERVICE_HOST = MARIADB_NAME;
private static final String ENV_RADICAL = "MARIADB";
private static final String DB_ENV_RADICAL = "MYSQL";
private static final String IMAGE = "registry.redhat.io/rhel8/mariadb-103";

public MariaDBDeployer() {
super(MARIADB_NAME,
IMAGE,
DB_ENV_RADICAL,
ENV_RADICAL,
MARIADB_SERVICE_HOST,
MARIADB_SERVICE_PORT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.wildfly.glow.deployment.openshift.mariadb.MariaDBDeployer

19 changes: 19 additions & 0 deletions openshift-deployment/mysql/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.wildfly.glow</groupId>
<artifactId>wildfly-glow-openshift-deployment</artifactId>
<version>1.0.0.Beta10-SNAPSHOT</version>
</parent>

<artifactId>wildfly-glow-openshift-deployment-mysql</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>wildfly-glow-openshift-deployment-api</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2024 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wildfly.glow.deployment.openshift.mysql;

import org.wildfly.glow.deployment.openshift.api.AbstractDatabaseDeployer;

/**
*
* @author jdenise
*/
public class MySQLDeployer extends AbstractDatabaseDeployer {

private static final String MYSQL_NAME = "mysql";
private static final int MYSQL_SERVICE_PORT = 3306;
private static final String MYSQL_SERVICE_HOST = MYSQL_NAME;
private static final String ENV_RADICAL = "MYSQL";
private static final String IMAGE = "registry.redhat.io/rhel8/mysql-80";

public MySQLDeployer() {
super(MYSQL_NAME,
IMAGE,
ENV_RADICAL,
ENV_RADICAL,
MYSQL_SERVICE_HOST,
MYSQL_SERVICE_PORT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.wildfly.glow.deployment.openshift.mysql.MySQLDeployer

2 changes: 2 additions & 0 deletions openshift-deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<modules>
<module>api</module>
<module>keycloak</module>
<module>mariadb</module>
<module>mysql</module>
<module>postgresql</module>
<module>artemis-broker</module>
</modules>
Expand Down
Loading

0 comments on commit bd20e4a

Please sign in to comment.