Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
backup: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ukinau committed Dec 18, 2018
1 parent fce86f1 commit d519aa7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/backup/writer/s3_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s3w *s3Writer) List(ctx context.Context, basePath string) ([]string, error
}
objectKeys := []string{}
for _, object := range objects.Contents {
objectKeys = append(objectKeys, bk+"/"+ *object.Key)
objectKeys = append(objectKeys, bk+"/"+*object.Key)
}
return objectKeys, nil
}
Expand Down
29 changes: 15 additions & 14 deletions pkg/controller/backup-operator/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ func (b *Backup) processItem(key string) error {
}
return nil
}
isPeriodic := isPeriodicBackup(&eb.Spec)

// don't process the CR if it has a status since
// having a status means that the backup is either made or failed.
if !isPeriodicBackup(&eb.Spec) &&
if !isPeriodic &&
(eb.Status.Succeeded || len(eb.Status.Reason) != 0) {
return nil
}

if isPeriodicBackup(&eb.Spec) && b.isChanged(eb) {
if isPeriodic && b.isChanged(eb) {
// Stop previous backup runner if it exists
b.deletePeriodicBackupRunner(eb.ObjectMeta.UID)

Expand All @@ -107,9 +109,9 @@ func (b *Backup) processItem(key string) error {
// Store cancel function for periodic
b.backupRunnerStore.Store(eb.ObjectMeta.UID, BackupRunner{eb.Spec, cancel})

} else if !isPeriodicBackup(&eb.Spec) {
} else if !isPeriodic {
// Perform backup
bs, err := b.handleBackup(nil, &eb.Spec)
bs, err := b.handleBackup(nil, &eb.Spec, false)
// Report backup status
b.reportBackupStatus(bs, err, eb)
}
Expand Down Expand Up @@ -146,10 +148,9 @@ func (b *Backup) addFinalizerOfPeriodicBackupIfNeed(eb *api.EtcdBackup) (*api.Et
if err != nil {
return eb, err
}
} else {
return eb, nil
return ebNew.(*api.EtcdBackup), nil
}
return ebNew.(*api.EtcdBackup), nil
return eb, nil
}

func (b *Backup) removeFinalizerOfPeriodicBackup(eb *api.EtcdBackup) error {
Expand Down Expand Up @@ -192,7 +193,7 @@ func (b *Backup) periodicRunnerFunc(ctx context.Context, t *time.Ticker, eb *api
break
}
// Perform backup
bs, err := b.handleBackup(&ctx, &latestEb.Spec)
bs, err := b.handleBackup(&ctx, &latestEb.Spec, true)
// Report backup status
b.reportBackupStatus(bs, err, latestEb)
}
Expand Down Expand Up @@ -240,7 +241,7 @@ func (b *Backup) handleErr(err error, key interface{}) {
b.logger.Infof("Dropping etcd backup (%v) out of the queue: %v", key, err)
}

func (b *Backup) handleBackup(parentContext *context.Context, spec *api.BackupSpec) (*api.BackupStatus, error) {
func (b *Backup) handleBackup(parentContext *context.Context, spec *api.BackupSpec, isPeriodic bool) (*api.BackupStatus, error) {
err := validate(spec)
if err != nil {
return nil, err
Expand All @@ -265,21 +266,21 @@ func (b *Backup) handleBackup(parentContext *context.Context, spec *api.BackupSp
switch spec.StorageType {
case api.BackupStorageTypeS3:
bs, err := handleS3(ctx, b.kubecli, spec.S3, spec.EtcdEndpoints, spec.ClientTLSSecret,
b.namespace, isPeriodicBackup(spec), backupMaxCount)
b.namespace, isPeriodic, backupMaxCount)
if err != nil {
return nil, err
}
return bs, nil
case api.BackupStorageTypeABS:
bs, err := handleABS(ctx, b.kubecli, spec.ABS, spec.EtcdEndpoints, spec.ClientTLSSecret,
b.namespace, isPeriodicBackup(spec), backupMaxCount)
b.namespace, isPeriodic, backupMaxCount)
if err != nil {
return nil, err
}
return bs, nil
case api.BackupStorageTypeGCS:
bs, err := handleGCS(ctx, b.kubecli, spec.GCS, spec.EtcdEndpoints, spec.ClientTLSSecret,
b.namespace, isPeriodicBackup(spec), backupMaxCount)
b.namespace, isPeriodic, backupMaxCount)
if err != nil {
return nil, err
}
Expand All @@ -297,10 +298,10 @@ func validate(spec *api.BackupSpec) error {
}
if spec.BackupPolicy != nil {
if spec.BackupPolicy.BackupIntervalInSecond < 0 {
return errors.New("spec.backupPolicy.backupIntervalInSecond should not be lower than 0")
return errors.New("spec.BackupPolicy.BackupIntervalInSecond should not be lower than 0")
}
if spec.BackupPolicy.MaxBackups < 0 {
return errors.New("spec.backupPolicy.MaxBackups should not be lower than 0")
return errors.New("spec.BackupPolicy.MaxBackups should not be lower than 0")
}
}
return nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/controller/backup-operator/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func generateTLSConfig(kubecli kubernetes.Interface, clientTLSSecret, namespace
func isPeriodicBackup(ebSpec *api.BackupSpec) bool {
if ebSpec.BackupPolicy != nil {
return ebSpec.BackupPolicy.BackupIntervalInSecond != 0
} else {
return false
}
return false
}

0 comments on commit d519aa7

Please sign in to comment.