Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option continueAndFail on step.onError #8638

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ var (
breakpointOnFailure = flag.Bool("breakpoint_on_failure", false, "If specified, expect steps to not skip on failure")
debugBeforeStep = flag.Bool("debug_before_step", false, "If specified, wait for a debugger to attach before executing the step")
onError = flag.String("on_error", "", "Set to \"continue\" to ignore an error and continue when a container terminates with a non-zero exit code."+
" Set to \"stopAndFail\" to declare a failure with a step error and stop executing the rest of the steps.")
" Set to \"stopAndFail\" to declare a failure with a step error and stop executing the rest of the steps."+
" Set to \"continueAndFail\" to declare a failure with a step error after having executed the rest of the steps.")
stepMetadataDir = flag.String("step_metadata_dir", "", "If specified, create directory to store the step metadata e.g. /tekton/steps/<step-name>/")
resultExtractionMethod = flag.String("result_from", entrypoint.ResultExtractionMethodTerminationMessage, "The method using which to extract results from tasks. Default is using the termination message.")
)
Expand Down
5 changes: 4 additions & 1 deletion docs/pipeline-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,9 @@ IncludeParamsList
<tbody><tr><td><p>&#34;continue&#34;</p></td>
<td><p>Continue indicates continue executing the rest of the steps irrespective of the container exit code</p>
</td>
</tr><tr><td><p>&#34;continueAndFail&#34;</p></td>
<td><p>ContinueAndFail indicates continue executing the rest of the steps irrespective of the container exit code and exit after all steps are completed with the first possible non-zero exit code of the step that has this OnErrorType</p>
</td>
</tr><tr><td><p>&#34;stopAndFail&#34;</p></td>
<td><p>StopAndFail indicates exit the taskRun if the container exits with non-zero exit code</p>
</td>
Expand Down Expand Up @@ -4602,7 +4605,7 @@ OnErrorType
</td>
<td>
<p>OnError defines the exiting behavior of a container on error
can be set to [ continue | stopAndFail ]</p>
can be set to [ continue | stopAndFail | continueAndFail ]</p>
</td>
</tr>
<tr>
Expand Down
6 changes: 5 additions & 1 deletion docs/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,15 @@ When a `step` in a `task` results in a failure, the rest of the steps in the `ta
declared a failure. If you would like to ignore such step errors and continue executing the rest of the steps in
the task, you can specify `onError` for such a `step`.

`onError` can be set to either `continue` or `stopAndFail` as part of the step definition. If `onError` is
`onError` can be set to `continue`, `stopAndFail` or `continueAndFail` as part of the step definition. If `onError` is
set to `continue`, the entrypoint sets the original failed exit code of the [script](#running-scripts-within-steps)
in the container terminated state. A `step` with `onError` set to `continue` does not fail the `taskRun` and continues
executing the rest of the steps in a task.

If `onError` is set to `continueAndFail`, the `taskRun` continues executing the rest of the steps in a task and if no
following `step` fails, the `taskRun` fails eventually with the exit code of the step that had set `onError` with
`continueAndFail`.

To ignore a step error, set `onError` to `continue`:

```yaml
Expand Down
4 changes: 3 additions & 1 deletion pkg/apis/pipeline/v1/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ type Step struct {
Workspaces []WorkspaceUsage `json:"workspaces,omitempty"`

// OnError defines the exiting behavior of a container on error
// can be set to [ continue | stopAndFail ]
// can be set to [ continue | stopAndFail | continueAndFail ]
OnError OnErrorType `json:"onError,omitempty"`
// Stores configuration for the stdout stream of the step.
// +optional
Expand Down Expand Up @@ -176,6 +176,8 @@ const (
StopAndFail OnErrorType = "stopAndFail"
// Continue indicates continue executing the rest of the steps irrespective of the container exit code
Continue OnErrorType = "continue"
// Continue indicates continue executing the rest of the steps irrespective of the container exit code and exit after all steps are completed with the first possible non-zero exit code of the step that has this OnErrorType
ContinueAndFail OnErrorType = "continueAndFail"
)

// StepOutputConfig stores configuration for a step output stream.
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,7 @@
"default": ""
},
"onError": {
"description": "OnError defines the exiting behavior of a container on error can be set to [ continue | stopAndFail ]",
"description": "OnError defines the exiting behavior of a container on error can be set to [ continue | stopAndFail | continueAndFail ]",
"type": "string"
},
"params": {
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1/task_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,11 @@ func validateStep(ctx context.Context, s Step, names sets.String) (errs *apis.Fi
}

if s.OnError != "" {
if !isParamRefs(string(s.OnError)) && s.OnError != Continue && s.OnError != StopAndFail {
if !isParamRefs(string(s.OnError)) && s.OnError != Continue && s.OnError != StopAndFail && s.OnError != ContinueAndFail {
errs = errs.Also(&apis.FieldError{
Message: fmt.Sprintf("invalid value: \"%v\"", s.OnError),
Paths: []string{"onError"},
Details: "Task step onError must be either \"continue\" or \"stopAndFail\"",
Details: "Task step onError must be \"continue\", \"stopAndFail\" or \"continueAndFail\"",
})
}
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/apis/pipeline/v1/task_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2279,6 +2279,13 @@ func TestStepOnError(t *testing.T) {
Image: "image",
Args: []string{"arg"},
}},
}, {
name: "valid step - valid onError usage - set to continueAndFail",
steps: []v1.Step{{
OnError: v1.ContinueAndFail,
Image: "image",
Args: []string{"arg"},
}},
}, {
name: "valid step - valid onError usage - set to a task parameter",
params: []v1.ParamSpec{{
Expand All @@ -2300,7 +2307,7 @@ func TestStepOnError(t *testing.T) {
expectedError: &apis.FieldError{
Message: `invalid value: "onError"`,
Paths: []string{"steps[0].onError"},
Details: `Task step onError must be either "continue" or "stopAndFail"`,
Details: `Task step onError must be "continue", "stopAndFail" or "continueAndFail"`,
},
}}
for _, tt := range tests {
Expand Down
18 changes: 15 additions & 3 deletions pkg/entrypoint/entrypointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ import (

// RFC3339 with millisecond
const (
timeFormat = "2006-01-02T15:04:05.000Z07:00"
ContinueOnError = "continue"
FailOnError = "stopAndFail"
timeFormat = "2006-01-02T15:04:05.000Z07:00"
ContinueOnError = "continue"
StopAndFailOnError = "stopAndFail"
ContinueAndFailOnError = "continueAndFail"
)

const (
Expand Down Expand Up @@ -143,6 +144,7 @@ type Entrypointer struct {
// OnError defines exiting behavior of the entrypoint
// set it to "stopAndFail" to indicate the entrypoint to exit the taskRun if the container exits with non zero exit code
// set it to "continue" to indicate the entrypoint to continue executing the rest of the steps irrespective of the container exit code
// set it to "continueAndFail" to indicate the entrypoint to continue executing the rest of the steps irrespective of the container exit code and exit the taskRun if the container exits with non zero exit code
OnError string
// StepMetadataDir is the directory for a step where the step related metadata can be stored
StepMetadataDir string
Expand Down Expand Up @@ -294,6 +296,16 @@ func (e Entrypointer) Go() error {
})
e.WritePostFile(e.PostFile, nil)
e.WriteExitCodeFile(e.StepMetadataDir, exitCode)
case e.OnError == ContinueAndFailOnError && errors.As(err, &ee):
// with continueAndFail on error and an ExitError, write non-zero exit code and a post file with the error
exitCode := strconv.Itoa(ee.ExitCode())
output = append(output, result.RunResult{
Key: "ExitCode",
Value: exitCode,
ResultType: result.InternalTektonResultType,
})
e.WritePostFile(e.PostFile, err)
e.WriteExitCodeFile(e.StepMetadataDir, exitCode)
case err == nil:
// if err is nil, write zero exit code and a post file
e.WritePostFile(e.PostFile, nil)
Expand Down
34 changes: 31 additions & 3 deletions pkg/entrypoint/entrypointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,24 @@ func TestEntrypointer_OnError(t *testing.T) {
runner: &fakeExitErrorRunner{},
expectedError: true,
postFile: "step-one",
onError: FailOnError,
onError: StopAndFailOnError,
}, {
desc: "the step is exiting with 0, treat the step error (but there is none) as failure with onError set to stopAndFail",
runner: &fakeRunner{},
postFile: "step-one",
onError: FailOnError,
onError: StopAndFailOnError,
expectedError: false,
}, {
desc: "the step is exiting with 1, treat the step error as failure with onError set to continueAndFail",
runner: &fakeExitErrorRunner{},
postFile: "step-one",
onError: ContinueAndFailOnError,
expectedError: true,
}, {
desc: "the step is exiting with 0, treat the step error (but there is none) as failure with onError set to continueAndFail",
runner: &fakeRunner{},
postFile: "step-one",
onError: ContinueAndFailOnError,
expectedError: false,
}, {
desc: "the step set debug before step, and before step breakpoint fail-continue",
Expand Down Expand Up @@ -566,14 +578,30 @@ func TestEntrypointer_OnError(t *testing.T) {
}
}

if c.onError == FailOnError {
if c.onError == StopAndFailOnError {
switch {
case fpw.wrote == nil:
t.Error("Wanted post file written, got nil")
case c.expectedError && *fpw.wrote != c.postFile+".err":
t.Errorf("Wrote post file %q, want %q", *fpw.wrote, c.postFile+".err")
}
}

if c.onError == ContinueAndFailOnError {
switch {
case fpw.wrote == nil:
t.Error("Wanted post file written, got nil")
case fpw.exitCodeFile == nil:
t.Error("Wanted exitCode file written, got nil")
case *fpw.exitCodeFile != "exitCode":
t.Errorf("Wrote exitCode file %q, want %q", *fpw.exitCodeFile, "exitCode")
case c.expectedError && *fpw.exitCode == "0":
t.Errorf("Wrote zero exit code but want non-zero when expecting an error")
case c.expectedError && *fpw.wrote != c.postFile+".err":
t.Errorf("Wrote post file %q, want %q", *fpw.wrote, c.postFile+".err")
}
}

})
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/pod/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ func orderContainers(ctx context.Context, commonExtraEntrypointArgs []string, st
if taskSpec != nil {
if taskSpec.Steps != nil && len(taskSpec.Steps) >= i+1 {
if taskSpec.Steps[i].OnError != "" {
if taskSpec.Steps[i].OnError != v1.Continue && taskSpec.Steps[i].OnError != v1.StopAndFail {
return nil, fmt.Errorf("task step onError must be either \"%s\" or \"%s\" but it is set to an invalid value \"%s\"",
v1.Continue, v1.StopAndFail, taskSpec.Steps[i].OnError)
if taskSpec.Steps[i].OnError != v1.Continue && taskSpec.Steps[i].OnError != v1.StopAndFail && taskSpec.Steps[i].OnError != v1.ContinueAndFail {
return nil, fmt.Errorf("task step onError must have one of the following values: \"%s\", \"%s\" or \"%s\". But it is set to an invalid value \"%s\"",
v1.Continue, v1.StopAndFail, v1.ContinueAndFail, taskSpec.Steps[i].OnError)
}
argsForEntrypoint = append(argsForEntrypoint, "-on_error", string(taskSpec.Steps[i].OnError))
}
Expand Down
23 changes: 21 additions & 2 deletions pkg/pod/entrypoint_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*/
Copyright 2019 The Tekton Authors

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -645,6 +645,10 @@ func TestEntryPointOnError(t *testing.T) {
Name: "passing-step",
Image: "step-2",
Command: []string{"cmd"},
}, {
Name: "passing-step",
Image: "step-3",
Command: []string{"cmd"},
}}

for _, tc := range []struct {
Expand All @@ -658,6 +662,8 @@ func TestEntryPointOnError(t *testing.T) {
OnError: v1.Continue,
}, {
OnError: v1.StopAndFail,
}, {
OnError: v1.ContinueAndFail,
}},
},
wantContainers: []corev1.Container{{
Expand Down Expand Up @@ -688,14 +694,27 @@ func TestEntryPointOnError(t *testing.T) {
"-entrypoint", "cmd", "--",
},
TerminationMessagePath: "/tekton/termination",
}, {
Name: "passing-step",
Image: "step-3",
Command: []string{entrypointBinary},
Args: []string{
"-wait_file", "/tekton/run/1/out",
"-post_file", "/tekton/run/2/out",
"-termination_path", "/tekton/termination",
"-step_metadata_dir", "/tekton/run/2/status",
"-on_error", "continueAndFail",
"-entrypoint", "cmd", "--",
},
TerminationMessagePath: "/tekton/termination",
}},
}, {
taskSpec: v1.TaskSpec{
Steps: []v1.Step{{
OnError: "invalid-on-error",
}},
},
err: errors.New("task step onError must be either \"continue\" or \"stopAndFail\" but it is set to an invalid value \"invalid-on-error\""),
err: errors.New("task step onError must have one of the following values: \"continue\", \"stopAndFail\" or \"continueAndFail\". But it is set to an invalid value \"invalid-on-error\""),
}} {
t.Run(tc.desc, func(t *testing.T) {
got, err := orderContainers(context.Background(), []string{}, steps, &tc.taskSpec, nil, true, false)
Expand Down
Loading