Most of the state machine is implemented

This commit is contained in:
Jean-Marc ANDRE 2023-02-07 23:15:13 +01:00
parent 8f180f5f52
commit 06999eb553
3 changed files with 42 additions and 6 deletions

View File

@ -47,9 +47,7 @@ type Step struct {
type TargetContainer struct {
Name string `json:"name"`
Paths []string `json:"paths,omitempty"`
// +kubebuilder:default:=2
Retry int `json:"retry"`
Steps []Step `json:"steps,omitempty"`
Steps []Step `json:"steps,omitempty"`
}
type Target struct {
@ -57,6 +55,8 @@ type Target struct {
TargetKind `json:"targetKind"`
TargetName string `json:"targetName"`
Containers []TargetContainer `json:"containers"`
// +kubebuilder:default:=2
Retry int `json:"retry"`
}
type Keep struct {

View File

@ -53,9 +53,10 @@ type BackupSessionSpec struct {
// BackupSessionStatus defines the observed state of BackupSession
type BackupSessionStatus struct {
SessionState `json:"state"`
StartTime *metav1.Time `json:"startTime"`
Targets []TargetStatus `json:"target,omitempty"`
Keep string `json:"keep"`
StartTime *metav1.Time `json:"startTime"`
// +optional
Targets []TargetStatus `json:"target,omitempty"`
Keep string `json:"keep"`
}
//+kubebuilder:object:root=true

View File

@ -116,8 +116,43 @@ func (r *BackupSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
case formolv1alpha1.Running:
// Backup ongoing. Check the status of the last backup task and decide what to do next.
currentTargetStatus := &(backupSession.Status.Targets[len(backupSession.Status.Targets)-1])
switch currentTargetStatus.SessionState {
case formolv1alpha1.Running:
r.Log.V(0).Info("Current task is still running. Wait until it's finished")
case formolv1alpha1.Success:
r.Log.V(0).Info("Last backup task was a success. Start a new one")
if nextTargetStatus := r.startNextTask(&backupSession, backupConf); nextTargetStatus != nil {
r.Log.V(0).Info("Starting a new task", "task", nextTargetStatus)
} else {
r.Log.V(0).Info("No more tasks to start. The backup is a success. Let's do some cleanup")
backupSession.Status.SessionState = formolv1alpha1.Success
}
if err := r.Status().Update(ctx, &backupSession); err != nil {
r.Log.Error(err, "unable to update BackupSession")
}
return ctrl.Result{}, err
case formolv1alpha1.Failure:
// Last task failed. Try to run it again
if currentTargetStatus.Try < backupConf.Spec.Targets[len(backupSession.Status.Targets)-1].Retry {
r.Log.V(0).Info("Last task failed. Try to run it again")
currentTargetStatus.Try++
currentTargetStatus.SessionState = formolv1alpha1.New
currentTargetStatus.StartTime = &metav1.Time{Time: time.Now()}
} else {
r.Log.V(0).Info("Task failed again and for the last time")
backupSession.Status.SessionState = formolv1alpha1.Failure
}
if err := r.Status().Update(ctx, &backupSession); err != nil {
r.Log.Error(err, "unable to update BackupSession")
}
return ctrl.Result{}, err
}
case formolv1alpha1.Failure:
// Failed backup. Don't do anything anymore
case formolv1alpha1.Success:
// Backup was a success
default:
// BackupSession has just been created
backupSession.Status.SessionState = formolv1alpha1.New