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 <sseago@redhat.com>
  • Loading branch information
sseago authored and kaovilai committed Jan 7, 2025
1 parent 674e397 commit d5ef00a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/8585-kaovilai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Don't include excluded items in ItemBlocks
39 changes: 23 additions & 16 deletions pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ func (kb *kubernetesBackupper) BackupWithResolvers(
"name": items[i].name,
}).Infof("Processing item")

// Skip if this item has already been added to an ItemBlock
if items[i].inItemBlock {
// Skip if this item has already been processed (in a block or previously excluded)
if items[i].inItemBlockOrExcluded {
log.Debugf("Not creating new ItemBlock for %s %s/%s because it's already in an ItemBlock", items[i].groupResource.String(), items[i].namespace, items[i].name)
} else {
if itemBlock == nil {
Expand Down Expand Up @@ -623,12 +623,23 @@ func (kb *kubernetesBackupper) executeItemBlockActions(
continue
}
itemsMap[relatedItem] = append(itemsMap[relatedItem], &kubernetesResource{
groupResource: relatedItem.GroupResource,
preferredGVR: gvr,
namespace: relatedItem.Namespace,
name: relatedItem.Name,
inItemBlock: true,
groupResource: relatedItem.GroupResource,
preferredGVR: gvr,
namespace: relatedItem.Namespace,
name: relatedItem.Name,
inItemBlockOrExcluded: 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 @@ -644,15 +655,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.getItemKey(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 _, exists := itemBlock.itemBackupper.backupRequest.BackedUpItems[key]; !exists {
preHookPods = append(preHookPods, item)
Expand All @@ -663,7 +670,7 @@ func (kb *kubernetesBackupper) backupItemBlock(ctx context.Context, itemBlock Ba
for i, pod := range failedPods {
itemBlock.Log.WithError(errs[i]).WithField("name", pod.Item.GetName()).Error("Error running pre hooks for pod")
// if pre hook fails, flag pod as backed-up and move on
_, key, err := kb.itemMetadataAndKey(pod)
key, err := kb.getItemKey(pod)
if err != nil {
itemBlock.Log.WithError(errors.WithStack(err)).Error("Error accessing pod metadata")
continue
Expand All @@ -688,17 +695,17 @@ func (kb *kubernetesBackupper) backupItemBlock(ctx context.Context, itemBlock Ba
return grList
}

func (kb *kubernetesBackupper) itemMetadataAndKey(item itemblock.ItemBlockItem) (metav1.Object, itemKey, error) {
func (kb *kubernetesBackupper) getItemKey(item itemblock.ItemBlockItem) (itemKey, error) {
metadata, err := meta.Accessor(item.Item)
if err != nil {
return nil, itemKey{}, err
return itemKey{}, err
}
key := itemKey{
resource: resourceKey(item.Item),
namespace: metadata.GetNamespace(),
name: metadata.GetName(),
}
return metadata, key, nil
return key, nil
}

func (kb *kubernetesBackupper) handleItemBlockPreHooks(itemBlock BackupItemBlock, hookPods []itemblock.ItemBlockItem) ([]itemblock.ItemBlockItem, []itemblock.ItemBlockItem, []error) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/backup/item_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ type kubernetesResource struct {
preferredGVR schema.GroupVersionResource
namespace, name, path string
orderedResource bool
inItemBlock bool // set to true during backup processing when added to an ItemBlock
// set to true during backup processing when added to an ItemBlock
// or if the item is excluded from backup.
inItemBlockOrExcluded bool
}

// getItemsFromResourceIdentifiers get the kubernetesResources
Expand Down
19 changes: 16 additions & 3 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 All @@ -41,12 +42,12 @@ func NewBackupItemBlock(log logrus.FieldLogger, itemBackupper *itemBackupper) *B
}

func (b *BackupItemBlock) addKubernetesResource(item *kubernetesResource, log logrus.FieldLogger) *unstructured.Unstructured {
// no-op if item is already in a block
if item.inItemBlock {
// no-op if item has already been processed (in a block or previously excluded)
if item.inItemBlockOrExcluded {
return nil
}
var unstructured unstructured.Unstructured
item.inItemBlock = true
item.inItemBlockOrExcluded = true

f, err := os.Open(item.path)
if err != nil {
Expand All @@ -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 d5ef00a

Please sign in to comment.