-
Notifications
You must be signed in to change notification settings - Fork 148
/
mysql.go
229 lines (195 loc) · 7.5 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
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
// Copyright 2019 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package app
import (
"context"
"strconv"
"strings"
"time"
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/helm"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/log"
"github.com/kanisterio/kanister/pkg/poll"
"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
)
const (
mysqlWaitTimeout = 1 * time.Minute
)
type MysqlDB struct {
cli kubernetes.Interface
namespace string
name string
chart helm.ChartInfo
}
// Last tested working version "1.6.7"
func NewMysqlDB(name string) App {
return &MysqlDB{
name: name,
chart: helm.ChartInfo{
Release: appendRandString(name),
RepoURL: helm.StableRepoURL,
Chart: "mysql",
RepoName: helm.StableRepoName,
Values: map[string]string{
"mysqlRootPassword": "mysecretpassword",
"persistence.enabled": "false",
"imagePullPolicy": "Always",
},
},
}
}
func (mdb *MysqlDB) Init(ctx context.Context) error {
cfg, err := kube.LoadConfig()
if err != nil {
return err
}
mdb.cli, err = kubernetes.NewForConfig(cfg)
if err != nil {
return err
}
return nil
}
func (mdb *MysqlDB) Install(ctx context.Context, namespace string) error {
mdb.namespace = namespace
cli, err := helm.NewCliClient()
if err != nil {
return errors.Wrap(err, "failed to create helm client")
}
log.Print("Adding repo.", field.M{"app": mdb.name})
err = cli.AddRepo(ctx, mdb.chart.RepoName, mdb.chart.RepoURL)
if err != nil {
return errors.Wrapf(err, "Error helm repo for app %s.", mdb.name)
}
log.Print("Installing mysql instance using helm.", field.M{"app": mdb.name})
err = cli.Install(ctx, mdb.chart.RepoName+"/"+mdb.chart.Chart, mdb.chart.Version, mdb.chart.Release, mdb.namespace, mdb.chart.Values)
if err != nil {
return errors.Wrapf(err, "Error intalling application %s through helm.", mdb.name)
}
return nil
}
func (mdb *MysqlDB) IsReady(ctx context.Context) (bool, error) {
log.Print("Waiting for the mysql instance to be ready.", field.M{"app": mdb.name})
ctx, cancel := context.WithTimeout(ctx, mysqlWaitTimeout)
defer cancel()
err := kube.WaitOnDeploymentReady(ctx, mdb.cli, mdb.namespace, mdb.chart.Release)
if err != nil {
return false, err
}
log.Print("Application instance is ready.", field.M{"app": mdb.name})
return true, nil
}
func (mdb *MysqlDB) Object() crv1alpha1.ObjectReference {
return crv1alpha1.ObjectReference{
Kind: "deployment",
Name: mdb.chart.Release,
Namespace: mdb.namespace,
}
}
func (mdb *MysqlDB) Uninstall(ctx context.Context) error {
cli, err := helm.NewCliClient()
if err != nil {
return errors.Wrap(err, "failed to create helm client")
}
err = cli.Uninstall(ctx, mdb.chart.Release, mdb.namespace)
if err != nil {
log.WithError(err).Print("Failed to uninstall app, you will have to uninstall it manually.", field.M{"app": mdb.name})
return err
}
log.Print("Uninstalled application.", field.M{"app": mdb.name})
return nil
}
func (mdb *MysqlDB) Ping(ctx context.Context) error {
log.Print("Pinging the mysql database.", field.M{"app": mdb.name})
// exec into the pod and create the test database, read password from secret
loginMysql := []string{"sh", "-c", "mysql -u root --password=$MYSQL_ROOT_PASSWORD"}
_, stderr, err := mdb.execCommand(ctx, loginMysql)
if err != nil {
return errors.Wrapf(err, "Error while Pinging the database %s", stderr)
}
log.Print("Ping to the application was success.", field.M{"app": mdb.name})
return nil
}
func (mdb *MysqlDB) Insert(ctx context.Context) error {
log.Print("Inserting some records in mysql instance.", field.M{"app": mdb.name})
insertRecordCMD := []string{"sh", "-c", "mysql -u root --password=$MYSQL_ROOT_PASSWORD -e 'use testdb; INSERT INTO pets VALUES (\"Puffball\",\"Diane\",\"hamster\",\"f\",\"1999-03-30\",NULL); '"}
_, stderr, err := mdb.execCommand(ctx, insertRecordCMD)
if err != nil {
return errors.Wrapf(err, "Error while inserting the data into msyql database: %s", stderr)
}
log.Print("Successfully inserted records in the application.", field.M{"app": mdb.name})
return nil
}
func (mdb *MysqlDB) Count(ctx context.Context) (int, error) {
log.Print("Counting the records from the mysql instance.", field.M{"app": mdb.name})
selectRowsCMD := []string{"sh", "-c", "mysql -u root --password=$MYSQL_ROOT_PASSWORD -e 'use testdb; select count(*) from pets; '"}
stdout, stderr, err := mdb.execCommand(ctx, selectRowsCMD)
if err != nil {
return 0, errors.Wrapf(err, "Error while counting the data of the database: %s", stderr)
}
// get the returned count and convert it to int, to return
rowsReturned, err := strconv.Atoi((strings.Split(stdout, "\n")[1]))
if err != nil {
return 0, errors.Wrapf(err, "Error while converting row count to int.")
}
log.Print("Count that we received from application is.", field.M{"app": mdb.name, "count": rowsReturned})
return rowsReturned, nil
}
func (mdb *MysqlDB) Reset(ctx context.Context) error {
timeoutCtx, waitCancel := context.WithTimeout(ctx, mysqlWaitTimeout)
defer waitCancel()
err := poll.Wait(timeoutCtx, func(ctx context.Context) (bool, error) {
err := mdb.Ping(ctx)
return err == nil, nil
})
if err != nil {
return errors.Wrapf(err, "Error waiting for application %s to be ready to reset it", mdb.name)
}
log.Print("Resetting the mysql instance.", field.M{"app": "mysql"})
// delete all the data from the table
deleteFromTableCMD := []string{"sh", "-c", "mysql -u root --password=$MYSQL_ROOT_PASSWORD -e 'DROP DATABASE IF EXISTS testdb'"}
_, stderr, err := mdb.execCommand(ctx, deleteFromTableCMD)
if err != nil {
return errors.Wrapf(err, "Error while dropping the mysql table: %s", stderr)
}
// create the database and a pets table
createTableCMD := []string{"sh", "-c", "mysql -u root --password=$MYSQL_ROOT_PASSWORD -e 'create database testdb; use testdb; CREATE TABLE pets (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);'"}
_, stderr, err = mdb.execCommand(ctx, createTableCMD)
if err != nil {
return errors.Wrapf(err, "Error while creating the mysql table: %s", stderr)
}
log.Print("Reset of the application was successful.", field.M{"app": mdb.name})
return nil
}
func (mdb *MysqlDB) ConfigMaps() map[string]crv1alpha1.ObjectReference {
return nil
}
func (mdb *MysqlDB) Secrets() map[string]crv1alpha1.ObjectReference {
return map[string]crv1alpha1.ObjectReference{
"mysql": crv1alpha1.ObjectReference{
Kind: "Secret",
Name: mdb.chart.Release,
Namespace: mdb.namespace,
},
}
}
func (mdb *MysqlDB) execCommand(ctx context.Context, command []string) (string, string, error) {
podname, containername, err := kube.GetPodContainerFromDeployment(ctx, mdb.cli, mdb.namespace, mdb.chart.Release)
if err != nil || podname == "" {
return "", "", errors.Wrapf(err, "Error getting pod and containername %s.", mdb.name)
}
return kube.Exec(mdb.cli, mdb.namespace, podname, containername, command, nil)
}