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

Commit

Permalink
Add UpdateFunc and fix duplicate backups (#213)
Browse files Browse the repository at this point in the history
Signed-off-by: SimonLi <[email protected]>
  • Loading branch information
Simon-Li authored and prydie committed Aug 31, 2018
1 parent ee732cf commit f9ccd3b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pkg/controllers/backup/agent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,19 @@ func NewAgentController(
cache.ResourceEventHandlerFuncs{
UpdateFunc: func(oldObj, newObj interface{}) {
new := newObj.(*v1alpha1.Backup)
_, cond := backuputil.GetBackupCondition(&new.Status, v1alpha1.BackupScheduled)

_, cond := backuputil.GetBackupCondition(&new.Status, v1alpha1.BackupComplete)
if cond != nil && cond.Status == corev1.ConditionTrue {
glog.V(2).Infof("Backup %q is Complete, skipping.", kubeutil.NamespaceAndName(new))
return
}
_, cond = backuputil.GetBackupCondition(&new.Status, v1alpha1.BackupRunning)
if cond != nil && cond.Status == corev1.ConditionTrue {
glog.V(2).Infof("Backup %q is Running, skipping.", kubeutil.NamespaceAndName(new))
return
}

_, cond = backuputil.GetBackupCondition(&new.Status, v1alpha1.BackupScheduled)
if cond != nil && cond.Status == corev1.ConditionTrue && new.Spec.ScheduledMember == c.podName {
key, err := cache.MetaNamespaceKeyFunc(new)
if err != nil {
Expand Down Expand Up @@ -342,6 +354,16 @@ func (controller *AgentController) performBackup(backup *v1alpha1.Backup, creds

finished := time.Now()

// Get (updated) resource from store.
backup, err = controller.backupLister.Backups(backup.GetNamespace()).Get(backup.GetName())
if err != nil {
return errors.Wrap(err, "error getting Backup")
}
// Don't modify items in the cache.
backup = backup.DeepCopy()
// Set defaults (incl. operator version label).
backup = backup.EnsureDefaults()

backup.Status.TimeStarted = metav1.Time{Time: started}
backup.Status.TimeCompleted = metav1.Time{Time: finished}
backup.Status.Outcome = v1alpha1.BackupOutcome{Location: key}
Expand Down
24 changes: 24 additions & 0 deletions pkg/controllers/backup/operator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,30 @@ func NewOperatorController(
}
c.queue.Add(key)
},
UpdateFunc: func(oldObj, newObj interface{}) {
new := newObj.(*v1alpha1.Backup)

_, cond := backuputil.GetBackupCondition(&new.Status, v1alpha1.BackupComplete)
if cond != nil && cond.Status == corev1.ConditionTrue {
glog.V(4).Infof("Backup %q is Complete, skipping.", kubeutil.NamespaceAndName(new))
return
}

_, cond = backuputil.GetBackupCondition(&new.Status, v1alpha1.BackupScheduled)
if cond != nil && cond.Status == corev1.ConditionTrue {
glog.V(4).Infof("Backup %q is already scheduled on Cluster member %q",
kubeutil.NamespaceAndName(new), new.Spec.ScheduledMember)
return
}

key, err := cache.MetaNamespaceKeyFunc(new)
if err != nil {
glog.Errorf("Error creating queue key, item not added to queue: %v", err)
return
}
c.queue.Add(key)
glog.V(4).Infof("Backup %q queued", kubeutil.NamespaceAndName(new))
},
},
)

Expand Down

0 comments on commit f9ccd3b

Please sign in to comment.