Skip to content

Commit

Permalink
feat: do not panic inside code
Browse files Browse the repository at this point in the history
this commit replaces all panics in cmd folder with regular error handling.
Removed custom error package and adjust code.

Fixes: https://issues.redhat.com/browse/CNV-54646

Signed-off-by: Karel Simon <[email protected]>
  • Loading branch information
ksimon1 committed Jan 20, 2025
1 parent e596976 commit 6b86eb2
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 607 deletions.
23 changes: 11 additions & 12 deletions cmd/create-vm/main.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,50 @@
package main

import (
"net/http"
"os"

goarg "github.com/alexflint/go-arg"
. "github.com/kubevirt/kubevirt-tekton-tasks/modules/create-vm/pkg/constants"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/create-vm/pkg/utils/parse"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/create-vm/pkg/vmcreator"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/shared/pkg/exit"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/shared/pkg/log"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/shared/pkg/output"
res "github.com/kubevirt/kubevirt-tekton-tasks/modules/shared/pkg/results"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/shared/pkg/zerrors"
"go.uber.org/zap"
kubevirtv1 "kubevirt.io/api/core/v1"
)

func main() {
defer exit.HandleExit()

cliOptions := &parse.CLIOptions{}
goarg.MustParse(cliOptions)

logger := log.InitLogger(cliOptions.GetDebugLevel())
defer logger.Sync()

if err := cliOptions.Init(); err != nil {
exit.ExitOrDieFromError(InvalidCLIInputExitCode, err)
log.Logger().Error(err.Error())
os.Exit(InvalidCLIInputExitCode)
}
log.Logger().Debug("parsed arguments", zap.Reflect("cliOptions", cliOptions))

vmCreator, err := vmcreator.NewVMCreator(cliOptions)
if err != nil {
exit.ExitOrDieFromError(GenericExitCode, err)
log.Logger().Error(err.Error())
os.Exit(GenericExitCode)
}

vm, err := vmCreator.CreateVM()
if err != nil {
exit.ExitOrDieFromError(CreateVMErrorExitCode, err,
zerrors.IsStatusError(err, http.StatusNotFound, http.StatusConflict, http.StatusUnprocessableEntity),
)
log.Logger().Error(err.Error())
os.Exit(CreateVMErrorExitCode)
}

if cliOptions.GetStartVMFlag() &&
(vm.Spec.RunStrategy == nil || *vm.Spec.RunStrategy != kubevirtv1.RunStrategyAlways) &&
(vm.Spec.Running == nil || !*vm.Spec.Running) {
if err := vmCreator.StartVM(vm.Namespace, vm.Name); err != nil {
exit.ExitFromError(StartVMErrorExitCode, err)
log.Logger().Error(err.Error())
os.Exit(StartVMErrorExitCode)
}
}

Expand All @@ -57,7 +55,8 @@ func main() {

log.Logger().Debug("recording results", zap.Reflect("results", results))
if err := res.RecordResults(results); err != nil {
exit.ExitOrDieFromError(WriteResultsExitCode, err)
log.Logger().Error(err.Error())
os.Exit(WriteResultsExitCode)
}

output.PrettyPrint(vm, cliOptions.Output)
Expand Down
21 changes: 12 additions & 9 deletions cmd/disk-uploader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-uploader/pkg/parse"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-uploader/pkg/secrets"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-uploader/pkg/vmexport"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/shared/pkg/exit"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/shared/pkg/log"
res "github.com/kubevirt/kubevirt-tekton-tasks/modules/shared/pkg/results"

Expand Down Expand Up @@ -115,44 +114,48 @@ func run(opts parse.CLIOptions, k8sClient kubernetes.Interface, virtClient kubec
}

func main() {
defer exit.HandleExit()

cliOptions := &parse.CLIOptions{}
goarg.MustParse(cliOptions)

logger := log.InitLogger(cliOptions.GetDebugLevel())
defer logger.Sync()

if err := cliOptions.Init(); err != nil {
exit.ExitOrDieFromError(constants.InvalidCLIInputExitCode, err)
log.Logger().Error(err.Error())
os.Exit(constants.InvalidCLIInputExitCode)
}
log.Logger().Debug("parsed arguments", zap.Reflect("cliOptions", cliOptions))

config, err := rest.InClusterConfig()
if err != nil {
exit.ExitOrDieFromError(constants.GenericExitCode, err)
log.Logger().Error(err.Error())
os.Exit(constants.GenericExitCode)
}

k8sClient, err := kubernetes.NewForConfig(config)
if err != nil {
exit.ExitOrDieFromError(constants.GenericExitCode, err)
log.Logger().Error(err.Error())
os.Exit(constants.GenericExitCode)
}

virtClient, err := kubecli.GetKubevirtClient()
if err != nil {
exit.ExitOrDieFromError(constants.GenericExitCode, err)
log.Logger().Error(err.Error())
os.Exit(constants.GenericExitCode)
}

imageDigest, err := run(*cliOptions, k8sClient, virtClient)
if err != nil {
exit.ExitOrDieFromError(constants.DiskUploaderErrorExitCode, err)
log.Logger().Error(err.Error())
os.Exit(constants.DiskUploaderErrorExitCode)
}

results := map[string]string{constants.DigestResultName: imageDigest}

log.Logger().Debug("recording results", zap.Reflect("results", results))

if err := res.RecordResults(results); err != nil {
exit.ExitOrDieFromError(constants.WriteResultsExitCode, err)
log.Logger().Error(err.Error())
os.Exit(constants.WriteResultsExitCode)
}
}
14 changes: 8 additions & 6 deletions cmd/disk-virt-customize/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package main

import (
"os"

goarg "github.com/alexflint/go-arg"
"go.uber.org/zap"

. "github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-virt/pkg/constants"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-virt/pkg/execute"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-virt/pkg/utils/log"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-virt/pkg/utils/parse"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/shared/pkg/exit"
)

func main() {
defer exit.HandleExit()

cliOptions := &parse.CLIOptions{}
goarg.MustParse(cliOptions)

Expand All @@ -22,15 +21,18 @@ func main() {

log.GetLogger().Debug("parsed arguments", zap.Reflect("cliOptions", cliOptions))
if err := cliOptions.Init(); err != nil {
exit.ExitOrDieFromError(InvalidArguments, err)
log.GetLogger().Error(err.Error())
os.Exit(InvalidArguments)
}
executor := execute.NewExecutor(cliOptions, DiskImagePath, "virt-customize")

if err := executor.PrepareGuestFSAppliance(); err != nil {
exit.ExitOrDieFromError(PrepareGuestFSApplianceFailed, err)
log.GetLogger().Error(err.Error())
os.Exit(PrepareGuestFSApplianceFailed)
}

if err := executor.Execute(); err != nil {
exit.ExitOrDieFromError(ExecuteFailed, err)
log.GetLogger().Error(err.Error())
os.Exit(ExecuteFailed)
}
}
14 changes: 8 additions & 6 deletions cmd/disk-virt-sysprep/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package main

import (
"os"

goarg "github.com/alexflint/go-arg"
"go.uber.org/zap"

. "github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-virt/pkg/constants"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-virt/pkg/execute"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-virt/pkg/utils/log"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-virt/pkg/utils/parse"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/shared/pkg/exit"
)

func main() {
defer exit.HandleExit()

cliOptions := &parse.CLIOptions{}
goarg.MustParse(cliOptions)

Expand All @@ -22,15 +21,18 @@ func main() {

log.GetLogger().Debug("parsed arguments", zap.Reflect("cliOptions", cliOptions))
if err := cliOptions.Init(); err != nil {
exit.ExitOrDieFromError(InvalidArguments, err)
log.GetLogger().Error(err.Error())
os.Exit(InvalidArguments)
}
executor := execute.NewExecutor(cliOptions, DiskImagePath, "virt-sysprep")

if err := executor.PrepareGuestFSAppliance(); err != nil {
exit.ExitOrDieFromError(PrepareGuestFSApplianceFailed, err)
log.GetLogger().Error(err.Error())
os.Exit(PrepareGuestFSApplianceFailed)
}

if err := executor.Execute(); err != nil {
exit.ExitOrDieFromError(ExecuteFailed, err)
log.GetLogger().Error(err.Error())
os.Exit(ExecuteFailed)
}
}
81 changes: 25 additions & 56 deletions cmd/execute-in-vm/main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package main

import (
"os"
"time"

goarg "github.com/alexflint/go-arg"
. "github.com/kubevirt/kubevirt-tekton-tasks/modules/execute-in-vm/pkg/constants"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/execute-in-vm/pkg/execute"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/execute-in-vm/pkg/utils"
log "github.com/kubevirt/kubevirt-tekton-tasks/modules/execute-in-vm/pkg/utils/log"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/execute-in-vm/pkg/utils/parse"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/shared/pkg/exit"
"github.com/kubevirt/kubevirt-tekton-tasks/modules/shared/pkg/zerrors"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/util/wait"
"time"
)

func main() {
defer exit.HandleExit()

cliOptions := &parse.CLIOptions{}
goarg.MustParse(cliOptions)

Expand All @@ -25,87 +22,59 @@ func main() {

log.Logger().Debug("parsed arguments", zap.Reflect("cliOptions", cliOptions))
if err := cliOptions.Init(); err != nil {
exit.ExitOrDieFromError(InvalidArguments, err)
log.Logger().Error(err.Error())
os.Exit(InvalidArguments)
}

executor, executorErr := execute.NewExecutor(cliOptions, ConnectionSecretPath)
if executorErr != nil {
exit.ExitOrDieFromError(ExecutorInitialization, executorErr)
}

multiError := zerrors.NewMultiError()
var exitError *exit.Exit

registerError := func(name string, err error) {
if err != nil {
if exitErr, ok := err.(exit.Exit); ok {
exitError = &exitErr
} else if err == wait.ErrWaitTimeout {
exitError = &exit.Exit{
Code: CommandTimeout,
Msg: "command timed out",
Soft: true,
}
} else {
multiError.Add(name, err)
}
}

log.Logger().Error(executorErr.Error())
os.Exit(ExecutorInitialization)
}

if cliOptions.GetScript() != "" {
runWithTimeout := utils.WithTimeout(cliOptions.GetScriptTimeout())

runWithTimeout(func(timeout time.Duration, finished bool) {
if multiError.IsEmpty() && !finished {
err := executor.EnsureVMRunning(timeout)
registerError("EnsureVMRunning", err)
if !finished {
if err := executor.EnsureVMRunning(timeout); err != nil {
log.Logger().Error(err.Error())
os.Exit(ExecutorActionsFailed)
}
}
})

runWithTimeout(func(timeout time.Duration, finished bool) {
if multiError.IsEmpty() && !finished {
err := executor.SetupConnection(timeout)
registerError("SetupConnection", err)
if !finished {
if err := executor.SetupConnection(timeout); err != nil {
log.Logger().Error(err.Error())
os.Exit(ExecutorActionsFailed)
}
}
})

runWithTimeout(func(timeout time.Duration, finished bool) {
if multiError.IsEmpty() {
if !finished {
err := executor.RemoteExecute(timeout)
registerError("RemoteExecute", err)
} else {
registerError("RemoteExecute", wait.ErrWaitTimeout)
if !finished {
if err := executor.RemoteExecute(timeout); err != nil {
log.Logger().Error(err.Error())
os.Exit(ExecutorActionsFailed)
}

}
})

}

if cliOptions.ShouldStop() {
if err := executor.EnsureVMStopped(); err != nil {
multiError.Add("VM Stop", err)
log.Logger().Error(err.Error())
os.Exit(ExecutorActionsFailed)
}
}

if cliOptions.ShouldDelete() {
if err := executor.EnsureVMDeleted(); err != nil {
multiError.Add("VM Delete", err)
log.Logger().Error(err.Error())
os.Exit(ExecutorActionsFailed)
}
}

if !multiError.IsEmpty() {
if exitError != nil {
multiError.Add("command exit", *exitError)
}
log.Logger().Debug("finished", zap.String("errMsg", multiError.Error()))
exit.ExitOrDieFromError(ExecutorActionsFailed, multiError)
}

if exitError != nil {
log.Logger().Debug("finished", zap.Reflect("err", exitError))
exit.ExitOrDieFromError(exitError.Code, exitError)
}
}
Loading

0 comments on commit 6b86eb2

Please sign in to comment.