Skip to content

Commit

Permalink
handle export retention
Browse files Browse the repository at this point in the history
  • Loading branch information
Heping authored and Heping committed Jan 19, 2022
1 parent 0ba6eb7 commit e7bfe79
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
1 change: 1 addition & 0 deletions api/v1alpha1/veleroexport_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type VeleroExportStatus struct {
VeleroBackupRef *corev1.ObjectReference `json:"veleroBackupRef,omitempty"`
StartTimestamp *metav1.Time `json:"startTimestamp,omitempty"`
CompletionTimestamp *metav1.Time `json:"completionTimestamp,omitempty"`
StopTimestamp *metav1.Time `json:"stopTimestamp,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
4 changes: 4 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/crd/bases/ys.jibudata.com_veleroexports.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ spec:
type: string
state:
type: string
stopTimestamp:
format: date-time
type: string
veleroBackupRef:
description: 'ObjectReference contains enough information to let you
inspect or modify the referred object. --- New uses of this type
Expand Down
68 changes: 45 additions & 23 deletions controllers/veleroexport_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,36 +128,37 @@ func (r *VeleroExportReconciler) Reconcile(ctx context.Context, req ctrl.Request

if veleroExport.Status.Phase == dmapi.PhaseCompleted ||
veleroExport.Status.State == dmapi.StateFailed {
// do nothing
return ctrl.Result{}, nil
}
_ = r.validatePolicy(veleroExport)

// check retention
if veleroExport.Spec.Policy.Retention == time.Duration(0) {
veleroExport.Spec.Policy.Retention = time.Duration(24)
}
retention := veleroExport.Spec.Policy.Retention * time.Hour
if veleroExport.Status.StartTimestamp != nil {
stop := veleroExport.Status.StartTimestamp.Time.Add(retention)
stopTime := veleroExport.Status.StopTimestamp.Time
now := time.Now()
if time.Now().After(stop) {
r.Log.Info("backupjob expired", "now", now)
if now.After(stopTime) {
r.Log.Info("velero export expired", "now", now)
err = r.deleteVeleroExport(veleroExport)
if err != nil {
return ctrl.Result{Requeue: true}, err
}
return ctrl.Result{}, nil
} else {
duration := stopTime.Sub(now)
return ctrl.Result{RequeueAfter: duration}, nil
}
}
_ = r.validatePolicy(veleroExport)

// check retention
if veleroExport.Spec.Policy.Retention == time.Duration(0) {
veleroExport.Spec.Policy.Retention = time.Duration(24)
}

if veleroExport.Status.Phase == dmapi.PhaseCreated {
r.Log.Info(
"snapshot export started",
"retention",
int32(veleroExport.Spec.Policy.Retention),
)
r.updateStatus(r.Client, veleroExport, nil)
err = r.updateStatus(r.Client, veleroExport, nil)
if err != nil {
return ctrl.Result{Requeue: true}, err
}
}

// precheck
Expand All @@ -167,7 +168,10 @@ func (r *VeleroExportReconciler) Reconcile(ctx context.Context, req ctrl.Request
if err != nil && errors.IsConflict(err) {
// do nothing
} else {
r.updateStatus(r.Client, veleroExport, err)
err = r.updateStatus(r.Client, veleroExport, err)
if err != nil {
return ctrl.Result{Requeue: true}, err
}
}
return ctrl.Result{Requeue: true}, err
}
Expand All @@ -182,10 +186,16 @@ func (r *VeleroExportReconciler) Reconcile(ctx context.Context, req ctrl.Request
// do nothing
return ctrl.Result{Requeue: true}, err
} else if err != nil {
r.updateStatus(r.Client, veleroExport, err)
err = r.updateStatus(r.Client, veleroExport, err)
if err != nil {
return ctrl.Result{Requeue: true}, nil
}
}
}
r.updateStatus(r.Client, veleroExport, nil)
err = r.updateStatus(r.Client, veleroExport, nil)
if err != nil {
return ctrl.Result{Requeue: true}, err
}
}

if veleroExport.Status.Phase == dmapi.PhaseWaitPrepareComplete {
Expand All @@ -199,7 +209,10 @@ func (r *VeleroExportReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{Requeue: true}, nil
}
}
r.updateStatus(r.Client, veleroExport, nil)
err = r.updateStatus(r.Client, veleroExport, nil)
if err != nil {
return ctrl.Result{Requeue: true}, err
}
}

// create temp namespaces
Expand All @@ -208,11 +221,17 @@ func (r *VeleroExportReconciler) Reconcile(ctx context.Context, req ctrl.Request
tmpNamespace := config.TempNamespacePrefix + namespace
err = opt.CreateNamespace(tmpNamespace, false)
if err != nil {
r.updateStatus(r.Client, veleroExport, err)
err = r.updateStatus(r.Client, veleroExport, err)
if err != nil {
return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{Requeue: true}, err
}
}
r.updateStatus(r.Client, veleroExport, nil)
err = r.updateStatus(r.Client, veleroExport, nil)
if err != nil {
return ctrl.Result{Requeue: true}, err
}
}

if veleroExport.Status.Phase == dmapi.PhaseCreateVolumeSnapshot {
Expand Down Expand Up @@ -520,7 +539,7 @@ func (r *VeleroExportReconciler) updateVeleroExportLabel(client k8sclient.Client
return nil
}

func (r *VeleroExportReconciler) updateStatus(client k8sclient.Client, veleroExport *dmapi.VeleroExport, err error) {
func (r *VeleroExportReconciler) updateStatus(client k8sclient.Client, veleroExport *dmapi.VeleroExport, err error) error {
if err != nil {
veleroExport.Status.Message = err.Error()
veleroExport.Status.State = dmapi.StateFailed
Expand All @@ -529,6 +548,8 @@ func (r *VeleroExportReconciler) updateStatus(client k8sclient.Client, veleroExp
veleroExport.Status.Message = ""
if veleroExport.Status.Phase == dmapi.PhaseCreated {
veleroExport.Status.StartTimestamp = &metav1.Time{Time: time.Now()}
retention := veleroExport.Spec.Policy.Retention * time.Hour
veleroExport.Status.StopTimestamp = &metav1.Time{Time: veleroExport.Status.StartTimestamp.Time.Add(retention)}
}
veleroExport.Status.Phase = r.nextPhase(veleroExport.Status.Phase)
if veleroExport.Status.Phase == dmapi.GetLastPhase(veleroExportSteps) {
Expand All @@ -538,8 +559,9 @@ func (r *VeleroExportReconciler) updateStatus(client k8sclient.Client, veleroExp
veleroExport.Status.State = dmapi.StateInProgress
}
}
_ = r.Client.Status().Update(context.TODO(), veleroExport)
result := r.Client.Status().Update(context.TODO(), veleroExport)
r.Log.Info("snapshot export status update", "phase", veleroExport.Status.Phase, "state", veleroExport.Status.State)
return result
}

func (r *VeleroExportReconciler) precheck(client k8sclient.Client, veleroExport *dmapi.VeleroExport, opt *ops.Operation) error {
Expand Down

0 comments on commit e7bfe79

Please sign in to comment.