Skip to content

Commit

Permalink
fix(vd): set ready phase
Browse files Browse the repository at this point in the history
Signed-off-by: Isteb4k <[email protected]>
  • Loading branch information
Isteb4k committed Jul 9, 2024
1 parent e336c82 commit a9a4c90
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 75 deletions.
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

0 comments on commit a9a4c90

Please sign in to comment.