Skip to content

Commit bcdb347

Browse files
committed
feat(vi): create volume snapshot for vi on the pvc
Signed-off-by: Isteb4k <[email protected]>
1 parent 9baa163 commit bcdb347

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

images/virtualization-artifact/pkg/controller/service/disk_service.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,34 @@ func (s DiskService) CheckProvisioning(ctx context.Context, pvc *corev1.Persiste
238238
return nil
239239
}
240240

241+
func (s DiskService) CreateVolumeSnapshot(ctx context.Context, pvc *corev1.PersistentVolumeClaim) error {
242+
if pvc == nil || pvc.Status.Phase == corev1.ClaimBound {
243+
return errors.New("pvc not Bound")
244+
}
245+
246+
vs := &vsv1.VolumeSnapshot{
247+
ObjectMeta: metav1.ObjectMeta{
248+
Name: pvc.Name,
249+
Namespace: pvc.Namespace,
250+
OwnerReferences: []metav1.OwnerReference{
251+
MakeOwnerReference(pvc),
252+
},
253+
},
254+
Spec: vsv1.VolumeSnapshotSpec{
255+
Source: vsv1.VolumeSnapshotSource{
256+
PersistentVolumeClaimName: &pvc.Name,
257+
},
258+
},
259+
}
260+
261+
err := s.client.Create(ctx, vs)
262+
if err != nil && !k8serrors.IsAlreadyExists(err) {
263+
return fmt.Errorf("create vs: %w", err)
264+
}
265+
266+
return nil
267+
}
268+
241269
func (s DiskService) CreatePersistentVolumeClaim(ctx context.Context, pvc *corev1.PersistentVolumeClaim) error {
242270
err := s.client.Create(ctx, pvc)
243271
if err != nil && !k8serrors.IsAlreadyExists(err) {

images/virtualization-artifact/pkg/controller/vi/internal/source/http.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"time"
2424

25+
vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
2526
corev1 "k8s.io/api/core/v1"
2627
storagev1 "k8s.io/api/storage/v1"
2728
"k8s.io/apimachinery/pkg/api/resource"
@@ -379,6 +380,48 @@ func (ds HTTPDataSource) StoreToPVC(ctx context.Context, vi *v1alpha2.VirtualIma
379380
"The HTTP DataSource import has completed",
380381
)
381382

383+
_, exists := vi.Annotations["virtualization.deckhouse.io/use-volume-snapshot"]
384+
if exists {
385+
var vs *vsv1.VolumeSnapshot
386+
vs, err = ds.diskService.GetVolumeSnapshot(ctx, pvc.Name, pvc.Namespace)
387+
if err != nil {
388+
vi.Status.Phase = v1alpha2.ImageFailed
389+
cb.
390+
Status(metav1.ConditionFalse).
391+
Reason(vicondition.ProvisioningFailed).
392+
Message(err.Error())
393+
return reconcile.Result{}, nil
394+
}
395+
396+
if vs == nil {
397+
err = ds.diskService.CreateVolumeSnapshot(ctx, pvc)
398+
if err != nil {
399+
vi.Status.Phase = v1alpha2.ImageFailed
400+
cb.
401+
Status(metav1.ConditionFalse).
402+
Reason(vicondition.ProvisioningFailed).
403+
Message(err.Error())
404+
return reconcile.Result{}, nil
405+
}
406+
407+
vi.Status.Phase = v1alpha2.ImageProvisioning
408+
cb.
409+
Status(metav1.ConditionFalse).
410+
Reason(vicondition.Provisioning).
411+
Message("The VolumeSnapshot has been created.")
412+
return reconcile.Result{RequeueAfter: time.Second}, nil
413+
}
414+
415+
if vs.Status.ReadyToUse == nil || !(*vs.Status.ReadyToUse) {
416+
vi.Status.Phase = v1alpha2.ImageProvisioning
417+
cb.
418+
Status(metav1.ConditionFalse).
419+
Reason(vicondition.Provisioning).
420+
Message("Waiting for the VolumeSnapshot to be ready to use.")
421+
return reconcile.Result{RequeueAfter: time.Second}, nil
422+
}
423+
}
424+
382425
vi.Status.Phase = v1alpha2.ImageReady
383426
cb.
384427
Status(metav1.ConditionTrue).

0 commit comments

Comments
 (0)