Skip to content

Commit 94f0b75

Browse files
authored
fix: mysql sample delete part (#1081)
1 parent d5fd866 commit 94f0b75

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/MySQLSchemaReconciler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.javaoperatorsdk.operator.sample.dependent.SchemaDependentResource;
1010
import io.javaoperatorsdk.operator.sample.dependent.SecretDependentResource;
1111

12+
import static io.javaoperatorsdk.operator.sample.dependent.SchemaDependentResource.decode;
1213
import static io.javaoperatorsdk.operator.sample.dependent.SecretDependentResource.MYSQL_SECRET_USERNAME;
1314
import static java.lang.String.format;
1415

@@ -32,9 +33,9 @@ public UpdateControl<MySQLSchema> reconcile(MySQLSchema schema, Context<MySQLSch
3233
Secret secret = context.getSecondaryResource(Secret.class).orElseThrow();
3334
SchemaDependentResource schemaDependentResource = context.managedDependentResourceContext()
3435
.getDependentResource(SchemaDependentResource.class);
35-
return schemaDependentResource.fetchResource(schema).map(s -> {
36+
return schemaDependentResource.getResource(schema).map(s -> {
3637
updateStatusPojo(schema, secret.getMetadata().getName(),
37-
secret.getData().get(MYSQL_SECRET_USERNAME));
38+
decode(secret.getData().get(MYSQL_SECRET_USERNAME)));
3839
log.info("Schema {} created - updating CR status", schema.getMetadata().getName());
3940
return UpdateControl.updateStatus(schema);
4041
}).orElse(UpdateControl.noUpdate());

sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/dependent/SchemaDependentResource.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import io.fabric8.kubernetes.api.model.Secret;
1313
import io.javaoperatorsdk.operator.api.reconciler.Context;
14+
import io.javaoperatorsdk.operator.api.reconciler.dependent.Deleter;
1415
import io.javaoperatorsdk.operator.api.reconciler.dependent.EventSourceProvider;
1516
import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.DependentResourceConfigurator;
1617
import io.javaoperatorsdk.operator.processing.dependent.Creator;
@@ -28,10 +29,7 @@ public class SchemaDependentResource
2829
extends PerResourcePollingDependentResource<Schema, MySQLSchema>
2930
implements EventSourceProvider<MySQLSchema>,
3031
DependentResourceConfigurator<ResourcePollerConfig>,
31-
Creator<Schema, MySQLSchema>
32-
// todo fix
33-
// , Deleter<MySQLSchema>
34-
{
32+
Creator<Schema, MySQLSchema>, Deleter<MySQLSchema> {
3533

3634
private static final Logger log = LoggerFactory.getLogger(SchemaDependentResource.class);
3735

@@ -75,7 +73,7 @@ private Connection getConnection() throws SQLException {
7573
return DriverManager.getConnection(connectURL, dbConfig.getUser(), dbConfig.getPassword());
7674
}
7775

78-
// @Override
76+
@Override
7977
public void delete(MySQLSchema primary, Context<MySQLSchema> context) {
8078
try (Connection connection = getConnection()) {
8179
var userName = primary.getStatus() != null ? primary.getStatus().getUserName() : null;
@@ -97,7 +95,7 @@ public Optional<Schema> fetchResource(MySQLSchema primaryResource) {
9795
}
9896
}
9997

100-
private static String decode(String value) {
98+
public static String decode(String value) {
10199
return new String(Base64.getDecoder().decode(value.getBytes()));
102100
}
103101

sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/schema/SchemaService.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,14 @@ public static Schema createSchemaAndRelatedUser(Connection connection, String sc
5959
public static void deleteSchemaAndRelatedUser(Connection connection, String schemaName,
6060
String userName) {
6161
try {
62-
try (Statement statement = connection.createStatement()) {
63-
statement.execute(format("DROP DATABASE `%1$s`", schemaName));
62+
if (schemaExists(connection, schemaName)) {
63+
try (Statement statement = connection.createStatement()) {
64+
statement.execute(format("DROP DATABASE `%1$s`", schemaName));
65+
}
66+
log.info("Deleted Schema '{}'", schemaName);
6467
}
65-
log.info("Deleted Schema '{}'", schemaName);
66-
if (userName != null) {
68+
69+
if (userName != null && userExists(connection, userName)) {
6770
try (Statement statement = connection.createStatement()) {
6871
statement.execute(format("DROP USER '%1$s'", userName));
6972
}
@@ -88,6 +91,10 @@ private static boolean userExists(Connection connection, String username) {
8891
}
8992
}
9093

94+
public static boolean schemaExists(Connection connection, String schemaName) {
95+
return getSchema(connection, schemaName).isPresent();
96+
}
97+
9198
public static Optional<Schema> getSchema(Connection connection, String schemaName) {
9299
try (PreparedStatement ps =
93100
connection.prepareStatement(

sample-operators/mysql-schema/src/test/java/io/javaoperatorsdk/operator/sample/MySQLSchemaOperatorE2E.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class MySQLSchemaOperatorE2E {
4040

4141
private final static List<HasMetadata> infrastructure = new ArrayList<>();
4242
public static final String TEST_RESOURCE_NAME = "mydb1";
43+
public static final Integer LOCAL_PORT = 3307;
4344

4445
static {
4546
infrastructure.add(
@@ -70,10 +71,11 @@ boolean isLocal() {
7071
c.replaceDependentResourceConfig(
7172
SchemaDependentResource.class,
7273
new ResourcePollerConfig(
73-
700, new MySQLDbConfig("127.0.0.1", "3307", "root", "password")));
74+
700, new MySQLDbConfig("127.0.0.1", LOCAL_PORT.toString(), "root",
75+
"password")));
7476
})
7577
.withInfrastructure(infrastructure)
76-
.withPortForward(MY_SQL_NS, "app", "mysql", 3306, 3307)
78+
.withPortForward(MY_SQL_NS, "app", "mysql", 3306, LOCAL_PORT)
7779
.build()
7880
: E2EOperatorExtension.builder()
7981
.withOperatorDeployment(client.load(new FileInputStream("k8s/operator.yaml")).get())

0 commit comments

Comments
 (0)