Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vd): copy error from data volume #166

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,12 +235,26 @@ 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 {
var err error

if storageClassName == nil || *storageClassName == "" {
return s.checkDefaultStorageClass(ctx)
err = s.checkDefaultStorageClass(ctx)
} else {
err = s.checkStorageClass(ctx, *storageClassName)
}
if err != nil {
return err
}

return s.checkStorageClass(ctx, *storageClassName)
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)
}
}

return nil
}

func (s DiskService) checkDefaultStorageClass(ctx context.Context) error {
Expand All @@ -251,7 +265,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
Loading