forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_image_helpers.go
283 lines (256 loc) · 8.56 KB
/
db_image_helpers.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package util
import (
"fmt"
"os/exec"
"strings"
"time"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/util/wait"
)
// Database interface allows testing database images
type Database interface {
// GetPodName returns the name of the Pod this helper is bound to
GetPodName() string
// IsReady indicates whether the underlying Pod is ready for queries
IsReady(oc *CLI) (bool, error)
// Query queries the database as a regular user
Query(oc *CLI, query string) (string, error)
// Query queries the database as a privileged user
QueryPrivileged(oc *CLI, query string) (string, error)
// TestRemoteLogin tests weather it is possible to remote login to hostAddress
TestRemoteLogin(oc *CLI, hostAddress string) error
}
// MySQL is a MySQL helper for executing commands
type MySQL struct {
PodName string
MasterPodName string
}
// PostgreSQL is a PostgreSQL helper for executing commands
type PostgreSQL struct {
PodName string
MasterPodName string
}
// PodConfig holds configuration for a pod
type PodConfig struct {
Container string
Env map[string]string
}
// NewMysql queries OpenShift for a pod with given name, saving environment
// variables like username and password for easier use.
func NewMysql(podName, masterPodName string) Database {
if masterPodName == "" {
masterPodName = podName
}
return &MySQL{
PodName: podName,
MasterPodName: masterPodName,
}
}
// NewPostgreSQL queries OpenShift for a pod with given name, saving environment
// variables like username and password for easier use.
func NewPostgreSQL(podName, masterPodName string) Database {
if masterPodName == "" {
masterPodName = podName
}
return &PostgreSQL{
PodName: podName,
MasterPodName: masterPodName,
}
}
func (m MySQL) GetPodName() string {
return m.PodName
}
func (m PostgreSQL) GetPodName() string {
return m.PodName
}
func GetPodConfig(c kclient.PodInterface, podName string) (conf *PodConfig, err error) {
pod, err := c.Get(podName)
if err != nil {
return nil, err
}
env := make(map[string]string)
for _, container := range pod.Spec.Containers {
for _, e := range container.Env {
env[e.Name] = e.Value
}
}
return &PodConfig{pod.Spec.Containers[0].Name, env}, nil
}
// IsReady pings the MySQL server
func (m MySQL) IsReady(oc *CLI) (bool, error) {
conf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.PodName)
if err != nil {
return false, err
}
out, err := oc.Run("exec").Args(m.PodName, "-c", conf.Container, "--", "bash", "-c",
"mysqladmin -h 127.0.0.1 -uroot ping").Output()
if err != nil {
switch err.(type) {
case *exec.ExitError:
return false, nil
default:
return false, err
}
}
return strings.Contains(out, "mysqld is alive"), nil
}
// Query executes an SQL query as an ordinary user and returns the result.
func (m MySQL) Query(oc *CLI, query string) (string, error) {
conf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.PodName)
if err != nil {
return "", err
}
masterConf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.MasterPodName)
if err != nil {
return "", err
}
return oc.Run("exec").Args(m.PodName, "-c", conf.Container, "--", "bash", "-c",
fmt.Sprintf("mysql -h 127.0.0.1 -u%s -p%s -e \"%s\" %s",
masterConf.Env["MYSQL_USER"], masterConf.Env["MYSQL_PASSWORD"], query,
masterConf.Env["MYSQL_DATABASE"])).Output()
}
// QueryPrivileged executes an SQL query as a root user and returns the result.
func (m MySQL) QueryPrivileged(oc *CLI, query string) (string, error) {
conf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.PodName)
if err != nil {
return "", err
}
masterConf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.MasterPodName)
if err != nil {
return "", err
}
return oc.Run("exec").Args(m.PodName, "-c", conf.Container, "--", "bash", "-c",
fmt.Sprintf("mysql -h 127.0.0.1 -uroot -e \"%s\" %s",
query, masterConf.Env["MYSQL_DATABASE"])).Output()
}
// TestRemoteLogin will test whether we can login through to a remote database.
func (m MySQL) TestRemoteLogin(oc *CLI, hostAddress string) error {
conf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.PodName)
if err != nil {
return err
}
masterConf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.MasterPodName)
if err != nil {
return err
}
err = oc.Run("exec").Args(m.PodName, "-c", conf.Container, "--", "bash", "-c",
fmt.Sprintf("mysql -h %s -u%s -p%s -e \"SELECT 1;\" %s",
hostAddress, masterConf.Env["MYSQL_USER"], masterConf.Env["MYSQL_PASSWORD"],
masterConf.Env["MYSQL_DATABASE"])).Execute()
return err
}
// IsReady pings the PostgreSQL server
func (m PostgreSQL) IsReady(oc *CLI) (bool, error) {
conf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.PodName)
if err != nil {
return false, err
}
out, err := oc.Run("exec").Args(m.PodName, "-c", conf.Container, "--", "bash", "-c",
"psql postgresql://postgres@127.0.0.1 -x -c \"SELECT 1;\"").Output()
if err != nil {
switch err.(type) {
case *exec.ExitError:
return false, nil
default:
return false, err
}
}
return strings.Contains(out, "-[ RECORD 1 ]\n?column? | 1"), nil
}
// Query executes an SQL query as an ordinary user and returns the result.
func (m PostgreSQL) Query(oc *CLI, query string) (string, error) {
conf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.PodName)
if err != nil {
return "", err
}
masterConf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.MasterPodName)
if err != nil {
return "", err
}
return oc.Run("exec").Args(m.PodName, "-c", conf.Container, "--", "bash", "-c",
fmt.Sprintf("psql postgres://%s:%s@127.0.0.1/%s -x -c \"%s\"",
masterConf.Env["POSTGRESQL_USER"], masterConf.Env["POSTGRESQL_PASSWORD"],
masterConf.Env["POSTGRESQL_DATABASE"], query)).Output()
}
// QueryPrivileged executes an SQL query as a root user and returns the result.
func (m PostgreSQL) QueryPrivileged(oc *CLI, query string) (string, error) {
conf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.PodName)
if err != nil {
return "", err
}
masterConf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.MasterPodName)
if err != nil {
return "", err
}
return oc.Run("exec").Args(m.PodName, "-c", conf.Container, "--", "bash", "-c",
fmt.Sprintf("psql postgres://postgres:%s@127.0.0.1/%s -x -c \"%s\"",
masterConf.Env["POSTGRESQL_ADMIN_PASSWORD"],
masterConf.Env["POSTGRESQL_DATABASE"], query)).Output()
}
// TestRemoteLogin will test whether we can login through to a remote database.
func (m PostgreSQL) TestRemoteLogin(oc *CLI, hostAddress string) error {
conf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.PodName)
if err != nil {
return err
}
masterConf, err := GetPodConfig(oc.KubeREST().Pods(oc.Namespace()), m.MasterPodName)
if err != nil {
return err
}
err = oc.Run("exec").Args(m.PodName, "-c", conf.Container, "--", "bash", "-c",
fmt.Sprintf("psql postgres://%s:%s@%s/%s -x -c \"SELECT 1;\"",
masterConf.Env["POSTGRESQL_USER"], masterConf.Env["POSTGRESQL_PASSWORD"],
hostAddress, masterConf.Env["POSTGRESQL_DATABASE"])).Execute()
return err
}
// WaitForQueryOutput will execute the query multiple times, until the
// specified substring is found in the results. This function should be used for
// testing replication, since it might take some time untill the data is propagated
// to slaves.
func WaitForQueryOutput(oc *CLI, d Database, timeout time.Duration, admin bool, query, resultSubstr string) error {
err := wait.Poll(5*time.Second, timeout, func() (bool, error) {
var (
out string
err error
)
if admin {
out, err = d.QueryPrivileged(oc, query)
} else {
out, err = d.Query(oc, query)
}
if _, ok := err.(*exec.ExitError); ok {
// Ignore exit errors
return false, nil
}
if err != nil {
return false, err
}
if strings.Contains(out, resultSubstr) {
return true, nil
}
return false, nil
})
if err == wait.ErrWaitTimeout {
return fmt.Errorf("timed out waiting for query: %q", query)
}
return err
}
// WaitUntilUp continuously waits for the server to become ready, up until timeout.
func WaitUntilUp(oc *CLI, d Database, timeout time.Duration) error {
err := wait.Poll(2*time.Second, timeout, func() (bool, error) {
return d.IsReady(oc)
})
if err == wait.ErrWaitTimeout {
return fmt.Errorf("timed out waiting for pod %s get up", d.GetPodName())
}
return err
}
// WaitUntilAllHelpersAreUp waits until all helpers are ready to serve requests
func WaitUntilAllHelpersAreUp(oc *CLI, helpers []Database) error {
for _, m := range helpers {
if err := WaitUntilUp(oc, m, 3*time.Minute); err != nil {
return err
}
}
return nil
}