Skip to content

Commit

Permalink
[MySQLUser] Enable to set (Close #26) (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamasato authored Sep 18, 2021
1 parent 2002838 commit 98705c9
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/v1alpha1/mysqluser_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/mysql.nakamasato.com_mysqlusers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions config/samples/mysql_v1alpha1_mysqluser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ metadata:
name: nakamasato
spec:
mysqlName: mysql-sample
host: '%'
2 changes: 1 addition & 1 deletion controllers/mysqluser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 98705c9

Please sign in to comment.