Skip to content

Commit 7d139a8

Browse files
committed
ws-manager: return error if restore volume snapshot to smaller PVC size
Signed-off-by: JenTing Hsiao <[email protected]>
1 parent 35a2760 commit 7d139a8

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

components/ws-manager/pkg/manager/manager.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ func (m *Manager) StartWorkspace(ctx context.Context, req *api.StartWorkspaceReq
263263
_, createPVC = pod.Labels[pvcWorkspaceFeatureLabel]
264264

265265
if createPVC {
266+
var volumeSnapshot volumesnapshotv1.VolumeSnapshot
266267
if startContext.VolumeSnapshot != nil && startContext.VolumeSnapshot.VolumeSnapshotName != "" {
267-
var volumeSnapshot volumesnapshotv1.VolumeSnapshot
268268
err = m.Clientset.Get(ctx, types.NamespacedName{Namespace: m.Config.Namespace, Name: startContext.VolumeSnapshot.VolumeSnapshotName}, &volumeSnapshot)
269269
if k8serr.IsNotFound(err) {
270270
// restore volume snapshot from handle
@@ -273,11 +273,30 @@ func (m *Manager) StartWorkspace(ctx context.Context, req *api.StartWorkspaceReq
273273
clog.WithError(err).Error("was unable to restore volume snapshot")
274274
return nil, err
275275
}
276+
277+
// get again to update the volumeSnapshot variable
278+
if err := m.Clientset.Get(ctx, types.NamespacedName{Namespace: m.Config.Namespace, Name: startContext.VolumeSnapshot.VolumeSnapshotName}, &volumeSnapshot); err != nil {
279+
return nil, err
280+
}
276281
} else if err != nil {
277282
clog.WithError(err).Error("was unable to get volume snapshot")
278283
return nil, err
279284
}
285+
286+
// check the PVC size is not less than the volume snapshot size
287+
PVCConfig := m.Config.WorkspaceClasses[config.DefaultWorkspaceClass].PVC
288+
if startContext.Class != nil {
289+
PVCConfig = startContext.Class.PVC
290+
}
291+
292+
if volumeSnapshot.Status != nil &&
293+
volumeSnapshot.Status.RestoreSize != nil &&
294+
PVCConfig.Size.Cmp(*volumeSnapshot.Status.RestoreSize) == -1 {
295+
return nil, xerrors.Errorf("cannot restore volume snapshot from size %s to pvc size %s", volumeSnapshot.Status.RestoreSize.String(), PVCConfig.Size.String())
296+
}
280297
}
298+
299+
// create PVC object
281300
pvc, err = m.createPVCForWorkspacePod(startContext)
282301
if err != nil {
283302
return nil, xerrors.Errorf("cannot create pvc for workspace pod: %w", err)

0 commit comments

Comments
 (0)