Skip to content

Commit 351a528

Browse files
authored
Merge pull request #99 from ContainerSolutions/mysqlsample-operator-serviceaccount
Mysqlsample operator sample improvements
2 parents fa349e7 + 1a8572f commit 351a528

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

samples/mysql-schema/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ This should update the actual nginx deployment with new configuration.
4444

4545
### Build
4646

47-
You can build the sample using `mvn dockerfile:build` this will produce a Docker image you can push to the registry
47+
You can build the sample using `mvn package dockerfile:build` this will produce a Docker image you can push to the registry
4848
of your choice. The jar file is built using your local Maven and JDK and then copied into the Docker image.
4949

5050
### Deployment

samples/mysql-schema/k8s/deployment.yaml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ spec:
4545
initialDelaySeconds: 30
4646
timeoutSeconds: 1
4747
---
48+
apiVersion: rbac.authorization.k8s.io/v1beta1
49+
kind: ClusterRole
50+
metadata:
51+
name: mysql-schema-operator
52+
rules:
53+
- apiGroups:
54+
- mysql.sample.javaoperatorsdk
55+
resources:
56+
- schemas
57+
verbs:
58+
- "*"
59+
- apiGroups:
60+
- apiextensions.k8s.io
61+
resources:
62+
- customresourcedefinitions
63+
verbs:
64+
- "get"
65+
- "list"
66+
---
4867
apiVersion: v1
4968
kind: ServiceAccount
5069
metadata:
@@ -61,5 +80,5 @@ subjects:
6180
namespace: mysql-schema-operator
6281
roleRef:
6382
kind: ClusterRole
64-
name: cluster-admin
83+
name: mysql-schema-operator
6584
apiGroup: ""

samples/mysql-schema/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
<artifactId>dockerfile-maven-plugin</artifactId>
6161
<version>1.4.12</version>
6262
<configuration>
63-
<repository>mysql-schema-operator</repository>
64-
<tag>latest</tag>
63+
<repository>${docker-registry}/mysql-schema-operator</repository>
64+
<tag>${project.version}</tag>
6565
<buildArgs>
6666
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
6767
</buildArgs>

samples/mysql-schema/src/main/java/com/github/containersolutions/operator/sample/SchemaController.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ public class SchemaController implements ResourceController<Schema> {
2424
@Override
2525
public Optional<Schema> createOrUpdateResource(Schema schema) {
2626
try (Connection connection = getConnection()) {
27-
ResultSet resultSet = connection.createStatement().executeQuery(
28-
format("SELECT schema_name FROM information_schema.schemata WHERE schema_name = \"%1$s\"",
29-
schema.getMetadata().getName()));
30-
if (!resultSet.first()) {
27+
if (schemaExists(connection, schema.getMetadata().getName())) {
3128
connection.createStatement().execute(format("CREATE SCHEMA `%1$s` DEFAULT CHARACTER SET %2$s",
3229
schema.getMetadata().getName(),
3330
schema.getSpec().getEncoding()));
@@ -40,10 +37,8 @@ public Optional<Schema> createOrUpdateResource(Schema schema) {
4037
schema.setStatus(status);
4138

4239
log.info("Schema {} created", schema.getMetadata().getName());
43-
return Optional.of(schema);
44-
} else {
45-
return Optional.of(schema);
4640
}
41+
return Optional.of(schema);
4742
} catch (SQLException e) {
4843
log.error("Error while creating Schema", e);
4944

@@ -61,9 +56,13 @@ public boolean deleteResource(Schema schema) {
6156
log.info("Execution deleteResource for: {}", schema.getMetadata().getName());
6257

6358
try (Connection connection = getConnection()) {
64-
connection.createStatement().execute("DROP DATABASE `" + schema.getMetadata().getName() + "`");
65-
log.info("Deleted Schema '{}'", schema.getMetadata().getName());
66-
59+
if (schemaExists(connection, schema.getMetadata().getName())) {
60+
connection.createStatement().execute("DROP DATABASE `" + schema.getMetadata().getName() + "`");
61+
log.info("Deleted Schema '{}'", schema.getMetadata().getName());
62+
} else {
63+
log.info("Delete event ignored for schema '{}', real schema doesn't exist",
64+
schema.getMetadata().getName());
65+
}
6766
return true;
6867
} catch (SQLException e) {
6968
log.error("Error while trying to delete Schema", e);
@@ -79,4 +78,11 @@ private Connection getConnection() throws SQLException {
7978
System.getenv("MYSQL_PASSWORD")));
8079
}
8180

81+
private boolean schemaExists(Connection connection, String schemaName) throws SQLException {
82+
ResultSet resultSet = connection.createStatement().executeQuery(
83+
format("SELECT schema_name FROM information_schema.schemata WHERE schema_name = \"%1$s\"",
84+
schemaName));
85+
return resultSet.first();
86+
}
87+
8288
}

0 commit comments

Comments
 (0)