Skip to content

Commit

Permalink
chore: use mysql.Config.FormatDSN (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamasato authored Mar 31, 2023
1 parent fdf9006 commit 39536e7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
5 changes: 5 additions & 0 deletions api/v1alpha1/mysql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type MySQLSpec struct {

// Host is MySQL host of target MySQL cluster.
Host string `json:"host,omitempty"`

//+kubebuilder:default=3306

// Port is MySQL port of target MySQL cluster.
Port int16 `json:"port,omitempty"`
// AdminUser is MySQL user to connect target MySQL cluster.
AdminUser string `json:"admin_user"`
// AdminPassword is MySQL password to connect target MySQL cluster.
Expand Down
22 changes: 16 additions & 6 deletions api/v1alpha1/mysqluser_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ type MySQLUserSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// MySQL name
// MySQL (CRD) name to reference to, which decides the destination MySQL server
MysqlName string `json:"mysqlName"`

// +kubebuilder:default=%
// +kubebuilder:validation:Optional

// MySQL hostname for MySQL account
Host string `json:"host"`
}

Expand All @@ -45,11 +47,19 @@ type MySQLUserStatus struct {
// +patchStrategy=merge
// +listType=map
// +listMapKey=type
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
Phase string `json:"phase,omitempty"`
Reason string `json:"reason,omitempty"`
MySQLUserCreated bool `json:"mysql_user_created,omitempty"`
SecretCreated bool `json:"secret_created,omitempty"`
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
Phase string `json:"phase,omitempty"`
Reason string `json:"reason,omitempty"`

// +kubebuilder:default=false

// true if MySQL user is created
MySQLUserCreated bool `json:"mysql_user_created,omitempty"`

// +kubebuilder:default=false

// true if Secret is created
SecretCreated bool `json:"secret_created,omitempty"`
}

func (m *MySQLUser) GetConditions() []metav1.Condition {
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/mysql.nakamasato.com_mysqls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ spec:
host:
description: Host is MySQL host of target MySQL cluster.
type: string
port:
default: 3306
description: Port is MySQL port of target MySQL cluster.
type: integer
required:
- admin_password
- admin_user
Expand Down
8 changes: 7 additions & 1 deletion config/crd/bases/mysql.nakamasato.com_mysqlusers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ spec:
properties:
host:
default: '%'
description: MySQL hostname for MySQL account
type: string
mysqlName:
description: MySQL name
description: MySQL (CRD) name to reference to, which decides the destination
MySQL server
type: string
required:
- mysqlName
Expand Down Expand Up @@ -139,12 +141,16 @@ spec:
- type
x-kubernetes-list-type: map
mysql_user_created:
default: false
description: true if MySQL user is created
type: boolean
phase:
type: string
reason:
type: string
secret_created:
default: false
description: true if Secret is created
type: boolean
type: object
type: object
Expand Down
10 changes: 8 additions & 2 deletions controllers/mysql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"time"

. "github.com/go-sql-driver/mysql"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -139,8 +140,13 @@ func (r *MySQLReconciler) UpdateMySQLClients(ctx context.Context, mysql *mysqlv1
log.Info("MySQLClient already exists", "mysql.Name", mysql.Name)
return nil
}
// db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tpc(%s:%d)/", mysql.Spec.AdminUser, mysql.Spec.AdminPassword, mysql.Spec.Host, 3306))
db, err := sql.Open(r.MySQLDriverName, mysql.Spec.AdminUser+":"+mysql.Spec.AdminPassword+"@tcp("+mysql.Spec.Host+":3306)/")
c := Config{
User: mysql.Spec.AdminUser,
Passwd: mysql.Spec.AdminPassword,
Addr: fmt.Sprintf("%s:%d", mysql.Spec.Host, mysql.Spec.Port),
Net: "tcp",
}
db, err := sql.Open(r.MySQLDriverName, c.FormatDSN())
if err != nil {
log.Error(err, "Failed to open MySQL database", "mysql.Name", mysql.Name)
return err
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU=
github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts=
github.com/onsi/gomega v1.27.5 h1:T/X6I0RNFw/kTqgfkZPcQ5KU6vCnWNBGdtrIx2dpGeQ=
github.com/onsi/gomega v1.27.5/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -620,8 +618,6 @@ k8s.io/utils v0.0.0-20230313181309-38a27ef9d749/go.mod h1:OLgZIPagt7ERELqWJFomSt
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/controller-runtime v0.14.5 h1:6xaWFqzT5KuAQ9ufgUaj1G/+C4Y1GRkhrxl+BJ9i+5s=
sigs.k8s.io/controller-runtime v0.14.5/go.mod h1:WqIdsAY6JBsjfc/CqO0CORmNtoCtE4S6qbPc9s68h+0=
sigs.k8s.io/controller-runtime v0.14.6 h1:oxstGVvXGNnMvY7TAESYk+lzr6S3V5VFxQ6d92KcwQA=
sigs.k8s.io/controller-runtime v0.14.6/go.mod h1:WqIdsAY6JBsjfc/CqO0CORmNtoCtE4S6qbPc9s68h+0=
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 h1:iXTIw73aPyC+oRdyqqvVJuloN1p0AC/kzH07hu3NE+k=
Expand Down

0 comments on commit 39536e7

Please sign in to comment.