Skip to content

Commit

Permalink
feat(DocDB): implement restore functionality for docdb
Browse files Browse the repository at this point in the history
Co-authored-by: Maximilian Blatt <maximilian.blatt-extern@deutschebahn.com>

Signed-off-by: safboukhari <safae.boukhari@bluestone-tec.com>
(external expert on behalf of DB Netz AG)
Signed-off-by: Maximilian Blatt (external expert on behalf of DB Netz) <maximilian.blatt-extern@deutschebahn.com>
  • Loading branch information
safboukhari authored and MisterMX committed May 31, 2023
1 parent f6c38f7 commit d67688d
Show file tree
Hide file tree
Showing 6 changed files with 574 additions and 65 deletions.
22 changes: 15 additions & 7 deletions apis/docdb/v1alpha1/custom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ type CustomDBClusterParameters struct {
}

// SnapshotRestoreBackupConfiguration defines the details of the snapshot to restore from.
type SnapshotRestoreBackupConfiguration struct {
type RestoreSnapshotConfiguration struct {
// SnapshotIdentifier is the identifier of the snapshot to restore.
SnapshotIdentifier *string `json:"snapshotIdentifier"`
SnapshotIdentifier string `json:"snapshotIdentifier"`
}

// PointInTimeRestoreDBClusterBackupConfiguration defines the details of the time to restore from

Check failure on line 166 in apis/docdb/v1alpha1/custom_types.go

View workflow job for this annotation

GitHub Actions / lint

comment on exported type RestoreSnapshotConfiguration should be of the form "RestoreSnapshotConfiguration ..." (with optional leading article) (golint)
type PointInTimeRestoreDBClusterBackupConfiguration struct {
type RestorePointInTimeConfiguration struct {
// RestoreTime is the date and time (UTC) to restore from.
// Must be before the latest restorable time for the DB instance.
// Can't be specified if the useLatestRestorableTime parameter is enabled.
Expand All @@ -180,7 +180,7 @@ type PointInTimeRestoreDBClusterBackupConfiguration struct {
// SourceDBClusterIdentifier specifies the identifier of the source DB cluster from which to restore. Constraints:
// Must match the identifier of an existing DB instance.
// +optional
SourceDBClusterIdentifier *string `json:"sourceDBClusterIdentifier,omitempty"`
SourceDBClusterIdentifier string `json:"sourceDBClusterIdentifier"`

// The type of restore to be performed. You can specify one of the following
// values:
Expand All @@ -203,18 +203,26 @@ type PointInTimeRestoreDBClusterBackupConfiguration struct {
RestoreType *string `json:"restoreType,omitempty"`
}

type RestoreSource string

// RestoreSource values
const (
RestoreSourceSnapshot = "Snapshot"
RestoreSourcePointInTime = "PointInTime"
)

Check failure on line 212 in apis/docdb/v1alpha1/custom_types.go

View workflow job for this annotation

GitHub Actions / lint

exported type `RestoreSource` should have comment or be unexported (golint)

// RestoreDBClusterBackupConfiguration defines the backup to restore a new DBCluster from.
type RestoreDBClusterBackupConfiguration struct {
// Snapshot specifies the details of the snapshot to restore from.
// +optional
Snapshot *SnapshotRestoreBackupConfiguration `json:"snapshot,omitempty"`
Snapshot *RestoreSnapshotConfiguration `json:"snapshot,omitempty"`

// PointInTime specifies the details of the point in time restore.
// +optional
PointInTime *PointInTimeRestoreDBClusterBackupConfiguration `json:"pointInTime,omitempty"`
PointInTime *RestorePointInTimeConfiguration `json:"pointInTime,omitempty"`

// Source is the type of the backup to restore when creating a new DBCluster or DBInstance.
// Snapshot and PointInTime are supported.
// +kubebuilder:validation:Enum=Snapshot;PointInTime
Source string `json:"source"`
Source RestoreSource `json:"source"`
}
76 changes: 33 additions & 43 deletions apis/docdb/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require (
k8s.io/api v0.26.1
k8s.io/apimachinery v0.26.1
k8s.io/client-go v0.26.1
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448
sigs.k8s.io/controller-runtime v0.14.1
sigs.k8s.io/controller-tools v0.11.1
sigs.k8s.io/yaml v1.3.0
Expand Down Expand Up @@ -153,7 +154,6 @@ require (
k8s.io/component-base v0.26.1 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
34 changes: 34 additions & 0 deletions pkg/clients/docdb/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type MockDocDBClient struct {
MockModifyDBClusterWithContext func(context.Context, *docdb.ModifyDBClusterInput, []request.Option) (*docdb.ModifyDBClusterOutput, error)
MockDeleteDBClusterWithContext func(context.Context, *docdb.DeleteDBClusterInput, []request.Option) (*docdb.DeleteDBClusterOutput, error)

MockRestoreDBClusterFromSnapshotWithContext func(context.Context, *docdb.RestoreDBClusterFromSnapshotInput, []request.Option) (*docdb.RestoreDBClusterFromSnapshotOutput, error)
MockRestoreDBClusterToPointInTimeWithContext func(context.Context, *docdb.RestoreDBClusterToPointInTimeInput, []request.Option) (*docdb.RestoreDBClusterToPointInTimeOutput, error)

Called MockDocDBClientCall
}

Expand Down Expand Up @@ -328,6 +331,34 @@ func (m *MockDocDBClient) DeleteDBClusterWithContext(ctx context.Context, i *doc
return m.MockDeleteDBClusterWithContext(ctx, i, opts)
}

// CallRestoreDBClusterFromSnapshotWithContext to log call
type CallRestoreDBClusterFromSnapshotWithContext struct {
Ctx aws.Context
I *docdb.RestoreDBClusterFromSnapshotInput
Opts []request.Option
}

// RestoreDBClusterFromSnapshotWithContext calls MockRestoreDBClusterFromSnapshotWithContext
func (m *MockDocDBClient) RestoreDBClusterFromSnapshotWithContext(ctx context.Context, i *docdb.RestoreDBClusterFromSnapshotInput, opts ...request.Option) (*docdb.RestoreDBClusterFromSnapshotOutput, error) {
m.Called.RestoreDBClusterFromSnapshotWithContext = append(m.Called.RestoreDBClusterFromSnapshotWithContext, &CallRestoreDBClusterFromSnapshotWithContext{Ctx: ctx, I: i, Opts: opts})

return m.MockRestoreDBClusterFromSnapshotWithContext(ctx, i, opts)
}

// CallRestoreDBClusterToPointInTimeWithContext to log call
type CallRestoreDBClusterToPointInTimeWithContext struct {
Ctx aws.Context
I *docdb.RestoreDBClusterToPointInTimeInput
Opts []request.Option
}

// RestoreDBClusterToPointInTimeWithContext calls MockRestoreDBClusterToPointInTimeWithContext
func (m *MockDocDBClient) RestoreDBClusterToPointInTimeWithContext(ctx context.Context, i *docdb.RestoreDBClusterToPointInTimeInput, opts ...request.Option) (*docdb.RestoreDBClusterToPointInTimeOutput, error) {
m.Called.RestoreDBClusterToPointInTimeWithContext = append(m.Called.RestoreDBClusterToPointInTimeWithContext, &CallRestoreDBClusterToPointInTimeWithContext{Ctx: ctx, I: i, Opts: opts})

return m.MockRestoreDBClusterToPointInTimeWithContext(ctx, i, opts)
}

// MockDocDBClientCall to log calls
type MockDocDBClientCall struct {
ListTagsForResource []*CallListTagsForResource
Expand All @@ -353,4 +384,7 @@ type MockDocDBClientCall struct {
CreateDBClusterWithContext []*CallCreateDBClusterWithContext
ModifyDBClusterWithContext []*CallModifyDBClusterWithContext
DeleteDBClusterWithContext []*CallDeleteDBClusterWithContext

RestoreDBClusterFromSnapshotWithContext []*CallRestoreDBClusterFromSnapshotWithContext
RestoreDBClusterToPointInTimeWithContext []*CallRestoreDBClusterToPointInTimeWithContext
}
27 changes: 13 additions & 14 deletions pkg/controller/docdb/dbcluster/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,16 @@ func (e *hooks) preCreate(ctx context.Context, cr *svcapitypes.DBCluster, obj *s

obj.MasterUserPassword = awsclient.String(pw)
if cr.Spec.ForProvider.RestoreFrom != nil {
switch *cr.Spec.ForProvider.RestoreFrom.Source {
case "Snapshot":
switch cr.Spec.ForProvider.RestoreFrom.Source {
case svcapitypes.RestoreSourceSnapshot:
input := generateRestoreDBClusterFromSnapshotInput(cr)
input.DBClusterIdentifier = obj.DBClusterIdentifier
input.VpcSecurityGroupIds = obj.VpcSecurityGroupIds

if _, err = e.client.RestoreDBClusterFromSnapshotWithContext(ctx, input); err != nil {
return errors.Wrap(err, errRestore)
}
case "PointInTime":
case svcapitypes.RestoreSourcePointInTime:
input := generateRestoreDBClusterToPointInTimeInput(cr)
input.DBClusterIdentifier = obj.DBClusterIdentifier
input.VpcSecurityGroupIds = obj.VpcSecurityGroupIds
Expand Down Expand Up @@ -308,7 +308,7 @@ func generateRestoreDBClusterFromSnapshotInput(cr *svcapitypes.DBCluster) *svcsd
}

if cr.Spec.ForProvider.RestoreFrom != nil && cr.Spec.ForProvider.RestoreFrom.Snapshot != nil {
res.SetSnapshotIdentifier(*cr.Spec.ForProvider.RestoreFrom.Snapshot.SnapshotIdentifier)
res.SetSnapshotIdentifier(cr.Spec.ForProvider.RestoreFrom.Snapshot.SnapshotIdentifier)
}

if cr.Spec.ForProvider.Tags != nil {
Expand All @@ -324,25 +324,24 @@ func generateRestoreDBClusterFromSnapshotInput(cr *svcapitypes.DBCluster) *svcsd
}

func generateRestoreDBClusterToPointInTimeInput(cr *svcapitypes.DBCluster) *svcsdk.RestoreDBClusterToPointInTimeInput { // nolint:gocyclo

p := cr.Spec.ForProvider
res := &svcsdk.RestoreDBClusterToPointInTimeInput{
DBSubnetGroupName: p.DBSubnetGroupName,
DeletionProtection: p.DeletionProtection,
EnableCloudwatchLogsExports: p.EnableCloudwatchLogsExports,
KmsKeyId: p.KMSKeyID,
Port: p.Port,
UseLatestRestorableTime: &p.RestoreFrom.PointInTime.UseLatestRestorableTime,
UseLatestRestorableTime: p.RestoreFrom.PointInTime.UseLatestRestorableTime,
VpcSecurityGroupIds: p.VPCSecurityGroupIDs,
}
if p.RestoreFrom.PointInTime != nil && p.RestoreFrom.PointInTime.RestoreTime != nil {
res.RestoreToTime = &p.RestoreFrom.PointInTime.RestoreTime.Time
}
if p.RestoreFrom.PointInTime != nil && p.RestoreFrom.PointInTime.RestoreTime != nil {
res.RestoreType = p.RestoreFrom.PointInTime.RestoreType
}
if p.RestoreFrom.PointInTime != nil && p.RestoreFrom.PointInTime.SourceDBClusterIdentifier != nil {
res.SourceDBClusterIdentifier = p.RestoreFrom.PointInTime.SourceDBClusterIdentifier
if p.RestoreFrom != nil {
if p.RestoreFrom.PointInTime != nil {
if p.RestoreFrom.PointInTime.RestoreTime != nil {
res.SetRestoreToTime(p.RestoreFrom.PointInTime.RestoreTime.Time)
}
res.RestoreType = p.RestoreFrom.PointInTime.RestoreType
res.SourceDBClusterIdentifier = &p.RestoreFrom.PointInTime.SourceDBClusterIdentifier
}
}
if cr.Spec.ForProvider.Tags != nil {
var tags []*svcsdk.Tag
Expand Down
Loading

0 comments on commit d67688d

Please sign in to comment.