diff --git a/changelogs/unreleased/8585-kaovilai b/changelogs/unreleased/8585-kaovilai new file mode 100644 index 0000000000..c4530e44e2 --- /dev/null +++ b/changelogs/unreleased/8585-kaovilai @@ -0,0 +1 @@ +Don't include excluded items in ItemBlocks diff --git a/pkg/backup/backup.go b/pkg/backup/backup.go index 6af875874a..c970e07f6a 100644 --- a/pkg/backup/backup.go +++ b/pkg/backup/backup.go @@ -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 { @@ -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) @@ -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) @@ -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 @@ -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) { diff --git a/pkg/backup/item_collector.go b/pkg/backup/item_collector.go index a1fce5ee74..903b0e991f 100644 --- a/pkg/backup/item_collector.go +++ b/pkg/backup/item_collector.go @@ -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 diff --git a/pkg/backup/itemblock.go b/pkg/backup/itemblock.go index 4ea8e74b1f..dee553f721 100644 --- a/pkg/backup/itemblock.go +++ b/pkg/backup/itemblock.go @@ -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" @@ -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 { @@ -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