Skip to content

Commit

Permalink
fix(vd): copy error from data volume
Browse files Browse the repository at this point in the history
Signed-off-by: Isteb4k <[email protected]>
  • Loading branch information
Isteb4k committed Jun 26, 2024
1 parent 1689580 commit d1bf6b1
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 8 deletions.
10 changes: 10 additions & 0 deletions images/virtualization-artifact/pkg/controller/service/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"unicode"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"

"github.com/deckhouse/virtualization/api/core/v1alpha2/cvicondition"
)
Expand Down Expand Up @@ -60,3 +61,12 @@ func CapitalizeFirstLetter(s string) string {

return string(runes)
}

func GetDataVolumeCondition(conditionType cdiv1.DataVolumeConditionType, conditions []cdiv1.DataVolumeCondition) *cdiv1.DataVolumeCondition {
for i, condition := range conditions {
if condition.Type == conditionType {
return &conditions[i]
}
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,14 @@ func (s DiskService) GetPersistentVolume(ctx context.Context, pvc *corev1.Persis
return helper.FetchObject(ctx, types.NamespacedName{Name: pvc.Spec.VolumeName}, s.client, &corev1.PersistentVolume{})
}

func (s DiskService) CheckStorageClass(ctx context.Context, storageClassName *string) error {
func (s DiskService) CheckImportProcess(ctx context.Context, dv *cdiv1.DataVolume, storageClassName *string) error {
if dv != nil {
dvRunning := GetDataVolumeCondition(cdiv1.DataVolumeRunning, dv.Status.Conditions)
if dvRunning != nil && dvRunning.Status == corev1.ConditionFalse && dvRunning.Reason == "Error" {
return fmt.Errorf("%w: %s", ErrDataVolumeNotRunning, dvRunning.Message)
}
}

if storageClassName == nil || *storageClassName == "" {
return s.checkDefaultStorageClass(ctx)
}
Expand All @@ -251,7 +258,6 @@ func (s DiskService) checkDefaultStorageClass(ctx context.Context) error {
}

for _, sc := range scs.Items {
// TODO comment.
if sc.Annotations[common.AnnDefaultStorageClass] == "true" {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ var ErrTooSmallDiskSize = errors.New("virtual disk size is too small")
var (
ErrStorageClassNotFound = errors.New("storage class not found")
ErrDefaultStorageClassNotFound = errors.New("default storage class not found")
ErrDataVolumeNotRunning = errors.New("pvc import is not running")
)
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,20 @@ func (ds BlankDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (boo
return false, err
}

err = ds.diskService.CheckStorageClass(ctx, vd.Spec.PersistentVolumeClaim.StorageClass)
err = ds.diskService.CheckImportProcess(ctx, dv, vd.Spec.PersistentVolumeClaim.StorageClass)
switch {
case err == nil:
vd.Status.Phase = virtv2.DiskProvisioning
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Provisioning
condition.Message = "Import is in the process of provisioning to PVC."
return false, nil
case errors.Is(err, service.ErrDataVolumeNotRunning):
vd.Status.Phase = virtv2.DiskFailed
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.ProvisioningFailed
condition.Message = service.CapitalizeFirstLetter(err.Error())
return false, nil
case errors.Is(err, service.ErrStorageClassNotFound):
vd.Status.Phase = virtv2.DiskFailed
condition.Status = metav1.ConditionFalse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,20 @@ func (ds HTTPDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (bool
return false, err
}

err = ds.diskService.CheckStorageClass(ctx, vd.Spec.PersistentVolumeClaim.StorageClass)
err = ds.diskService.CheckImportProcess(ctx, dv, vd.Spec.PersistentVolumeClaim.StorageClass)
switch {
case err == nil:
vd.Status.Phase = virtv2.DiskProvisioning
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Provisioning
condition.Message = "Import is in the process of provisioning to PVC."
return false, nil
case errors.Is(err, service.ErrDataVolumeNotRunning):
vd.Status.Phase = virtv2.DiskFailed
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.ProvisioningFailed
condition.Message = service.CapitalizeFirstLetter(err.Error())
return false, nil
case errors.Is(err, service.ErrStorageClassNotFound):
vd.Status.Phase = virtv2.DiskFailed
condition.Status = metav1.ConditionFalse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,20 @@ func (ds ObjectRefDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk)
return false, err
}

err = ds.diskService.CheckStorageClass(ctx, vd.Spec.PersistentVolumeClaim.StorageClass)
err = ds.diskService.CheckImportProcess(ctx, dv, vd.Spec.PersistentVolumeClaim.StorageClass)
switch {
case err == nil:
vd.Status.Phase = virtv2.DiskProvisioning
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Provisioning
condition.Message = "Import is in the process of provisioning to PVC."
return false, nil
case errors.Is(err, service.ErrDataVolumeNotRunning):
vd.Status.Phase = virtv2.DiskFailed
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.ProvisioningFailed
condition.Message = service.CapitalizeFirstLetter(err.Error())
return false, nil
case errors.Is(err, service.ErrStorageClassNotFound):
vd.Status.Phase = virtv2.DiskFailed
condition.Status = metav1.ConditionFalse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,20 @@ func (ds RegistryDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (
return false, err
}

err = ds.diskService.CheckStorageClass(ctx, vd.Spec.PersistentVolumeClaim.StorageClass)
err = ds.diskService.CheckImportProcess(ctx, dv, vd.Spec.PersistentVolumeClaim.StorageClass)
switch {
case err == nil:
vd.Status.Phase = virtv2.DiskProvisioning
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Provisioning
condition.Message = "Import is in the process of provisioning to PVC."
return false, nil
case errors.Is(err, service.ErrDataVolumeNotRunning):
vd.Status.Phase = virtv2.DiskFailed
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.ProvisioningFailed
condition.Message = service.CapitalizeFirstLetter(err.Error())
return false, nil
case errors.Is(err, service.ErrStorageClassNotFound):
vd.Status.Phase = virtv2.DiskFailed
condition.Status = metav1.ConditionFalse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,20 @@ func (ds UploadDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (bo
return false, err
}

err = ds.diskService.CheckStorageClass(ctx, vd.Spec.PersistentVolumeClaim.StorageClass)
err = ds.diskService.CheckImportProcess(ctx, dv, vd.Spec.PersistentVolumeClaim.StorageClass)
switch {
case err == nil:
vd.Status.Phase = virtv2.DiskProvisioning
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Provisioning
condition.Message = "Import is in the process of provisioning to PVC."
return false, nil
case errors.Is(err, service.ErrDataVolumeNotRunning):
vd.Status.Phase = virtv2.DiskFailed
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.ProvisioningFailed
condition.Message = service.CapitalizeFirstLetter(err.Error())
return false, nil
case errors.Is(err, service.ErrStorageClassNotFound):
vd.Status.Phase = virtv2.DiskFailed
condition.Status = metav1.ConditionFalse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr
return false
}

return oldDV.Status.Progress != newDV.Status.Progress
if oldDV.Status.Progress != newDV.Status.Progress {
return true
}

dvRunning := service.GetDataVolumeCondition(cdiv1.DataVolumeRunning, newDV.Status.Conditions)
return dvRunning != nil && dvRunning.Reason == "Error"
},
},
); err != nil {
Expand Down

0 comments on commit d1bf6b1

Please sign in to comment.