Skip to content

Commit

Permalink
refactor(vm): wrap KVMI ready reasons (#565)
Browse files Browse the repository at this point in the history
* feat(vm): wrap VM ready reasons

Signed-off-by: Daniil Antoshin <[email protected]>
  • Loading branch information
danilrwx authored Dec 11, 2024
1 parent fb2423c commit afe682a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
5 changes: 5 additions & 0 deletions api/core/v1alpha2/vmcondition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,9 @@ const (

ReasonBlockDeviceCapacityAvailable Reason = "BlockDeviceCapacityAvailable"
ReasonBlockDeviceCapacityReached Reason = "BlockDeviceCapacityReached"

ReasonPodTerminatingReason Reason = "PodTerminating"
ReasonPodNotExistsReason Reason = "PodNotExists"
ReasonPodConditionMissingReason Reason = "PodConditionMissing"
ReasonGuestNotRunningReason Reason = "GuestNotRunning"
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,3 @@ func (cr CommonReason) String() string {
const (
ReasonUnknown CommonReason = "Unknown"
)

// Deprecated: avoid using this wrapper.
// TODO: get rid of this wrapper.
type DeprecatedWrappedString string

func (s DeprecatedWrappedString) String() string {
if s == "" {
return "Unknown"
}
return string(s)
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ func (h *LifeCycleHandler) syncRunning(vm *virtv2.VirtualMachine, kvvm *virtv1.V
for _, c := range kvvmi.Status.Conditions {
if c.Type == virtv1.VirtualMachineInstanceReady {
cb.Status(conditionStatus(string(c.Status))).
//nolint:staticcheck
Reason(conditions.DeprecatedWrappedString(c.Reason)).
Reason(getKVMIReadyReason(c.Reason)).
Message(c.Message)
conditions.SetCondition(cb, &vm.Status.Conditions)
return
Expand Down
23 changes: 23 additions & 0 deletions images/virtualization-artifact/pkg/controller/vm/internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,29 @@ var mapPhases = map[virtv1.VirtualMachinePrintableStatus]virtv2.MachinePhase{

const kvvmEmptyPhase virtv1.VirtualMachinePrintableStatus = ""

func getKVMIReadyReason(kvmiReason string) conditions.Stringer {
if r, ok := mapReasons[kvmiReason]; ok {
return r
}

if kvmiReason == "" {
return conditions.ReasonUnknown
}

return conditions.CommonReason(kvmiReason)
}

var mapReasons = map[string]vmcondition.Reason{
// PodTerminatingReason indicates on the Ready condition on the VMI if the underlying pod is terminating
virtv1.PodTerminatingReason: vmcondition.ReasonPodTerminatingReason,
// PodNotExistsReason indicates on the Ready condition on the VMI if the underlying pod does not exist
virtv1.PodNotExistsReason: vmcondition.ReasonPodNotExistsReason,
// PodConditionMissingReason indicates on the Ready condition on the VMI if the underlying pod does not report a Ready condition
virtv1.PodConditionMissingReason: vmcondition.ReasonPodConditionMissingReason,
// GuestNotRunningReason indicates on the Ready condition on the VMI if the underlying guest VM is not running
virtv1.GuestNotRunningReason: vmcondition.ReasonGuestNotRunningReason,
}

func isPodStartedError(phase virtv1.VirtualMachinePrintableStatus) bool {
return slices.Contains([]virtv1.VirtualMachinePrintableStatus{
virtv1.VirtualMachineStatusErrImagePull,
Expand Down

0 comments on commit afe682a

Please sign in to comment.