Skip to content

Commit 28d527c

Browse files
authored
RDS Create DBInstance From Snapshot (#78)
Fixes aws-controllers-k8s/community#1262 Description of changes: This change is allow customer create db instance from DBSnapshotIdentifier when it's present. When DBSnapshotIdentifier is present in yaml file, ACK will try call [RestoreDBInstanceFromDBSnapshot](https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBInstanceFromDBSnapshot.html#API_RestoreDBInstanceFromDBSnapshot) API instead of normal CreateDbInstance api. Tested this new behavior by adding `dbSnapshotIdentifier: "arn:aws:rds:us-west-2:980401675890:snapshot:brucegu-snapshot-1"` in yaml file and make sure DBInstance can be created from this snapshot. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 77da3ba commit 28d527c

File tree

12 files changed

+966
-3
lines changed

12 files changed

+966
-3
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2022-05-18T21:18:00Z"
2+
build_date: "2022-05-27T02:20:26Z"
33
build_hash: c651d2bb60694df1f7a5dad823258472a1a6fc8a
44
go_version: go1.18.2
55
version: v0.18.4-12-gc651d2b
6-
api_directory_checksum: 932f41dc8e3c84de66ba73cde9eecc2b94c847f3
6+
api_directory_checksum: 5e8e1929ea3e768826601ad5737bcf14bcfc4977
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.42.0
99
generator_config_info:
10-
file_checksum: 6f5ece4103c904b1e0417e817cdc437743a85fb3
10+
file_checksum: 18450c6db05caecfb0d53eec6fdbd80f3f0ce502
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/db_instance.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/v1alpha1/generator.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ resources:
131131
path: Parameters
132132
DBInstance:
133133
hooks:
134+
sdk_create_pre_build_request:
135+
template_path: hooks/db_instance/sdk_create_pre_build_request.go.tpl
134136
sdk_create_post_set_output:
135137
template_path: hooks/db_instance/sdk_create_post_set_output.go.tpl
136138
sdk_read_many_post_set_output:
@@ -145,6 +147,8 @@ resources:
145147
template_path: hooks/db_instance/sdk_delete_pre_build_request.go.tpl
146148
delta_pre_compare:
147149
template_path: hooks/db_instance/delta_pre_compare.go.tpl
150+
sdk_file_end:
151+
template_path: hooks/db_instance/sdk_file_end.go.tpl
148152
exceptions:
149153
terminal_codes:
150154
- InvalidParameter
@@ -178,6 +182,14 @@ resources:
178182
resource: Key
179183
service_name: kms
180184
path: Status.ACKResourceMetadata.ARN
185+
DBSnapshotIdentifier:
186+
from:
187+
operation: RestoreDBInstanceFromDBSnapshot
188+
path: DBSnapshotIdentifier
189+
UseDefaultProcessorFeatures:
190+
from:
191+
operation: RestoreDBInstanceFromDBSnapshot
192+
path: UseDefaultProcessorFeatures
181193
renames:
182194
operations:
183195
CreateDBInstance:

apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/rds.services.k8s.aws_dbinstances.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ spec:
218218
items:
219219
type: string
220220
type: array
221+
dbSnapshotIdentifier:
222+
description: "The identifier for the DB snapshot to restore from.
223+
\n Constraints: \n * Must match the identifier of an existing
224+
DBSnapshot. \n * If you are restoring from a shared manual DB
225+
snapshot, the DBSnapshotIdentifier must be the ARN of the shared
226+
DB snapshot."
227+
type: string
221228
dbSubnetGroupName:
222229
description: "A DB subnet group to associate with this DB instance.
223230
\n If there is no DB subnet group, then it is a non-VPC DB instance."
@@ -624,6 +631,11 @@ spec:
624631
description: The time zone of the DB instance. The time zone parameter
625632
is currently supported only by Microsoft SQL Server (https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_SQLServer.html#SQLServer.Concepts.General.TimeZone).
626633
type: string
634+
useDefaultProcessorFeatures:
635+
description: "A value that indicates whether the DB instance class
636+
of the DB instance uses its default processor features. \n This
637+
setting doesn't apply to RDS Custom."
638+
type: boolean
627639
vpcSecurityGroupIDs:
628640
description: "A list of Amazon EC2 VPC security groups to associate
629641
with this DB instance. \n Amazon Aurora \n Not applicable. The associated

generator.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ resources:
131131
path: Parameters
132132
DBInstance:
133133
hooks:
134+
sdk_create_pre_build_request:
135+
template_path: hooks/db_instance/sdk_create_pre_build_request.go.tpl
134136
sdk_create_post_set_output:
135137
template_path: hooks/db_instance/sdk_create_post_set_output.go.tpl
136138
sdk_read_many_post_set_output:
@@ -145,6 +147,8 @@ resources:
145147
template_path: hooks/db_instance/sdk_delete_pre_build_request.go.tpl
146148
delta_pre_compare:
147149
template_path: hooks/db_instance/delta_pre_compare.go.tpl
150+
sdk_file_end:
151+
template_path: hooks/db_instance/sdk_file_end.go.tpl
148152
exceptions:
149153
terminal_codes:
150154
- InvalidParameter
@@ -178,6 +182,14 @@ resources:
178182
resource: Key
179183
service_name: kms
180184
path: Status.ACKResourceMetadata.ARN
185+
DBSnapshotIdentifier:
186+
from:
187+
operation: RestoreDBInstanceFromDBSnapshot
188+
path: DBSnapshotIdentifier
189+
UseDefaultProcessorFeatures:
190+
from:
191+
operation: RestoreDBInstanceFromDBSnapshot
192+
path: UseDefaultProcessorFeatures
181193
renames:
182194
operations:
183195
CreateDBInstance:

helm/crds/rds.services.k8s.aws_dbinstances.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ spec:
218218
items:
219219
type: string
220220
type: array
221+
dbSnapshotIdentifier:
222+
description: "The identifier for the DB snapshot to restore from.
223+
\n Constraints: \n * Must match the identifier of an existing
224+
DBSnapshot. \n * If you are restoring from a shared manual DB
225+
snapshot, the DBSnapshotIdentifier must be the ARN of the shared
226+
DB snapshot."
227+
type: string
221228
dbSubnetGroupName:
222229
description: "A DB subnet group to associate with this DB instance.
223230
\n If there is no DB subnet group, then it is a non-VPC DB instance."
@@ -624,6 +631,11 @@ spec:
624631
description: The time zone of the DB instance. The time zone parameter
625632
is currently supported only by Microsoft SQL Server (https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_SQLServer.html#SQLServer.Concepts.General.TimeZone).
626633
type: string
634+
useDefaultProcessorFeatures:
635+
description: "A value that indicates whether the DB instance class
636+
of the DB instance uses its default processor features. \n This
637+
setting doesn't apply to RDS Custom."
638+
type: boolean
627639
vpcSecurityGroupIDs:
628640
description: "A list of Amazon EC2 VPC security groups to associate
629641
with this DB instance. \n Amazon Aurora \n Not applicable. The associated

pkg/resource/db_instance/delta.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/resource/db_instance/hooks.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
package db_instance
1515

1616
import (
17+
"context"
1718
"errors"
1819
"fmt"
1920

21+
ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition"
2022
ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue"
23+
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
24+
corev1 "k8s.io/api/core/v1"
2125
)
2226

2327
// NOTE(jaypipes): The below list is derived from looking at the RDS control
@@ -160,3 +164,33 @@ func instanceDeleting(r *resource) bool {
160164
dbis := *r.ko.Status.DBInstanceStatus
161165
return dbis == StatusDeleting
162166
}
167+
168+
// function to create restoreDbInstanceFromDbSnapshot payload and call restoreDbInstanceFromDbSnapshot API
169+
func (rm *resourceManager) restoreDbInstanceFromDbSnapshot(
170+
ctx context.Context,
171+
r *resource,
172+
) (created *resource, err error) {
173+
rlog := ackrtlog.FromContext(ctx)
174+
exit := rlog.Trace("rm.restoreDbInstanceFromDbSnapshot")
175+
defer func(err error) { exit(err) }(err)
176+
177+
resp, respErr := rm.sdkapi.RestoreDBInstanceFromDBSnapshotWithContext(ctx, rm.newRestoreDBInstanceFromDBSnapshotInput(r))
178+
rm.metrics.RecordAPICall("CREATE", "RestoreDbInstanceFromDbSnapshot", respErr)
179+
if respErr != nil {
180+
return nil, respErr
181+
}
182+
183+
rm.setResourceFromRestoreDBInstanceFromDBSnapshotOutput(r, resp)
184+
rm.setStatusDefaults(r.ko)
185+
186+
// We expect the DB instance to be in 'creating' status since we just
187+
// issued the call to create it, but I suppose it doesn't hurt to check
188+
// here.
189+
if instanceCreating(&resource{r.ko}) {
190+
// Setting resource synced condition to false will trigger a requeue of
191+
// the resource. No need to return a requeue error here.
192+
ackcondition.SetSynced(&resource{r.ko}, corev1.ConditionFalse, nil, nil)
193+
return &resource{r.ko}, nil
194+
}
195+
return &resource{r.ko}, nil
196+
}

0 commit comments

Comments
 (0)