From 8f180f5f52f0c6bde2b890e00a409b533be2ae08 Mon Sep 17 00:00:00 2001 From: Jean-Marc ANDRE Date: Tue, 7 Feb 2023 17:53:58 +0100 Subject: [PATCH] Moved BackupSession to Running. Set the first task to New. --- api/v1alpha1/backupsession_types.go | 4 ++-- controllers/backupsession_controller.go | 19 +++++++++++++++++++ .../backupsession_controller_helpers.go | 9 ++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/api/v1alpha1/backupsession_types.go b/api/v1alpha1/backupsession_types.go index 836f866..6ba8c27 100644 --- a/api/v1alpha1/backupsession_types.go +++ b/api/v1alpha1/backupsession_types.go @@ -41,7 +41,7 @@ type TargetStatus struct { SessionState `json:"state"` SnapshotId string `json:"snapshotId"` StartTime *metav1.Time `json:"startTime"` - Duration *metav1.Duration `json:"duration"` + Duration *metav1.Duration `json:"duration,omitempty"` Try int `json:"try"` } @@ -54,7 +54,7 @@ type BackupSessionSpec struct { type BackupSessionStatus struct { SessionState `json:"state"` StartTime *metav1.Time `json:"startTime"` - Targets []TargetStatus `json:"target"` + Targets []TargetStatus `json:"target,omitempty"` Keep string `json:"keep"` } diff --git a/controllers/backupsession_controller.go b/controllers/backupsession_controller.go index 3f09074..cf0fc62 100644 --- a/controllers/backupsession_controller.go +++ b/controllers/backupsession_controller.go @@ -99,6 +99,25 @@ func (r *BackupSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reques RequeueAfter: 30 * time.Second, }, nil } + if nextTargetStatus := r.startNextTask(&backupSession, backupConf); nextTargetStatus != nil { + r.Log.V(0).Info("New backup. Start the first task", "task", nextTargetStatus) + backupSession.Status.SessionState = formolv1alpha1.Running + if err := r.Status().Update(ctx, &backupSession); err != nil { + r.Log.Error(err, "unable to update BackupSession status") + } + return ctrl.Result{}, err + } else { + r.Log.V(0).Info("No first target? That should not happen. Mark the backup has failed") + backupSession.Status.SessionState = formolv1alpha1.Failure + if err := r.Status().Update(ctx, &backupSession); err != nil { + r.Log.Error(err, "unable to update BackupSession status") + } + return ctrl.Result{}, err + } + case formolv1alpha1.Running: + // Backup ongoing. Check the status of the last backup task and decide what to do next. + case formolv1alpha1.Failure: + // Failed backup. Don't do anything anymore default: // BackupSession has just been created backupSession.Status.SessionState = formolv1alpha1.New diff --git a/controllers/backupsession_controller_helpers.go b/controllers/backupsession_controller_helpers.go index ef4596e..86d678c 100644 --- a/controllers/backupsession_controller_helpers.go +++ b/controllers/backupsession_controller_helpers.go @@ -38,7 +38,7 @@ func (r *BackupSessionReconciler) isBackupOngoing(backupConf formolv1alpha1.Back return len(backupSessionList.Items) > 0 } -func (r *BackupSessionReconciler) startNextTask(backupSession formolv1alpha1.BackupSession, backupConf formolv1alpha1.BackupConfiguration) (*formolv1alpha1.TargetStatus, error) { +func (r *BackupSessionReconciler) startNextTask(backupSession *formolv1alpha1.BackupSession, backupConf formolv1alpha1.BackupConfiguration) *formolv1alpha1.TargetStatus { nextTargetIndex := len(backupSession.Status.Targets) if nextTargetIndex < len(backupConf.Spec.Targets) { nextTarget := backupConf.Spec.Targets[nextTargetIndex] @@ -51,13 +51,16 @@ func (r *BackupSessionReconciler) startNextTask(backupSession formolv1alpha1.Bac Try: 1, } switch nextTarget.BackupType { + case formolv1alpha1.OnlineKind: + r.Log.V(0).Info("Starts a new OnlineKind task", "target", nextTarget) case formolv1alpha1.JobKind: r.Log.V(0).Info("Starts a new JobKind task", "target", nextTarget) case formolv1alpha1.SnapshotKind: r.Log.V(0).Info("Starts a new SnapshotKind task", "target", nextTarget) } - return &nextTargetStatus, nil + backupSession.Status.Targets = append(backupSession.Status.Targets, nextTargetStatus) + return &nextTargetStatus } else { - return nil, nil + return nil } }