Skip to content

Commit 6cf250c

Browse files
brucegucodeTenglong Gu
andauthored
Allow restore db snapshot from snapshot (#101)
It's similar to what we did in #78 This PR will enable restore db cluster from snapshot, either dbSnapshot or dbClusterSnapshot. Fixed the issue during testing and verified it can work with both cluster snapshot and db instance snapshot. Controller can issue restore db cluster from snapshot API and resource is synced after creation succeed. Co-authored-by: Tenglong Gu <[email protected]>
1 parent fa017ea commit 6cf250c

File tree

13 files changed

+809
-6
lines changed

13 files changed

+809
-6
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2022-07-12T23:19:21Z"
3-
build_hash: d09bac5fa87cffd0028014833a5e7e786c0187dd
2+
build_date: "2022-07-18T22:15:47Z"
3+
build_hash: cb4bca850e1d1dd678d8ae9bd9d5fd537c06ca51
44
go_version: go1.18.2
5-
version: v0.19.2-6-gd09bac5
6-
api_directory_checksum: a60467e94dc7e76bf34ff50f21691507d576fefc
5+
version: v0.19.2-7-gcb4bca8
6+
api_directory_checksum: e768ca701f6e5b113f74fbde422fc59fb2e7b3fe
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.27
99
generator_config_info:
10-
file_checksum: 0b31df47b2d4db92ee4536c1713f64c60d266c72
10+
file_checksum: 1647eb82151b4d1e1115dec606abb62c842f881b
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/db_cluster.go

Lines changed: 12 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ resources:
7979
# resolved.
8080
custom_method_name: customUpdate
8181
hooks:
82+
sdk_create_pre_build_request:
83+
template_path: hooks/db_cluster/sdk_create_pre_build_request.go.tpl
8284
delta_pre_compare:
8385
template_path: hooks/db_cluster/delta_pre_compare.go.tpl
8486
sdk_create_post_set_output:
@@ -87,6 +89,8 @@ resources:
8789
template_path: hooks/db_cluster/sdk_read_many_post_set_output.go.tpl
8890
sdk_delete_pre_build_request:
8991
template_path: hooks/db_cluster/sdk_delete_pre_build_request.go.tpl
92+
sdk_file_end:
93+
template_path: hooks/db_cluster/sdk_file_end.go.tpl
9094
exceptions:
9195
terminal_codes:
9296
- DBClusterQuotaExceededFault
@@ -120,6 +124,10 @@ resources:
120124
resource: SecurityGroup
121125
service_name: ec2
122126
path: Status.ID
127+
SnapshotIdentifier:
128+
from:
129+
operation: RestoreDBClusterFromSnapshot
130+
path: SnapshotIdentifier
123131
renames:
124132
operations:
125133
CreateDBCluster:

apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 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_dbclusters.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,14 @@ spec:
544544
minCapacity:
545545
type: number
546546
type: object
547+
snapshotIdentifier:
548+
description: "The identifier for the DB snapshot or DB cluster snapshot
549+
to restore from. \n You can use either the name or the Amazon Resource
550+
Name (ARN) to specify a DB cluster snapshot. However, you can use
551+
only the ARN to specify a DB snapshot. \n Constraints: \n * Must
552+
match the identifier of an existing Snapshot. \n Valid for: Aurora
553+
DB clusters and Multi-AZ DB clusters"
554+
type: string
547555
sourceRegion:
548556
description: SourceRegion is the source region where the resource
549557
exists. This is not sent over the wire and is only used for presigning.

generator.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ resources:
7979
# resolved.
8080
custom_method_name: customUpdate
8181
hooks:
82+
sdk_create_pre_build_request:
83+
template_path: hooks/db_cluster/sdk_create_pre_build_request.go.tpl
8284
delta_pre_compare:
8385
template_path: hooks/db_cluster/delta_pre_compare.go.tpl
8486
sdk_create_post_set_output:
@@ -87,6 +89,8 @@ resources:
8789
template_path: hooks/db_cluster/sdk_read_many_post_set_output.go.tpl
8890
sdk_delete_pre_build_request:
8991
template_path: hooks/db_cluster/sdk_delete_pre_build_request.go.tpl
92+
sdk_file_end:
93+
template_path: hooks/db_cluster/sdk_file_end.go.tpl
9094
exceptions:
9195
terminal_codes:
9296
- DBClusterQuotaExceededFault
@@ -120,6 +124,10 @@ resources:
120124
resource: SecurityGroup
121125
service_name: ec2
122126
path: Status.ID
127+
SnapshotIdentifier:
128+
from:
129+
operation: RestoreDBClusterFromSnapshot
130+
path: SnapshotIdentifier
123131
renames:
124132
operations:
125133
CreateDBCluster:

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,14 @@ spec:
544544
minCapacity:
545545
type: number
546546
type: object
547+
snapshotIdentifier:
548+
description: "The identifier for the DB snapshot or DB cluster snapshot
549+
to restore from. \n You can use either the name or the Amazon Resource
550+
Name (ARN) to specify a DB cluster snapshot. However, you can use
551+
only the ARN to specify a DB snapshot. \n Constraints: \n * Must
552+
match the identifier of an existing Snapshot. \n Valid for: Aurora
553+
DB clusters and Multi-AZ DB clusters"
554+
type: string
547555
sourceRegion:
548556
description: SourceRegion is the source region where the resource
549557
exists. This is not sent over the wire and is only used for presigning.

helm/values.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
"type": "array",
193193
"items": {
194194
"type": "string",
195-
"pattern": "^.*=.*$"
195+
"pattern": "(^$|^.*=.*$)"
196196
}
197197
},
198198
"serviceAccount": {

pkg/resource/db_cluster/delta.go

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

pkg/resource/db_cluster/hooks.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020

2121
svcapitypes "github.com/aws-controllers-k8s/rds-controller/apis/v1alpha1"
2222
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
23+
ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition"
2324
ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue"
2425
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2526
svcsdk "github.com/aws/aws-sdk-go/service/rds"
27+
corev1 "k8s.io/api/core/v1"
2628
)
2729

