Skip to content

Commit 5e15a75

Browse files
androndolllamnyp
authored andcommitted
[backups] add restore jobs controller
Signed-off-by: Andrey Kolkov <[email protected]>
1 parent 15883d4 commit 5e15a75

8 files changed

Lines changed: 369 additions & 59 deletions

File tree

‎api/backups/strategy/v1alpha1/velero_types.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ type VeleroSpec struct {
5555
// VeleroTemplate describes the data a backup.velero.io should have when
5656
// templated from a Velero backup strategy.
5757
type VeleroTemplate struct {
58-
Spec velerov1.BackupSpec `json:"spec"`
58+
Spec velerov1.BackupSpec `json:"spec"`
59+
RestoreSpec velerov1.RestoreSpec `json:"restoreSpec"`
5960
}
6061

6162
type VeleroStatus struct {

‎api/backups/v1alpha1/restorejob_types.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ type RestoreJobStatus struct {
7171
}
7272

7373
// +kubebuilder:object:root=true
74+
// +kubebuilder:subresource:status
75+
// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase",priority=0
7476

7577
// RestoreJob represents a single execution of a restore from a Backup.
7678
type RestoreJob struct {

‎cmd/backup-controller/main.go‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ import (
3535
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3636
"sigs.k8s.io/controller-runtime/pkg/webhook"
3737

38-
strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1"
3938
backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1"
4039
"github.com/cozystack/cozystack/internal/backupcontroller"
41-
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
4240
// +kubebuilder:scaffold:imports
4341
)
4442

@@ -51,8 +49,6 @@ func init() {
5149
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
5250

5351
utilruntime.Must(backupsv1alpha1.AddToScheme(scheme))
54-
utilruntime.Must(strategyv1alpha1.AddToScheme(scheme))
55-
utilruntime.Must(velerov1.AddToScheme(scheme))
5652
// +kubebuilder:scaffold:scheme
5753
}
5854

@@ -159,15 +155,6 @@ func main() {
159155
os.Exit(1)
160156
}
161157

162-
if err = (&backupcontroller.BackupJobReconciler{
163-
Client: mgr.GetClient(),
164-
Scheme: mgr.GetScheme(),
165-
Recorder: mgr.GetEventRecorderFor("backup-controller"),
166-
}).SetupWithManager(mgr); err != nil {
167-
setupLog.Error(err, "unable to create controller", "controller", "BackupJob")
168-
os.Exit(1)
169-
}
170-
171158
// +kubebuilder:scaffold:builder
172159

173160
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {

‎cmd/backupstrategy-controller/main.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ import (
3535
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3636
"sigs.k8s.io/controller-runtime/pkg/webhook"
3737

38+
strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1"
3839
backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1"
3940
"github.com/cozystack/cozystack/internal/backupcontroller"
41+
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
4042
// +kubebuilder:scaffold:imports
4143
)
4244

@@ -49,6 +51,8 @@ func init() {
4951
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
5052

5153
utilruntime.Must(backupsv1alpha1.AddToScheme(scheme))
54+
utilruntime.Must(strategyv1alpha1.AddToScheme(scheme))
55+
utilruntime.Must(velerov1.AddToScheme(scheme))
5256
// +kubebuilder:scaffold:scheme
5357
}
5458

@@ -148,10 +152,20 @@ func main() {
148152
}
149153

150154
if err = (&backupcontroller.BackupJobReconciler{
151-
Client: mgr.GetClient(),
152-
Scheme: mgr.GetScheme(),
155+
Client: mgr.GetClient(),
156+
Scheme: mgr.GetScheme(),
157+
Recorder: mgr.GetEventRecorderFor("backup-controller"),
153158
}).SetupWithManager(mgr); err != nil {
154-
setupLog.Error(err, "unable to create controller", "controller", "Job")
159+
setupLog.Error(err, "unable to create controller", "controller", "BackupJob")
160+
os.Exit(1)
161+
}
162+
163+
if err = (&backupcontroller.RestoreJobReconciler{
164+
Client: mgr.GetClient(),
165+
Scheme: mgr.GetScheme(),
166+
Recorder: mgr.GetEventRecorderFor("restore-controller"),
167+
}).SetupWithManager(mgr); err != nil {
168+
setupLog.Error(err, "unable to create controller", "controller", "RestoreJob")
155169
os.Exit(1)
156170
}
157171

‎internal/backupcontroller/backupjob_controller.go‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package backupcontroller
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67

78
apierrors "k8s.io/apimachinery/pkg/api/errors"
89
"k8s.io/apimachinery/pkg/api/meta"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
911
"k8s.io/apimachinery/pkg/runtime"
1012
"k8s.io/apimachinery/pkg/types"
1113
"k8s.io/client-go/dynamic"
@@ -46,15 +48,11 @@ func (r *BackupJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
4648
}
4749

4850
if j.Spec.StrategyRef.APIGroup == nil {
49-
logger.V(1).Info("BackupJob has nil StrategyRef.APIGroup, skipping", "backupjob", j.Name)
50-
return ctrl.Result{}, nil
51+
return r.markBackupJobFailed(ctx, j, "StrategyRef.APIGroup is nil")
5152
}
5253

5354
if *j.Spec.StrategyRef.APIGroup != strategyv1alpha1.GroupVersion.Group {
54-
logger.V(1).Info("BackupJob StrategyRef.APIGroup doesn't match, skipping",
55-
"backupjob", j.Name,
56-
"expected", strategyv1alpha1.GroupVersion.Group,
57-
"got", *j.Spec.StrategyRef.APIGroup)
55+
// skip if the strategy group foreign to this controller
5856
return ctrl.Result{}, nil
5957
}
6058

@@ -65,11 +63,7 @@ func (r *BackupJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
6563
case strategyv1alpha1.VeleroStrategyKind:
6664
return r.reconcileVelero(ctx, j)
6765
default:
68-
logger.V(1).Info("BackupJob StrategyRef.Kind not supported, skipping",
69-
"backupjob", j.Name,
70-
"kind", j.Spec.StrategyRef.Kind,
71-
"supported", []string{strategyv1alpha1.JobStrategyKind, strategyv1alpha1.VeleroStrategyKind})
72-
return ctrl.Result{}, nil
66+
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("StrategyRef.Kind not supported: %s", j.Spec.StrategyRef.Kind))
7367
}
7468
}
7569

@@ -91,3 +85,27 @@ func (r *BackupJobReconciler) SetupWithManager(mgr ctrl.Manager) error {
9185
For(&backupsv1alpha1.BackupJob{}).
9286
Complete(r)
9387
}
88+
89+
func (r *BackupJobReconciler) markBackupJobFailed(ctx context.Context, backupJob *backupsv1alpha1.BackupJob, message string) (ctrl.Result, error) {
90+
logger := getLogger(ctx)
91+
now := metav1.Now()
92+
backupJob.Status.CompletedAt = &now
93+
backupJob.Status.Phase = backupsv1alpha1.BackupJobPhaseFailed
94+
backupJob.Status.Message = message
95+
96+
// Add condition
97+
backupJob.Status.Conditions = append(backupJob.Status.Conditions, metav1.Condition{
98+
Type: "Ready",
99+
Status: metav1.ConditionFalse,
100+
Reason: "BackupFailed",
101+
Message: message,
102+
LastTransitionTime: now,
103+
})
104+
105+
if err := r.Status().Update(ctx, backupJob); err != nil {
106+
logger.Error(err, "failed to update BackupJob status to Failed")
107+
return ctrl.Result{}, err
108+
}
109+
logger.Debug("BackupJob failed", "message", message)
110+
return ctrl.Result{}, nil
111+
}

‎internal/backupcontroller/jobstrategy_controller.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ func (r *BackupJobReconciler) reconcileJob(ctx context.Context, j *backupsv1alph
1313
_ = log.FromContext(ctx)
1414
return ctrl.Result{}, nil
1515
}
16+
17+
func (r *RestoreJobReconciler) reconcileJobRestore(ctx context.Context, restoreJob *backupsv1alpha1.RestoreJob, backup *backupsv1alpha1.Backup) (ctrl.Result, error) {
18+
_ = log.FromContext(ctx)
19+
return ctrl.Result{}, nil
20+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

Comments
 (0)