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): set ready phase #202

Merged
merged 1 commit into from
Jul 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package source
import (
"context"
"errors"
"fmt"
"log/slog"

"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -76,20 +75,7 @@ func (ds BlankDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (boo
case isDiskProvisioningFinished(condition):
logger.Info("Disk provisioning finished: clean up")

switch {
case pvc == nil:
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PVC %s not found.", supgen.PersistentVolumeClaim().String())
case pv == nil:
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PV %s not found.", pvc.Spec.VolumeName)
default:
condition.Status = metav1.ConditionTrue
condition.Reason = vdcondition.Ready
condition.Message = ""
}
setPhaseConditionForFinishedDisk(pv, pvc, &condition, &vd.Status.Phase, supgen)

// Protect Ready Disk and underlying PVC and PV.
err = ds.diskService.Protect(ctx, vd, nil, pvc, pv)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package source
import (
"context"
"errors"
"fmt"
"log/slog"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -92,23 +91,7 @@ func (ds HTTPDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (bool
case isDiskProvisioningFinished(condition):
logger.Info("Disk provisioning finished: clean up")

switch {
case pvc == nil:
vd.Status.Phase = virtv2.DiskLost
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PVC %s not found.", supgen.PersistentVolumeClaim().String())
case pv == nil:
vd.Status.Phase = virtv2.DiskLost
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PV %s not found.", pvc.Spec.VolumeName)
default:
vd.Status.Phase = virtv2.DiskReady
condition.Status = metav1.ConditionTrue
condition.Reason = vdcondition.Ready
condition.Message = ""
}
setPhaseConditionForFinishedDisk(pv, pvc, &condition, &vd.Status.Phase, supgen)

// Protect Ready Disk and underlying PVC and PV.
err = ds.diskService.Protect(ctx, vd, nil, pvc, pv)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,7 @@ func (ds ObjectRefDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk)
case isDiskProvisioningFinished(condition):
logger.Info("Disk provisioning finished: clean up")

switch {
case pvc == nil:
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PVC %s not found.", supgen.PersistentVolumeClaim().String())
case pv == nil:
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PV %s not found.", pvc.Spec.VolumeName)
default:
condition.Status = metav1.ConditionTrue
condition.Reason = vdcondition.Ready
condition.Message = ""
}
setPhaseConditionForFinishedDisk(pv, pvc, &condition, &vd.Status.Phase, supgen)

// Protect Ready Disk and underlying PVC and PV.
err = ds.diskService.Protect(ctx, vd, nil, pvc, pv)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,7 @@ func (ds RegistryDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (
case isDiskProvisioningFinished(condition):
logger.Info("Disk provisioning finished: clean up")

switch {
case pvc == nil:
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PVC %s not found.", supgen.PersistentVolumeClaim().String())
case pv == nil:
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PV %s not found.", pvc.Spec.VolumeName)
default:
condition.Status = metav1.ConditionTrue
condition.Reason = vdcondition.Ready
condition.Message = ""
}
setPhaseConditionForFinishedDisk(pv, pvc, &condition, &vd.Status.Phase, supgen)

// Protect Ready Disk and underlying PVC and PV.
err = ds.diskService.Protect(ctx, vd, nil, pvc, pv)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ package source

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

cc "github.com/deckhouse/virtualization-controller/pkg/controller/common"
"github.com/deckhouse/virtualization-controller/pkg/controller/supplements"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition"
)
Expand Down Expand Up @@ -89,3 +92,29 @@ func CleanUpSupplements(ctx context.Context, vd *virtv2.VirtualDisk, c Cleaner)
func isDiskProvisioningFinished(c metav1.Condition) bool {
return c.Reason == vdcondition.Ready || c.Reason == vdcondition.Lost
}

func setPhaseConditionForFinishedDisk(
pv *corev1.PersistentVolume,
pvc *corev1.PersistentVolumeClaim,
condition *metav1.Condition,
phase *virtv2.DiskPhase,
supgen *supplements.Generator,
) {
switch {
case pvc == nil:
*phase = virtv2.DiskLost
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PVC %s not found.", supgen.PersistentVolumeClaim().String())
case pv == nil:
*phase = virtv2.DiskLost
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PV %s not found.", pvc.Spec.VolumeName)
default:
*phase = virtv2.DiskReady
condition.Status = metav1.ConditionTrue
condition.Reason = vdcondition.Ready
condition.Message = ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,7 @@ func (ds UploadDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (bo
case isDiskProvisioningFinished(condition):
logger.Info("Disk provisioning finished: clean up")

switch {
case pvc == nil:
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PVC %s not found.", supgen.PersistentVolumeClaim().String())
case pv == nil:
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PV %s not found.", pvc.Spec.VolumeName)
default:
condition.Status = metav1.ConditionTrue
condition.Reason = vdcondition.Ready
condition.Message = ""
}
setPhaseConditionForFinishedDisk(pv, pvc, &condition, &vd.Status.Phase, supgen)

// Protect Ready Disk and underlying PVC and PV.
err = ds.diskService.Protect(ctx, vd, nil, pvc, pv)
Expand Down
Loading