Skip to content

Commit

Permalink
feat: support image argument in ExportRDSSnapshotToLocation and Res…
Browse files Browse the repository at this point in the history
…toreRDSSnapshot (#3019)
  • Loading branch information
hairyhum authored Aug 7, 2024
1 parent 4feb7d1 commit d4be096
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
2 changes: 2 additions & 0 deletions docs/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ Arguments:
`databases`, No, `[]string`, list of databases to take backup of
`securityGroupID`, No, `[]string`, list of ``securityGroupID`` to be passed to temporary RDS instance. ()
`dbSubnetGroup`, No, `string`, DB Subnet Group to be passed to temporary RDS instance
`image`, No, `string`, kanister-tools image to be used for running export job

.. note::
- If ``databases`` argument is not set, backup of all the databases will be taken.
Expand Down Expand Up @@ -1080,6 +1081,7 @@ Arguments:
`namespace`, No, `string`, namespace in which to execute. Required if ``snapshotID`` is nil
`dbEngine`, No, `string`, one of the RDS db engines. Supported engines: ``PostgreSQL`` ``aurora`` ``aurora-mysql`` and ``aurora-postgresql``. Required if ``snapshotID`` is nil or Aurora is run in RDS instance
`dbSubnetGroup`, No, `string`, DB Subnet Group to be passed to restored RDS instance
`image`, No, `string`, kanister-tools image to be used for running restore. Only relevant when restoring from data dump (if `snapshotID` is empty)

.. note::
- If ``snapshotID`` is not set, restore will be done from data dump. In that case ``backupID`` `arg` is required.
Expand Down
2 changes: 2 additions & 0 deletions docs_new/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ Arguments:
| databases | No | []string | list of databases to take backup of |
| securityGroupID | No | []string | list of `securityGroupID` to be passed to temporary RDS instance |
| dbSubnetGroup | No | string | DB Subnet Group to be passed to temporary RDS instance |
| image | No | string | kanister-tools image to be used for running export job |

::: tip NOTE

Expand Down Expand Up @@ -954,6 +955,7 @@ Arguments:
| namespace | No | string | namespace in which to execute. Required if `snapshotID` is nil |
| dbEngine | No | string | one of the RDS db engines. Supported engines: `PostgreSQL`, `aurora`, `aurora-mysql` and `aurora-postgresql`. Required if `snapshotID` is nil or Aurora is run in RDS instance |
| dbSubnetGroup | No | string | DB Subnet Group to be passed to restored RDS instance |
| image | No | string | kanister-tools image to be used for running restore, only relevant when restoring from data dump (if `snapshotID` is empty) |

::: tip NOTE

Expand Down
30 changes: 18 additions & 12 deletions pkg/function/export_rds_snapshot_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ const (
ExportRDSSnapshotToLocSecGrpIDArg = "securityGroupID"
ExportRDSSnapshotToLocBackupID = "backupID"
ExportRDSSnapshotToLocDBSubnetGroupArg = "dbSubnetGroup"
ExportRDSSnapshotToLocImageArg = "image"

PostgrSQLEngine RDSDBEngine = "PostgreSQL"

BackupAction RDSAction = "backup"
RestoreAction RDSAction = "restore"

postgresToolsImage = "ghcr.io/kanisterio/postgres-kanister-tools:0.110.0"
defaultPostgresToolsImage = "ghcr.io/kanisterio/postgres-kanister-tools:0.110.0"
)

type exportRDSSnapshotToLocationFunc struct {
Expand Down Expand Up @@ -95,6 +96,7 @@ func exportRDSSnapshotToLoc(
dbEngine RDSDBEngine,
sgIDs []string,
profile *param.Profile,
postgresToolsImage string,
) (map[string]interface{}, error) {
// Validate profilextractDumpFromDBe
if err := ValidateProfile(profile); err != nil {
Expand Down Expand Up @@ -150,7 +152,7 @@ func exportRDSSnapshotToLoc(
}

// Extract dump from DB
output, err := execDumpCommand(ctx, dbEngine, BackupAction, namespace, dbEndpoint, username, password, databases, backupPrefix, backupID, profile, dbEngineVersion)
output, err := execDumpCommand(ctx, dbEngine, BackupAction, namespace, dbEndpoint, username, password, databases, backupPrefix, backupID, profile, dbEngineVersion, postgresToolsImage)
if err != nil {
return nil, errors.Wrap(err, "Unable to extract and push db dump to location")
}
Expand All @@ -174,7 +176,7 @@ func (e *exportRDSSnapshotToLocationFunc) Exec(ctx context.Context, tp param.Tem
e.progressPercent = progress.StartedPercent
defer func() { e.progressPercent = progress.CompletedPercent }()

var namespace, instanceID, snapshotID, username, password, dbSubnetGroup, backupArtifact string
var namespace, instanceID, snapshotID, username, password, dbSubnetGroup, backupArtifact, postgresToolsImage string
var dbEngine RDSDBEngine

if err := Arg(args, ExportRDSSnapshotToLocNamespaceArg, &namespace); err != nil {
Expand All @@ -201,6 +203,9 @@ func (e *exportRDSSnapshotToLocationFunc) Exec(ctx context.Context, tp param.Tem
if err := OptArg(args, ExportRDSSnapshotToLocDBSubnetGroupArg, &dbSubnetGroup, "default"); err != nil {
return nil, err
}
if err := OptArg(args, ExportRDSSnapshotToLocImageArg, &postgresToolsImage, defaultPostgresToolsImage); err != nil {
return nil, err
}
// Find databases
databases, err := GetYamlList(args, ExportRDSSnapshotToLocDatabasesArg)
if err != nil {
Expand All @@ -213,7 +218,7 @@ func (e *exportRDSSnapshotToLocationFunc) Exec(ctx context.Context, tp param.Tem
return nil, err
}

return exportRDSSnapshotToLoc(ctx, namespace, instanceID, snapshotID, username, password, databases, dbSubnetGroup, backupArtifact, dbEngine, sgIDs, tp.Profile)
return exportRDSSnapshotToLoc(ctx, namespace, instanceID, snapshotID, username, password, databases, dbSubnetGroup, backupArtifact, dbEngine, sgIDs, tp.Profile, postgresToolsImage)
}

func (*exportRDSSnapshotToLocationFunc) RequiredArgs() []string {
Expand Down Expand Up @@ -269,13 +274,14 @@ func execDumpCommand(
backupID string,
profile *param.Profile,
dbEngineVersion string,
postgresToolsImage string,
) (map[string]interface{}, error) {
// Trim "\n" from creds
username = strings.TrimSpace(username)
password = strings.TrimSpace(password)

// Prepare and execute command with kubetask
command, image, err := prepareCommand(ctx, dbEngine, action, dbEndpoint, username, password, databases, backupPrefix, backupID, profile, dbEngineVersion)
command, err := prepareCommand(ctx, dbEngine, action, dbEndpoint, username, password, databases, backupPrefix, backupID, profile, dbEngineVersion)
if err != nil {
return nil, err
}
Expand All @@ -299,7 +305,7 @@ func execDumpCommand(
}
}()

return kubeTask(ctx, cli, namespace, image, command, injectPostgresSecrets(secretName))
return kubeTask(ctx, cli, namespace, postgresToolsImage, command, injectPostgresSecrets(secretName))
}

func prepareCommand(
Expand All @@ -314,11 +320,11 @@ func prepareCommand(
backupID string,
profile *param.Profile,
dbEngineVersion string,
) ([]string, string, error) {
) ([]string, error) {
// Convert profile object into json
profileJSON, err := json.Marshal(profile)
if err != nil {
return nil, "", err
return nil, err
}

if dbEngine == PostgrSQLEngine {
Expand All @@ -328,17 +334,17 @@ func prepareCommand(
if dbList == nil {
dbList, err = findDBList(ctx, dbEndpoint, username, password)
if err != nil {
return nil, "", err
return nil, err
}
}
command, err := postgresBackupCommand(dbEndpoint, username, password, dbList, backupPrefix, backupID, profileJSON)
return command, postgresToolsImage, err
return command, err
case RestoreAction:
command, err := postgresRestoreCommand(dbEndpoint, username, password, backupPrefix, backupID, profileJSON, dbEngineVersion)
return command, postgresToolsImage, err
return command, err
}
}
return nil, "", errors.New("Invalid RDSDBEngine or RDSAction")
return nil, errors.New("Invalid RDSDBEngine or RDSAction")
}

func findDBList(ctx context.Context, dbEndpoint, username, password string) ([]string, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/function/rds_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (s *RDSFunctionsTest) TestPrepareCommand(c *C) {
}

for _, tc := range testCases {
outCommand, _, err := prepareCommand(context.Background(), tc.dbEngine, tc.action, tc.dbEndpoint, tc.username, tc.password, tc.dbList, tc.backupPrefix, tc.backupID, tc.tp.Profile, tc.dbEngineVersion)
outCommand, err := prepareCommand(context.Background(), tc.dbEngine, tc.action, tc.dbEndpoint, tc.username, tc.password, tc.dbList, tc.backupPrefix, tc.backupID, tc.tp.Profile, tc.dbEngineVersion)

c.Check(err, tc.errChecker, Commentf("Case %s failed", tc.name))
c.Assert(outCommand, DeepEquals, tc.command)
Expand Down
12 changes: 9 additions & 3 deletions pkg/function/restore_rds_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const (
RestoreRDSSnapshotUsername = "username"
// RestoreRDSSnapshotPassword stores the password of the database
RestoreRDSSnapshotPassword = "password"
// RestoreRDSSnapshotImage provides the image of the container with required tools
RestoreRDSSnapshotImage = "image"

// PostgreSQLEngine stores the postgres appname
PostgreSQLEngine RDSDBEngine = "PostgreSQL"
Expand Down Expand Up @@ -119,7 +121,7 @@ func (r *restoreRDSSnapshotFunc) Exec(ctx context.Context, tp param.TemplatePara
r.progressPercent = progress.StartedPercent
defer func() { r.progressPercent = progress.CompletedPercent }()

var namespace, instanceID, subnetGroup, snapshotID, backupArtifactPrefix, backupID, username, password string
var namespace, instanceID, subnetGroup, snapshotID, backupArtifactPrefix, backupID, username, password, postgresToolsImage string
var dbEngine RDSDBEngine

if err := Arg(args, RestoreRDSSnapshotInstanceID, &instanceID); err != nil {
Expand All @@ -136,6 +138,9 @@ func (r *restoreRDSSnapshotFunc) Exec(ctx context.Context, tp param.TemplatePara
if err := OptArg(args, RestoreRDSSnapshotDBSubnetGroup, &subnetGroup, "default"); err != nil {
return nil, err
}
if err := OptArg(args, RestoreRDSSnapshotImage, &postgresToolsImage, defaultPostgresToolsImage); err != nil {
return nil, err
}
// Find security groups
sgIDs, err := GetYamlList(args, RestoreRDSSnapshotSecGrpID)
if err != nil {
Expand All @@ -162,7 +167,7 @@ func (r *restoreRDSSnapshotFunc) Exec(ctx context.Context, tp param.TemplatePara
}
}

return restoreRDSSnapshot(ctx, namespace, instanceID, subnetGroup, snapshotID, backupArtifactPrefix, backupID, username, password, dbEngine, sgIDs, tp.Profile)
return restoreRDSSnapshot(ctx, namespace, instanceID, subnetGroup, snapshotID, backupArtifactPrefix, backupID, username, password, dbEngine, sgIDs, tp.Profile, postgresToolsImage)
}

func (r *restoreRDSSnapshotFunc) ExecutionProgress() (crv1alpha1.PhaseProgress, error) {
Expand All @@ -186,6 +191,7 @@ func restoreRDSSnapshot(
dbEngine RDSDBEngine,
sgIDs []string,
profile *param.Profile,
postgresToolsImage string,
) (map[string]interface{}, error) {
// Validate profile
if err := ValidateProfile(profile); err != nil {
Expand Down Expand Up @@ -233,7 +239,7 @@ func restoreRDSSnapshot(
return nil, errors.Wrapf(err, "Couldn't find DBInstance Version")
}

if _, err = execDumpCommand(ctx, dbEngine, RestoreAction, namespace, dbEndpoint, username, password, nil, backupArtifactPrefix, backupID, profile, dbEngineVersion); err != nil {
if _, err = execDumpCommand(ctx, dbEngine, RestoreAction, namespace, dbEndpoint, username, password, nil, backupArtifactPrefix, backupID, profile, dbEngineVersion, postgresToolsImage); err != nil {
return nil, errors.Wrapf(err, "Failed to restore RDS from dump. InstanceID=%s", instanceID)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
features: Support ``image`` argument for ``ExportRDSSnapshotToLocation`` and ``RestoreRDSSnapshot`` functions to override default postgres-kanister-tools image.

0 comments on commit d4be096

Please sign in to comment.