-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from jfdenise/openshift-prov
Add MySQL and MariaDB deployers
- Loading branch information
Showing
12 changed files
with
321 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
...api/src/main/java/org/wildfly/glow/deployment/openshift/api/AbstractDatabaseDeployer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
43 changes: 43 additions & 0 deletions
43
.../mariadb/src/main/java/org/wildfly/glow/deployment/openshift/mariadb/MariaDBDeployer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...b/src/main/resources/META-INF/services/org.wildfly.glow.deployment.openshift.api.Deployer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
org.wildfly.glow.deployment.openshift.mariadb.MariaDBDeployer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
42 changes: 42 additions & 0 deletions
42
...oyment/mysql/src/main/java/org/wildfly/glow/deployment/openshift/mysql/MySQLDeployer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...l/src/main/resources/META-INF/services/org.wildfly.glow.deployment.openshift.api.Deployer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
org.wildfly.glow.deployment.openshift.mysql.MySQLDeployer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.