Skip to content

Commit

Permalink
PB-8482 :: Handling pre-provisioned VSC restore
Browse files Browse the repository at this point in the history
- Removing nil check of restore size as per external-snapshotter
  defaults to nil
- Getting object before removal of finalizer and updation
- Passing snapshot.jsnon restorSize as annotaion in VS
- If created VS.status.RestoreSize is nil then compare with annotation to
  handle PB-3966

Signed-off-by: Vikas Kumar <[email protected]>
  • Loading branch information
vikasit12 committed Oct 29, 2024
1 parent 9dc3204 commit 2ae9d2b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
18 changes: 18 additions & 0 deletions drivers/volume/csi/csi.go
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,15 @@ func (c *csi) restoreVolumeSnapshot(
vsObj.Spec.Source.PersistentVolumeClaimName = nil
vsObj.Spec.Source.VolumeSnapshotContentName = &vsc.(*kSnapshotv1.VolumeSnapshotContent).Name
vsObj.Namespace = namespace
// PB-8482 Add restoreSize to annotations
// Convert RestoreSize to string and store it in restoreSizeStr
restoreSizeStr := vsObj.Status.RestoreSize.String()
// Initialize Annotations map if it is nil
if vsObj.Annotations == nil {
vsObj.Annotations = make(map[string]string)
}
// Add the restoreSize annotation to the VolumeSnapshot object
vsObj.Annotations["px-backup/restoreSize"] = restoreSizeStr
vs, err = c.snapshotClient.SnapshotV1().VolumeSnapshots(namespace).Create(context.TODO(), vsObj, metav1.CreateOptions{})
if err != nil {
if k8s_errors.IsAlreadyExists(err) {
Expand All @@ -1363,6 +1372,15 @@ func (c *csi) restoreVolumeSnapshot(
vsObj.Spec.Source.PersistentVolumeClaimName = nil
vsObj.Spec.Source.VolumeSnapshotContentName = &vsc.(*kSnapshotv1beta1.VolumeSnapshotContent).Name
vsObj.Namespace = namespace
// PB-8482 Add restoreSize to annotations
// Convert RestoreSize to string and store it in restoreSizeStr
restoreSizeStr := vsObj.Status.RestoreSize.String()
// Initialize Annotations map if it is nil
if vsObj.Annotations == nil {
vsObj.Annotations = make(map[string]string)
}
// Add the restoreSize annotation to the VolumeSnapshot object
vsObj.Annotations["px-backup/restoreSize"] = restoreSizeStr
vs, err = c.snapshotClient.SnapshotV1beta1().VolumeSnapshots(namespace).Create(context.TODO(), vsObj, metav1.CreateOptions{})
if err != nil {
if k8s_errors.IsAlreadyExists(err) {
Expand Down
9 changes: 8 additions & 1 deletion pkg/applicationmanager/controllers/applicationbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,15 @@ func (a *ApplicationBackupController) handle(ctx context.Context, backup *stork_
}

if backup.GetFinalizers() != nil {
// Fetch the latest backup object as we need to update after removing the finalizer
key := runtimeclient.ObjectKeyFromObject(backup)
err := a.client.Get(context.TODO(), key, backup)
if err != nil {
log.ApplicationBackupLog(backup).Errorf("Error while getting applicationbackup [%v/%v]: %v", backup.Namespace, backup.Name, err)
return err
}
controllers.RemoveFinalizer(backup, controllers.FinalizerCleanup)
err := a.client.Update(ctx, backup)
err = a.client.Update(ctx, backup)
if err != nil {
log.ApplicationBackupLog(backup).Errorf("Error while updating applicationbackup [%v/%v]: %v", backup.Namespace, backup.Name, err)
return err
Expand Down
55 changes: 52 additions & 3 deletions pkg/snapshotter/snapshotter_csi.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ func (c *csiDriver) RestoreVolumeClaim(opts ...Option) (*v1.PersistentVolumeClai
errMsg := fmt.Sprintf("failed to get volumesnapshot [%v/%v]", snapshot.Namespace, snapshot.Name)
return "", true, fmt.Errorf("%v", errMsg)
}
if snapshot.Status == nil || snapshot.Status.RestoreSize == nil {
if snapshot.Status == nil {
errMsg := fmt.Sprintf("volumesnapshot [%v/%v] status is not updated", snapshot.Namespace, snapshot.Name)
return "", true, fmt.Errorf("%v", errMsg)
}
Expand All @@ -601,7 +601,22 @@ func (c *csiDriver) RestoreVolumeClaim(opts ...Option) (*v1.PersistentVolumeClai
logrus.Debugf("setting size of pvc %s/%s same as snapshot size %s", pvc.Namespace, pvc.Name, quantity.String())
pvc.Spec.Resources.Requests[v1.ResourceStorage] = quantity
}

}
// PB-8482 if restore size is nil then set the pvc size to annotation restoreSize
if snapshot.Status.RestoreSize == nil {
restoreSizeStr, ok := snapshot.Annotations["px-backup/restoreSize"]
if !ok {
return nil, fmt.Errorf("restoreSize not found either in status or in annotation")
}
restoreSize, err := resource.ParseQuantity(restoreSizeStr)
if err != nil {
return nil, fmt.Errorf("failed to parse restoreSize quantity provided in annotation px-backup/restoreSize: %v", err)
}
// Update the pvc size only when the volumesnapshot size is greater than PVC size
if restoreSize.Cmp(pvc.Spec.Resources.Requests[v1.ResourceStorage]) == 1 {
logrus.Debugf("setting size of pvc %s/%s same as snapshot size %s", pvc.Namespace, pvc.Name, restoreSizeStr)
pvc.Spec.Resources.Requests[v1.ResourceStorage] = restoreSize
}
}
} else {
snapshot, err := c.snapshotClient.SnapshotV1beta1().VolumeSnapshots(o.RestoreNamespace).Get(context.TODO(), o.RestoreSnapshotName, metav1.GetOptions{})
Expand All @@ -615,7 +630,7 @@ func (c *csiDriver) RestoreVolumeClaim(opts ...Option) (*v1.PersistentVolumeClai
errMsg := fmt.Sprintf("failed to get volumesnapshot [%v/%v]", snapshot.Namespace, snapshot.Name)
return "", true, fmt.Errorf("%v", errMsg)
}
if snapshot.Status == nil || snapshot.Status.RestoreSize == nil {
if snapshot.Status == nil {
errMsg := fmt.Sprintf("volumesnapshot [%v/%v] status is not updated", snapshot.Namespace, snapshot.Name)
return "", true, fmt.Errorf("%v", errMsg)
}
Expand All @@ -640,6 +655,22 @@ func (c *csiDriver) RestoreVolumeClaim(opts ...Option) (*v1.PersistentVolumeClai
}

}
// PB-8482 if restore size is nil then set the pvc size to annotation restoreSize
if snapshot.Status.RestoreSize == nil {
restoreSizeStr, ok := snapshot.Annotations["px-backup/restoreSize"]
if !ok {
return nil, fmt.Errorf("restoreSize not found either in status or in annotation")
}
restoreSize, err := resource.ParseQuantity(restoreSizeStr)
if err != nil {
return nil, fmt.Errorf("failed to parse restoreSize quantity provided in annotation px-backup/restoreSize: %v", err)
}
// Update the pvc size only when the volumesnapshot size is greater than PVC size
if restoreSize.Cmp(pvc.Spec.Resources.Requests[v1.ResourceStorage]) == 1 {
logrus.Debugf("setting size of pvc %s/%s same as snapshot size %s", pvc.Namespace, pvc.Name, restoreSizeStr)
pvc.Spec.Resources.Requests[v1.ResourceStorage] = restoreSize
}
}
}

pvc.Spec.DataSource = &v1.TypedLocalObjectReference{
Expand Down Expand Up @@ -1259,6 +1290,15 @@ func (c *csiDriver) restoreVolumeSnapshot(
vs.(*kSnapshotv1.VolumeSnapshot).Spec.Source.PersistentVolumeClaimName = nil
vs.(*kSnapshotv1.VolumeSnapshot).Spec.Source.VolumeSnapshotContentName = &vsc.(*kSnapshotv1.VolumeSnapshotContent).Name
vs.(*kSnapshotv1.VolumeSnapshot).Namespace = namespace
// PB-8482 Add restoreSize to annotations
// Convert RestoreSize to string and store it in restoreSizeStr
restoreSizeStr := vs.(*kSnapshotv1.VolumeSnapshot).Status.RestoreSize.String()
// Initialize Annotations map if it is nil
if vs.(*kSnapshotv1.VolumeSnapshot).Annotations == nil {
vs.(*kSnapshotv1.VolumeSnapshot).Annotations = make(map[string]string)
}
// Add the restoreSize annotation to the VolumeSnapshot object
vs.(*kSnapshotv1.VolumeSnapshot).Annotations["px-backup/restoreSize"] = restoreSizeStr
newVS, err = c.snapshotClient.SnapshotV1().VolumeSnapshots(namespace).Create(context.TODO(), vs.(*kSnapshotv1.VolumeSnapshot), metav1.CreateOptions{})
if err != nil {
if k8s_errors.IsAlreadyExists(err) {
Expand All @@ -1271,6 +1311,15 @@ func (c *csiDriver) restoreVolumeSnapshot(
vs.(*kSnapshotv1beta1.VolumeSnapshot).Spec.Source.PersistentVolumeClaimName = nil
vs.(*kSnapshotv1beta1.VolumeSnapshot).Spec.Source.VolumeSnapshotContentName = &vsc.(*kSnapshotv1beta1.VolumeSnapshotContent).Name
vs.(*kSnapshotv1beta1.VolumeSnapshot).Namespace = namespace
// PB-8482 Add restoreSize to annotations
// Convert RestoreSize to string and store it in restoreSizeStr
restoreSizeStr := vs.(*kSnapshotv1.VolumeSnapshot).Status.RestoreSize.String()
// Initialize Annotations map if it is nil
if vs.(*kSnapshotv1.VolumeSnapshot).Annotations == nil {
vs.(*kSnapshotv1.VolumeSnapshot).Annotations = make(map[string]string)
}
// Add the restoreSize annotation to the VolumeSnapshot object
vs.(*kSnapshotv1.VolumeSnapshot).Annotations["px-backup/restoreSize"] = restoreSizeStr
newVS, err = c.snapshotClient.SnapshotV1beta1().VolumeSnapshots(namespace).Create(context.TODO(), vs.(*kSnapshotv1beta1.VolumeSnapshot), metav1.CreateOptions{})
if err != nil {
if k8s_errors.IsAlreadyExists(err) {
Expand Down

0 comments on commit 2ae9d2b

Please sign in to comment.