22
33import com .github .containersolutions .operator .api .Controller ;
44import com .github .containersolutions .operator .api .ResourceController ;
5+ import io .fabric8 .kubernetes .api .model .Secret ;
6+ import io .fabric8 .kubernetes .api .model .SecretBuilder ;
7+ import io .fabric8 .kubernetes .client .KubernetesClient ;
8+ import org .apache .commons .lang .RandomStringUtils ;
59import org .slf4j .Logger ;
610import org .slf4j .LoggerFactory ;
711
812import java .sql .Connection ;
913import java .sql .DriverManager ;
1014import java .sql .ResultSet ;
1115import java .sql .SQLException ;
16+ import java .util .Base64 ;
1217import java .util .Optional ;
1318
1419import static java .lang .String .format ;
1722 crdName = "schemas.mysql.sample.javaoperatorsdk" ,
1823 customResourceClass = Schema .class )
1924public class SchemaController implements ResourceController <Schema > {
20-
25+ static final String USERNAME_FORMAT = "%s-user" ;
26+ static final String SECRET_FORMAT = "%s-secret" ;
2127
2228 private final Logger log = LoggerFactory .getLogger (getClass ());
2329
30+ private final KubernetesClient kubernetesClient ;
31+
32+ public SchemaController (KubernetesClient kubernetesClient ) { this .kubernetesClient = kubernetesClient ; }
33+
2434 @ Override
2535 public Optional <Schema > createOrUpdateResource (Schema schema ) {
2636 try (Connection connection = getConnection ()) {
@@ -29,14 +39,36 @@ public Optional<Schema> createOrUpdateResource(Schema schema) {
2939 schema .getMetadata ().getName (),
3040 schema .getSpec ().getEncoding ()));
3141
42+ String password = RandomStringUtils .randomAlphanumeric (16 );
43+ String userName = String .format (USERNAME_FORMAT ,
44+ schema .getMetadata ().getName ());
45+ String secretName = String .format (SECRET_FORMAT ,
46+ schema .getMetadata ().getName ());
47+ connection .createStatement ().execute (format (
48+ "CREATE USER '%1$s' IDENTIFIED BY '%2$s'" ,
49+ userName , password ));
50+ connection .createStatement ().execute (format (
51+ "GRANT ALL ON `%1$s`.* TO '%2$s'" ,
52+ schema .getMetadata ().getName (), userName ));
53+ Secret credentialsSecret = new SecretBuilder ()
54+ .withNewMetadata ().withName (secretName ).endMetadata ()
55+ .addToData ("MYSQL_USERNAME" , Base64 .getEncoder ().encodeToString (userName .getBytes ()))
56+ .addToData ("MYSQL_PASSWORD" , Base64 .getEncoder ().encodeToString (password .getBytes ()))
57+ .build ();
58+ this .kubernetesClient .secrets ()
59+ .inNamespace (schema .getMetadata ().getNamespace ())
60+ .create (credentialsSecret );
61+
3262 SchemaStatus status = new SchemaStatus ();
3363 status .setUrl (format ("jdbc:mysql://%1$s/%2$s" ,
3464 System .getenv ("MYSQL_HOST" ),
3565 schema .getMetadata ().getName ()));
66+ status .setUserName (userName );
67+ status .setSecretName (secretName );
3668 status .setStatus ("CREATED" );
3769 schema .setStatus (status );
38-
3970 log .info ("Schema {} created" , schema .getMetadata ().getName ());
71+
4072 return Optional .of (schema );
4173 }
4274 return Optional .empty ();
@@ -45,6 +77,8 @@ public Optional<Schema> createOrUpdateResource(Schema schema) {
4577
4678 SchemaStatus status = new SchemaStatus ();
4779 status .setUrl (null );
80+ status .setUserName (null );
81+ status .setSecretName (null );
4882 status .setStatus ("ERROR" );
4983 schema .setStatus (status );
5084
@@ -60,6 +94,16 @@ public boolean deleteResource(Schema schema) {
6094 if (schemaExists (connection , schema .getMetadata ().getName ())) {
6195 connection .createStatement ().execute ("DROP DATABASE `" + schema .getMetadata ().getName () + "`" );
6296 log .info ("Deleted Schema '{}'" , schema .getMetadata ().getName ());
97+
98+ if (userExists (connection , schema .getStatus ().getUserName ())) {
99+ connection .createStatement ().execute ("DROP USER '" + schema .getStatus ().getUserName () + "'" );
100+ log .info ("Deleted User '{}'" , schema .getStatus ().getUserName ());
101+ }
102+
103+ this .kubernetesClient .secrets ()
104+ .inNamespace (schema .getMetadata ().getNamespace ())
105+ .withName (schema .getStatus ().getSecretName ())
106+ .delete ();
63107 } else {
64108 log .info ("Delete event ignored for schema '{}', real schema doesn't exist" ,
65109 schema .getMetadata ().getName ());
@@ -86,4 +130,10 @@ private boolean schemaExists(Connection connection, String schemaName) throws SQ
86130 return resultSet .first ();
87131 }
88132
133+ private boolean userExists (Connection connection , String userName ) throws SQLException {
134+ ResultSet resultSet = connection .createStatement ().executeQuery (
135+ format ("SELECT User FROM mysql.user WHERE User='%1$s'" , userName )
136+ );
137+ return resultSet .first ();
138+ }
89139}
0 commit comments