Skip to content

Commit

Permalink
Add PVC UID label to PVR (as we already have for PVB)
Browse files Browse the repository at this point in the history
This has also been submitted upstream. Most likely Velero 1.7 will include it,
and this commit can then be dropped.
  • Loading branch information
sseago committed May 13, 2021
1 parent 05bae5e commit c003d24
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/restic/repository_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
32 changes: 29 additions & 3 deletions pkg/restic/restorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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),
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 + "-",
Expand Down Expand Up @@ -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
}

0 comments on commit c003d24

Please sign in to comment.