From da9c289e572057543370a23f9ae6b41df1a15a8b Mon Sep 17 00:00:00 2001 From: Jean-Francois Denise Date: Fri, 8 Mar 2024 15:34:03 +0100 Subject: [PATCH] Add MySQL and MariaDB deployers --- cli/pom.xml | 8 + docs/guide/intro/index.adoc | 2 +- .../api/AbstractDatabaseDeployer.java | 160 ++++++++++++++++++ openshift-deployment/mariadb/pom.xml | 19 +++ .../openshift/mariadb/MariaDBDeployer.java | 43 +++++ ...fly.glow.deployment.openshift.api.Deployer | 2 + openshift-deployment/mysql/pom.xml | 19 +++ .../openshift/mysql/MySQLDeployer.java | 42 +++++ ...fly.glow.deployment.openshift.api.Deployer | 2 + openshift-deployment/pom.xml | 2 + .../postgresql/PostgreSQLDeployer.java | 131 ++------------ pom.xml | 10 ++ 12 files changed, 321 insertions(+), 119 deletions(-) create mode 100644 openshift-deployment/api/src/main/java/org/wildfly/glow/deployment/openshift/api/AbstractDatabaseDeployer.java create mode 100644 openshift-deployment/mariadb/pom.xml create mode 100644 openshift-deployment/mariadb/src/main/java/org/wildfly/glow/deployment/openshift/mariadb/MariaDBDeployer.java create mode 100644 openshift-deployment/mariadb/src/main/resources/META-INF/services/org.wildfly.glow.deployment.openshift.api.Deployer create mode 100644 openshift-deployment/mysql/pom.xml create mode 100644 openshift-deployment/mysql/src/main/java/org/wildfly/glow/deployment/openshift/mysql/MySQLDeployer.java create mode 100644 openshift-deployment/mysql/src/main/resources/META-INF/services/org.wildfly.glow.deployment.openshift.api.Deployer diff --git a/cli/pom.xml b/cli/pom.xml index ad696e8c..39e2dc42 100644 --- a/cli/pom.xml +++ b/cli/pom.xml @@ -37,6 +37,14 @@ ${project.groupId} wildfly-glow-openshift-deployment-postgresql + + ${project.groupId} + wildfly-glow-openshift-deployment-mysql + + + ${project.groupId} + wildfly-glow-openshift-deployment-mariadb + ${project.groupId} wildfly-glow-openshift-deployment-artemis diff --git a/docs/guide/intro/index.adoc b/docs/guide/intro/index.adoc index 83b4e167..197cbc58 100644 --- a/docs/guide/intro/index.adoc +++ b/docs/guide/intro/index.adoc @@ -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. diff --git a/openshift-deployment/api/src/main/java/org/wildfly/glow/deployment/openshift/api/AbstractDatabaseDeployer.java b/openshift-deployment/api/src/main/java/org/wildfly/glow/deployment/openshift/api/AbstractDatabaseDeployer.java new file mode 100644 index 00000000..ad3b82ef --- /dev/null +++ b/openshift-deployment/api/src/main/java/org/wildfly/glow/deployment/openshift/api/AbstractDatabaseDeployer.java @@ -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 CONNECTION_MAP = new HashMap<>(); + private final Map 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 disabledDeploy(String appHost, String appName, String matching, Map env) { + Map 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 getExistingEnv(Map env) { + Map ret = new HashMap<>(); + for (Entry entry : env.entrySet()) { + if (entry.getKey().startsWith(envRadical + "_")) { + ret.put(entry.getKey(), entry.getValue()); + } + } + return ret; + } + + @Override + public Map deploy(GlowMessageWriter writer, Path target, OpenShiftClient osClient, + Map env, String appHost, String appName, String matching) throws Exception { + writer.info("\nDeploying " + dbName + " server"); + Map labels = new HashMap<>(); + labels.put(LABEL, dbName); + ContainerPort port = new ContainerPort(); + port.setContainerPort(this.port); + port.setProtocol("TCP"); + List ports = new ArrayList<>(); + ports.add(port); + List vars = new ArrayList<>(); + for (Map.Entry 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 ret = new HashMap<>(); + ret.putAll(getExistingEnv(env)); + ret.putAll(APP_MAP); + return ret; + } + + @Override + public Set getSupportedLayers() { + Set ret = new HashSet<>(); + ret.add(dbName + "-datasource"); + ret.add(dbName + "-driver"); + return ret; + } + + @Override + public String getName() { + return dbName; + } + +} diff --git a/openshift-deployment/mariadb/pom.xml b/openshift-deployment/mariadb/pom.xml new file mode 100644 index 00000000..3c15d2dc --- /dev/null +++ b/openshift-deployment/mariadb/pom.xml @@ -0,0 +1,19 @@ + + + 4.0.0 + + org.wildfly.glow + wildfly-glow-openshift-deployment + 1.0.0.Beta10-SNAPSHOT + + + wildfly-glow-openshift-deployment-mariadb + jar + + + ${project.groupId} + wildfly-glow-openshift-deployment-api + + + \ No newline at end of file diff --git a/openshift-deployment/mariadb/src/main/java/org/wildfly/glow/deployment/openshift/mariadb/MariaDBDeployer.java b/openshift-deployment/mariadb/src/main/java/org/wildfly/glow/deployment/openshift/mariadb/MariaDBDeployer.java new file mode 100644 index 00000000..86509f83 --- /dev/null +++ b/openshift-deployment/mariadb/src/main/java/org/wildfly/glow/deployment/openshift/mariadb/MariaDBDeployer.java @@ -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); + } +} diff --git a/openshift-deployment/mariadb/src/main/resources/META-INF/services/org.wildfly.glow.deployment.openshift.api.Deployer b/openshift-deployment/mariadb/src/main/resources/META-INF/services/org.wildfly.glow.deployment.openshift.api.Deployer new file mode 100644 index 00000000..34036f50 --- /dev/null +++ b/openshift-deployment/mariadb/src/main/resources/META-INF/services/org.wildfly.glow.deployment.openshift.api.Deployer @@ -0,0 +1,2 @@ +org.wildfly.glow.deployment.openshift.mariadb.MariaDBDeployer + diff --git a/openshift-deployment/mysql/pom.xml b/openshift-deployment/mysql/pom.xml new file mode 100644 index 00000000..c553afcd --- /dev/null +++ b/openshift-deployment/mysql/pom.xml @@ -0,0 +1,19 @@ + + + 4.0.0 + + org.wildfly.glow + wildfly-glow-openshift-deployment + 1.0.0.Beta10-SNAPSHOT + + + wildfly-glow-openshift-deployment-mysql + jar + + + ${project.groupId} + wildfly-glow-openshift-deployment-api + + + \ No newline at end of file diff --git a/openshift-deployment/mysql/src/main/java/org/wildfly/glow/deployment/openshift/mysql/MySQLDeployer.java b/openshift-deployment/mysql/src/main/java/org/wildfly/glow/deployment/openshift/mysql/MySQLDeployer.java new file mode 100644 index 00000000..a6dcb03b --- /dev/null +++ b/openshift-deployment/mysql/src/main/java/org/wildfly/glow/deployment/openshift/mysql/MySQLDeployer.java @@ -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); + } +} diff --git a/openshift-deployment/mysql/src/main/resources/META-INF/services/org.wildfly.glow.deployment.openshift.api.Deployer b/openshift-deployment/mysql/src/main/resources/META-INF/services/org.wildfly.glow.deployment.openshift.api.Deployer new file mode 100644 index 00000000..c118bde9 --- /dev/null +++ b/openshift-deployment/mysql/src/main/resources/META-INF/services/org.wildfly.glow.deployment.openshift.api.Deployer @@ -0,0 +1,2 @@ +org.wildfly.glow.deployment.openshift.mysql.MySQLDeployer + diff --git a/openshift-deployment/pom.xml b/openshift-deployment/pom.xml index 93d592f5..0eed850e 100644 --- a/openshift-deployment/pom.xml +++ b/openshift-deployment/pom.xml @@ -19,6 +19,8 @@ api keycloak + mariadb + mysql postgresql artemis-broker diff --git a/openshift-deployment/postgresql/src/main/java/org/wildfly/glow/deployment/openshift/postgresql/PostgreSQLDeployer.java b/openshift-deployment/postgresql/src/main/java/org/wildfly/glow/deployment/openshift/postgresql/PostgreSQLDeployer.java index 00365c87..08c485e8 100644 --- a/openshift-deployment/postgresql/src/main/java/org/wildfly/glow/deployment/openshift/postgresql/PostgreSQLDeployer.java +++ b/openshift-deployment/postgresql/src/main/java/org/wildfly/glow/deployment/openshift/postgresql/PostgreSQLDeployer.java @@ -17,131 +17,26 @@ */ package org.wildfly.glow.deployment.openshift.postgresql; -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; -import org.wildfly.glow.deployment.openshift.api.Deployer; +import org.wildfly.glow.deployment.openshift.api.AbstractDatabaseDeployer; /** * * @author jdenise */ -public class PostgreSQLDeployer implements Deployer { +public class PostgreSQLDeployer extends AbstractDatabaseDeployer { private static final String POSTGRESQL_NAME = "postgresql"; - private static final String POSTGRESQL_SAMPLEDB = "sampledb"; - private static final String POSTGRESQL_PASSWORD = "admin"; - private static final String POSTGRESQL_USER = "admin"; - private static final String POSTGRESQL_SERVICE_PORT = "5432"; + private static final int POSTGRESQL_SERVICE_PORT = 5432; private static final String POSTGRESQL_SERVICE_HOST = POSTGRESQL_NAME; - - private static final String POSTGRESQL_SERVICE_PORT_ENV = "POSTGRESQL_SERVICE_PORT"; - private static final String POSTGRESQL_SERVICE_HOST_ENV = "POSTGRESQL_SERVICE_HOST"; - private static final Map POSTGRESQL_CONNECTION_MAP = new HashMap<>(); - private static final Map POSTGRESQL_APP_MAP = new HashMap<>(); - - static { - POSTGRESQL_CONNECTION_MAP.put("POSTGRESQL_DATABASE", POSTGRESQL_SAMPLEDB); - POSTGRESQL_CONNECTION_MAP.put("POSTGRESQL_PASSWORD", POSTGRESQL_PASSWORD); - POSTGRESQL_CONNECTION_MAP.put("POSTGRESQL_USER", POSTGRESQL_USER); - POSTGRESQL_APP_MAP.putAll(POSTGRESQL_CONNECTION_MAP); - POSTGRESQL_APP_MAP.put(POSTGRESQL_SERVICE_PORT_ENV, POSTGRESQL_SERVICE_PORT); - POSTGRESQL_APP_MAP.put(POSTGRESQL_SERVICE_HOST_ENV, POSTGRESQL_SERVICE_HOST); - } - - @Override - public Map disabledDeploy(String appHost, String appName, String matching, Map env) { - Map ret = new HashMap<>(); - ret.put(POSTGRESQL_SERVICE_HOST_ENV, "PostgreSQL server host name."); - ret.put(POSTGRESQL_SERVICE_PORT_ENV, "PostgreSQL server port."); - ret.putAll(getExistingEnv(env)); - return ret; - } - - private Map getExistingEnv(Map env) { - Map ret = new HashMap<>(); - for(Entry entry : env.entrySet()) { - if(entry.getKey().startsWith("POSTGRESQL_")) { - ret.put(entry.getKey(), entry.getValue()); - } - } - return ret; - } - - @Override - public Map deploy(GlowMessageWriter writer, Path target, OpenShiftClient osClient, - Map env, String appHost, String appName, String matching) throws Exception { - writer.info("\nDeploying PosgreSQL server"); - Map labels = new HashMap<>(); - labels.put(LABEL, POSTGRESQL_NAME); - ContainerPort port = new ContainerPort(); - port.setContainerPort(5432); - port.setProtocol("TCP"); - List ports = new ArrayList<>(); - ports.add(port); - List vars = new ArrayList<>(); - for (Map.Entry entry : POSTGRESQL_CONNECTION_MAP.entrySet()) { - vars.add(new EnvVar().toBuilder().withName(entry.getKey()).withValue(entry.getValue()).build()); - } - Container container = new Container(); - container.setName(POSTGRESQL_NAME); - container.setImage("registry.redhat.io/rhel8/postgresql-15"); - container.setPorts(ports); - container.setEnv(vars); - container.setImagePullPolicy("IfNotPresent"); - - Deployment deployment = new DeploymentBuilder().withNewMetadata().withName(POSTGRESQL_NAME).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(POSTGRESQL_NAME + "-deployment.yaml"), Serialization.asYaml(deployment).getBytes()); - IntOrString v = new IntOrString(); - v.setValue(5432); - Service service = new ServiceBuilder().withNewMetadata().withName(POSTGRESQL_NAME).endMetadata(). - withNewSpec().withPorts(new ServicePort().toBuilder().withName("5432-tcp").withProtocol("TCP"). - withPort(5432). - withTargetPort(v).build()).withType("ClusterIP").withSessionAffinity("None").withSelector(labels).endSpec().build(); - osClient.services().resource(service).createOr(NonDeletingOperation::update); - Files.write(target.resolve(POSTGRESQL_NAME + "-service.yaml"), Serialization.asYaml(service).getBytes()); - Map ret = new HashMap<>(); - ret.putAll(getExistingEnv(env)); - ret.putAll(POSTGRESQL_APP_MAP); - return ret; - } - - @Override - public Set getSupportedLayers() { - Set ret = new HashSet<>(); - ret.add("postgresql-datasource"); - ret.add("postgresql-driver"); - return ret; + private static final String ENV_RADICAL = "POSTGRESQL"; + private static final String IMAGE = "registry.redhat.io/rhel8/postgresql-15"; + + public PostgreSQLDeployer() { + super(POSTGRESQL_NAME, + IMAGE, + ENV_RADICAL, + ENV_RADICAL, + POSTGRESQL_SERVICE_HOST, + POSTGRESQL_SERVICE_PORT); } - - @Override - public String getName() { - return "postgresql"; - } - } diff --git a/pom.xml b/pom.xml index 9a2ae45d..e3305245 100644 --- a/pom.xml +++ b/pom.xml @@ -229,6 +229,16 @@ wildfly-glow-openshift-deployment-postgresql ${project.version} + + ${project.groupId} + wildfly-glow-openshift-deployment-mysql + ${project.version} + + + ${project.groupId} + wildfly-glow-openshift-deployment-mariadb + ${project.version} + ${project.groupId} wildfly-glow-openshift-deployment-artemis