Skip to content

Commit

Permalink
Change to use NotifyContext method
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Morales committed Dec 20, 2024
1 parent f571e6d commit 350bb8a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
18 changes: 7 additions & 11 deletions cmd/kar/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,28 @@ import (
"github.com/spf13/cobra"
)

func NewRootCommand(ctx context.Context, cmdOptions Opts) *cobra.Command {
func NewRootCommand(ctx context.Context, runner *runner.Runner, opts Opts) *cobra.Command {
cmd := &cobra.Command{
Use: "kar",
Short: "Tool that creates a GitHub Self-Host runner with Kubevirt Virtual Machine Instance",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
return initializeConfig(cmd)
},
RunE: func(_ *cobra.Command, _ []string) error {
return run(ctx, cmdOptions)
return run(ctx, runner, opts)
},
}

installFlags(cmd.Flags(), &cmdOptions)
installFlags(cmd.Flags(), &opts)

return cmd
}

func run(ctx context.Context, c Opts) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
func run(ctx context.Context, runner *runner.Runner, opts Opts) error {
runner.CreateResources(ctx, opts.vmTemplate, opts.runnerName, opts.jsonConfig)
defer runner.DeleteResources(ctx)

runnerClient := runner.NewRunner()
runnerClient.CreateResources(ctx, c.vmTemplate, c.runnerName, c.jsonConfig)
defer runnerClient.DeleteResources(ctx)

runnerClient.WaitForVirtualMachineInstance(ctx)
runner.WaitForVirtualMachineInstance(ctx)

return nil
}
21 changes: 12 additions & 9 deletions cmd/kar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,29 @@ import (
"log"
"os"
"os/signal"
"syscall"

"github.com/electrocucaracha/kubevirt-actions-runner/cmd/kar/app"
runner "github.com/electrocucaracha/kubevirt-actions-runner/internal"
"github.com/pkg/errors"
)

func main() {
ctx, cancel := context.WithCancel(context.Background())
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
var opts app.Opts

runner := runner.NewRunner()

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

go func() {
<-signalChan
cancel()
<-ctx.Done()
runner.DeleteResources(ctx)
stop()
}()

var opts app.Opts
rootCmd := app.NewRootCommand(ctx, opts)
rootCmd := app.NewRootCommand(ctx, runner, opts)

if err := rootCmd.Execute(); err != nil && !errors.Is(errors.Cause(err), context.Canceled) {
log.Fatalln(err.Error())
log.Panicln(err.Error())
}
}
2 changes: 1 addition & 1 deletion internal/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Runner struct {
virtualMachineInstance string
}

func (rc Runner) getResources(ctx context.Context, vmTemplate, runnerName, jitConfig string) (
func (rc *Runner) getResources(ctx context.Context, vmTemplate, runnerName, jitConfig string) (
*v1.VirtualMachineInstance, *v1beta1.DataVolume,
) {
virtualMachine, err := rc.virtClient.VirtualMachine(rc.namespace).Get(
Expand Down

0 comments on commit 350bb8a

Please sign in to comment.