Track actual resource names for GenerateName in restore status

When restoring resources with GenerateName, Kubernetes assigns the actual name
after creation, but Velero only tracked the original name from the backup in
itemKey. This caused volume information collection to fail when trying to fetch
PVCs using the original name instead of the actual created name.

Example:
- Original PVC name from backup: "test-vm-disk-1"
- Actual created PVC name: "test-vm-backup-2025-10-27-test-vm-disk-1-mdjkd"
- Volume info tried to fetch: "test-vm-disk-1" → Failed with "not found"

This affects any plugin or workflow using GenerateName during restore:
- kubevirt-velero-plugin (VMFR use case with PVC collision avoidance)
- Custom restore item actions using generateName
- Secrets/ConfigMaps restored with generateName

Changes:
1. Add createdName field to restoredItemStatus struct (pkg/restore/request.go)
2. Capture actual name from createdObj.GetName() (pkg/restore/restore.go:1520)
3. Use createdName in RestoredResourceList() when available (pkg/restore/request.go:93-95)

This fix is backwards compatible:
- createdName defaults to empty string
- When empty, falls back to itemKey.name (original behavior)
- Only populated for GenerateName resources where needed

Fixes volume information collection errors like:
"Failed to get PVC" error="persistentvolumeclaims \"<original-name>\" not found"

Signed-off-by: Shubham Pampattiwar <spampatt@redhat.com>
This commit is contained in:
Shubham Pampattiwar
2025-10-27 12:04:46 -07:00
parent f4af6156a1
commit 07f30d06b9
2 changed files with 16 additions and 5 deletions

View File

@@ -71,6 +71,7 @@ type Request struct {
type restoredItemStatus struct {
action string
itemExists bool
createdName string // Actual name assigned by K8s for GenerateName resources
}
// GetItemOperationsList returns ItemOperationsList, initializing it if necessary
@@ -87,9 +88,15 @@ func (r *Request) GetItemOperationsList() *[]*itemoperation.RestoreOperation {
func (r *Request) RestoredResourceList() map[string][]string {
resources := map[string][]string{}
for i, item := range r.RestoredItems {
entry := i.name
// Use createdName if available (GenerateName case), otherwise itemKey.name
name := i.name
if item.createdName != "" {
name = item.createdName
}
entry := name
if i.namespace != "" {
entry = fmt.Sprintf("%s/%s", i.namespace, i.name)
entry = fmt.Sprintf("%s/%s", i.namespace, name)
}
entry = fmt.Sprintf("%s(%s)", entry, item.action)
resources[i.resource] = append(resources[i.resource], entry)

View File

@@ -1514,7 +1514,11 @@ func (ctx *restoreContext) restoreItem(obj *unstructured.Unstructured, groupReso
createdObj, restoreErr = resourceClient.Create(obj)
if restoreErr == nil {
itemExists = true
ctx.restoredItems[itemKey] = restoredItemStatus{action: ItemRestoreResultCreated, itemExists: itemExists}
ctx.restoredItems[itemKey] = restoredItemStatus{
action: ItemRestoreResultCreated,
itemExists: itemExists,
createdName: createdObj.GetName(),
}
}
}