Skip to content

Commit 245525c

Browse files
authored
Merge pull request #9547 from blackpiglet/1.18_add_bia_skip_resource_logic
Add BIA skip resource logic
2 parents 55737b9 + 8f32696 commit 245525c

4 files changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
If BIA return updateObj with SkipFromBackupAnnotation, treat it as skip the resource from backup.

pkg/apis/velero/v1/labels_annotations.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ const (
102102
// even if the resource contains a matching selector label.
103103
ExcludeFromBackupLabel = "velero.io/exclude-from-backup"
104104

105+
// SkipFromBackupAnnotation is the annotation used by internal BackupItemActions
106+
// to indicate that a resource should be skipped from backup,
107+
// even if it doesn't have the ExcludeFromBackupLabel.
108+
// This is used in cases where we want to skip backup of a resource based on some logic in a plugin.
109+
//
110+
// Notice: SkipFromBackupAnnotation's priority is higher than MustIncludeAdditionalItemAnnotation.
111+
// If SkipFromBackupAnnotation is set, the resource will be skipped even if MustIncludeAdditionalItemAnnotation is set.
112+
SkipFromBackupAnnotation = "velero.io/skip-from-backup"
113+
105114
// defaultVGSLabelKey is the default label key used to group PVCs under a VolumeGroupSnapshot
106115
DefaultVGSLabelKey = "velero.io/volume-group"
107116

pkg/backup/backed_up_items_map.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ func (m *backedUpItemsMap) AddItem(key itemKey) {
9898
m.totalItems[key] = struct{}{}
9999
}
100100

101+
func (m *backedUpItemsMap) DeleteItem(key itemKey) {
102+
m.Lock()
103+
defer m.Unlock()
104+
105+
delete(m.backedUpItems, key)
106+
delete(m.totalItems, key)
107+
}
108+
101109
func (m *backedUpItemsMap) AddItemToTotal(key itemKey) {
102110
m.Lock()
103111
defer m.Unlock()

pkg/backup/item_backupper.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ func (ib *itemBackupper) backupItemInternal(logger logrus.FieldLogger, obj runti
244244
return false, itemFiles, kubeerrs.NewAggregate(backupErrs)
245245
}
246246

247+
// If err is nil and updatedObj is nil, it means the item is skipped by plugin action,
248+
// we should return here to avoid backing up the item, and avoid potential NPE in the following code.
249+
if updatedObj == nil {
250+
log.Infof("Remove item from the backup's backupItems list and totalItems list because it's skipped by plugin action.")
251+
ib.backupRequest.BackedUpItems.DeleteItem(key)
252+
return false, itemFiles, nil
253+
}
254+
247255
itemFiles = append(itemFiles, additionalItemFiles...)
248256
obj = updatedObj
249257
if metadata, err = meta.Accessor(obj); err != nil {
@@ -398,6 +406,13 @@ func (ib *itemBackupper) executeActions(
398406
}
399407

400408
u := &unstructured.Unstructured{Object: updatedItem.UnstructuredContent()}
409+
410+
if _, ok := u.GetAnnotations()[velerov1api.SkipFromBackupAnnotation]; ok {
411+
log.Infof("Resource (groupResource=%s, namespace=%s, name=%s) is skipped from backup by action %s.",
412+
groupResource.String(), namespace, name, actionName)
413+
return nil, itemFiles, nil
414+
}
415+
401416
if actionName == csiBIAPluginName {
402417
if additionalItemIdentifiers == nil && u.GetAnnotations()[velerov1api.SkippedNoCSIPVAnnotation] == "true" {
403418
// snapshot was skipped by CSI plugin

0 commit comments

Comments
 (0)