2830
// NOTE(jaypipes): The below list is derived from looking at the RDS control
@@ -318,3 +320,32 @@ func equalStrings(a, b *string) bool {
318320
}
319321
return (*a == "" && b == nil) || *a == *b
320322
}
323+
324+
// function to create restoreDbClusterFromSnapshot payload and call restoreDbClusterFromSnapshot API
325+
func (rm *resourceManager) restoreDbClusterFromSnapshot(
326+
ctx context.Context,
327+
r *resource,
328+
) (created *resource, err error) {
329+
rlog := ackrtlog.FromContext(ctx)
330+
exit := rlog.Trace("rm.restoreDbClusterFromSnapshot")
331+
defer func(err error) { exit(err) }(err)
332+
333+
resp, respErr := rm.sdkapi.RestoreDBClusterFromSnapshotWithContext(ctx, rm.newRestoreDBClusterFromSnapshotInput(r))
334+
rm.metrics.RecordAPICall("CREATE", "RestoreDbClusterFromSnapshot", respErr)
335+
if respErr != nil {
336+
return nil, respErr
337+
}
338+
339+
rm.setResourceFromRestoreDBClusterFromSnapshotOutput(r, resp)
340+
rm.setStatusDefaults(r.ko)
341+
342+
// We expect the DB cluster to be in 'creating' status since we just
343+
// issued the call to create it, but I suppose it doesn't hurt to check
344+
// here.
345+
if clusterCreating(&resource{r.ko}) {
346+
// Setting resource synced condition to false will trigger a requeue of
347+
// the resource. No need to return a requeue error here.
348+
ackcondition.SetSynced(&resource{r.ko}, corev1.ConditionFalse, nil, nil)
349+
}
350+
return &resource{r.ko}, nil
351+
}

0 commit comments

Comments
 (0)