/
rds_aurora_mysql.go
400 lines (345 loc) · 13.4 KB
/
rds_aurora_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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2021 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"
"fmt"
"os"
"strconv"
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
awsrds "github.com/aws/aws-sdk-go/service/rds"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
aws "github.com/kanisterio/kanister/pkg/aws"
"github.com/kanisterio/kanister/pkg/aws/ec2"
"github.com/kanisterio/kanister/pkg/aws/rds"
"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/function"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/log"
)
const (
AuroraDBInstanceClass = "db.r5.large"
AuroraDBStorage = 20
DetailsCMName = "dbconfig"
mysqlConnectionString = "mysql -h %s -u %s -p%s %s -N -e"
)
type RDSAuroraMySQLDB struct {
name string
cli kubernetes.Interface
namespace string
id string
host string
dbName string
dbSubnetGroup string
username string
password string
accessID string
secretKey string
region string
sessionToken string
securityGroupID string
securityGroupName string
bastionDebugWorkloadName string
publicAccess bool
vpcID string
}
func NewRDSAuroraMySQLDB(name, region string) App {
return &RDSAuroraMySQLDB{
name: name,
id: fmt.Sprintf("test-%s", name),
securityGroupName: fmt.Sprintf("%s-sg", name),
region: region,
username: "admin",
password: "secret99",
dbName: "testdb",
publicAccess: false,
}
}
func (a *RDSAuroraMySQLDB) Init(context.Context) error {
cfg, err := kube.LoadConfig()
if err != nil {
return err
}
var ok bool
a.cli, err = kubernetes.NewForConfig(cfg)
if err != nil {
return err
}
if a.region == "" {
a.region, ok = os.LookupEnv(aws.Region)
if !ok {
return errors.New(fmt.Sprintf("Env var %s is not set", aws.Region))
}
}
// If sessionToken is set, accessID and secretKey not required
a.sessionToken, ok = os.LookupEnv(aws.SessionToken)
if ok {
return nil
}
a.accessID, ok = os.LookupEnv(aws.AccessKeyID)
if !ok {
return errors.New(fmt.Sprintf("Env var %s is not set", aws.AccessKeyID))
}
a.secretKey, ok = os.LookupEnv(aws.SecretAccessKey)
if !ok {
return errors.New(fmt.Sprintf("Env var %s is not set", aws.SecretAccessKey))
}
return nil
}
func (a *RDSAuroraMySQLDB) Install(ctx context.Context, namespace string) error {
a.namespace = namespace
// Get aws config
awsConfig, region, err := a.getAWSConfig(ctx)
if err != nil {
return errors.Wrapf(err, "Error getting aws config app=%s", a.name)
}
// Create ec2 client
ec2Cli, err := ec2.NewClient(ctx, awsConfig, region)
if err != nil {
return err
}
a.bastionDebugWorkloadName = fmt.Sprintf("%s-workload", a.name)
deploymentSpec := bastionDebugWorkloadSpec(ctx, a.bastionDebugWorkloadName, "mysql", a.namespace)
_, err = a.cli.AppsV1().Deployments(a.namespace).Create(ctx, deploymentSpec, metav1.CreateOptions{})
if err != nil {
return errors.Wrapf(err, "Failed to create deployment %s, app=%s", a.bastionDebugWorkloadName, a.name)
}
if err := kube.WaitOnDeploymentReady(ctx, a.cli, a.namespace, a.bastionDebugWorkloadName); err != nil {
return errors.Wrapf(err, "Failed while waiting for deployment %s to be ready, app=%s", a.bastionDebugWorkloadName, a.name)
}
rdsCli, err := rds.NewClient(ctx, awsConfig, region)
if err != nil {
return err
}
a.vpcID, err = vpcIDForRDSInstance(ctx, ec2Cli)
if err != nil {
return err
}
dbSubnetGroup, err := dbSubnetGroup(ctx, ec2Cli, rdsCli, a.vpcID, a.name, subnetGroupDescription)
if err != nil {
return err
}
a.dbSubnetGroup = dbSubnetGroup
// Create security group
log.Info().Print("Creating security group.", field.M{"app": a.name, "name": a.securityGroupName})
sg, err := ec2Cli.CreateSecurityGroup(ctx, a.securityGroupName, "To allow ingress to Aurora DB cluster", a.vpcID)
if err != nil {
return errors.Wrap(err, "Error creating security group")
}
a.securityGroupID = *sg.GroupId
// Add ingress rule
_, err = ec2Cli.AuthorizeSecurityGroupIngress(ctx, a.securityGroupID, "0.0.0.0/0", "tcp", 3306)
if err != nil {
return errors.Wrap(err, "Error authorizing security group")
}
// Create RDS instance
log.Info().Print("Creating RDS Aurora DB cluster.", field.M{"app": a.name, "id": a.id})
_, err = rdsCli.CreateDBCluster(ctx, AuroraDBStorage, AuroraDBInstanceClass, a.id, a.dbSubnetGroup, string(function.DBEngineAuroraMySQL), a.dbName, a.username, a.password, []string{a.securityGroupID})
if err != nil {
return errors.Wrap(err, "Error creating DB cluster")
}
err = rdsCli.WaitUntilDBClusterAvailable(ctx, a.id)
if err != nil {
return errors.Wrap(err, "Error waiting for DB cluster to be available")
}
_, err = rdsCli.CreateDBInstance(ctx, nil, AuroraDBInstanceClass, fmt.Sprintf("%s-instance-1", a.id), string(function.DBEngineAuroraMySQL), "", "", nil, awssdk.Bool(a.publicAccess), awssdk.String(a.id), a.dbSubnetGroup)
if err != nil {
return errors.Wrap(err, "Error creating an instance in Aurora DB cluster")
}
err = rdsCli.WaitUntilDBInstanceAvailable(ctx, fmt.Sprintf("%s-instance-1", a.id))
if err != nil {
return errors.Wrap(err, "Error waiting for DB instance to be available")
}
dbCluster, err := rdsCli.DescribeDBClusters(ctx, a.id)
if err != nil {
return err
}
if len(dbCluster.DBClusters) == 0 {
return errors.New(fmt.Sprintf("Error installing application %s, DBCluster not available", a.name))
}
a.host = *dbCluster.DBClusters[0].Endpoint
// Configmap that is going to store the details for blueprint
cm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: DetailsCMName,
},
Data: map[string]string{
"aurora.clusterID": a.id,
},
}
_, err = a.cli.CoreV1().ConfigMaps(namespace).Create(ctx, cm, metav1.CreateOptions{})
return err
}
func (a *RDSAuroraMySQLDB) IsReady(context.Context) (bool, error) {
// we are already waiting for dbcluster using WaitUntilDBClusterAvailable while installing it
return true, nil
}
func (a *RDSAuroraMySQLDB) Ping(ctx context.Context) error {
log.Print("Pinging rds aurora database", field.M{"app": a.name})
pingQuery := fmt.Sprintf(mysqlConnectionString+"'SELECT 1;'", a.host, a.username, a.password, a.dbName)
pingCommand := []string{"sh", "-c", pingQuery}
_, stderr, err := a.execCommand(ctx, pingCommand)
if err != nil {
return errors.Wrapf(err, "Error while Pinging the database: %s, app: %s", stderr, a.name)
}
log.Print("Ping to the application was success.", field.M{"app": a.name})
return nil
}
func (a *RDSAuroraMySQLDB) Insert(ctx context.Context) error {
log.Print("Adding entry to database", field.M{"app": a.name})
insertQuery := fmt.Sprintf(mysqlConnectionString+
"\"INSERT INTO pets VALUES ('Puffball', 'Diane', 'hamster', 'f', '1999-03-30', 'NULL');\"", a.host, a.username, a.password, a.dbName)
insertCommand := []string{"sh", "-c", insertQuery}
_, stderr, err := a.execCommand(ctx, insertCommand)
if err != nil {
return errors.Wrapf(err, "Error while inserting data into table: %s, app: %s", stderr, a.name)
}
log.Info().Print("Inserted a row in test db.", field.M{"app": a.name})
return nil
}
func (a *RDSAuroraMySQLDB) Count(ctx context.Context) (int, error) {
log.Print("Counting entries from database", field.M{"app": a.name})
countQuery := fmt.Sprintf(mysqlConnectionString+
"\"SELECT COUNT(*) FROM pets;\"", a.host, a.username, a.password, a.dbName)
countCommand := []string{"sh", "-c", countQuery}
stdout, stderr, err := a.execCommand(ctx, countCommand)
if err != nil {
return 0, errors.Wrapf(err, "Error while counting data of table: %s, app: %s", stderr, a.name)
}
rowsReturned, err := strconv.Atoi(stdout)
if err != nil {
return 0, errors.Wrapf(err, "Error while converting response of count query to int: %s, app: %s", stderr, a.name)
}
log.Info().Print("Number of rows in test DB.", field.M{"app": a.name, "count": rowsReturned})
return rowsReturned, nil
}
func (a *RDSAuroraMySQLDB) Reset(ctx context.Context) error {
log.Print("Resetting the mysql instance.", field.M{"app": a.name})
deleteQuery := fmt.Sprintf(mysqlConnectionString+"\"DROP TABLE IF EXISTS pets;\"", a.host, a.username, a.password, a.dbName)
deleteCommand := []string{"sh", "-c", deleteQuery}
_, stderr, err := a.execCommand(ctx, deleteCommand)
if err != nil {
return errors.Wrapf(err, "Error while deleting data from table: %s, app: %s", stderr, a.name)
}
log.Info().Print("Database reset was successful!", field.M{"app": a.name})
return nil
}
func (a *RDSAuroraMySQLDB) Initialize(ctx context.Context) error {
log.Print("Initializing database", field.M{"app": a.name})
createQuery := fmt.Sprintf(mysqlConnectionString+"\"CREATE TABLE pets (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);\"", a.host, a.username, a.password, a.dbName)
createCommand := []string{"sh", "-c", createQuery}
_, stderr, err := a.execCommand(ctx, createCommand)
if err != nil {
return errors.Wrapf(err, "Error while creating the database: %s, app: %s", stderr, a.name)
}
return nil
}
func (a *RDSAuroraMySQLDB) Object() crv1alpha1.ObjectReference {
return crv1alpha1.ObjectReference{
APIVersion: "v1",
Name: DetailsCMName,
Namespace: a.namespace,
Resource: "configmaps",
}
}
func (a *RDSAuroraMySQLDB) Uninstall(ctx context.Context) error {
awsConfig, region, err := a.getAWSConfig(ctx)
if err != nil {
return errors.Wrapf(err, "app=%s", a.name)
}
// Create rds client
rdsCli, err := rds.NewClient(ctx, awsConfig, region)
if err != nil {
return errors.Wrap(err, "Failed to create rds client. You may need to delete RDS resources manually. app=rds-postgresql")
}
descOp, err := rdsCli.DescribeDBClusters(ctx, a.id)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() != awsrds.ErrCodeDBClusterNotFoundFault {
return err
}
log.Print("Aurora DB cluster is not found")
}
} else {
// DB Cluster is present, delete and wait for it to be deleted
if err := function.DeleteAuroraDBCluster(ctx, rdsCli, descOp, a.id); err != nil {
return nil
}
}
// Create ec2 client
ec2Cli, err := ec2.NewClient(ctx, awsConfig, region)
if err != nil {
return errors.Wrap(err, "Failed to create ec2 client.")
}
log.Info().Print("Deleting db subnet group.", field.M{"app": a.name})
_, err = rdsCli.DeleteDBSubnetGroup(ctx, a.dbSubnetGroup)
if err != nil {
// If the subnet group does not exist, ignore the error and return
if err, ok := err.(awserr.Error); ok {
switch err.Code() {
case awsrds.ErrCodeDBSubnetGroupNotFoundFault:
log.Info().Print("Subnet Group Does not exist: ErrCodeDBSubnetGroupNotFoundFault.", field.M{"app": a.name, "name": a.dbSubnetGroup})
default:
return errors.Wrapf(err, "Failed to delete subnet group. You may need to delete it manually. app=%s name=%s", a.name, a.dbSubnetGroup)
}
}
}
// delete security group
log.Info().Print("Deleting security group.", field.M{"app": a.name})
_, err = ec2Cli.DeleteSecurityGroup(ctx, a.securityGroupID)
if err != nil {
if err, ok := err.(awserr.Error); ok {
switch err.Code() {
case "InvalidGroup.NotFound":
log.Error().Print("Security group already deleted: InvalidGroup.NotFound.", field.M{"app": a.name, "name": a.securityGroupName})
default:
return errors.Wrapf(err, "Failed to delete security group. You may need to delete it manually. app=%s name=%s", a.name, a.securityGroupName)
}
}
}
// Remove workload object created for executing commands
err = a.cli.AppsV1().Deployments(a.namespace).Delete(ctx, a.bastionDebugWorkloadName, metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "Error deleting Workload %s, app=%s", a.bastionDebugWorkloadName, a.name)
}
return nil
}
func (a *RDSAuroraMySQLDB) GetClusterScopedResources(ctx context.Context) []crv1alpha1.ObjectReference {
return nil
}
func (a *RDSAuroraMySQLDB) getAWSConfig(ctx context.Context) (*awssdk.Config, string, error) {
config := make(map[string]string)
config[aws.ConfigRegion] = a.region
config[aws.AccessKeyID] = a.accessID
config[aws.SecretAccessKey] = a.secretKey
config[aws.SessionToken] = a.sessionToken
return aws.GetConfig(ctx, config)
}
func (a RDSAuroraMySQLDB) execCommand(ctx context.Context, command []string) (string, string, error) {
podName, containerName, err := kube.GetPodContainerFromDeployment(ctx, a.cli, a.namespace, a.bastionDebugWorkloadName)
if err != nil || podName == "" {
return "", "", err
}
return kube.Exec(a.cli, a.namespace, podName, containerName, command, nil)
}