Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OADP-1830: Only create VSR if it does not exist for a VSB #15

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 34 additions & 31 deletions internal/backup/volumesnapshotcontent_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package backup
import (
"context"
"fmt"
"strings"
"time"

snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -27,8 +30,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"
"time"

datamoverv1alpha1 "github.com/konveyor/volume-snapshot-mover/api/v1alpha1"
"github.com/vmware-tanzu/velero-plugin-for-csi/internal/util"
Expand Down Expand Up @@ -99,34 +100,36 @@ func (p *VolumeSnapshotContentBackupItemActionV2) Execute(item runtime.Unstructu
return nil, nil, "", nil, errors.WithStack(err)
}

// craft a VolumeBackupSnapshot object to be created
vsb := datamoverv1alpha1.VolumeSnapshotBackup{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "vsb-",
Namespace: snapCont.Spec.VolumeSnapshotRef.Namespace,
Labels: map[string]string{
util.BackupNameLabel: backup.Name,
},
},
Spec: datamoverv1alpha1.VolumeSnapshotBackupSpec{
VolumeSnapshotContent: corev1api.ObjectReference{
Name: snapCont.Name,
},
ProtectedNamespace: backup.Namespace,
ResticSecretRef: corev1api.LocalObjectReference{
Name: resticSecretName,
},
},
}

// check if VolumeBackupSnapshot CR exists for VSC
VSBExists, err := util.DoesVolumeSnapshotBackupExistForVSC(&snapCont, p.Log)
// check if VolumeSnapshotBackup CR exists for VolumeSnapshotContent
VSBExists, err := util.VSBExistsForVSC(&snapCont, p.Log)
if err != nil {
return nil, nil, "", nil, errors.WithStack(err)
}

// Create VSB only if does not exist for the VSC
if !VSBExists {

// craft a VolumeBackupSnapshot object to be created
vsb := datamoverv1alpha1.VolumeSnapshotBackup{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "vsb-",
Namespace: snapCont.Spec.VolumeSnapshotRef.Namespace,
Labels: map[string]string{
util.BackupNameLabel: backup.Name,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add backup UID here?

backup.Name isn't going to cause any issues here but it would be helpful to be able to compare if backup CR in the cluster still applies to this VSB.

util.VolumeSnapshotBackupVolumeSnapshotContent: snapCont.Name,
},
},
Spec: datamoverv1alpha1.VolumeSnapshotBackupSpec{
VolumeSnapshotContent: corev1api.ObjectReference{
Name: snapCont.Name,
},
ProtectedNamespace: backup.Namespace,
ResticSecretRef: corev1api.LocalObjectReference{
Name: resticSecretName,
},
},
}

vsbClient, err := util.GetVolumeSnapshotMoverClient()
if err != nil {
return nil, nil, "", nil, errors.Wrapf(err, "error getting volumesnapshotbackup client")
Expand All @@ -150,14 +153,14 @@ func (p *VolumeSnapshotContentBackupItemActionV2) Execute(item runtime.Unstructu
operationID = vsb.Namespace + "/" + vsb.Name

p.Log.Infof("Got operationID: %s", operationID)
}

// adding volumesnapshotbackup instance as an item that needs to be updated in backup's finalizing phase with all its annotations and status
itemsToUpdate = append(itemsToUpdate, velero.ResourceIdentifier{
GroupResource: schema.GroupResource{Group: "datamover.oadp.openshift.io", Resource: "volumesnapshotbackups"},
Name: vsb.Name,
Namespace: vsb.Namespace,
})
// adding volumesnapshotbackup instance as an item that needs to be updated in backup's finalizing phase with all its annotations and status
itemsToUpdate = append(itemsToUpdate, velero.ResourceIdentifier{
GroupResource: schema.GroupResource{Group: "datamover.oadp.openshift.io", Resource: "volumesnapshotbackups"},
Name: vsb.Name,
Namespace: vsb.Namespace,
})
}
}

