-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this commit implements tep-0049 - ignore a step error When a `step` in a `task` results in a failure, the rest of the steps in the `task` are skipped and the `taskRun` is 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 `fail` as part of the step definition. If `onError` is set to `continue`, the entrypoint sets the original failed exit code of the script 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. This is an alpha feature. The `enable-api-fields` feature flag must be set to `"alpha"` to specify `onError` for a `step`. This commit includes following changes: * Changing entrypoint to include three new flags `onError`, `stepPath`, and `stepPathLink`. * Adding two new functions as part of the runner WriteFileContent and CreatePath * Creating a volume `/tekton/steps/` * Supporting a path variable $(steps.step-<stepName>.exitCode.path) and $(steps.step-unnamed-<stepIndex>.exitCode.path) * API spec `onError` while defining a step * Writing exitCode at /tekton/steps/step-<step-name>/exitCode or /tekton/steps/step-unnamed-<step-index>/exitCode * Set the exitCode of a terminated state to a non-zero exit code * Doc, unit test, and examples for this feature
- Loading branch information
1 parent
51f3ce8
commit c56c41e
Showing
27 changed files
with
1,200 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestRealPostWriter_WriteFileContent(t *testing.T) { | ||
tests := []struct { | ||
name, file, content string | ||
}{{ | ||
name: "write a file content", | ||
file: "sample.txt", | ||
content: "this is a sample file", | ||
}, { | ||
name: "write a file without specifying any path", | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
rw := realPostWriter{} | ||
rw.WriteFileContent(tt.file, tt.content) | ||
if tt.file != "" { | ||
defer os.Remove(tt.file) | ||
if _, err := os.Stat(tt.file); err != nil { | ||
t.Fatalf("Failed to create a file %q", tt.file) | ||
} | ||
b, err := os.ReadFile(tt.file) | ||
if err != nil { | ||
t.Fatalf("Failed to read the file %q", tt.file) | ||
} | ||
if tt.content != string(b) { | ||
t.Fatalf("Failed to write the desired content %q to the file %q", tt.content, tt.file) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRealPostWriter_CreateStepPath(t *testing.T) { | ||
tests := []struct { | ||
name, source, link string | ||
}{{ | ||
name: "Create a path with a file", | ||
source: "sample.txt", | ||
link: "0", | ||
}, { | ||
name: "Create a path without specifying any path", | ||
}, { | ||
name: "Create a sym link without specifying any link path", | ||
source: "sample.txt", | ||
}, { | ||
name: "Create a sym link without specifying any source", | ||
link: "0.txt", | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
rw := realPostWriter{} | ||
rw.CreatePath(tt.source, tt.link) | ||
if tt.source != "" { | ||
defer os.Remove(tt.source) | ||
if _, err := os.Stat(tt.source); err != nil { | ||
t.Fatalf("Failed to create a file %q", tt.source) | ||
} | ||
} | ||
if tt.source != "" && tt.link != "" { | ||
defer os.Remove(tt.link) | ||
if _, err := os.Stat(tt.link); err != nil { | ||
t.Fatalf("Failed to create a sym link %q", tt.link) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
examples/v1beta1/pipelineruns/alpha/ignore-step-error.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
kind: PipelineRun | ||
apiVersion: tekton.dev/v1beta1 | ||
metadata: | ||
generateName: pipelinerun-with-failing-step- | ||
spec: | ||
serviceAccountName: 'default' | ||
pipelineSpec: | ||
tasks: | ||
- name: task1 | ||
taskSpec: | ||
steps: | ||
# not really doing anything here, just a hurdle to test the "ignore step error" | ||
- image: alpine | ||
onError: continue | ||
name: exit-with-1 | ||
script: | | ||
exit 1 | ||
# initialize a task result which will be validated by the next task | ||
- image: alpine | ||
name: write-a-result | ||
onError: continue | ||
script: | | ||
echo -n 123 | tee $(results.task1-result.path) | ||
exit 11 | ||
results: | ||
- name: task1-result | ||
description: result of a task1 | ||
- name: task2 | ||
runAfter: [ "task1" ] | ||
params: | ||
- name: task1-result | ||
value: $(tasks.task1.results.task1-result) | ||
taskSpec: | ||
params: | ||
- name: task1-result | ||
steps: | ||
# again, not really doing anything here, just a hurdle to test the "ignore step error" | ||
- image: alpine | ||
onError: continue | ||
name: exit-with-255 | ||
script: | | ||
exit 255 | ||
# verify that the task result was produced by the first task, fail if the result does not match | ||
- image: alpine | ||
name: verify-a-task-result | ||
script: | | ||
ls /tekton/results/ | ||
if [ $(params.task1-result) == 123 ]; then | ||
echo "Yay! the task result matches which was initialized in the previous task while ignoring the step error" | ||
else | ||
echo "the task result does not match." | ||
exit 1 | ||
fi | ||
# the last step of a task and one more hurdle | ||
- image: alpine | ||
name: exit-with-20 | ||
onError: continue | ||
script: | | ||
exit 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
kind: TaskRun | ||
apiVersion: tekton.dev/v1beta1 | ||
metadata: | ||
generateName: taskrun-with-failing-step- | ||
spec: | ||
taskSpec: | ||
steps: | ||
# exit with 1 and ignore non zero exit code | ||
- image: alpine | ||
onError: continue | ||
name: exit-with-1 | ||
script: | | ||
exit 1 | ||
# check if the /tekton/steps/step-<step-name>/exitCode got created and contains the exit code | ||
# check if the symlink /tekton/steps/0/ got created | ||
- image: alpine | ||
name: verify-step-path | ||
script: | | ||
exitCode=`cat $(steps.step-exit-with-1.exitCode.path)` | ||
if [ $exitCode == 1 ]; then | ||
echo "Yay! the exit code can be accessed using the path variable and matches the previous step exit code" | ||
else | ||
echo "the exit code does not match." | ||
exit 1 | ||
fi | ||
FILE=/tekton/steps/step-exit-with-1/exitCode | ||
if [ -f "$FILE" ]; then | ||
echo "$FILE exists." | ||
echo "Yay! the file exists which was created by the controller to record the step exit code." | ||
else | ||
echo "$FILE does not exist." | ||
exit 1 | ||
fi | ||
FILE=/tekton/steps/0/exitCode | ||
if [ -f "$FILE" ]; then | ||
echo "$FILE exists." | ||
echo "Yay! the symlink exists which was created by the controller to record the step exit code." | ||
else | ||
echo "$FILE does not exist." | ||
exit 1 | ||
fi | ||
exitCode=`cat $FILE` | ||
if [ $exitCode == 1 ]; then | ||
echo "Yay! the exit code matches to the previous step exit code" | ||
else | ||
echo "the exit code does not match." | ||
exit 1 | ||
fi | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.