-
Notifications
You must be signed in to change notification settings - Fork 362
/
rds.go
646 lines (617 loc) · 31.3 KB
/
rds.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
Copyright 2019 The Crossplane 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 rds
import (
"context"
"encoding/json"
"strconv"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/rds"
rdstypes "github.com/aws/aws-sdk-go-v2/service/rds/types"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/crossplane/provider-aws/apis/database/v1beta1"
awsclients "github.com/crossplane/provider-aws/pkg/clients"
)
const (
errGetPasswordSecretFailed = "cannot get password secret"
)
// Client defines RDS RDSClient operations
type Client interface {
CreateDBInstance(context.Context, *rds.CreateDBInstanceInput, ...func(*rds.Options)) (*rds.CreateDBInstanceOutput, error)
RestoreDBInstanceFromS3(context.Context, *rds.RestoreDBInstanceFromS3Input, ...func(*rds.Options)) (*rds.RestoreDBInstanceFromS3Output, error)
RestoreDBInstanceFromDBSnapshot(context.Context, *rds.RestoreDBInstanceFromDBSnapshotInput, ...func(*rds.Options)) (*rds.RestoreDBInstanceFromDBSnapshotOutput, error)
DescribeDBInstances(context.Context, *rds.DescribeDBInstancesInput, ...func(*rds.Options)) (*rds.DescribeDBInstancesOutput, error)
ModifyDBInstance(context.Context, *rds.ModifyDBInstanceInput, ...func(*rds.Options)) (*rds.ModifyDBInstanceOutput, error)
DeleteDBInstance(context.Context, *rds.DeleteDBInstanceInput, ...func(*rds.Options)) (*rds.DeleteDBInstanceOutput, error)
AddTagsToResource(context.Context, *rds.AddTagsToResourceInput, ...func(*rds.Options)) (*rds.AddTagsToResourceOutput, error)
}
// NewClient creates new RDS RDSClient with provided AWS Configurations/Credentials
func NewClient(cfg *aws.Config) Client {
return rds.NewFromConfig(*cfg)
}
// IsErrorNotFound helper function to test for ErrCodeDBInstanceNotFoundFault error
func IsErrorNotFound(err error) bool {
var nff *rdstypes.DBInstanceNotFoundFault
return errors.As(err, &nff)
}
// GenerateCreateDBInstanceInput from RDSInstanceSpec
func GenerateCreateDBInstanceInput(name, password string, p *v1beta1.RDSInstanceParameters) *rds.CreateDBInstanceInput {
// Partially duplicates GenerateRestoreDBInstanceFromS3Input and GenerateRestoreDBInstanceFromSnapshotInput:
// Make sure any relevant changes are applied there too.
c := &rds.CreateDBInstanceInput{
DBInstanceIdentifier: aws.String(name),
AllocatedStorage: awsclients.Int32Address(p.AllocatedStorage),
AutoMinorVersionUpgrade: p.AutoMinorVersionUpgrade,
AvailabilityZone: p.AvailabilityZone,
BackupRetentionPeriod: awsclients.Int32Address(p.BackupRetentionPeriod),
CharacterSetName: p.CharacterSetName,
CopyTagsToSnapshot: p.CopyTagsToSnapshot,
DBClusterIdentifier: p.DBClusterIdentifier,
DBInstanceClass: aws.String(p.DBInstanceClass),
DBName: p.DBName,
DBParameterGroupName: p.DBParameterGroupName,
DBSecurityGroups: p.DBSecurityGroups,
DBSubnetGroupName: p.DBSubnetGroupName,
DeletionProtection: p.DeletionProtection,
Domain: p.Domain,
DomainIAMRoleName: p.DomainIAMRoleName,
EnableCloudwatchLogsExports: p.EnableCloudwatchLogsExports,
EnableIAMDatabaseAuthentication: p.EnableIAMDatabaseAuthentication,
EnablePerformanceInsights: p.EnablePerformanceInsights,
Engine: aws.String(p.Engine),
EngineVersion: p.EngineVersion,
Iops: awsclients.Int32Address(p.IOPS),
KmsKeyId: p.KMSKeyID,
LicenseModel: p.LicenseModel,
MasterUserPassword: awsclients.String(password),
MasterUsername: p.MasterUsername,
MaxAllocatedStorage: awsclients.Int32Address(p.MaxAllocatedStorage),
MonitoringInterval: awsclients.Int32Address(p.MonitoringInterval),
MonitoringRoleArn: p.MonitoringRoleARN,
MultiAZ: p.MultiAZ,
OptionGroupName: p.OptionGroupName,
PerformanceInsightsKMSKeyId: p.PerformanceInsightsKMSKeyID,
PerformanceInsightsRetentionPeriod: awsclients.Int32Address(p.PerformanceInsightsRetentionPeriod),
Port: awsclients.Int32Address(p.Port),
PreferredBackupWindow: p.PreferredBackupWindow,
PreferredMaintenanceWindow: p.PreferredMaintenanceWindow,
PromotionTier: awsclients.Int32Address(p.PromotionTier),
PubliclyAccessible: p.PubliclyAccessible,
StorageEncrypted: p.StorageEncrypted,
Timezone: p.Timezone,
StorageType: p.StorageType,
VpcSecurityGroupIds: p.VPCSecurityGroupIDs,
}
if len(p.ProcessorFeatures) != 0 {
c.ProcessorFeatures = make([]rdstypes.ProcessorFeature, len(p.ProcessorFeatures))
for i, val := range p.ProcessorFeatures {
c.ProcessorFeatures[i] = rdstypes.ProcessorFeature{
Name: aws.String(val.Name),
Value: aws.String(val.Value),
}
}
}
if len(p.Tags) != 0 {
c.Tags = make([]rdstypes.Tag, len(p.Tags))
for i, val := range p.Tags {
c.Tags[i] = rdstypes.Tag{
Key: aws.String(val.Key),
Value: aws.String(val.Value),
}
}
}
return c
}
// GenerateRestoreDBInstanceFromS3Input from RDSInstanceSpec
func GenerateRestoreDBInstanceFromS3Input(name, password string, p *v1beta1.RDSInstanceParameters) *rds.RestoreDBInstanceFromS3Input {
// Partially duplicates GenerateCreateDBInstanceInput - make sure any relevant changes are applied there too.
c := &rds.RestoreDBInstanceFromS3Input{
DBInstanceIdentifier: aws.String(name),
AllocatedStorage: awsclients.Int32Address(p.AllocatedStorage),
AutoMinorVersionUpgrade: p.AutoMinorVersionUpgrade,
AvailabilityZone: p.AvailabilityZone,
BackupRetentionPeriod: awsclients.Int32Address(p.BackupRetentionPeriod),
CopyTagsToSnapshot: p.CopyTagsToSnapshot,
DBInstanceClass: aws.String(p.DBInstanceClass),
DBName: p.DBName,
DBParameterGroupName: p.DBParameterGroupName,
DBSecurityGroups: p.DBSecurityGroups,
DBSubnetGroupName: p.DBSubnetGroupName,
DeletionProtection: p.DeletionProtection,
EnableCloudwatchLogsExports: p.EnableCloudwatchLogsExports,
EnableIAMDatabaseAuthentication: p.EnableIAMDatabaseAuthentication,
EnablePerformanceInsights: p.EnablePerformanceInsights,
Engine: aws.String(p.Engine),
EngineVersion: p.EngineVersion,
Iops: awsclients.Int32Address(p.IOPS),
KmsKeyId: p.KMSKeyID,
LicenseModel: p.LicenseModel,
MasterUserPassword: awsclients.String(password),
MasterUsername: p.MasterUsername,
MonitoringInterval: awsclients.Int32Address(p.MonitoringInterval),
MonitoringRoleArn: p.MonitoringRoleARN,
MultiAZ: p.MultiAZ,
OptionGroupName: p.OptionGroupName,
PerformanceInsightsKMSKeyId: p.PerformanceInsightsKMSKeyID,
PerformanceInsightsRetentionPeriod: awsclients.Int32Address(p.PerformanceInsightsRetentionPeriod),
Port: awsclients.Int32Address(p.Port),
PreferredBackupWindow: p.PreferredBackupWindow,
PreferredMaintenanceWindow: p.PreferredMaintenanceWindow,
PubliclyAccessible: p.PubliclyAccessible,
S3BucketName: p.RestoreFrom.S3.BucketName,
S3IngestionRoleArn: p.RestoreFrom.S3.IngestionRoleARN,
S3Prefix: p.RestoreFrom.S3.Prefix,
SourceEngine: p.RestoreFrom.S3.SourceEngine,
SourceEngineVersion: p.RestoreFrom.S3.SourceEngineVersion,
StorageEncrypted: p.StorageEncrypted,
StorageType: p.StorageType,
VpcSecurityGroupIds: p.VPCSecurityGroupIDs,
}
if len(p.ProcessorFeatures) != 0 {
c.ProcessorFeatures = make([]rdstypes.ProcessorFeature, len(p.ProcessorFeatures))
for i, val := range p.ProcessorFeatures {
c.ProcessorFeatures[i] = rdstypes.ProcessorFeature{
Name: aws.String(val.Name),
Value: aws.String(val.Value),
}
}
}
if len(p.Tags) != 0 {
c.Tags = make([]rdstypes.Tag, len(p.Tags))
for i, val := range p.Tags {
c.Tags[i] = rdstypes.Tag{
Key: aws.String(val.Key),
Value: aws.String(val.Value),
}
}
}
return c
}
// GenerateRestoreDBInstanceFromSnapshotInput from RDSInstanceSpec
func GenerateRestoreDBInstanceFromSnapshotInput(name string, p *v1beta1.RDSInstanceParameters) *rds.RestoreDBInstanceFromDBSnapshotInput {
// Partially duplicates GenerateCreateDBInstanceInput - make sure any relevant changes are applied there too.
c := &rds.RestoreDBInstanceFromDBSnapshotInput{
DBInstanceIdentifier: aws.String(name),
AutoMinorVersionUpgrade: p.AutoMinorVersionUpgrade,
AvailabilityZone: p.AvailabilityZone,
CopyTagsToSnapshot: p.CopyTagsToSnapshot,
DBInstanceClass: aws.String(p.DBInstanceClass),
DBName: p.DBName,
DBParameterGroupName: p.DBParameterGroupName,
DBSnapshotIdentifier: p.RestoreFrom.Snapshot.SnapshotIdentifier,
DBSubnetGroupName: p.DBSubnetGroupName,
DeletionProtection: p.DeletionProtection,
Domain: p.Domain,
DomainIAMRoleName: p.DomainIAMRoleName,
EnableCloudwatchLogsExports: p.EnableCloudwatchLogsExports,
EnableIAMDatabaseAuthentication: p.EnableIAMDatabaseAuthentication,
Engine: aws.String(p.Engine),
Iops: awsclients.Int32Address(p.IOPS),
LicenseModel: p.LicenseModel,
MultiAZ: p.MultiAZ,
OptionGroupName: p.OptionGroupName,
Port: awsclients.Int32Address(p.Port),
PubliclyAccessible: p.PubliclyAccessible,
StorageType: p.StorageType,
VpcSecurityGroupIds: p.VPCSecurityGroupIDs,
}
if len(p.ProcessorFeatures) != 0 {
c.ProcessorFeatures = make([]rdstypes.ProcessorFeature, len(p.ProcessorFeatures))
for i, val := range p.ProcessorFeatures {
c.ProcessorFeatures[i] = rdstypes.ProcessorFeature{
Name: aws.String(val.Name),
Value: aws.String(val.Value),
}
}
}
if len(p.Tags) != 0 {
c.Tags = make([]rdstypes.Tag, len(p.Tags))
for i, val := range p.Tags {
c.Tags[i] = rdstypes.Tag{
Key: aws.String(val.Key),
Value: aws.String(val.Value),
}
}
}
return c
}
// CreatePatch creates a *v1beta1.RDSInstanceParameters that has only the changed
// values between the target *v1beta1.RDSInstanceParameters and the current
// *rds.DBInstance
func CreatePatch(in *rdstypes.DBInstance, target *v1beta1.RDSInstanceParameters) (*v1beta1.RDSInstanceParameters, error) {
currentParams := &v1beta1.RDSInstanceParameters{}
LateInitialize(currentParams, in)
// Don't attempt to scale down storage if autoscaling is enabled,
// and the current storage is larger than what was once
// requested. We still want to allow the user to manually scale
// the storage, so we only remove it if we're certain AWS will
// reject the value
if target.MaxAllocatedStorage != nil && aws.ToInt(target.AllocatedStorage) < aws.ToInt(currentParams.AllocatedStorage) {
// By making the values equal, CreateJSONPatch will exclude
// the field. It might seem more sensible to change the target
// object, but it's a pointer to the CR so we do not want to
// mutate it.
currentParams.AllocatedStorage = target.AllocatedStorage
}
jsonPatch, err := awsclients.CreateJSONPatch(currentParams, target)
if err != nil {
return nil, err
}
patch := &v1beta1.RDSInstanceParameters{}
if err := json.Unmarshal(jsonPatch, patch); err != nil {
return nil, err
}
return patch, nil
}
// GenerateModifyDBInstanceInput from RDSInstanceSpec
func GenerateModifyDBInstanceInput(name string, p *v1beta1.RDSInstanceParameters) *rds.ModifyDBInstanceInput {
// NOTE(muvaf): MasterUserPassword is not used here. So, password is set once
// and kept that way.
// NOTE(muvaf): Change of DBInstanceIdentifier is supported by AWS but
// Crossplane assumes identification info never changes, so, we don't support
// it.
m := &rds.ModifyDBInstanceInput{
DBInstanceIdentifier: aws.String(name),
AllocatedStorage: awsclients.Int32Address(p.AllocatedStorage),
AllowMajorVersionUpgrade: aws.ToBool(p.AllowMajorVersionUpgrade),
ApplyImmediately: aws.ToBool(p.ApplyModificationsImmediately),
AutoMinorVersionUpgrade: p.AutoMinorVersionUpgrade,
BackupRetentionPeriod: awsclients.Int32Address(p.BackupRetentionPeriod),
CACertificateIdentifier: p.CACertificateIdentifier,
CopyTagsToSnapshot: p.CopyTagsToSnapshot,
DBInstanceClass: awsclients.String(p.DBInstanceClass),
DBParameterGroupName: p.DBParameterGroupName,
DBPortNumber: awsclients.Int32Address(p.Port),
DBSecurityGroups: p.DBSecurityGroups,
DBSubnetGroupName: p.DBSubnetGroupName,
DeletionProtection: p.DeletionProtection,
Domain: p.Domain,
DomainIAMRoleName: p.DomainIAMRoleName,
EnableIAMDatabaseAuthentication: p.EnableIAMDatabaseAuthentication,
EnablePerformanceInsights: p.EnablePerformanceInsights,
EngineVersion: p.EngineVersion,
Iops: awsclients.Int32Address(p.IOPS),
LicenseModel: p.LicenseModel,
MaxAllocatedStorage: awsclients.Int32Address(p.MaxAllocatedStorage),
MonitoringInterval: awsclients.Int32Address(p.MonitoringInterval),
MonitoringRoleArn: p.MonitoringRoleARN,
MultiAZ: p.MultiAZ,
OptionGroupName: p.OptionGroupName,
PerformanceInsightsKMSKeyId: p.PerformanceInsightsKMSKeyID,
PerformanceInsightsRetentionPeriod: awsclients.Int32Address(p.PerformanceInsightsRetentionPeriod),
PreferredBackupWindow: p.PreferredBackupWindow,
PreferredMaintenanceWindow: p.PreferredMaintenanceWindow,
PromotionTier: awsclients.Int32Address(p.PromotionTier),
PubliclyAccessible: p.PubliclyAccessible,
StorageType: p.StorageType,
UseDefaultProcessorFeatures: p.UseDefaultProcessorFeatures,
VpcSecurityGroupIds: p.VPCSecurityGroupIDs,
}
if len(p.ProcessorFeatures) != 0 {
m.ProcessorFeatures = make([]rdstypes.ProcessorFeature, len(p.ProcessorFeatures))
for i, val := range p.ProcessorFeatures {
m.ProcessorFeatures[i] = rdstypes.ProcessorFeature{
Name: aws.String(val.Name),
Value: aws.String(val.Value),
}
}
}
if p.CloudwatchLogsExportConfiguration != nil {
m.CloudwatchLogsExportConfiguration = &rdstypes.CloudwatchLogsExportConfiguration{
DisableLogTypes: p.CloudwatchLogsExportConfiguration.DisableLogTypes,
EnableLogTypes: p.CloudwatchLogsExportConfiguration.EnableLogTypes,
}
}
return m
}
// GenerateObservation is used to produce v1alpha3.RDSInstanceObservation from
// rds.DBInstance.
func GenerateObservation(db rdstypes.DBInstance) v1beta1.RDSInstanceObservation { // nolint:gocyclo
o := v1beta1.RDSInstanceObservation{
AllocatedStorage: int(db.AllocatedStorage),
DBInstanceStatus: aws.ToString(db.DBInstanceStatus),
DBInstanceArn: aws.ToString(db.DBInstanceArn),
DBInstancePort: int(db.DbInstancePort),
DBResourceID: aws.ToString(db.DbiResourceId),
EnhancedMonitoringResourceArn: aws.ToString(db.EnhancedMonitoringResourceArn),
PerformanceInsightsEnabled: aws.ToBool(db.PerformanceInsightsEnabled),
ReadReplicaDBClusterIdentifiers: db.ReadReplicaDBClusterIdentifiers,
ReadReplicaDBInstanceIdentifiers: db.ReadReplicaDBInstanceIdentifiers,
ReadReplicaSourceDBInstanceIdentifier: aws.ToString(db.ReadReplicaSourceDBInstanceIdentifier),
SecondaryAvailabilityZone: aws.ToString(db.SecondaryAvailabilityZone),
}
if db.LatestRestorableTime != nil {
t := metav1.NewTime(*db.LatestRestorableTime)
o.LatestRestorableTime = &t
}
if db.InstanceCreateTime != nil {
t := metav1.NewTime(*db.InstanceCreateTime)
o.InstanceCreateTime = &t
}
if len(db.DBParameterGroups) != 0 {
o.DBParameterGroups = make([]v1beta1.DBParameterGroupStatus, len(db.DBParameterGroups))
for i, val := range db.DBParameterGroups {
o.DBParameterGroups[i] = v1beta1.DBParameterGroupStatus{
DBParameterGroupName: aws.ToString(val.DBParameterGroupName),
ParameterApplyStatus: aws.ToString(val.ParameterApplyStatus),
}
}
}
if len(db.DBSecurityGroups) != 0 {
o.DBSecurityGroups = make([]v1beta1.DBSecurityGroupMembership, len(db.DBSecurityGroups))
for i, val := range db.DBSecurityGroups {
o.DBSecurityGroups[i] = v1beta1.DBSecurityGroupMembership{
DBSecurityGroupName: aws.ToString(val.DBSecurityGroupName),
Status: aws.ToString(val.Status),
}
}
}
if db.DBSubnetGroup != nil {
o.DBSubnetGroup = v1beta1.DBSubnetGroupInRDS{
DBSubnetGroupARN: aws.ToString(db.DBSubnetGroup.DBSubnetGroupArn),
DBSubnetGroupDescription: aws.ToString(db.DBSubnetGroup.DBSubnetGroupDescription),
DBSubnetGroupName: aws.ToString(db.DBSubnetGroup.DBSubnetGroupName),
SubnetGroupStatus: aws.ToString(db.DBSubnetGroup.SubnetGroupStatus),
VPCID: aws.ToString(db.DBSubnetGroup.VpcId),
}
if len(db.DBSubnetGroup.Subnets) != 0 {
o.DBSubnetGroup.Subnets = make([]v1beta1.SubnetInRDS, len(db.DBSubnetGroup.Subnets))
for i, val := range db.DBSubnetGroup.Subnets {
o.DBSubnetGroup.Subnets[i] = v1beta1.SubnetInRDS{
SubnetIdentifier: aws.ToString(val.SubnetIdentifier),
SubnetStatus: aws.ToString(val.SubnetStatus),
}
if val.SubnetAvailabilityZone != nil {
o.DBSubnetGroup.Subnets[i].SubnetAvailabilityZone = v1beta1.AvailabilityZone{
Name: aws.ToString(val.SubnetAvailabilityZone.Name),
}
}
}
}
}
if len(db.DomainMemberships) != 0 {
o.DomainMemberships = make([]v1beta1.DomainMembership, len(db.DomainMemberships))
for i, val := range db.DomainMemberships {
o.DomainMemberships[i] = v1beta1.DomainMembership{
Domain: aws.ToString(val.Domain),
FQDN: aws.ToString(val.FQDN),
IAMRoleName: aws.ToString(val.IAMRoleName),
Status: aws.ToString(val.Status),
}
}
}
if db.Endpoint != nil {
o.Endpoint = v1beta1.Endpoint{
Address: aws.ToString(db.Endpoint.Address),
HostedZoneID: aws.ToString(db.Endpoint.HostedZoneId),
Port: int(db.Endpoint.Port),
}
}
if len(db.OptionGroupMemberships) != 0 {
o.OptionGroupMemberships = make([]v1beta1.OptionGroupMembership, len(db.OptionGroupMemberships))
for i, val := range db.OptionGroupMemberships {
o.OptionGroupMemberships[i] = v1beta1.OptionGroupMembership{
OptionGroupName: aws.ToString(val.OptionGroupName),
Status: aws.ToString(val.Status),
}
}
}
if db.PendingModifiedValues != nil {
o.PendingModifiedValues = v1beta1.PendingModifiedValues{
AllocatedStorage: int(aws.ToInt32(db.PendingModifiedValues.AllocatedStorage)),
BackupRetentionPeriod: int(aws.ToInt32(db.PendingModifiedValues.BackupRetentionPeriod)),
CACertificateIdentifier: aws.ToString(db.PendingModifiedValues.CACertificateIdentifier),
DBInstanceClass: aws.ToString(db.PendingModifiedValues.DBInstanceClass),
DBSubnetGroupName: aws.ToString(db.PendingModifiedValues.DBSubnetGroupName),
IOPS: int(aws.ToInt32(db.PendingModifiedValues.Iops)),
LicenseModel: aws.ToString(db.PendingModifiedValues.LicenseModel),
MultiAZ: aws.ToBool(db.PendingModifiedValues.MultiAZ),
Port: int(aws.ToInt32(db.PendingModifiedValues.Port)),
StorageType: aws.ToString(db.PendingModifiedValues.StorageType),
}
if db.PendingModifiedValues.PendingCloudwatchLogsExports != nil {
o.PendingModifiedValues.PendingCloudwatchLogsExports = v1beta1.PendingCloudwatchLogsExports{
LogTypesToDisable: db.PendingModifiedValues.PendingCloudwatchLogsExports.LogTypesToDisable,
LogTypesToEnable: db.PendingModifiedValues.PendingCloudwatchLogsExports.LogTypesToEnable,
}
}
if len(db.PendingModifiedValues.ProcessorFeatures) != 0 {
o.PendingModifiedValues.ProcessorFeatures = make([]v1beta1.ProcessorFeature, len(db.PendingModifiedValues.ProcessorFeatures))
for i, val := range db.PendingModifiedValues.ProcessorFeatures {
o.PendingModifiedValues.ProcessorFeatures[i] = v1beta1.ProcessorFeature{
Name: aws.ToString(val.Name),
Value: aws.ToString(val.Value),
}
}
}
}
if len(db.StatusInfos) != 0 {
o.StatusInfos = make([]v1beta1.DBInstanceStatusInfo, len(db.StatusInfos))
for i, val := range db.StatusInfos {
o.StatusInfos[i] = v1beta1.DBInstanceStatusInfo{
Message: aws.ToString(val.Message),
Status: aws.ToString(val.Status),
StatusType: aws.ToString(val.StatusType),
Normal: val.Normal,
}
}
}
if len(db.VpcSecurityGroups) != 0 {
o.VPCSecurityGroups = make([]v1beta1.VPCSecurityGroupMembership, len(db.VpcSecurityGroups))
for i, val := range db.VpcSecurityGroups {
o.VPCSecurityGroups[i] = v1beta1.VPCSecurityGroupMembership{
Status: aws.ToString(val.Status),
VPCSecurityGroupID: aws.ToString(val.VpcSecurityGroupId),
}
}
}
return o
}
// LateInitialize fills the empty fields in *v1beta1.RDSInstanceParameters with
// the values seen in rds.DBInstance.
func LateInitialize(in *v1beta1.RDSInstanceParameters, db *rdstypes.DBInstance) { // nolint:gocyclo
if db == nil {
return
}
in.DBInstanceClass = awsclients.LateInitializeString(in.DBInstanceClass, db.DBInstanceClass)
in.Engine = awsclients.LateInitializeString(in.Engine, db.Engine)
in.AllocatedStorage = awsclients.LateInitializeIntFrom32Ptr(in.AllocatedStorage, &db.AllocatedStorage)
in.AutoMinorVersionUpgrade = awsclients.LateInitializeBoolPtr(in.AutoMinorVersionUpgrade, awsclients.Bool(db.AutoMinorVersionUpgrade, awsclients.FieldRequired))
in.AvailabilityZone = awsclients.LateInitializeStringPtr(in.AvailabilityZone, db.AvailabilityZone)
in.BackupRetentionPeriod = awsclients.LateInitializeIntFromInt32Ptr(in.BackupRetentionPeriod, &db.BackupRetentionPeriod)
in.CACertificateIdentifier = awsclients.LateInitializeStringPtr(in.CACertificateIdentifier, db.CACertificateIdentifier)
in.CharacterSetName = awsclients.LateInitializeStringPtr(in.CharacterSetName, db.CharacterSetName)
in.CopyTagsToSnapshot = awsclients.LateInitializeBoolPtr(in.CopyTagsToSnapshot, awsclients.Bool(db.CopyTagsToSnapshot, awsclients.FieldRequired))
in.DBClusterIdentifier = awsclients.LateInitializeStringPtr(in.DBClusterIdentifier, db.DBClusterIdentifier)
in.DBName = awsclients.LateInitializeStringPtr(in.DBName, db.DBName)
in.DeletionProtection = awsclients.LateInitializeBoolPtr(in.DeletionProtection, awsclients.Bool(db.DeletionProtection, awsclients.FieldRequired))
in.EnableIAMDatabaseAuthentication = awsclients.LateInitializeBoolPtr(in.EnableIAMDatabaseAuthentication, awsclients.Bool(db.IAMDatabaseAuthenticationEnabled, awsclients.FieldRequired))
in.EnablePerformanceInsights = awsclients.LateInitializeBoolPtr(in.EnablePerformanceInsights, db.PerformanceInsightsEnabled)
in.IOPS = awsclients.LateInitializeIntFrom32Ptr(in.IOPS, db.Iops)
in.KMSKeyID = awsclients.LateInitializeStringPtr(in.KMSKeyID, db.KmsKeyId)
in.LicenseModel = awsclients.LateInitializeStringPtr(in.LicenseModel, db.LicenseModel)
in.MasterUsername = awsclients.LateInitializeStringPtr(in.MasterUsername, db.MasterUsername)
in.MaxAllocatedStorage = awsclients.LateInitializeIntFrom32Ptr(in.MaxAllocatedStorage, db.MaxAllocatedStorage)
in.MonitoringInterval = awsclients.LateInitializeIntFrom32Ptr(in.MonitoringInterval, db.MonitoringInterval)
in.MonitoringRoleARN = awsclients.LateInitializeStringPtr(in.MonitoringRoleARN, db.MonitoringRoleArn)
in.MultiAZ = awsclients.LateInitializeBoolPtr(in.MultiAZ, awsclients.Bool(db.MultiAZ, awsclients.FieldRequired))
in.PerformanceInsightsKMSKeyID = awsclients.LateInitializeStringPtr(in.PerformanceInsightsKMSKeyID, db.PerformanceInsightsKMSKeyId)
in.PerformanceInsightsRetentionPeriod = awsclients.LateInitializeIntFrom32Ptr(in.PerformanceInsightsRetentionPeriod, db.PerformanceInsightsRetentionPeriod)
in.PreferredBackupWindow = awsclients.LateInitializeStringPtr(in.PreferredBackupWindow, db.PreferredBackupWindow)
in.PreferredMaintenanceWindow = awsclients.LateInitializeStringPtr(in.PreferredMaintenanceWindow, db.PreferredMaintenanceWindow)
in.PromotionTier = awsclients.LateInitializeIntFrom32Ptr(in.PromotionTier, db.PromotionTier)
in.PubliclyAccessible = awsclients.LateInitializeBoolPtr(in.PubliclyAccessible, awsclients.Bool(db.PubliclyAccessible, awsclients.FieldRequired))
in.StorageEncrypted = awsclients.LateInitializeBoolPtr(in.StorageEncrypted, awsclients.Bool(db.StorageEncrypted, awsclients.FieldRequired))
in.StorageType = awsclients.LateInitializeStringPtr(in.StorageType, db.StorageType)
in.Timezone = awsclients.LateInitializeStringPtr(in.Timezone, db.Timezone)
// NOTE(muvaf): Do not use db.DbInstancePort as that always returns 0 for
// some reason. See the bug here:
// https://github.com/aws/aws-sdk-java/issues/924#issuecomment-658089792
if db.Endpoint != nil {
in.Port = awsclients.LateInitializeIntFrom32Ptr(in.Port, &db.Endpoint.Port)
}
if len(in.DBSecurityGroups) == 0 && len(db.DBSecurityGroups) != 0 {
in.DBSecurityGroups = make([]string, len(db.DBSecurityGroups))
for i, val := range db.DBSecurityGroups {
in.DBSecurityGroups[i] = aws.ToString(val.DBSecurityGroupName)
}
}
if aws.ToString(in.DBSubnetGroupName) == "" && db.DBSubnetGroup != nil {
in.DBSubnetGroupName = db.DBSubnetGroup.DBSubnetGroupName
}
if len(in.EnableCloudwatchLogsExports) == 0 && len(db.EnabledCloudwatchLogsExports) != 0 {
in.EnableCloudwatchLogsExports = db.EnabledCloudwatchLogsExports
}
if len(in.ProcessorFeatures) == 0 && len(db.ProcessorFeatures) != 0 {
in.ProcessorFeatures = make([]v1beta1.ProcessorFeature, len(db.ProcessorFeatures))
for i, val := range db.ProcessorFeatures {
in.ProcessorFeatures[i] = v1beta1.ProcessorFeature{
Name: aws.ToString(val.Name),
Value: aws.ToString(val.Value),
}
}
}
if len(in.VPCSecurityGroupIDs) == 0 && len(db.VpcSecurityGroups) != 0 {
in.VPCSecurityGroupIDs = make([]string, len(db.VpcSecurityGroups))
for i, val := range db.VpcSecurityGroups {
in.VPCSecurityGroupIDs[i] = aws.ToString(val.VpcSecurityGroupId)
}
}
in.EngineVersion = awsclients.LateInitializeStringPtr(in.EngineVersion, db.EngineVersion)
// When version 5.6 is chosen, AWS creates 5.6.41 and that's totally valid.
// But we detect as if we need to update it all the time. Here, we assign
// the actual full version to our spec to avoid unnecessary update signals.
if strings.HasPrefix(aws.ToString(db.EngineVersion), aws.ToString(in.EngineVersion)) {
in.EngineVersion = db.EngineVersion
}
if in.DBParameterGroupName == nil {
for i := range db.DBParameterGroups {
if db.DBParameterGroups[i].DBParameterGroupName != nil {
in.DBParameterGroupName = db.DBParameterGroups[i].DBParameterGroupName
break
}
}
}
}
// IsUpToDate checks whether there is a change in any of the modifiable fields.
func IsUpToDate(ctx context.Context, kube client.Client, r *v1beta1.RDSInstance, db rdstypes.DBInstance) (bool, error) {
_, pwdChanged, err := GetPassword(ctx, kube, r.Spec.ForProvider.MasterPasswordSecretRef, r.Spec.WriteConnectionSecretToReference)
if err != nil {
return false, err
}
patch, err := CreatePatch(&db, &r.Spec.ForProvider)
if err != nil {
return false, err
}
return cmp.Equal(&v1beta1.RDSInstanceParameters{}, patch, cmpopts.EquateEmpty(),
cmpopts.IgnoreTypes(&xpv1.Reference{}, &xpv1.Selector{}, []xpv1.Reference{}),
cmpopts.IgnoreFields(v1beta1.RDSInstanceParameters{}, "Region"),
cmpopts.IgnoreFields(v1beta1.RDSInstanceParameters{}, "Tags"),
cmpopts.IgnoreFields(v1beta1.RDSInstanceParameters{}, "SkipFinalSnapshotBeforeDeletion"),
cmpopts.IgnoreFields(v1beta1.RDSInstanceParameters{}, "FinalDBSnapshotIdentifier"),
cmpopts.IgnoreFields(v1beta1.RDSInstanceParameters{}, "ApplyModificationsImmediately"),
cmpopts.IgnoreFields(v1beta1.RDSInstanceParameters{}, "AllowMajorVersionUpgrade"),
cmpopts.IgnoreFields(v1beta1.RDSInstanceParameters{}, "MasterPasswordSecretRef"),
) && !pwdChanged, nil
}
// GetPassword fetches the referenced input password for an RDSInstance CRD and determines whether it has changed or not
func GetPassword(ctx context.Context, kube client.Client, in *xpv1.SecretKeySelector, out *xpv1.SecretReference) (newPwd string, changed bool, err error) {
if in == nil {
return "", false, nil
}
nn := types.NamespacedName{
Name: in.Name,
Namespace: in.Namespace,
}
s := &corev1.Secret{}
if err := kube.Get(ctx, nn, s); err != nil {
return "", false, errors.Wrap(err, errGetPasswordSecretFailed)
}
newPwd = string(s.Data[in.Key])
if out != nil {
nn = types.NamespacedName{
Name: out.Name,
Namespace: out.Namespace,
}
s = &corev1.Secret{}
// the output secret may not exist yet, so we can skip returning an
// error if the error is NotFound
if err := kube.Get(ctx, nn, s); resource.IgnoreNotFound(err) != nil {
return "", false, err
}
// if newPwd was set to some value, compare value in output secret with
// newPwd
changed = newPwd != "" && newPwd != string(s.Data[xpv1.ResourceCredentialsSecretPasswordKey])
}
return newPwd, changed, nil
}
// GetConnectionDetails extracts managed.ConnectionDetails out of v1beta1.RDSInstance.
func GetConnectionDetails(in v1beta1.RDSInstance) managed.ConnectionDetails {
if in.Status.AtProvider.Endpoint.Address == "" {
return nil
}
return managed.ConnectionDetails{
xpv1.ResourceCredentialsSecretEndpointKey: []byte(in.Status.AtProvider.Endpoint.Address),
xpv1.ResourceCredentialsSecretPortKey: []byte(strconv.Itoa(in.Status.AtProvider.Endpoint.Port)),
}
}