[virt-controller]: Small refactoring to avoid code duplication
@@ -543,20 +543,26 @@ func (c *VMController) startStop(vm *virtv1.VirtualMachine, vmi *virtv1.VirtualM
}
log.Log.Object(vm).V(4).Infof("VirtualMachine RunStrategy: %s", runStrategy)
- switch runStrategy {
- case virtv1.RunStrategyAlways:
- // For this RunStrategy, a VMI should always be running. If a StateChangeRequest
- // asks to stop a VMI, a new one must be immediately re-started.
- if vmi != nil {
- forceRestart := false
+ isStopRequestForVMI := func(vm *virtv1.VirtualMachine) bool {
if len(vm.Status.StateChangeRequests) != 0 {
stateChange := vm.Status.StateChangeRequests[0]
if stateChange.Action == virtv1.StopRequest &&
stateChange.UID != nil &&
*stateChange.UID == vmi.UID {
- forceRestart = true
- log.Log.Object(vm).Infof("processing forced restart request for VMI with phase %s and VM runStrategy: %s", vmi.Status.Phase, runStrategy)
+ return true
+ }
}
+ return false
+ }
+
+ switch runStrategy {
+ case virtv1.RunStrategyAlways:
+ // For this RunStrategy, a VMI should always be running. If a StateChangeRequest
+ // asks to stop a VMI, a new one must be immediately re-started.
+ if vmi != nil {
+ var forceRestart bool
+ if forceRestart = isStopRequestForVMI(vm); forceRestart {
+ log.Log.Object(vm).Infof("processing forced restart request for VMI with phase %s and VM runStrategy: %s", vmi.Status.Phase, runStrategy)
}
if forceRestart || vmi.IsFinal() {
@@ -588,16 +594,10 @@ func (c *VMController) startStop(vm *virtv1.VirtualMachine, vmi *virtv1.VirtualM
// For this RunStrategy, a VMI should only be restarted if it failed.
// If a VMI enters the Succeeded phase, it should not be restarted.
if vmi != nil {
- forceStop := false
- // If there's a stop request that matches the existing VMI's UUID
- if len(vm.Status.StateChangeRequests) != 0 {
- stateChange := vm.Status.StateChangeRequests[0]
- if stateChange.Action == virtv1.StopRequest &&
- stateChange.UID != nil &&
- *stateChange.UID == vmi.UID {
+ var forceStop bool
+ if forceStop = isStopRequestForVMI(vm); forceStop {
log.Log.Object(vm).Infof("processing stop request for VMI with phase %s and VM runStrategy: %s", vmi.Status.Phase, runStrategy)
- forceStop = true
- }
+
}
if forceStop || vmi.Status.Phase == virtv1.Failed {
@@ -626,16 +626,8 @@ func (c *VMController) startStop(vm *virtv1.VirtualMachine, vmi *virtv1.VirtualM
// For this RunStrategy, VMI's will be started/stopped/restarted using api endpoints only
if vmi != nil {
log.Log.Object(vm).V(4).Info("VMI exists")
- forceStop := false
- if len(vm.Status.StateChangeRequests) != 0 {
- stateChange := vm.Status.StateChangeRequests[0]
- if stateChange.Action == virtv1.StopRequest &&
- stateChange.UID != nil &&
- *stateChange.UID == vmi.UID {
- forceStop = true
- }
- }
- if forceStop {
+
+ if forceStop := isStopRequestForVMI(vm); forceStop {
log.Log.Object(vm).Infof("%s with VMI in phase %s due to stop request and VM runStrategy: %s", vmi.Status.Phase, stoppingVmMsg, runStrategy)
err := c.stopVMI(vm, vmi)
if err != nil {