p.Log.Infof("Returning from VolumeSnapshotContentBackupItemActionV2 with %d itemsToUpdate to backup", len(itemsToUpdate))
Expand Down
98 changes: 53 additions & 45 deletions internal/restore/volumesnapshotrestore_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,59 +50,67 @@ func (p *VolumeSnapshotBackupRestoreItemActionV2) Execute(input *velero.RestoreI

operationID := ""

// create VSR per VSB
vsr := datamoverv1alpha1.VolumeSnapshotRestore{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "vsr-",
Namespace: vsb.Namespace,
Labels: map[string]string{
util.RestoreNameLabel: input.Restore.Name,
util.BackupNameLabel: vsb.Labels[util.BackupNameLabel],
util.PersistentVolumeClaimLabel: vsb.Annotations[util.VolumeSnapshotMoverSourcePVCName],
util.VolumeSnapshotBackupLabel: vsb.Name,
},
},
Spec: datamoverv1alpha1.VolumeSnapshotRestoreSpec{
ResticSecretRef: corev1.LocalObjectReference{
Name: vsb.Spec.ResticSecretRef.Name,
// check if VolumeSnaphotRestore CR exists for VolumeSnaoshotBackup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// check if VolumeSnaphotRestore CR exists for VolumeSnaoshotBackup
// check if VolumeSnaphotRestore CR exists for VolumeSnapshotBackup

VSRExists, err := util.VSRExistsForVSB(&vsb, p.Log)
if err != nil {
return nil, err
}

if !VSRExists {
// create VSR per VSB
vsr := datamoverv1alpha1.VolumeSnapshotRestore{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "vsr-",
Namespace: vsb.Namespace,
Labels: map[string]string{
util.RestoreNameLabel: input.Restore.Name,
util.BackupNameLabel: vsb.Labels[util.BackupNameLabel],
util.PersistentVolumeClaimLabel: vsb.Annotations[util.VolumeSnapshotMoverSourcePVCName],
util.VolumeSnapshotBackupLabel: vsb.Name,
},
},
VolumeSnapshotMoverBackupref: datamoverv1alpha1.VSBRef{
BackedUpPVCData: datamoverv1alpha1.PVCData{
Name: vsb.Annotations[util.VolumeSnapshotMoverSourcePVCName],
Size: vsb.Annotations[util.VolumeSnapshotMoverSourcePVCSize],
StorageClassName: vsb.Annotations[util.VolumeSnapshotMoverSourcePVCStorageClass],
Spec: datamoverv1alpha1.VolumeSnapshotRestoreSpec{
ResticSecretRef: corev1.LocalObjectReference{
Name: vsb.Spec.ResticSecretRef.Name,
},
ResticRepository: vsb.Annotations[util.VolumeSnapshotMoverResticRepository],
VolumeSnapshotClassName: vsb.Annotations[util.VolumeSnapshotMoverVolumeSnapshotClass],
VolumeSnapshotMoverBackupref: datamoverv1alpha1.VSBRef{
BackedUpPVCData: datamoverv1alpha1.PVCData{
Name: vsb.Annotations[util.VolumeSnapshotMoverSourcePVCName],
Size: vsb.Annotations[util.VolumeSnapshotMoverSourcePVCSize],
StorageClassName: vsb.Annotations[util.VolumeSnapshotMoverSourcePVCStorageClass],
},
ResticRepository: vsb.Annotations[util.VolumeSnapshotMoverResticRepository],
VolumeSnapshotClassName: vsb.Annotations[util.VolumeSnapshotMoverVolumeSnapshotClass],
},
ProtectedNamespace: vsb.Spec.ProtectedNamespace,
},
ProtectedNamespace: vsb.Spec.ProtectedNamespace,
},
}
}

vsrClient, err := util.GetVolumeSnapshotMoverClient()
if err != nil {
return nil, err
}
vsrClient, err := util.GetVolumeSnapshotMoverClient()
if err != nil {
return nil, err
}

// if namespace mapping is specified
if val, ok := input.Restore.Spec.NamespaceMapping[vsr.GetNamespace()]; ok {
vsr.SetNamespace(val)
}
// if namespace mapping is specified
if val, ok := input.Restore.Spec.NamespaceMapping[vsr.GetNamespace()]; ok {
vsr.SetNamespace(val)
}

err = vsrClient.Create(context.Background(), &vsr)
if err != nil {
return nil, errors.Wrapf(err, "error creating volumesnapshotrestore CR")
}
p.Log.Infof("[vsb-restore] vsr created: %s", vsr.Name)
err = vsrClient.Create(context.Background(), &vsr)
if err != nil {
return nil, errors.Wrapf(err, "error creating volumesnapshotrestore CR")
}
p.Log.Infof("[vsb-restore] vsr created: %s", vsr.Name)

// fetch the VSR so we get the name of the VSR as we use generate name for VSR CR creation
err = vsrClient.Get(context.Background(), client.ObjectKey{Namespace: vsr.Namespace, Name: vsr.Name}, &vsr)
if err != nil {
return nil, errors.Wrapf(err, "error fetching volumesnapshotrestore CR for suppyling operationID")
}
// fetch the VSR so we get the name of the VSR as we use generate name for VSR CR creation
err = vsrClient.Get(context.Background(), client.ObjectKey{Namespace: vsr.Namespace, Name: vsr.Name}, &vsr)
if err != nil {
return nil, errors.Wrapf(err, "error fetching volumesnapshotrestore CR for suppyling operationID")
}

// operationID for our datamover usecase is VSR NamespacedName which will unique per operation
operationID = vsr.Namespace + "/" + vsr.Name
// operationID for our datamover usecase is VSR NamespacedName which will unique per operation
operationID = vsr.Namespace + "/" + vsr.Name
}

p.Log.Info("Returning from VolumeSnapshotBackupRestoreItemActionV2")

Expand Down
13 changes: 7 additions & 6 deletions internal/util/labels_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ const (
PrefixedSnapshotterSecretNamespaceKey = "csi.storage.k8s.io/snapshotter-secret-namespace"

// VolumeSnapshotMover annotation keys
VolumeSnapshotMoverResticRepository = "datamover.io/restic-repository"
VolumeSnapshotMoverSourcePVCName = "datamover.io/source-pvc-name"
VolumeSnapshotMoverSourcePVCSize = "datamover.io/source-pvc-size"
VolumeSnapshotMoverSourcePVCStorageClass = "datamover.io/source-pvc-storageclass"
VolumeSnapshotMoverVolumeSnapshotClass = "datamover.io/source-pvc-volumesnapshotclass"
WaitVolumeSnapshotBackup = "datamover.io/wait-for-vsb"
VolumeSnapshotMoverResticRepository = "datamover.io/restic-repository"
VolumeSnapshotMoverSourcePVCName = "datamover.io/source-pvc-name"
VolumeSnapshotMoverSourcePVCSize = "datamover.io/source-pvc-size"
VolumeSnapshotMoverSourcePVCStorageClass = "datamover.io/source-pvc-storageclass"
VolumeSnapshotMoverVolumeSnapshotClass = "datamover.io/source-pvc-volumesnapshotclass"
WaitVolumeSnapshotBackup = "datamover.io/wait-for-vsb"
VolumeSnapshotBackupVolumeSnapshotContent = "datamover.io/vsb-volumesnapshotcontent"

// Env vars
VolumeSnapshotMoverEnv = "VOLUME_SNAPSHOT_MOVER"
Expand Down
48 changes: 37 additions & 11 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,28 +399,54 @@ func GetVolumeSnapshotRestoreWithStatusData(restoreName string, PVCName string,
}

// Check if volumesnapshotbackup CR exists for a given volumesnapshotcontent
func DoesVolumeSnapshotBackupExistForVSC(snapCont *snapshotv1api.VolumeSnapshotContent, log logrus.FieldLogger) (bool, error) {
func VSBExistsForVSC(snapCont *snapshotv1api.VolumeSnapshotContent, log logrus.FieldLogger) (bool, error) {

snapMoverClient, err := GetVolumeSnapshotMoverClient()
if err != nil {
return false, err
}
vsb := datamoverv1alpha1.VolumeSnapshotBackup{}
vsbList := datamoverv1alpha1.VolumeSnapshotBackupList{}
VSBListOptions := client.MatchingLabels(map[string]string{
VolumeSnapshotBackupVolumeSnapshotContent: snapCont.Name,
})

err = snapMoverClient.Get(context.TODO(), client.ObjectKey{Namespace: snapCont.Spec.VolumeSnapshotRef.Namespace, Name: fmt.Sprint("vsb-" + snapCont.Spec.VolumeSnapshotRef.Name)}, &vsb)
err = snapMoverClient.List(context.TODO(), &vsbList, VSBListOptions)
if err != nil {
if apierrors.IsNotFound(err) {
log.Infof("could not find volumesnapshotbackup for the given volumesnapshotcontent")
return false, nil
if apierrors.IsAlreadyExists(err) {
log.Infof("found volumesnapshotbackup for the given volumesnapshotcontent")
return true, nil
}
return false, err
}

if len(vsb.Spec.VolumeSnapshotContent.Name) > 0 && vsb.Spec.VolumeSnapshotContent.Name == snapCont.Name {
log.Infof("found volumesnapshotbackup for the given volumesnapshotcontent")
return true, nil
log.Infof("did not find volumesnapshotbackup for the given volumesnapshotcontent %v", snapCont.Name)
return false, nil
}

// Check if volumesnapshotrestore CR exists for a given volumesnapshotbackup
func VSRExistsForVSB(vsb *datamoverv1alpha1.VolumeSnapshotBackup, log logrus.FieldLogger) (bool, error) {

snapMoverClient, err := GetVolumeSnapshotMoverClient()
if err != nil {
return false, err
}
log.Infof("could not find volumesnapshotbackup for the given volumesnapshotcontent")
return false, err

vsrList := datamoverv1alpha1.VolumeSnapshotRestoreList{}
VSRListOptions := client.MatchingLabels(map[string]string{
VolumeSnapshotBackupLabel: vsb.Name,
})

err = snapMoverClient.List(context.TODO(), &vsrList, VSRListOptions)
if err != nil {
if apierrors.IsAlreadyExists(err) {
log.Infof("found volumesnapshotrestore for the given volumesnapshotbackup")
return true, nil
}
return false, err
}

log.Infof("did not find volumesnapshotrestore for the given volumesnapshotbackup %v", vsb.Name)
return false, nil
}

//Waits for volumesnapshotcontent to be in ready state
Expand Down