Skip to content

Commit

Permalink
refactor: improve code for wait vm to reach the specified state (#75)
Browse files Browse the repository at this point in the history
In previous implementations, there was a need to maintain a timeout
error variable to ignore while waiting for the VM to stop.
This part of the code was not intuitive enough, and other maintainers
couldn’t grasp its intention at first glance.
This improvement has optimized the `waitForVMState` function by adding a
timeout parameter, making the code more intuitive.
  • Loading branch information
openshift-merge-bot[bot] authored Jan 22, 2024
2 parents 441c134 + 65fc3c0 commit 8ab5712
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions cmd/vfkit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ limitations under the License.
package main

import (
"errors"
"fmt"
"os"
"os/signal"
Expand Down Expand Up @@ -91,9 +90,7 @@ func newVMConfiguration(opts *cmdline.Options) (*config.VirtualMachine, error) {
return vmConfig, nil
}

var errVMStateTimeout = fmt.Errorf("timeout waiting for VM state")

func waitForVMState(vm *vz.VirtualMachine, state vz.VirtualMachineState) error {
func waitForVMState(vm *vz.VirtualMachine, state vz.VirtualMachineState, timeout <-chan time.Time) error {
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGPIPE)

Expand All @@ -108,8 +105,8 @@ func waitForVMState(vm *vz.VirtualMachine, state vz.VirtualMachineState) error {
if newState == vz.VirtualMachineStateError {
return fmt.Errorf("hypervisor virtualization error")
}
case <-time.After(5 * time.Second):
return errVMStateTimeout
case <-timeout:
return fmt.Errorf("timeout waiting for VM state")
}
}
}
Expand Down Expand Up @@ -145,13 +142,11 @@ func runVFKit(vmConfig *config.VirtualMachine, opts *cmdline.Options) error {
}

func runVirtualMachine(vmConfig *config.VirtualMachine, vm *vz.VirtualMachine) error {
err := vm.Start()
if err != nil {
if err := vm.Start(); err != nil {
return err
}

err = waitForVMState(vm, vz.VirtualMachineStateRunning)
if err != nil {
if err := waitForVMState(vm, vz.VirtualMachineStateRunning, time.After(5*time.Second)); err != nil {
return err
}
log.Infof("virtual machine is running")
Expand Down Expand Up @@ -182,18 +177,11 @@ func runVirtualMachine(vmConfig *config.VirtualMachine, vm *vz.VirtualMachine) e

errCh := make(chan error, 1)
go func() {
for {
err := waitForVMState(vm, vz.VirtualMachineStateStopped)
if err == nil {
log.Infof("VM is stopped")
errCh <- nil
return
}
if !errors.Is(err, errVMStateTimeout) {
errCh <- fmt.Errorf("virtualization error: %v", err)
return
}
// errVMStateTimeout -> keep looping
if err := waitForVMState(vm, vz.VirtualMachineStateStopped, nil); err != nil {
errCh <- fmt.Errorf("virtualization error: %v", err)
} else {
log.Infof("VM is stopped")
errCh <- nil
}
}()

Expand Down

0 comments on commit 8ab5712

Please sign in to comment.