diff --git a/api/v1alpha1/mysqluser_types.go b/api/v1alpha1/mysqluser_types.go index 761e2fee..a6200fd7 100644 --- a/api/v1alpha1/mysqluser_types.go +++ b/api/v1alpha1/mysqluser_types.go @@ -29,6 +29,7 @@ type MySQLUserSpec struct { // Important: Run "make" to regenerate code after modifying this file MysqlName string `json:"mysqlName"` + Host string `json:"host"` } // MySQLUserStatus defines the observed state of MySQLUser diff --git a/config/crd/bases/mysql.nakamasato.com_mysqlusers.yaml b/config/crd/bases/mysql.nakamasato.com_mysqlusers.yaml index 13e5ebde..4e7a5a0d 100644 --- a/config/crd/bases/mysql.nakamasato.com_mysqlusers.yaml +++ b/config/crd/bases/mysql.nakamasato.com_mysqlusers.yaml @@ -36,9 +36,12 @@ spec: spec: description: MySQLUserSpec defines the desired state of MySQLUser properties: + host: + type: string mysqlName: type: string required: + - host - mysqlName type: object status: diff --git a/config/samples/mysql_v1alpha1_mysqluser.yaml b/config/samples/mysql_v1alpha1_mysqluser.yaml index 3a385728..157f50fb 100644 --- a/config/samples/mysql_v1alpha1_mysqluser.yaml +++ b/config/samples/mysql_v1alpha1_mysqluser.yaml @@ -4,3 +4,4 @@ metadata: name: nakamasato spec: mysqlName: mysql-sample + host: '%' diff --git a/controllers/mysqluser_controller.go b/controllers/mysqluser_controller.go index 485c776e..ead44d4d 100644 --- a/controllers/mysqluser_controller.go +++ b/controllers/mysqluser_controller.go @@ -155,7 +155,7 @@ func (r *MySQLUserReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( // Create MySQL user if not exists with the password set above. log.Info("Create MySQL user if not.", "name", mysqlUserName, "mysqlUser.Namespace", mysqlUser.Namespace) - _, err = db.Exec("CREATE USER IF NOT EXISTS '" + mysqlUserName + "'@'%' IDENTIFIED BY '" + password + "';") + _, err = db.Exec("CREATE USER IF NOT EXISTS '" + mysqlUserName + "'@'" + mysqlUser.Spec.Host + "' IDENTIFIED BY '" + password + "';") if err != nil { log.Error(err, "Failed to create MySQL user.", "mysqlName", mysqlName, "mysqlUserName", mysqlUserName) return r.ManageError(ctx, mysqlUser, err) // requeue