Skip to content

WIP: logical backups #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions api/v1/zalandopostgres_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ type ZalandoPostgres struct {
}

type ZalandoPostgresSpec struct {
MaintenanceWindows []MaintenanceWindow `json:"maintenanceWindows,omitempty"`
NumberOfInstances int32 `json:"numberOfInstances"`
PostgresqlParam PostgresqlParam `json:"postgresql"`
Resources *Resources `json:"resources,omitempty"`
TeamID string `json:"teamId"`
Volume Volume `json:"volume"`
MaintenanceWindows []MaintenanceWindow `json:"maintenanceWindows,omitempty"`
NumberOfInstances int32 `json:"numberOfInstances"`
PostgresqlParam PostgresqlParam `json:"postgresql"`
Resources *Resources `json:"resources,omitempty"`
TeamID string `json:"teamId"`
Volume Volume `json:"volume"`
EnableLogicalBackup bool `json:"enableLogicalBackup,omitempty"`
}

type MaintenanceWindow struct {
Expand Down
16 changes: 15 additions & 1 deletion controllers/postgres_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ func (r *PostgresReconciler) createZalandoPostgresql(ctx context.Context, instan
}
}

// TODO make configurable
log.Info("Enabling logical backup", "zalando", z)
z.Spec.EnableLogicalBackup = true

u, err := z.ToUnstructured()
if err != nil {
log.Error(err, "error while converting to unstructured")
Expand Down Expand Up @@ -248,8 +252,18 @@ func (r *PostgresReconciler) ensureZalandoDependencies(ctx context.Context, p *p
backupSchedule := string(backupSecret.Data[pg.BackupSecretSchedule])
backupNumToRetain := string(backupSecret.Data[pg.BackupSecretRetention])

backupConf := operatormanager.LogicalBackupConfig{
S3Endpoint: awsEndpoint,
// TODO use different bucket for logical backups?
S3BucketName: bucketName,
S3AccessKeyID: awsAccessKeyID,
S3SecretAccessKey: awsSecretAccessKey,
// TODO use different schedule
Schedule: "*/5 * * * *",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also have the encryptionkey available if i remember correct

}

if !isInstalled {
_, err := r.InstallOperator(ctx, namespace, awsEndpoint+"/"+bucketName) // TODO check the s3BucketUrl...
_, err := r.InstallOperator(ctx, namespace, backupConf)
if err != nil {
return fmt.Errorf("error while installing zalando dependencies: %v", err)
}
Expand Down
25 changes: 18 additions & 7 deletions pkg/operatormanager/operatormanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func New(client client.Client, fileName string, scheme *runtime.Scheme, log logr
}

// InstallOperator installs the operator Stored in `OperatorManager`
func (m *OperatorManager) InstallOperator(ctx context.Context, namespace, s3BucketURL string) ([]runtime.Object, error) {
func (m *OperatorManager) InstallOperator(ctx context.Context, namespace string, backupConfig LogicalBackupConfig) ([]runtime.Object, error) {
objs := []runtime.Object{}

// Make sure the namespace exists.
Expand All @@ -97,7 +97,7 @@ func (m *OperatorManager) InstallOperator(ctx context.Context, namespace, s3Buck
return objs, fmt.Errorf("error while converting yaml to `runtime.Object`: %v", err)
}

if objs, err := m.createNewRuntimeObject(ctx, objs, obj, namespace, s3BucketURL); err != nil {
if objs, err := m.createNewRuntimeObject(ctx, objs, obj, namespace, backupConfig); err != nil {
return objs, fmt.Errorf("error while creating the `runtime.Object`: %v", err)
}
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func (m *OperatorManager) UninstallOperator(ctx context.Context, namespace strin
}

// createNewRuntimeObject adds namespace to obj and creates or patches it
func (m *OperatorManager) createNewRuntimeObject(ctx context.Context, objs []runtime.Object, obj runtime.Object, namespace, s3BucketURL string) ([]runtime.Object, error) {
func (m *OperatorManager) createNewRuntimeObject(ctx context.Context, objs []runtime.Object, obj runtime.Object, namespace string, backupConf LogicalBackupConfig) ([]runtime.Object, error) {
if err := m.ensureCleanMetadata(obj); err != nil {
return objs, fmt.Errorf("error while ensuring the metadata of the `runtime.Object` is clean: %v", err)
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func (m *OperatorManager) createNewRuntimeObject(ctx context.Context, objs []run
}
case *v1.ConfigMap:
m.Log.Info("handling ConfigMap")
m.editConfigMap(v, namespace, s3BucketURL)
m.editConfigMap(v, namespace, backupConf)
err = m.Get(ctx, key, &v1.ConfigMap{})
case *v1.Service:
m.Log.Info("handling Service")
Expand Down Expand Up @@ -292,10 +292,21 @@ func (m *OperatorManager) createNewRuntimeObject(ctx context.Context, objs []run
return objs, nil
}

type LogicalBackupConfig struct {
S3Endpoint string
S3BucketName string
S3AccessKeyID string
S3SecretAccessKey string
Schedule string
}

// editConfigMap adds info to cm
func (m *OperatorManager) editConfigMap(cm *v1.ConfigMap, namespace, s3BucketURL string) {
// TODO re-enable
// cm.Data["logical_backup_s3_bucket"] = s3BucketURL
func (m *OperatorManager) editConfigMap(cm *v1.ConfigMap, namespace string, config LogicalBackupConfig) {
cm.Data["logical_backup_s3_access_key_id"] = config.S3AccessKeyID
cm.Data["logical_backup_s3_secret_access_key"] = config.S3SecretAccessKey
cm.Data["logical_backup_s3_endpoint"] = config.S3Endpoint
cm.Data["logical_backup_s3_bucket"] = config.S3BucketName
cm.Data["logical_backup_schedule"] = config.Schedule
cm.Data["watched_namespace"] = namespace
// TODO don't use the same serviceaccount for operator and databases, see #88
cm.Data["pod_service_account_name"] = serviceAccountName
Expand Down