This repository was archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpostgresql.go
More file actions
198 lines (171 loc) · 5.43 KB
/
postgresql.go
File metadata and controls
198 lines (171 loc) · 5.43 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"database/sql"
"fmt"
"log"
"net/url"
"strings"
_ "github.com/lib/pq"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiv1 "k8s.io/client-go/pkg/api/v1"
)
const postgresDefaultPort = "5432"
func (cllr *DatabaseController) handleAddPostgresql(db *Database) {
var server *PostgreSQLConfig = nil
for _, candidate := range cllr.DBConfig.PostgreSQL {
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("dbname=%s user=%s password=%s host=%s sslmode=disable",
strings.TrimLeft(u.Path, "/"), u.User.Username(), password, u.Host)
dbconn, err := sql.Open("postgres", 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 ROLE \"%s\" LOGIN PASSWORD '%s'",
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
}
_, err = dbconn.Exec(fmt.Sprintf("CREATE DATABASE \"%s\" TEMPLATE \"template0\"", 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 ON DATABASE \"%s\" TO \"%s\"", dbname, dbname))
if err != nil {
cllr.setError(db, fmt.Sprintf("failed to create database: %v", err))
log.Printf("%s/%s: failed to grant privileges: %v\n",
db.Namespace, db.Name, err)
return
}
surl := []byte(fmt.Sprintf("postgresql://%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
}
port := u.Port()
if port == "" {
port = postgresDefaultPort
}
secret = &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: db.Spec.Secret,
Namespace: db.Namespace,
},
Data: map[string][]byte{
"database-url": surl,
"database-host": []byte(u.Hostname()),
"database-port": []byte(port),
"database-name": []byte(dbname),
"database-user": []byte(dbname),
"database-password": []byte(gen_password),
},
}
_, 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) handleDeletePostgresql(db *Database) {
var server *PostgreSQLConfig
for _, candidate := range cllr.DBConfig.PostgreSQL {
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("dbname=%s user=%s password=%s host=%s sslmode=disable",
strings.TrimLeft(u.Path, "/"), u.User.Username(), password, u.Host)
dbconn, err := sql.Open("postgres", 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 := strings.Replace(
fmt.Sprintf("%s_%s", db.Namespace, db.Name),
"-", "_", -1)
_, err = dbconn.Exec(fmt.Sprintf("ALTER DATABASE \"%s\" WITH allow_connections false", dbname))
if err != nil {
log.Printf("%s/%s: failed to stop connections of database \"%s\": %v\n",
db.Namespace, db.Name, dbname, err)
return
}
_, err = dbconn.Exec("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname=$1", dbname)
if err != nil {
log.Printf("%s/%s: failed to terminate connections of database \"%s\": %v\n",
db.Namespace, db.Name, dbname, err)
return
}
_, 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 ROLE \"%s\"", dbname))
if err != nil {
log.Printf("%s/%s: failed to drop user \"%s\": %v\n",
db.Namespace, db.Name, err)
return
}
}