Skip to content

Commit 31b605f

Browse files
committed
Create VolumeSnapshotContent and then VolumeSnapshot
1 parent 50f7410 commit 31b605f

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

pkg/snapshot/volumes/csi/restorer.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,35 +158,37 @@ func (r *Restorer) reconcileInProgressPVC(ctx context.Context, restoreRequestNam
158158
// PVC hasn't been found, restore it from VolumeSnapshot. For this we need pre-provisioned VolumeSnapshot
159159
// and VolumeSnapshotContent resources.
160160

161-
// Check if the pre-provisioned VolumeSnapshot resource exists. If it doesn't, create it.
162161
volumeSnapshotName := fmt.Sprintf("%s-%s", config.PersistentVolumeClaim.Name, restoreRequestName)
163162
pvcName := types.NamespacedName{
164163
Namespace: config.PersistentVolumeClaim.Namespace,
165164
Name: config.PersistentVolumeClaim.Name,
166165
}
167-
volumeSnapshot, err := r.snapshotsClient.SnapshotV1().VolumeSnapshots(pvcName.Namespace).Get(ctx, volumeSnapshotName, metav1.GetOptions{})
166+
167+
// Check if the pre-provisioned VolumeSnapshotContent resource exists. If it doesn't, create it.
168168
justCreated := false
169+
volumeSnapshotContent, err := r.snapshotsClient.SnapshotV1().VolumeSnapshotContents().Get(ctx, volumeSnapshotName, metav1.GetOptions{})
169170
if kerrors.IsNotFound(err) {
170-
// create new VolumeSnapshot
171-
volumeSnapshot, err = r.createVolumeSnapshotResource(ctx, restoreRequestName, volumeSnapshotName, pvcName, config.VolumeSnapshotClassName)
171+
// create new VolumeSnapshotContent
172+
volumeSnapshotContent, err = r.createVolumeSnapshotContentResource(ctx, restoreRequestName, volumeSnapshotName, config, restoreStatus)
172173
if err != nil {
173-
return status, fmt.Errorf("failed to create VolumeSnapshot for the PersistentVolumeClaim %s: %w", pvcName, err)
174+
return status, fmt.Errorf("failed to create VolumeSnapshotContent for the PersistentVolumeClaim %s: %w", pvcName, err)
174175
}
175176
justCreated = true
176-
// we don't wait for the VolumeSnapshot to be ready, because it also needs the VolumeSnapshotContent to become ready
177177
} else if err != nil {
178-
return status, fmt.Errorf("failed to get VolumeSnapshot %s/%s: %w", volumeSnapshot.Namespace, volumeSnapshot.Name, err)
178+
return status, fmt.Errorf("failed to get VolumeSnapshotContent %s: %w", volumeSnapshotContent.Name, err)
179179
}
180180

181-
// Check if the pre-provisioned VolumeSnapshotContent resource exists. If it doesn't, create it.
182-
volumeSnapshotContent, err := r.snapshotsClient.SnapshotV1().VolumeSnapshotContents().Get(ctx, volumeSnapshotName, metav1.GetOptions{})
181+
// Check if the pre-provisioned VolumeSnapshot resource exists. If it doesn't, create it.
182+
volumeSnapshot, err := r.snapshotsClient.SnapshotV1().VolumeSnapshots(pvcName.Namespace).Get(ctx, volumeSnapshotName, metav1.GetOptions{})
183183
if kerrors.IsNotFound(err) {
184-
// create new VolumeSnapshotContent
185-
volumeSnapshotContent, err = r.createVolumeSnapshotContentResource(ctx, restoreRequestName, volumeSnapshotName, config, restoreStatus)
184+
// create new VolumeSnapshot
185+
volumeSnapshot, err = r.createVolumeSnapshotResource(ctx, restoreRequestName, volumeSnapshotName, pvcName, config.VolumeSnapshotClassName)
186186
if err != nil {
187-
return status, fmt.Errorf("failed to create VolumeSnapshotContent for the PersistentVolumeClaim %s: %w", pvcName, err)
187+
return status, fmt.Errorf("failed to create VolumeSnapshot for the PersistentVolumeClaim %s: %w", pvcName, err)
188188
}
189189
justCreated = true
190+
} else if err != nil {
191+
return status, fmt.Errorf("failed to get VolumeSnapshot %s/%s: %w", volumeSnapshot.Namespace, volumeSnapshot.Name, err)
190192
}
191193

192194
if justCreated {
@@ -196,10 +198,14 @@ func (r *Restorer) reconcileInProgressPVC(ctx context.Context, restoreRequestNam
196198

197199
// check if VolumeSnapshot has failed
198200
if volumeSnapshot.Status.Error != nil {
199-
// VolumeSnapshot has failed
200201
var errorMessage string
201202
if volumeSnapshot.Status.Error.Message != nil {
202-
errorMessage = *volumeSnapshot.Status.Error.Message
203+
errorMessage = fmt.Sprintf(
204+
"VolumeSnapshot %s/%s (for PersistentVolumeClaim %s) has a status error message %s",
205+
volumeSnapshot.Namespace,
206+
volumeSnapshot.Name,
207+
pvcName.String(),
208+
*volumeSnapshot.Status.Error.Message)
203209
} else {
204210
errorMessage = fmt.Sprintf(
205211
"VolumeSnapshot %s/%s (for PersistentVolumeClaim %s) has failed with an unknown error",
@@ -219,10 +225,13 @@ func (r *Restorer) reconcileInProgressPVC(ctx context.Context, restoreRequestNam
219225

220226
// check if VolumeSnapshotContent has failed
221227
if volumeSnapshotContent.Status.Error != nil {
222-
// volumeSnapshotContent has failed
223228
var errorMessage string
224229
if volumeSnapshotContent.Status.Error.Message != nil {
225-
errorMessage = *volumeSnapshotContent.Status.Error.Message
230+
errorMessage = fmt.Sprintf(
231+
"VolumeSnapshotContent %s (for PersistentVolumeClaim %s) has a status error message: %s",
232+
volumeSnapshotContent.Name,
233+
pvcName.String(),
234+
*volumeSnapshotContent.Status.Error.Message)
226235
} else {
227236
errorMessage = fmt.Sprintf(
228237
"VolumeSnapshotContent %s (for PersistentVolumeClaim %s) has failed with an unknown error",

0 commit comments

Comments
 (0)