Skip to content

Commit

Permalink
Don't include excluded items in ItemBlocks
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Seago <[email protected]>
  • Loading branch information
sseago committed Jan 2, 2025
1 parent 03d0bd9 commit acb6f9f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,17 @@ func (kb *kubernetesBackupper) executeItemBlockActions(
name: relatedItem.Name,
inItemBlock: true,
})

relatedItemMetadata, err := meta.Accessor(item)
if err != nil {
log.WithError(errors.WithStack(err)).Warn("Failed to get object metadata.")
continue
}
// Don't add to ItemBlock if item is excluded
// itemInclusionChecks logs the reason
if !itemBlock.itemBackupper.itemInclusionChecks(log, false, relatedItemMetadata, item, relatedItem.GroupResource) {
continue
}
log.Infof("adding %s %s/%s to ItemBlock", relatedItem.GroupResource, relatedItem.Namespace, relatedItem.Name)
itemBlock.AddUnstructured(relatedItem.GroupResource, item, gvr)
kb.executeItemBlockActions(log, item, relatedItem.GroupResource, relatedItem.Name, relatedItem.Namespace, itemsMap, itemBlock)
Expand All @@ -660,15 +671,11 @@ func (kb *kubernetesBackupper) backupItemBlock(ctx context.Context, itemBlock Ba
itemBlock.Log.Debug("Executing pre hooks")
for _, item := range itemBlock.Items {
if item.Gr == kuberesource.Pods {
metadata, key, err := kb.itemMetadataAndKey(item)
_, key, err := kb.itemMetadataAndKey(item)
if err != nil {
itemBlock.Log.WithError(errors.WithStack(err)).Error("Error accessing pod metadata")
continue
}
// Don't run hooks if pod is excluded
if !itemBlock.itemBackupper.itemInclusionChecks(itemBlock.Log, false, metadata, item.Item, item.Gr) {
continue
}
// Don't run hooks if pod has already been backed up
if !itemBlock.itemBackupper.backupRequest.BackedUpItems.Has(key) {
preHookPods = append(preHookPods, item)
Expand Down
13 changes: 13 additions & 0 deletions pkg/backup/itemblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/vmware-tanzu/velero/pkg/itemblock"
Expand Down Expand Up @@ -60,6 +61,18 @@ func (b *BackupItemBlock) addKubernetesResource(item *kubernetesResource, log lo
log.WithError(errors.WithStack(err)).Error("Error decoding JSON from file")
return nil
}

metadata, err := meta.Accessor(unstructured)
if err != nil {
log.WithError(errors.WithStack(err)).Warn("Error accessing item metadata")
return nil
}
// Don't add to ItemBlock if item is excluded
// itemInclusionChecks logs the reason
if !b.itemBackupper.itemInclusionChecks(log, false, metadata, &unstructured, item.groupResource) {
return nil
}

log.Infof("adding %s %s/%s to ItemBlock", item.groupResource, item.namespace, item.name)
b.AddUnstructured(item.groupResource, &unstructured, item.preferredGVR)
return &unstructured
Expand Down

0 comments on commit acb6f9f

Please sign in to comment.