diff --git a/pkg/backup/backup.go b/pkg/backup/backup.go index a430712fd2..11690fc809 100644 --- a/pkg/backup/backup.go +++ b/pkg/backup/backup.go @@ -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) @@ -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) diff --git a/pkg/backup/itemblock.go b/pkg/backup/itemblock.go index 4ea8e74b1f..b845e03ea3 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" @@ -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