Skip to content

Commit

Permalink
chore: assign container computation errors to the 1st step requiring it
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 committed Oct 28, 2024
1 parent 1460d08 commit c4a9d76
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
29 changes: 24 additions & 5 deletions cmd/testworkflow-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ func main() {
delayedPauses := make([]string, 0)

// Interpret the operations
for _, action := range state.GetActions(int(groupIndex)) {
actions := state.GetActions(int(groupIndex))
for i, action := range actions {
switch action.Type() {
case lite.ActionTypeDeclare:
state.GetStep(action.Declare.Ref).
Expand All @@ -175,7 +176,19 @@ func main() {

case lite.ActionTypeContainerTransition:
orchestration.Setup.SetConfig(action.Container.Config)
orchestration.Setup.AdvanceEnv()
err := orchestration.Setup.AdvanceEnv()
// Attach the error to the next consecutive step
if err != nil {
for _, next := range actions[i:] {
if next.Type() != lite.ActionTypeStart || *next.Start == "" {
continue
}
step := state.GetStep(*next.Start)
orchestration.Start(step)
break
}
output.ExitErrorf(data.CodeInputError, err.Error())
}
stdout.SetSensitiveWords(orchestration.Setup.GetSensitiveWords())
currentContainer = *action.Container

Expand Down Expand Up @@ -225,10 +238,13 @@ func main() {
orchestration.End(step)

case lite.ActionTypeSetup:
orchestration.Setup.UseEnv(constants.EnvGroupDebug)
err := orchestration.Setup.UseEnv(constants.EnvGroupDebug)
if err != nil {
output.ExitErrorf(data.CodeInputError, err.Error())
}
stdout.SetSensitiveWords(orchestration.Setup.GetSensitiveWords())
step := state.GetStep(data.InitStepName)
err := commands.Setup(*action.Setup)
err = commands.Setup(*action.Setup)
if err == nil {
step.SetStatus(data.StepStatusPassed)
} else {
Expand Down Expand Up @@ -257,7 +273,10 @@ func main() {
}

// Configure the environment
orchestration.Setup.UseCurrentEnv()
err := orchestration.Setup.UseCurrentEnv()
if err != nil {
output.ExitErrorf(data.CodeInputError, err.Error())
}
if action.Execute.Toolkit {
serialized, _ := json.Marshal(state.InternalConfig)
_ = os.Setenv("TK_CFG", string(serialized))
Expand Down
15 changes: 9 additions & 6 deletions cmd/testworkflow-init/orchestration/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"regexp"
"strings"

"github.com/pkg/errors"

"github.com/kubeshop/testkube/cmd/testworkflow-init/constants"
"github.com/kubeshop/testkube/cmd/testworkflow-init/data"
"github.com/kubeshop/testkube/cmd/testworkflow-init/output"
Expand Down Expand Up @@ -168,7 +170,7 @@ func (c *setup) GetInternalConfig() (config testworkflowconfig.InternalConfig) {
return config
}

func (c *setup) UseEnv(group string) {
func (c *setup) UseEnv(group string) error {
c.UseBaseEnv()
c.envSelectedGroup = group

Expand Down Expand Up @@ -226,20 +228,21 @@ func (c *setup) UseEnv(group string) {
for name, expr := range envTemplates {
value, err := expressions.CompileAndResolveTemplate(expr, localEnvMachine, addonMachine, expressions.FinalizerFail)
if err != nil {
output.ExitErrorf(data.CodeInputError, "failed to compute '%s' environment variable: %s", name, err.Error())
return errors.Wrapf(err, "failed to compute '%s' environment variable", name)
}
str, _ := value.Static().StringValue()
os.Setenv(name, str)
}
return nil
}

func (c *setup) UseCurrentEnv() {
c.UseEnv(fmt.Sprintf("%d", c.envCurrentGroup))
func (c *setup) UseCurrentEnv() error {
return c.UseEnv(fmt.Sprintf("%d", c.envCurrentGroup))
}

func (c *setup) AdvanceEnv() {
func (c *setup) AdvanceEnv() error {
c.envCurrentGroup++
c.UseCurrentEnv()
return c.UseCurrentEnv()
}

func (c *setup) SetWorkingDir(workingDir string) {
Expand Down

0 comments on commit c4a9d76

Please sign in to comment.