Skip to content

fix: mysql sample delete part #1081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,9 +33,9 @@ public UpdateControl<MySQLSchema> reconcile(MySQLSchema schema, Context<MySQLSch
Secret secret = context.getSecondaryResource(Secret.class).orElseThrow();
SchemaDependentResource schemaDependentResource = context.managedDependentResourceContext()
.getDependentResource(SchemaDependentResource.class);
return schemaDependentResource.fetchResource(schema).map(s -> {
return schemaDependentResource.getResource(schema).map(s -> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unrelated to the problem. I think we should rather get the actual state from cache, and handle the details when to call the actual service to the dependent resource implementation.

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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,10 +29,7 @@ public class SchemaDependentResource
extends PerResourcePollingDependentResource<Schema, MySQLSchema>
implements EventSourceProvider<MySQLSchema>,
DependentResourceConfigurator<ResourcePollerConfig>,
Creator<Schema, MySQLSchema>
// todo fix
// , Deleter<MySQLSchema>
{
Creator<Schema, MySQLSchema>, Deleter<MySQLSchema> {

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

Expand Down Expand Up @@ -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<MySQLSchema> context) {
try (Connection connection = getConnection()) {
var userName = primary.getStatus() != null ? primary.getStatus().getUserName() : null;
Expand All @@ -97,7 +95,7 @@ public Optional<Schema> fetchResource(MySQLSchema primaryResource) {
}
}

private static String decode(String value) {
public static String decode(String value) {
return new String(Base64.getDecoder().decode(value.getBytes()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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<Schema> getSchema(Connection connection, String schemaName) {
try (PreparedStatement ps =
connection.prepareStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MySQLSchemaOperatorE2E {

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

static {
infrastructure.add(
Expand Down Expand Up @@ -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())
Expand Down