|
| 1 | +package backupcontroller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 10 | + "k8s.io/apimachinery/pkg/api/meta" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/apimachinery/pkg/types" |
| 14 | + "k8s.io/client-go/dynamic" |
| 15 | + "k8s.io/client-go/rest" |
| 16 | + "k8s.io/client-go/tools/record" |
| 17 | + ctrl "sigs.k8s.io/controller-runtime" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client/apiutil" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 21 | + |
| 22 | + strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1" |
| 23 | + backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1" |
| 24 | +) |
| 25 | + |
| 26 | +// RestoreJobReconciler reconciles RestoreJob objects. |
| 27 | +// It routes RestoreJobs to strategy-specific handlers based on the strategy |
| 28 | +// referenced in the Backup that the RestoreJob is restoring from. |
| 29 | +type RestoreJobReconciler struct { |
| 30 | + client.Client |
| 31 | + dynamic.Interface |
| 32 | + meta.RESTMapper |
| 33 | + Scheme *runtime.Scheme |
| 34 | + Recorder record.EventRecorder |
| 35 | +} |
| 36 | + |
| 37 | +func (r *RestoreJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 38 | + logger := log.FromContext(ctx) |
| 39 | + logger.Info("reconciling RestoreJob", "namespace", req.Namespace, "name", req.Name) |
| 40 | + |
| 41 | + restoreJob := &backupsv1alpha1.RestoreJob{} |
| 42 | + err := r.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: req.Name}, restoreJob) |
| 43 | + if err != nil { |
| 44 | + if apierrors.IsNotFound(err) { |
| 45 | + logger.V(1).Info("RestoreJob not found, skipping") |
| 46 | + return ctrl.Result{}, nil |
| 47 | + } |
| 48 | + logger.Error(err, "failed to get RestoreJob") |
| 49 | + return ctrl.Result{}, err |
| 50 | + } |
| 51 | + |
| 52 | + // If already completed, no need to reconcile |
| 53 | + if restoreJob.Status.Phase == backupsv1alpha1.RestoreJobPhaseSucceeded || |
| 54 | + restoreJob.Status.Phase == backupsv1alpha1.RestoreJobPhaseFailed { |
| 55 | + logger.V(1).Info("RestoreJob already completed, skipping", "phase", restoreJob.Status.Phase) |
| 56 | + return ctrl.Result{}, nil |
| 57 | + } |
| 58 | + |
| 59 | + // Step 1: Fetch the referenced Backup |
| 60 | + backup := &backupsv1alpha1.Backup{} |
| 61 | + backupKey := types.NamespacedName{Namespace: req.Namespace, Name: restoreJob.Spec.BackupRef.Name} |
| 62 | + if err := r.Get(ctx, backupKey, backup); err != nil { |
| 63 | + return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf("failed to get Backup: %v", err)) |
| 64 | + } |
| 65 | + |
| 66 | + // Step 2: Determine effective strategy from backup.spec.strategyRef |
| 67 | + if backup.Spec.StrategyRef.APIGroup == nil { |
| 68 | + return r.markRestoreJobFailed(ctx, restoreJob, "Backup has nil StrategyRef.APIGroup") |
| 69 | + } |
| 70 | + |
| 71 | + if *backup.Spec.StrategyRef.APIGroup != strategyv1alpha1.GroupVersion.Group { |
| 72 | + return r.markRestoreJobFailed(ctx, restoreJob, |
| 73 | + fmt.Sprintf("StrategyRef.APIGroup doesn't match: %s", *backup.Spec.StrategyRef.APIGroup)) |
| 74 | + } |
| 75 | + |
| 76 | + logger.Info("processing RestoreJob", "restorejob", restoreJob.Name, "backup", backup.Name, "strategyKind", backup.Spec.StrategyRef.Kind) |
| 77 | + switch backup.Spec.StrategyRef.Kind { |
| 78 | + case strategyv1alpha1.JobStrategyKind: |
| 79 | + return r.reconcileJobRestore(ctx, restoreJob, backup) |
| 80 | + case strategyv1alpha1.VeleroStrategyKind: |
| 81 | + return r.reconcileVeleroRestore(ctx, restoreJob, backup) |
| 82 | + default: |
| 83 | + return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf("StrategyRef.Kind not supported: %s", backup.Spec.StrategyRef.Kind)) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// SetupWithManager registers our controller with the Manager and sets up watches. |
| 88 | +func (r *RestoreJobReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 89 | + cfg := mgr.GetConfig() |
| 90 | + var err error |
| 91 | + if r.Interface, err = dynamic.NewForConfig(cfg); err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + var h *http.Client |
| 95 | + if h, err = rest.HTTPClientFor(cfg); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + if r.RESTMapper, err = apiutil.NewDynamicRESTMapper(cfg, h); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + return ctrl.NewControllerManagedBy(mgr). |
| 102 | + For(&backupsv1alpha1.RestoreJob{}). |
| 103 | + Complete(r) |
| 104 | +} |
| 105 | + |
| 106 | +// getTargetApplicationRef determines the effective target application reference. |
| 107 | +// According to DESIGN.md, if spec.targetApplicationRef is omitted, drivers SHOULD |
| 108 | +// restore into backup.spec.applicationRef. |
| 109 | +func (r *RestoreJobReconciler) getTargetApplicationRef(restoreJob *backupsv1alpha1.RestoreJob, backup *backupsv1alpha1.Backup) corev1.TypedLocalObjectReference { |
| 110 | + if restoreJob.Spec.TargetApplicationRef != nil { |
| 111 | + return *restoreJob.Spec.TargetApplicationRef |
| 112 | + } |
| 113 | + return backup.Spec.ApplicationRef |
| 114 | +} |
| 115 | + |
| 116 | +// markRestoreJobFailed updates the RestoreJob status to Failed with the given message. |
| 117 | +func (r *RestoreJobReconciler) markRestoreJobFailed(ctx context.Context, restoreJob *backupsv1alpha1.RestoreJob, message string) (ctrl.Result, error) { |
| 118 | + logger := getLogger(ctx) |
| 119 | + now := metav1.Now() |
| 120 | + restoreJob.Status.CompletedAt = &now |
| 121 | + restoreJob.Status.Phase = backupsv1alpha1.RestoreJobPhaseFailed |
| 122 | + restoreJob.Status.Message = message |
| 123 | + |
| 124 | + // Add condition |
| 125 | + restoreJob.Status.Conditions = append(restoreJob.Status.Conditions, metav1.Condition{ |
| 126 | + Type: "Ready", |
| 127 | + Status: metav1.ConditionFalse, |
| 128 | + Reason: "RestoreFailed", |
| 129 | + Message: message, |
| 130 | + LastTransitionTime: now, |
| 131 | + }) |
| 132 | + |
| 133 | + if err := r.Status().Update(ctx, restoreJob); err != nil { |
| 134 | + logger.Error(err, "failed to update RestoreJob status to Failed") |
| 135 | + return ctrl.Result{}, err |
| 136 | + } |
| 137 | + logger.Debug("RestoreJob failed", "message", message) |
| 138 | + return ctrl.Result{}, nil |
| 139 | +} |
0 commit comments