From 65f1a201e7773f4cd62a3e5e4e9efef8443a8824 Mon Sep 17 00:00:00 2001 From: csviri Date: Thu, 24 Mar 2022 09:50:22 +0100 Subject: [PATCH] fix: mysql sample delete part --- .../operator/sample/MySQLSchemaReconciler.java | 5 +++-- .../sample/dependent/SchemaDependentResource.java | 10 ++++------ .../operator/sample/schema/SchemaService.java | 15 +++++++++++---- .../operator/sample/MySQLSchemaOperatorE2E.java | 6 ++++-- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/MySQLSchemaReconciler.java b/sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/MySQLSchemaReconciler.java index f306292ff8..f8f312ba8e 100644 --- a/sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/MySQLSchemaReconciler.java +++ b/sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/MySQLSchemaReconciler.java @@ -9,6 +9,7 @@ import io.javaoperatorsdk.operator.sample.dependent.SchemaDependentResource; import io.javaoperatorsdk.operator.sample.dependent.SecretDependentResource; +import static io.javaoperatorsdk.operator.sample.dependent.SchemaDependentResource.decode; import static io.javaoperatorsdk.operator.sample.dependent.SecretDependentResource.MYSQL_SECRET_USERNAME; import static java.lang.String.format; @@ -32,9 +33,9 @@ public UpdateControl reconcile(MySQLSchema schema, Context { + return schemaDependentResource.getResource(schema).map(s -> { updateStatusPojo(schema, secret.getMetadata().getName(), - secret.getData().get(MYSQL_SECRET_USERNAME)); + decode(secret.getData().get(MYSQL_SECRET_USERNAME))); log.info("Schema {} created - updating CR status", schema.getMetadata().getName()); return UpdateControl.updateStatus(schema); }).orElse(UpdateControl.noUpdate()); diff --git a/sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/dependent/SchemaDependentResource.java b/sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/dependent/SchemaDependentResource.java index b3155f9199..c2be8c86da 100644 --- a/sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/dependent/SchemaDependentResource.java +++ b/sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/dependent/SchemaDependentResource.java @@ -11,6 +11,7 @@ import io.fabric8.kubernetes.api.model.Secret; import io.javaoperatorsdk.operator.api.reconciler.Context; +import io.javaoperatorsdk.operator.api.reconciler.dependent.Deleter; import io.javaoperatorsdk.operator.api.reconciler.dependent.EventSourceProvider; import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.DependentResourceConfigurator; import io.javaoperatorsdk.operator.processing.dependent.Creator; @@ -28,10 +29,7 @@ public class SchemaDependentResource extends PerResourcePollingDependentResource implements EventSourceProvider, DependentResourceConfigurator, - Creator -// todo fix -// , Deleter -{ + Creator, Deleter { private static final Logger log = LoggerFactory.getLogger(SchemaDependentResource.class); @@ -75,7 +73,7 @@ private Connection getConnection() throws SQLException { return DriverManager.getConnection(connectURL, dbConfig.getUser(), dbConfig.getPassword()); } - // @Override + @Override public void delete(MySQLSchema primary, Context context) { try (Connection connection = getConnection()) { var userName = primary.getStatus() != null ? primary.getStatus().getUserName() : null; @@ -97,7 +95,7 @@ public Optional fetchResource(MySQLSchema primaryResource) { } } - private static String decode(String value) { + public static String decode(String value) { return new String(Base64.getDecoder().decode(value.getBytes())); } diff --git a/sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/schema/SchemaService.java b/sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/schema/SchemaService.java index e1eebc3c21..84504a5fec 100644 --- a/sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/schema/SchemaService.java +++ b/sample-operators/mysql-schema/src/main/java/io/javaoperatorsdk/operator/sample/schema/SchemaService.java @@ -59,11 +59,14 @@ public static Schema createSchemaAndRelatedUser(Connection connection, String sc public static void deleteSchemaAndRelatedUser(Connection connection, String schemaName, String userName) { try { - try (Statement statement = connection.createStatement()) { - statement.execute(format("DROP DATABASE `%1$s`", schemaName)); + if (schemaExists(connection, schemaName)) { + try (Statement statement = connection.createStatement()) { + statement.execute(format("DROP DATABASE `%1$s`", schemaName)); + } + log.info("Deleted Schema '{}'", schemaName); } - log.info("Deleted Schema '{}'", schemaName); - if (userName != null) { + + if (userName != null && userExists(connection, userName)) { try (Statement statement = connection.createStatement()) { statement.execute(format("DROP USER '%1$s'", userName)); } @@ -88,6 +91,10 @@ private static boolean userExists(Connection connection, String username) { } } + public static boolean schemaExists(Connection connection, String schemaName) { + return getSchema(connection, schemaName).isPresent(); + } + public static Optional getSchema(Connection connection, String schemaName) { try (PreparedStatement ps = connection.prepareStatement( diff --git a/sample-operators/mysql-schema/src/test/java/io/javaoperatorsdk/operator/sample/MySQLSchemaOperatorE2E.java b/sample-operators/mysql-schema/src/test/java/io/javaoperatorsdk/operator/sample/MySQLSchemaOperatorE2E.java index f2cc138a86..06d9d1d184 100644 --- a/sample-operators/mysql-schema/src/test/java/io/javaoperatorsdk/operator/sample/MySQLSchemaOperatorE2E.java +++ b/sample-operators/mysql-schema/src/test/java/io/javaoperatorsdk/operator/sample/MySQLSchemaOperatorE2E.java @@ -40,6 +40,7 @@ class MySQLSchemaOperatorE2E { private final static List infrastructure = new ArrayList<>(); public static final String TEST_RESOURCE_NAME = "mydb1"; + public static final Integer LOCAL_PORT = 3307; static { infrastructure.add( @@ -70,10 +71,11 @@ boolean isLocal() { c.replaceDependentResourceConfig( SchemaDependentResource.class, new ResourcePollerConfig( - 700, new MySQLDbConfig("127.0.0.1", "3307", "root", "password"))); + 700, new MySQLDbConfig("127.0.0.1", LOCAL_PORT.toString(), "root", + "password"))); }) .withInfrastructure(infrastructure) - .withPortForward(MY_SQL_NS, "app", "mysql", 3306, 3307) + .withPortForward(MY_SQL_NS, "app", "mysql", 3306, LOCAL_PORT) .build() : E2EOperatorExtension.builder() .withOperatorDeployment(client.load(new FileInputStream("k8s/operator.yaml")).get())