From c003d241157fd336b85065b6fd52069efcfa55b0 Mon Sep 17 00:00:00 2001 From: Scott Seago Date: Wed, 12 May 2021 10:42:54 -0400 Subject: [PATCH] Add PVC UID label to PVR (as we already have for PVB) This has also been submitted upstream. Most likely Velero 1.7 will include it, and this commit can then be dropped. --- pkg/restic/repository_manager.go | 2 +- pkg/restic/restorer.go | 32 +++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/pkg/restic/repository_manager.go b/pkg/restic/repository_manager.go index 6fa1417fc9..908af9c9f0 100644 --- a/pkg/restic/repository_manager.go +++ b/pkg/restic/repository_manager.go @@ -162,7 +162,7 @@ func (rm *repositoryManager) NewRestorer(ctx context.Context, restore *velerov1a }, ) - r := newRestorer(ctx, rm, rm.repoEnsurer, informer, rm.log) + r := newRestorer(ctx, rm, rm.repoEnsurer, informer, rm.pvcClient, rm.log) go informer.Run(ctx.Done()) if !cache.WaitForCacheSync(ctx.Done(), informer.HasSynced, rm.repoInformerSynced) { diff --git a/pkg/restic/restorer.go b/pkg/restic/restorer.go index 3c915e61ab..62dba4e652 100644 --- a/pkg/restic/restorer.go +++ b/pkg/restic/restorer.go @@ -24,6 +24,7 @@ import ( "github.com/sirupsen/logrus" corev1api "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + corev1client "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/client-go/tools/cache" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" @@ -48,6 +49,7 @@ type restorer struct { ctx context.Context repoManager *repositoryManager repoEnsurer *repositoryEnsurer + pvcClient corev1client.PersistentVolumeClaimsGetter resultsLock sync.Mutex results map[string]chan *velerov1api.PodVolumeRestore @@ -58,12 +60,14 @@ func newRestorer( rm *repositoryManager, repoEnsurer *repositoryEnsurer, podVolumeRestoreInformer cache.SharedIndexInformer, + pvcClient corev1client.PersistentVolumeClaimsGetter, log logrus.FieldLogger, ) *restorer { r := &restorer{ ctx: ctx, repoManager: rm, repoEnsurer: repoEnsurer, + pvcClient: pvcClient, results: make(map[string]chan *velerov1api.PodVolumeRestore), } @@ -116,10 +120,27 @@ func (r *restorer) RestorePodVolumes(data RestoreData) []error { var ( errs []error numRestores int + podVolumes = make(map[string]corev1api.Volume) ) + // put the pod's volumes in a map for efficient lookup below + for _, podVolume := range data.Pod.Spec.Volumes { + podVolumes[podVolume.Name] = podVolume + } for volume, snapshot := range volumesToRestore { - volumeRestore := newPodVolumeRestore(data.Restore, data.Pod, data.BackupLocation, volume, snapshot, repo.Spec.ResticIdentifier) + volumeObj, ok := podVolumes[volume] + var pvc *corev1api.PersistentVolumeClaim + if ok { + if volumeObj.PersistentVolumeClaim != nil { + pvc, err = r.pvcClient.PersistentVolumeClaims(data.Pod.Namespace).Get(context.TODO(), volumeObj.PersistentVolumeClaim.ClaimName, metav1.GetOptions{}) + if err != nil { + errs = append(errs, errors.Wrap(err, "error getting persistent volume claim for volume")) + continue + } + } + } + + volumeRestore := newPodVolumeRestore(data.Restore, data.Pod, data.BackupLocation, volume, snapshot, repo.Spec.ResticIdentifier, pvc) if err := errorOnly(r.repoManager.veleroClient.VeleroV1().PodVolumeRestores(volumeRestore.Namespace).Create(context.TODO(), volumeRestore, metav1.CreateOptions{})); err != nil { errs = append(errs, errors.WithStack(err)) @@ -162,8 +183,8 @@ ForEachVolume: return errs } -func newPodVolumeRestore(restore *velerov1api.Restore, pod *corev1api.Pod, backupLocation, volume, snapshot, repoIdentifier string) *velerov1api.PodVolumeRestore { - return &velerov1api.PodVolumeRestore{ +func newPodVolumeRestore(restore *velerov1api.Restore, pod *corev1api.Pod, backupLocation, volume, snapshot, repoIdentifier string, pvc *corev1api.PersistentVolumeClaim) *velerov1api.PodVolumeRestore { + pvr := &velerov1api.PodVolumeRestore{ ObjectMeta: metav1.ObjectMeta{ Namespace: restore.Namespace, GenerateName: restore.Name + "-", @@ -195,4 +216,9 @@ func newPodVolumeRestore(restore *velerov1api.Restore, pod *corev1api.Pod, backu RepoIdentifier: repoIdentifier, }, } + if pvc != nil { + // this label is not used by velero, but useful for debugging. + pvr.Labels[velerov1api.PVCUIDLabel] = string(pvc.UID) + } + return pvr }