Skip to content

Commit

Permalink
Merge pull request #458 from Feggah/feat/add-backup-retention-setting…
Browse files Browse the repository at this point in the history
…s-cloudsql

Add support for backupRetentionSettings in CloudSQLInstance
  • Loading branch information
Feggah authored Sep 28, 2022
2 parents d24f532 + c2f3a56 commit ad7a973
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
24 changes: 24 additions & 0 deletions apis/database/v1beta1/cloudsql_instance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ type MaintenanceWindow struct {

// BackupConfiguration is database instance backup configuration.
type BackupConfiguration struct {
// BackupRetentionSettings: Backup retention settings.
// +optional
BackupRetentionSettings *BackupRetentionSettings `json:"backupRetentionSettings,omitempty"`

// BinaryLogEnabled: Whether binary log is enabled. If backup
// configuration is disabled, binary log must be disabled as well.
// +optional
Expand Down Expand Up @@ -332,6 +336,26 @@ type BackupConfiguration struct {
PointInTimeRecoveryEnabled *bool `json:"pointInTimeRecoveryEnabled,omitempty"`
}

// BackupRetentionSettings configures the number of backups to retain.
type BackupRetentionSettings struct {
// RetainedBackups: Depending on the value of retention_unit, this is
// used to determine if a backup needs to be deleted. If retention_unit
// is 'COUNT', we will retain this many backups.
// +optional
RetainedBackups *int64 `json:"retainedBackups,omitempty"`

// RetentionUnit: The unit that 'retained_backups' represents.
//
// Possible values:
// "RETENTION_UNIT_UNSPECIFIED" - Backup retention unit is
// unspecified, will be treated as COUNT.
// "COUNT" - Retention will be by count, eg. "retain the most recent 7
// backups".
// +kubebuilder:validation:Enum=RETENTION_UNIT_UNSPECIFIED;COUNT
// +optional
RetentionUnit *string `json:"retentionUnit,omitempty"`
}

// DatabaseFlags are database flags for Cloud SQL instances.
type DatabaseFlags struct {
// Name: The name of the flag. These flags are passed at instance
Expand Down
30 changes: 30 additions & 0 deletions apis/database/v1beta1/zz_generated.deepcopy.go

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

22 changes: 22 additions & 0 deletions package/crds/database.gcp.crossplane.io_cloudsqlinstances.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,28 @@ spec:
description: BackupConfiguration is the daily backup configuration
for the instance.
properties:
backupRetentionSettings:
description: 'BackupRetentionSettings: Backup retention
settings.'
properties:
retainedBackups:
description: 'RetainedBackups: Depending on the value
of retention_unit, this is used to determine if
a backup needs to be deleted. If retention_unit
is ''COUNT'', we will retain this many backups.'
format: int64
type: integer
retentionUnit:
description: "RetentionUnit: The unit that 'retained_backups'
represents. \n Possible values: \"RETENTION_UNIT_UNSPECIFIED\"
- Backup retention unit is unspecified, will be
treated as COUNT. \"COUNT\" - Retention will be
by count, eg. \"retain the most recent 7 backups\"."
enum:
- RETENTION_UNIT_UNSPECIFIED
- COUNT
type: string
type: object
binaryLogEnabled:
description: 'BinaryLogEnabled: Whether binary log is
enabled. If backup configuration is disabled, binary
Expand Down
19 changes: 19 additions & 0 deletions pkg/clients/cloudsql/cloudsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ func GenerateDatabaseInstance(name string, in v1beta1.CloudSQLInstanceParameters
db.Settings.BackupConfiguration.ReplicationLogArchivingEnabled = gcp.BoolValue(in.Settings.BackupConfiguration.ReplicationLogArchivingEnabled)
db.Settings.BackupConfiguration.StartTime = gcp.StringValue(in.Settings.BackupConfiguration.StartTime)
db.Settings.BackupConfiguration.PointInTimeRecoveryEnabled = gcp.BoolValue(in.Settings.BackupConfiguration.PointInTimeRecoveryEnabled)

if in.Settings.BackupConfiguration.BackupRetentionSettings != nil {
if db.Settings.BackupConfiguration.BackupRetentionSettings == nil {
db.Settings.BackupConfiguration.BackupRetentionSettings = &sqladmin.BackupRetentionSettings{}
}
db.Settings.BackupConfiguration.BackupRetentionSettings.RetainedBackups = gcp.Int64Value(in.Settings.BackupConfiguration.BackupRetentionSettings.RetainedBackups)
db.Settings.BackupConfiguration.BackupRetentionSettings.RetentionUnit = gcp.StringValue(in.Settings.BackupConfiguration.BackupRetentionSettings.RetentionUnit)
}
}
if in.Settings.IPConfiguration != nil {
if db.Settings.IpConfiguration == nil {
Expand Down Expand Up @@ -247,6 +255,17 @@ func LateInitializeSpec(spec *v1beta1.CloudSQLInstanceParameters, in sqladmin.Da
spec.Settings.BackupConfiguration.PointInTimeRecoveryEnabled = gcp.LateInitializeBool(
spec.Settings.BackupConfiguration.PointInTimeRecoveryEnabled,
in.Settings.BackupConfiguration.PointInTimeRecoveryEnabled)
if in.Settings.BackupConfiguration.BackupRetentionSettings != nil {
if spec.Settings.BackupConfiguration.BackupRetentionSettings == nil {
spec.Settings.BackupConfiguration.BackupRetentionSettings = &v1beta1.BackupRetentionSettings{}
}
spec.Settings.BackupConfiguration.BackupRetentionSettings.RetainedBackups = gcp.LateInitializeInt64(
spec.Settings.BackupConfiguration.BackupRetentionSettings.RetainedBackups,
in.Settings.BackupConfiguration.BackupRetentionSettings.RetainedBackups)
spec.Settings.BackupConfiguration.BackupRetentionSettings.RetentionUnit = gcp.LateInitializeString(
spec.Settings.BackupConfiguration.BackupRetentionSettings.RetentionUnit,
in.Settings.BackupConfiguration.BackupRetentionSettings.RetentionUnit)
}
}
if in.Settings.IpConfiguration != nil {
if spec.Settings.IPConfiguration == nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/clients/cloudsql/cloudsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func params(m ...func(*v1beta1.CloudSQLInstanceParameters)) *v1beta1.CloudSQLIns
ReplicationLogArchivingEnabled: gcp.BoolPtr(true),
StartTime: gcp.StringPtr("20191018"),
PointInTimeRecoveryEnabled: gcp.BoolPtr(false),
BackupRetentionSettings: &v1beta1.BackupRetentionSettings{
RetainedBackups: gcp.Int64Ptr(7),
RetentionUnit: gcp.StringPtr("COUNT"),
},
},
IPConfiguration: &v1beta1.IPConfiguration{
AuthorizedNetworks: []*v1beta1.ACLEntry{
Expand Down Expand Up @@ -167,6 +171,10 @@ func db(m ...func(*sqladmin.DatabaseInstance)) *sqladmin.DatabaseInstance {
ReplicationLogArchivingEnabled: true,
StartTime: "20191018",
PointInTimeRecoveryEnabled: false,
BackupRetentionSettings: &sqladmin.BackupRetentionSettings{
RetainedBackups: 7,
RetentionUnit: "COUNT",
},
},
IpConfiguration: &sqladmin.IpConfiguration{
AuthorizedNetworks: []*sqladmin.AclEntry{
Expand Down

0 comments on commit ad7a973

Please sign in to comment.