/
rds_postgres.go
481 lines (425 loc) · 15.7 KB
/
rds_postgres.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// 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"
"fmt"
"os"
"strconv"
"time"
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"
"sigs.k8s.io/yaml"
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/kube"
"github.com/kanisterio/kanister/pkg/log"
)
type RDSPostgresDB struct {
name string
cli kubernetes.Interface
namespace string
id string
host string
databases []string
dbSubnetGroup string
username string
password string
accessID string
secretKey string
region string
sessionToken string
securityGroupID string
securityGroupName string
configMapName string
secretName string
bastionDebugWorkloadName string
publicAccess bool
vpcID string
}
const (
dbInstanceType = "db.t3.micro"
postgresConnectionString = "PGPASSWORD=%s psql -h %s -p 5432 -U %s -d %s -t -c"
subnetGroupDescription = "kanister-test-subnet-group"
)
func NewRDSPostgresDB(name string, customRegion string) App {
return &RDSPostgresDB{
name: name,
id: fmt.Sprintf("test-%s", name),
securityGroupName: fmt.Sprintf("%s-sg", name),
databases: []string{"postgres", "template1"},
username: "master",
password: "secret99",
region: customRegion,
configMapName: fmt.Sprintf("%s-config", name),
secretName: fmt.Sprintf("%s-secret", name),
publicAccess: false,
}
}
func (pdb *RDSPostgresDB) Init(ctx context.Context) error {
// Instantiate Client SDKs
cfg, err := kube.LoadConfig()
if err != nil {
return err
}
var ok bool
pdb.cli, err = kubernetes.NewForConfig(cfg)
if err != nil {
return err
}
if pdb.region == "" {
pdb.region, ok = os.LookupEnv(aws.Region)
if !ok {
return fmt.Errorf("env var %s is not set", aws.Region)
}
}
// If sessionToken is set, accessID and secretKey not required
pdb.sessionToken, ok = os.LookupEnv(aws.SessionToken)
if ok {
return nil
}
pdb.accessID, ok = os.LookupEnv(aws.AccessKeyID)
if !ok {
return fmt.Errorf("env var %s is not set", aws.AccessKeyID)
}
pdb.secretKey, ok = os.LookupEnv(aws.SecretAccessKey)
if !ok {
return fmt.Errorf("env var %s is not set", aws.SecretAccessKey)
}
return nil
}
func (pdb *RDSPostgresDB) Install(ctx context.Context, ns string) error {
var err error
pdb.namespace = ns
// Create AWS config
awsConfig, region, err := pdb.getAWSConfig(ctx)
if err != nil {
return errors.Wrapf(err, "app=%s", pdb.name)
}
ec2Cli, err := ec2.NewClient(ctx, awsConfig, region)
if err != nil {
return err
}
rdsCli, err := rds.NewClient(ctx, awsConfig, region)
if err != nil {
return err
}
pdb.bastionDebugWorkloadName = fmt.Sprintf("%s-workload", pdb.name)
deploymentSpec := bastionDebugWorkloadSpec(ctx, pdb.bastionDebugWorkloadName, "postgres", pdb.namespace)
_, err = pdb.cli.AppsV1().Deployments(pdb.namespace).Create(ctx, deploymentSpec, metav1.CreateOptions{})
if err != nil {
return errors.Wrapf(err, "Failed to create deployment %s, app: %s", pdb.bastionDebugWorkloadName, pdb.name)
}
if err := kube.WaitOnDeploymentReady(ctx, pdb.cli, pdb.namespace, pdb.bastionDebugWorkloadName); err != nil {
return errors.Wrapf(err, "Failed while waiting for deployment %s to be ready, app: %s", pdb.bastionDebugWorkloadName, pdb.name)
}
pdb.vpcID, err = vpcIDForRDSInstance(ctx, ec2Cli)
if err != nil {
return err
}
dbSubnetGroup, err := dbSubnetGroup(ctx, ec2Cli, rdsCli, pdb.vpcID, pdb.name, subnetGroupDescription)
if err != nil {
return err
}
pdb.dbSubnetGroup = dbSubnetGroup
// Create security group
log.Info().Print("Creating security group.", field.M{"app": pdb.name, "name": pdb.securityGroupName})
sg, err := ec2Cli.CreateSecurityGroup(ctx, pdb.securityGroupName, "kanister-test-security-group", pdb.vpcID)
if err != nil {
return err
}
pdb.securityGroupID = *sg.GroupId
// Add ingress rule
log.Info().Print("Adding ingress rule to security group.", field.M{"app": pdb.name})
_, err = ec2Cli.AuthorizeSecurityGroupIngress(ctx, pdb.securityGroupID, "0.0.0.0/0", "tcp", 5432)
if err != nil {
return err
}
// Create RDS instance
log.Info().Print("Creating RDS instance.", field.M{"app": pdb.name, "id": pdb.id})
_, err = rdsCli.CreateDBInstance(ctx, awssdk.Int64(20), dbInstanceType, pdb.id, "postgres", pdb.username, pdb.password, []string{pdb.securityGroupID}, awssdk.Bool(pdb.publicAccess), nil, pdb.dbSubnetGroup)
if err != nil {
return err
}
// Wait for DB to be ready
log.Info().Print("Waiting for rds to be ready.", field.M{"app": pdb.name})
err = rdsCli.WaitUntilDBInstanceAvailable(ctx, pdb.id)
if err != nil {
return err
}
// Find host of the instance
dbInstance, err := rdsCli.DescribeDBInstances(ctx, pdb.id)
if err != nil {
return err
}
pdb.host = *dbInstance.DBInstances[0].Endpoint.Address
// Create configmap
dbconfig := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: pdb.configMapName,
},
Data: map[string]string{
"postgres.instanceid": pdb.id,
"postgres.host": pdb.host,
"postgres.databases": makeYamlList(pdb.databases),
"postgres.user": pdb.username,
"postgres.secret": pdb.secretName,
},
}
_, err = pdb.cli.CoreV1().ConfigMaps(ns).Create(ctx, dbconfig, metav1.CreateOptions{})
if err != nil {
return err
}
// Create secret
dbsecret := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: pdb.secretName,
},
StringData: map[string]string{
"password": pdb.password,
"username": pdb.username,
"access_key_id": pdb.accessID,
"secret_access_key": pdb.secretKey,
"aws_region": pdb.region,
},
}
_, err = pdb.cli.CoreV1().Secrets(ns).Create(ctx, dbsecret, metav1.CreateOptions{})
if err != nil {
return err
}
return nil
}
func (pdb *RDSPostgresDB) IsReady(ctx context.Context) (bool, error) {
return true, nil
}
func (pdb *RDSPostgresDB) Object() crv1alpha1.ObjectReference {
return crv1alpha1.ObjectReference{
APIVersion: "v1",
Name: pdb.configMapName,
Namespace: pdb.namespace,
Resource: "configmaps",
}
}
// Ping makes and tests DB connection
func (pdb *RDSPostgresDB) Ping(ctx context.Context) error {
log.Print("Pinging rds postgres database", field.M{"app": pdb.name})
// Get connection info from configmap
dbconfig, err := pdb.cli.CoreV1().ConfigMaps(pdb.namespace).Get(ctx, pdb.configMapName, metav1.GetOptions{})
if err != nil {
return err
}
// Get secret creds
dbsecret, err := pdb.cli.CoreV1().Secrets(pdb.namespace).Get(ctx, pdb.secretName, metav1.GetOptions{})
if err != nil {
return err
}
// Parse databases from config data
var databases []string
if err := yaml.Unmarshal([]byte(dbconfig.Data["postgres.databases"]), &databases); err != nil {
return err
}
if databases == nil {
return errors.New("Databases are missing from configmap")
}
isReadyQuery := fmt.Sprintf(postgresConnectionString+"'SELECT version();'", dbsecret.Data["password"], dbconfig.Data["postgres.host"], dbconfig.Data["postgres.user"], databases[0])
pingCommand := []string{"sh", "-c", isReadyQuery}
_, stderr, err := pdb.execCommand(ctx, pingCommand)
if err != nil {
return errors.Wrapf(err, "Error while Pinging the database: %s, app: %s", stderr, pdb.name)
}
log.Print("Ping to the application was successful.", field.M{"app": pdb.name})
return nil
}
func (pdb RDSPostgresDB) Insert(ctx context.Context) error {
log.Print("Adding entry to database", field.M{"app": pdb.name})
now := time.Now().Format(time.RFC3339Nano)
insertQuery := fmt.Sprintf(postgresConnectionString+
"\"INSERT INTO inventory (name) VALUES ('%s');\"", pdb.password, pdb.host, pdb.username, pdb.databases[0], now)
insertCommand := []string{"sh", "-c", insertQuery}
_, stderr, err := pdb.execCommand(ctx, insertCommand)
if err != nil {
return errors.Wrapf(err, "Error while inserting data into table: %s, app: %s", stderr, pdb.name)
}
log.Info().Print("Inserted a row in test db.", field.M{"app": pdb.name})
return nil
}
func (pdb RDSPostgresDB) Count(ctx context.Context) (int, error) {
log.Print("Counting entries from database", field.M{"app": pdb.name})
countQuery := fmt.Sprintf(postgresConnectionString+
"\"SELECT COUNT(*) FROM inventory;\"", pdb.password, pdb.host, pdb.username, pdb.databases[0])
countCommand := []string{"sh", "-c", countQuery}
stdout, stderr, err := pdb.execCommand(ctx, countCommand)
if err != nil {
return 0, errors.Wrapf(err, "Error while counting data of table: %s, app: %s", stderr, pdb.name)
}
rowsReturned, err := strconv.Atoi(stdout)
if err != nil {
return 0, errors.Wrapf(err, "Error while converting response of count query: %s, app: %s", stderr, pdb.name)
}
log.Info().Print("Counting rows in test db.", field.M{"app": pdb.name, "count": rowsReturned})
return rowsReturned, nil
}
func (pdb RDSPostgresDB) Reset(ctx context.Context) error {
log.Print("Resetting database", field.M{"app": pdb.name})
deleteQuery := fmt.Sprintf(postgresConnectionString+"\"DROP TABLE IF EXISTS inventory;\"", pdb.password, pdb.host, pdb.username, pdb.databases[0])
deleteCommand := []string{"sh", "-c", deleteQuery}
_, stderr, err := pdb.execCommand(ctx, deleteCommand)
if err != nil {
return errors.Wrapf(err, "Error while deleting data from table: %s, app: %s", stderr, pdb.name)
}
log.Info().Print("Database reset successful!", field.M{"app": pdb.name})
return nil
}
// Initialize is used to initialize the database or create schema
func (pdb RDSPostgresDB) Initialize(ctx context.Context) error {
// Create table.
log.Print("Initializing database", field.M{"app": pdb.name})
createQuery := fmt.Sprintf(postgresConnectionString+"\"CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50));\"", pdb.password, pdb.host, pdb.username, pdb.databases[0])
createCommand := []string{"sh", "-c", createQuery}
_, stderr, err := pdb.execCommand(ctx, createCommand)
if err != nil {
return errors.Wrapf(err, "Error while initializing the database: %s, app: %s", stderr, pdb.name)
}
return nil
}
func (pdb RDSPostgresDB) ConfigMaps() map[string]crv1alpha1.ObjectReference {
return map[string]crv1alpha1.ObjectReference{
"dbconfig": {
Kind: "configmap",
Name: pdb.configMapName,
Namespace: pdb.namespace,
},
}
}
func (pdb RDSPostgresDB) Secrets() map[string]crv1alpha1.ObjectReference {
return map[string]crv1alpha1.ObjectReference{
"dbsecret": {
Kind: "secret",
Name: pdb.secretName,
Namespace: pdb.namespace,
},
}
}
func (pdb RDSPostgresDB) Uninstall(ctx context.Context) error {
// Create AWS config
awsConfig, region, err := pdb.getAWSConfig(ctx)
if err != nil {
return errors.Wrapf(err, "app=%s", pdb.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")
}
// Delete rds instance
log.Info().Print("Deleting rds instance", field.M{"app": pdb.name})
_, err = rdsCli.DeleteDBInstance(ctx, pdb.id)
if err != nil {
if err, ok := err.(awserr.Error); ok {
switch err.Code() {
case awsrds.ErrCodeDBInstanceNotFoundFault:
log.Info().Print("RDS instance already deleted: ErrCodeDBInstanceNotFoundFault.", field.M{"app": pdb.name, "id": pdb.id})
default:
return errors.Wrapf(err, "Failed to delete rds instance. You may need to delete it manually. app=rds-postgresql id=%s", pdb.id)
}
}
}
// Waiting for rds to be deleted
if err == nil {
log.Info().Print("Waiting for rds to be deleted", field.M{"app": pdb.name})
err = rdsCli.WaitUntilDBInstanceDeleted(ctx, pdb.id)
if err != nil {
return errors.Wrapf(err, "Failed to wait for rds instance till delete succeeds. app=rds-postgresql id=%s", pdb.id)
}
}
// Create ec2 client
ec2Cli, err := ec2.NewClient(ctx, awsConfig, region)
if err != nil {
return errors.Wrap(err, "Failed to ec2 client. You may need to delete EC2 resources manually. app=rds-postgresql")
}
log.Info().Print("Deleting db subnet group.", field.M{"app": pdb.name})
_, err = rdsCli.DeleteDBSubnetGroup(ctx, pdb.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": pdb.name, "id": pdb.id})
default:
return errors.Wrapf(err, "Failed to delete db subnet group. You may need to delete it manually. app=rds-postgresql name=%s", pdb.dbSubnetGroup)
}
}
}
// Delete security group
log.Info().Print("Deleting security group.", field.M{"app": pdb.name})
_, err = ec2Cli.DeleteSecurityGroup(ctx, pdb.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": pdb.name, "name": pdb.securityGroupName})
default:
return errors.Wrapf(err, "Failed to delete security group. You may need to delete it manually. app=rds-postgresql name=%s", pdb.securityGroupName)
}
}
}
// Remove workload object created for executing commands
err = pdb.cli.AppsV1().Deployments(pdb.namespace).Delete(ctx, pdb.bastionDebugWorkloadName, metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "Error deleting Workload name=%s app=%s", pdb.bastionDebugWorkloadName, pdb.name)
}
return nil
}
func (pdb RDSPostgresDB) GetClusterScopedResources(ctx context.Context) []crv1alpha1.ObjectReference {
return nil
}
func (pdb RDSPostgresDB) getAWSConfig(ctx context.Context) (*awssdk.Config, string, error) {
config := make(map[string]string)
config[aws.ConfigRegion] = pdb.region
config[aws.AccessKeyID] = pdb.accessID
config[aws.SecretAccessKey] = pdb.secretKey
config[aws.SessionToken] = pdb.sessionToken
return aws.GetConfig(ctx, config)
}
func makeYamlList(dbs []string) string {
dbsYaml := ""
for _, db := range dbs {
dbsYaml += fmt.Sprintf("- %s\n", db)
}
return dbsYaml
}
func (pdb RDSPostgresDB) execCommand(ctx context.Context, command []string) (string, string, error) {
podName, containerName, err := kube.GetPodContainerFromDeployment(ctx, pdb.cli, pdb.namespace, pdb.bastionDebugWorkloadName)
if err != nil || podName == "" {
return "", "", err
}
return kube.Exec(pdb.cli, pdb.namespace, podName, containerName, command, nil)
}