forked from APSL/k8s-database-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
178 lines (154 loc) · 4.51 KB
/
mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* vim:set sw=8 ts=8 noet:
*
* Copyright (c) 2017 Torchbox Ltd.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely. This software is provided 'as-is', without any express or implied
* warranty.
*/
package main
import (
"database/sql"
"fmt"
"log"
"net/url"
"strings"
_ "github.com/go-sql-driver/mysql"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiv1 "k8s.io/client-go/pkg/api/v1"
)
func (cllr *DatabaseController) handleAddMysql(db *Database) {
var server *MySQLConfig = nil
for _, candidate := range cllr.DBConfig.MySQL {
if db.Spec.Class != candidate.Class {
continue
}
server = &candidate
break
}
if server == nil {
cllr.setError(db, "no available providers")
return
}
u, err := url.Parse(server.URL)
password, _ := u.User.Password()
dsn := fmt.Sprintf("%s:%s@tcp(%s)/",
u.User.Username(),
password,
u.Host)
dbconn, err := sql.Open("mysql", dsn)
if err != nil {
log.Printf("%s/%s: database connection failed: %v\n",
db.Namespace, db.Name, err)
cllr.setError(db, fmt.Sprintf("database connection failed: %v", err))
return
}
defer dbconn.Close()
dbname := strings.Replace(
fmt.Sprintf("%s_%s", db.Namespace, db.Name),
"-", "_", -1)
gen_password, err := makepassword()
if err != nil {
cllr.setError(db, "failed to generate password")
log.Printf("%s/%s: failed to generate password: %s",
db.Namespace, db.Name, err)
return
}
_, err = dbconn.Exec(fmt.Sprintf("CREATE DATABASE `%s`", dbname))
if err != nil {
cllr.setError(db, fmt.Sprintf("failed to create database: %v", err))
log.Printf("%s/%s: failed to create database: %v\n",
db.Namespace, db.Name, err)
return
}
_, err = dbconn.Exec(fmt.Sprintf("GRANT ALL PRIVILEGES ON `%s`.* TO `%s`@`%%` IDENTIFIED BY \"%s\"",
strings.Replace(dbname, "_", "\\_", -1),
dbname, gen_password))
if err != nil {
cllr.setError(db, fmt.Sprintf("failed to create user: %v", err))
log.Printf("%s/%s: failed to create user: %v\n",
db.Namespace, db.Name, err)
return
}
surl := []byte(fmt.Sprintf("mysql://%s:%s@%s/%s", dbname, gen_password, u.Host, dbname))
secret, err := cllr.Clientset.Secrets(db.Namespace).Get(db.Spec.Secret, metav1.GetOptions{})
if err != nil {
if !errors.IsNotFound(err) {
cllr.setError(db, fmt.Sprintf("failed to create secret: %v", err))
log.Printf("%s/%s: failed to create secret: %v\n",
db.Namespace, db.Name, err)
return
}
secret := &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: db.Spec.Secret,
Namespace: db.Namespace,
},
Data: map[string][]byte{
"database-url": surl,
},
}
_, err := cllr.Clientset.Secrets(db.Namespace).Create(secret)
if err != nil {
cllr.setError(db, fmt.Sprintf("failed to create secret: %v", err))
log.Printf("%s/%s: failed to create secret: %v\n",
db.Namespace, db.Name, err)
return
}
} else {
secret.Data["database-url"] = surl
_, err := cllr.Clientset.Secrets(db.Namespace).Update(secret)
if err != nil {
cllr.setError(db, fmt.Sprintf("failed to create secret: %v", err))
log.Printf("%s/%s: failed to create secret: %v\n",
db.Namespace, db.Name, err)
return
}
}
db.Status.Phase = "done"
db.Status.Server = server.Name
cllr.client.Put().Namespace(db.Namespace).Resource("databases").Name(db.Name).Body(db).Do()
}
func (cllr *DatabaseController) handleDeleteMysql(db *Database) {
var server *MySQLConfig
for _, candidate := range cllr.DBConfig.MySQL {
if db.Status.Server != candidate.Name {
continue
}
server = &candidate
break
}
if server == nil {
log.Printf("%s/%s: delete failed: server \"%s\" not found in config\n",
db.Namespace, db.Name, db.Status.Server)
return
}
u, err := url.Parse(server.URL)
password, _ := u.User.Password()
dsn := fmt.Sprintf("%s:%s@tcp(%s)/",
u.User.Username(),
password,
u.Host)
dbconn, err := sql.Open("mysql", dsn)
if err != nil {
log.Printf("%s/%s: delete failed: database connection failed: %v\n",
db.Namespace, db.Name, err)
return
}
defer dbconn.Close()
dbname := fmt.Sprintf("%s_%s", db.Namespace, db.Name)
_, err = dbconn.Exec(fmt.Sprintf("DROP DATABASE `%s`", dbname))
if err != nil {
log.Printf("%s/%s: failed to drop database \"%s\": %v\n",
db.Namespace, db.Name, dbname, err)
return
}
_, err = dbconn.Exec(fmt.Sprintf("DROP USER `%s`@`%%`", dbname))
if err != nil {
log.Printf("%s/%s: failed to drop user \"%s\": %v\n",
db.Namespace, db.Name, err)
return
}
}