Skip to content

Commit 354c782

Browse files
committed
Cancel value in request
Signed-off-by: Joseph <[email protected]>
1 parent 359b293 commit 354c782

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

design/backup_cancellation.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@
4646
- The existing `backup_finalizer_controller` will handle cancelled backups by cleaning up backup data while preserving logs, then transitioning to `BackupPhaseFailed`
4747

4848

49+
- Extend the backup request struct to include:
50+
- `Cancel` (bool): reflects whether cancellation has been requested.
51+
- `LastCancelCheck` (timestamp): records the last time cancellation status was checked.
52+
53+
- Cancellation check logic:
54+
1. If `LastCancelCheck` is zero, set it to the current time and continue as usual.
55+
2. If more than a configured interval (e.g., 1–3 seconds) has passed since `LastCancelCheck`:
56+
- Update `LastCancelCheck` to now.
57+
- Retrieve the latest backup object from the API.
58+
- Set `request.Cancel` to the current value of `backup.Spec.Cancel`.
59+
3. Always use the current value of `request.Cancel` to determine if cancellation is requested.
60+
61+
- If backup processing is concurrent (multi-threaded), protect access to `request.Cancel` and `LastCancelCheck` with read/write mutexes to avoid race conditions.
62+
63+
64+
65+
4966
## Detailed Design
5067

5168
### API Changes

pkg/backup/backup.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ type Backupper interface {
8989
volumeSnapshotterGetter VolumeSnapshotterGetter,
9090
) error
9191

92+
// BackupWithResolvers performs the core Kubernetes backup operation, creating a gzipped tar archive
93+
// of selected cluster resources with plugin transformations applied.
94+
//
95+
// It discovers resources based on the backup spec, applies BackupItemAction and ItemBlockAction
96+
// plugins, handles volume snapshots, executes backup hooks, and writes everything to a compressed
97+
// tar archive. Accepts pre-resolved plugin actions for efficiency.
98+
//
99+
// Returns an error only for complete backup failures. Individual resource failures are logged
100+
// but don't stop the backup process.
92101
BackupWithResolvers(
93102
log logrus.FieldLogger,
94103
backupRequest *Request,
@@ -98,6 +107,13 @@ type Backupper interface {
98107
volumeSnapshotterGetter VolumeSnapshotterGetter,
99108
) error
100109

110+
// FinalizeBackup completes the backup process by updating resources that were modified
111+
// during async operations and creating the final backup archive.
112+
//
113+
// It reads the original backup archive, collects resources specified in PostOperationItems
114+
// from completed async operations, re-backs up those resources with their updated state,
115+
// and writes a new archive with the updated content. This ensures the backup contains
116+
// the final state of resources after async operations (like volume snapshots) complete.
101117
FinalizeBackup(
102118
log logrus.FieldLogger,
103119
backupRequest *Request,

pkg/controller/backup_finalizer_controller.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,12 @@ func NewBackupFinalizerReconciler(
8787
// +kubebuilder:rbac:groups=velero.io,resources=backups/status,verbs=get;update;patch
8888

8989
func (r *backupFinalizerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
90+
91+
// LOG
9092
log := r.log.WithFields(logrus.Fields{
9193
"controller": "backup-finalizer",
9294
"backup": req.NamespacedName,
9395
})
94-
9596
// Fetch the Backup instance.
9697
log.Debug("Getting Backup")
9798
backup := &velerov1api.Backup{}
@@ -105,6 +106,7 @@ func (r *backupFinalizerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
105106
return ctrl.Result{}, errors.WithStack(err)
106107
}
107108

109+
// Check phase, only process if phase matches
108110
switch backup.Status.Phase {
109111
case velerov1api.BackupPhaseFinalizing, velerov1api.BackupPhaseFinalizingPartiallyFailed:
110112
// only process backups finalizing after plugin operations are complete
@@ -113,6 +115,7 @@ func (r *backupFinalizerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
113115
return ctrl.Result{}, nil
114116
}
115117

118+
// Defer func to patch backup object and status after each reconciliation
116119
original := backup.DeepCopy()
117120
defer func() {
118121
switch backup.Status.Phase {
@@ -133,7 +136,9 @@ func (r *backupFinalizerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
133136
return
134137
}
135138
}()
136-
139+
140+
// Get the operations associated with the backup
141+
// BackupLocation -> backupStore -> backupItemOperations
137142
location := &velerov1api.BackupStorageLocation{}
138143
if err := r.client.Get(ctx, kbclient.ObjectKey{
139144
Namespace: backup.Namespace,
@@ -163,8 +168,11 @@ func (r *backupFinalizerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
163168
SkippedPVTracker: pkgbackup.NewSkipPVTracker(),
164169
BackedUpItems: pkgbackup.NewBackedUpItemsMap(),
165170
}
171+
166172
var outBackupFile *os.File
167173
if len(operations) > 0 {
174+
175+
// File inits
168176
log.Info("Setting up finalized backup temp file")
169177
inBackupFile, err := downloadToTempFile(backup.Name, backupStore, log)
170178
if err != nil {
@@ -178,6 +186,7 @@ func (r *backupFinalizerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
178186
}
179187
defer closeAndRemoveFile(outBackupFile, log)
180188

189+
// Get all possible plugin actions
181190
log.Info("Getting backup item actions")
182191
actions, err := pluginManager.GetBackupItemActionsV2()
183192
if err != nil {
@@ -201,6 +210,8 @@ func (r *backupFinalizerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
201210
return ctrl.Result{}, errors.WithStack(err)
202211
}
203212
}
213+
214+
204215
backupScheduleName := backupRequest.GetLabels()[velerov1api.ScheduleNameLabel]
205216
switch backup.Status.Phase {
206217
case velerov1api.BackupPhaseFinalizing:

0 commit comments

Comments
 (0)