Skip to content

Commit

Permalink
Allow cross-namespace restore of CSI snapshots
Browse files Browse the repository at this point in the history
- Check for namespace mapping information.
- If there is a map, replace VolumeSnapshots namespace to the target namespace

Fixes KUBEDR-364 and KUBEDR-406

Fixes Issues these similar issues from Velero:
1. vmware-tanzu/velero#2143
2. https://github.com/vmware-tanzu/velero-plugin-for-csi/issues/75
  • Loading branch information
sshende-catalogicsoftware committed Dec 10, 2020
1 parent 1bc2fb8 commit b5627c3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ BIN ?= velero-plugin-for-csi

BUILD_IMAGE ?= golang:1.13-stretch

REGISTRY ?= velero
REGISTRY ?= catalogicsoftware
IMAGE_NAME ?= $(REGISTRY)/velero-plugin-for-csi
TAG ?= dev
TAG ?= v0.1.2.2

IMAGE ?= $(IMAGE_NAME):$(TAG)

Expand Down
6 changes: 6 additions & 0 deletions internal/restore/pvc_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ func (p *PVCRestoreItemAction) Execute(input *velero.RestoreItemActionExecuteInp

resetPVCAnnotations(&pvc, []string{velerov1api.BackupNameLabel, util.VolumeSnapshotLabel})

// If cross-namespace restore is configured, change the namespace
// for PVC object to be restored
if val, ok := input.Restore.Spec.NamespaceMapping[pvc.GetNamespace()]; ok {
pvc.SetNamespace(val)
}

volumeSnapshotName, ok := pvc.Annotations[util.VolumeSnapshotLabel]
if !ok {
p.Log.Infof("Skipping PVCRestoreItemAction for PVC %s/%s, PVC does not have a CSI volumesnapshot.", pvc.Namespace, pvc.Name)
Expand Down
10 changes: 8 additions & 2 deletions internal/restore/volumesnapshot_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func (p *VolumeSnapshotRestoreItemAction) Execute(input *velero.RestoreItemActio
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(input.Item.UnstructuredContent(), &vs); err != nil {
return &velero.RestoreItemActionExecuteOutput{}, errors.Wrapf(err, "failed to convert input.Item from unstructured")
}

// If cross-namespace restore is configured, change the namespace
// for VolumeSnapshot object to be restored
if val, ok := input.Restore.Spec.NamespaceMapping[vs.GetNamespace()]; ok {
vs.SetNamespace(val)
}

_, snapClient, err := util.GetClients()
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -86,10 +93,9 @@ func (p *VolumeSnapshotRestoreItemAction) Execute(input *velero.RestoreItemActio
deletionPolicy = string(snapshotv1beta1api.VolumeSnapshotContentRetain)
}

// TODO: generated name will be like velero-velero-something. Fix that.
vsc := snapshotv1beta1api.VolumeSnapshotContent{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "velero-" + vs.Name + "-",
GenerateName: input.Restore.GetName() + "-" + vs.Name + "-",
Labels: map[string]string{
velerov1api.RestoreNameLabel: label.GetValidName(input.Restore.Name),
},
Expand Down

0 comments on commit b5627c3

Please sign in to comment.