Skip to content

Commit

Permalink
Synthesize v1a1 Prereq condition from v1a2 conditions
Browse files Browse the repository at this point in the history
There is various weirdness around how and what we set the v1a1 Prereq
condition to, so this isn't quite a complete match but hopefully good
enough.

For now just leave the conditions as-is when going from v1a1 to v1a2. We
might later want to remove the Prereq - or other v1a1 only conditions -
during that conversion.
  • Loading branch information
bryanv committed Oct 17, 2023
1 parent 0b17895 commit bf08fa9
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions api/v1alpha1/virtualmachine_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,88 @@ func Convert_v1alpha1_VirtualMachineStatus_To_v1alpha2_VirtualMachineStatus(
return nil
}

func convert_v1alpha2_Conditions_To_v1alpha1_Conditions(conditions []Condition) []Condition {
var preReqCond, vmClassCond, vmImageCond, vmSetResourcePolicy, vmBootstrap *Condition

for i := range conditions {
c := &conditions[i]

switch c.Type {
case VirtualMachinePrereqReadyCondition:
preReqCond = c
case v1alpha2.VirtualMachineClassConditionReady:
vmClassCond = c
case v1alpha2.VirtualMachineConditionImageReady:
vmImageCond = c
case v1alpha2.VirtualMachineConditionVMSetResourcePolicyReady:
vmSetResourcePolicy = c
case v1alpha2.VirtualMachineConditionBootstrapReady:
vmBootstrap = c
}
}

// Try to replicate how the v1a1 provider would set the singular prereqs condition. The class is checked
// first, then the image. Note that the set resource policy and metadata (bootstrap) are not a part of
// the v1a1 prereqs, and are optional.
if vmClassCond != nil && vmClassCond.Status == corev1.ConditionTrue &&
vmImageCond != nil && vmImageCond.Status == corev1.ConditionTrue &&
(vmSetResourcePolicy == nil || vmSetResourcePolicy.Status == corev1.ConditionTrue) &&
(vmBootstrap == nil || vmBootstrap.Status == corev1.ConditionTrue) {

p := Condition{
Type: VirtualMachinePrereqReadyCondition,
Status: corev1.ConditionTrue,
}

if preReqCond != nil {
p.LastTransitionTime = preReqCond.LastTransitionTime
*preReqCond = p
return conditions
}

p.LastTransitionTime = vmImageCond.LastTransitionTime
return append(conditions, p)
}

p := Condition{
Type: VirtualMachinePrereqReadyCondition,
Status: corev1.ConditionFalse,
Severity: ConditionSeverityError,
}

if vmClassCond != nil && vmClassCond.Status == corev1.ConditionFalse {
p.Reason = VirtualMachineClassNotFoundReason
p.Message = vmClassCond.Message
p.LastTransitionTime = vmClassCond.LastTransitionTime
} else if vmImageCond != nil && vmImageCond.Status == corev1.ConditionFalse {
p.Reason = VirtualMachineImageNotFoundReason
p.Message = vmImageCond.Message
p.LastTransitionTime = vmImageCond.LastTransitionTime
}

if p.Reason != "" {
if preReqCond != nil {
*preReqCond = p
return conditions
}

return append(conditions, p)
}

if vmSetResourcePolicy != nil && vmSetResourcePolicy.Status == corev1.ConditionFalse &&
vmBootstrap != nil && vmBootstrap.Status == corev1.ConditionFalse {

// These are not a part of the v1a1 Prereqs. If either is false, the v1a1 provider would not
// update the prereqs condition, but we don't set the condition to true either until all these
// conditions are true. Just leave things as is to see how strict we really need to be here.
return conditions
}

// TBD: For now, leave the v1a2 conditions if present since those provide more details.

return conditions
}

func Convert_v1alpha2_VirtualMachineStatus_To_v1alpha1_VirtualMachineStatus(
in *v1alpha2.VirtualMachineStatus, out *VirtualMachineStatus, s apiconversion.Scope) error {

Expand All @@ -552,6 +634,7 @@ func Convert_v1alpha2_VirtualMachineStatus_To_v1alpha1_VirtualMachineStatus(
out.Phase = convert_v1alpha2_Conditions_To_v1alpha1_Phase(in.Conditions)
out.VmIp, out.NetworkInterfaces = convert_v1alpha2_NetworkStatus_To_v1alpha1_Network(in.Network)
out.LastRestartTime = in.LastRestartTime
out.Conditions = convert_v1alpha2_Conditions_To_v1alpha1_Conditions(out.Conditions)

// WARNING: in.Image requires manual conversion: does not exist in peer-type
// WARNING: in.Class requires manual conversion: does not exist in peer-type
Expand Down

0 comments on commit bf08fa9

Please sign in to comment.