Skip to content

Commit

Permalink
Raise VMI failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Morales committed Jan 23, 2025
1 parent 8f5d519 commit 18208ce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 6 additions & 0 deletions cmd/kar/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package app

import (
"context"
"errors"

runner "github.com/electrocucaracha/kubevirt-actions-runner/internal"
"github.com/spf13/cobra"
kubevirt_v1 "kubevirt.io/api/core/v1"
)

func NewRootCommand(ctx context.Context, runner *runner.Runner, opts Opts) *cobra.Command {
Expand All @@ -46,5 +48,9 @@ func run(ctx context.Context, runner *runner.Runner, opts Opts) error {

runner.WaitForVirtualMachineInstance(ctx)

if runner.CurrentStatus == kubevirt_v1.Failed {
return errors.New("virtual machine instance has failed")
}

return nil
}
18 changes: 11 additions & 7 deletions internal/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Runner struct {
namespace string
dataVolume string
virtualMachineInstance string
CurrentStatus v1.VirtualMachineInstancePhase
}

func (rc *Runner) getResources(ctx context.Context, vmTemplate, runnerName, jitConfig string) (
Expand Down Expand Up @@ -162,23 +163,26 @@ func (rc *Runner) WaitForVirtualMachineInstance(ctx context.Context) {
}
defer watch.Stop()

lastPhase := ""
lastTimeChecked := time.Now()

for event := range watch.ResultChan() {
vmi, ok := event.Object.(*v1.VirtualMachineInstance)
if ok && vmi.Name == rc.virtualMachineInstance {
if vmi.Status.Phase != v1.VirtualMachineInstancePhase(lastPhase) {
lastPhase = string(vmi.Status.Phase)
log.Printf("%s has transitioned to %s phase \n", rc.virtualMachineInstance, lastPhase)
if vmi.Status.Phase != rc.CurrentStatus {
rc.CurrentStatus = vmi.Status.Phase
log.Printf("%s has transitioned to %s phase \n", rc.virtualMachineInstance, rc.CurrentStatus)
lastTimeChecked = time.Now()

switch lastPhase {
case "Succeeded", "Failed":
switch rc.CurrentStatus {
case v1.Succeeded:
log.Printf("%s has successfuly completed\n", rc.virtualMachineInstance)
return
case v1.Failed:
log.Printf("%s has failed\n", rc.virtualMachineInstance)
return
}
} else if time.Since(lastTimeChecked).Minutes() > 5.0 {
log.Printf("%s is in %s phase \n", rc.virtualMachineInstance, lastPhase)
log.Printf("%s is in %s phase \n", rc.virtualMachineInstance, rc.CurrentStatus)
lastTimeChecked = time.Now()
}
}
Expand Down

0 comments on commit 18208ce

Please sign in to comment.