Skip to content

Commit 4205928

Browse files
committed
Add Reason and Message into terminationMessage
We should get a detail information in order to indicate the reason of why entrypointer failed Sometimes, it would be DeadlineExecced or others and we want to represent it on TaskStep. It is a PRE-PR to implement the issue tektoncd#1690 Signed-off-by: jtcheng <jtcheng0616@gmail.com>
1 parent a7a4c5d commit 4205928

File tree

5 files changed

+69
-6
lines changed

5 files changed

+69
-6
lines changed

cmd/entrypoint/runner.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"os"
56
"os/exec"
67

@@ -15,13 +16,13 @@ type realRunner struct{}
1516

1617
var _ entrypoint.Runner = (*realRunner)(nil)
1718

18-
func (*realRunner) Run(args ...string) error {
19+
func (*realRunner) Run(ctx context.Context, args ...string) error {
1920
if len(args) == 0 {
2021
return nil
2122
}
2223
name, args := args[0], args[1:]
2324

24-
cmd := exec.Command(name, args...)
25+
cmd := exec.CommandContext(ctx, name, args...)
2526
cmd.Stdout = os.Stdout
2627
cmd.Stderr = os.Stderr
2728

pkg/entrypoint/entrypointer.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package entrypoint
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"io/ioutil"
2223
"os"
@@ -68,7 +69,7 @@ type Waiter interface {
6869

6970
// Runner encapsulates running commands.
7071
type Runner interface {
71-
Run(args ...string) error
72+
Run(ctx context.Context, args ...string) error
7273
}
7374

7475
// PostWriter encapsulates writing a file when complete.
@@ -113,7 +114,8 @@ func (e Entrypointer) Go() error {
113114
Value: time.Now().Format(time.RFC3339),
114115
})
115116

116-
err := e.Runner.Run(e.Args...)
117+
err := e.Runner.Run(context.TODO(), e.Args...)
118+
output = append(output, e.fmtErrOutPut(err)...)
117119

118120
// Write the post file *no matter what*
119121
e.WritePostFile(e.PostFile, err)
@@ -129,6 +131,26 @@ func (e Entrypointer) Go() error {
129131
return err
130132
}
131133

134+
// fmtErrOutPut will format error to PipelineResourceResult
135+
func (e Entrypointer) fmtErrOutPut(err error) []v1alpha1.PipelineResourceResult {
136+
if err == nil {
137+
return []v1alpha1.PipelineResourceResult{}
138+
}
139+
// TODO: will add logic to format reason according error type sooner, example: DeadlineExceeded
140+
// For more details, refer to: https://github.com/tektoncd/pipeline/issues/1690
141+
142+
return []v1alpha1.PipelineResourceResult{
143+
{
144+
Key: "Reason",
145+
Value: err.Error(),
146+
},
147+
{
148+
Key: "Message",
149+
Value: err.Error(),
150+
},
151+
}
152+
}
153+
132154
func (e Entrypointer) readResultsFromDisk() error {
133155
output := []v1alpha1.PipelineResourceResult{}
134156
for _, resultFile := range e.Results {

pkg/entrypoint/entrypointer_test.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package entrypoint
1818

1919
import (
20+
"context"
2021
"encoding/json"
2122
"errors"
2223
"io/ioutil"
@@ -93,6 +94,43 @@ func TestEntrypointerFailures(t *testing.T) {
9394
if c.postFile == "" && fpw.wrote != nil {
9495
t.Errorf("Wrote post file when not required")
9596
}
97+
98+
if c.waiter == nil {
99+
fileContents, err := ioutil.ReadFile("termination")
100+
if err == nil {
101+
var entries []v1alpha1.PipelineResourceResult
102+
if err := json.Unmarshal([]byte(fileContents), &entries); err == nil {
103+
index := make(map[string]v1alpha1.PipelineResourceResult, len(entries))
104+
for _, item := range entries {
105+
index[item.Key] = item
106+
}
107+
if _, ok := index["StartedAt"]; !ok {
108+
t.Error("Didn't find the StartedAt entry")
109+
}
110+
if reason, ok := index["Reason"]; ok {
111+
if reason.Value != "runner failed" {
112+
t.Error("Reason should be runner failed")
113+
}
114+
} else {
115+
t.Error("Didn't find the Reason entry")
116+
}
117+
118+
if message, ok := index["Message"]; ok {
119+
if message.Value != "runner failed" {
120+
t.Error("Message should be runner failed")
121+
}
122+
} else {
123+
t.Error("Didn't find the Message entry ")
124+
}
125+
}
126+
} else if !os.IsNotExist(err) {
127+
t.Error("Wanted termination file written, got nil")
128+
}
129+
if err := os.Remove("termination"); err != nil {
130+
t.Errorf("Could not remove termination path: %s", err)
131+
}
132+
}
133+
96134
})
97135
}
98136
}
@@ -213,7 +251,7 @@ func (f *fakeWaiter) Wait(file string, _ bool) error {
213251

214252
type fakeRunner struct{ args *[]string }
215253

216-
func (f *fakeRunner) Run(args ...string) error {
254+
func (f *fakeRunner) Run(ctx context.Context, args ...string) error {
217255
f.args = &args
218256
return nil
219257
}
@@ -231,7 +269,7 @@ func (f *fakeErrorWaiter) Wait(file string, expectContent bool) error {
231269

232270
type fakeErrorRunner struct{ args *[]string }
233271

234-
func (f *fakeErrorRunner) Run(args ...string) error {
272+
func (f *fakeErrorRunner) Run(ctx context.Context, args ...string) error {
235273
f.args = &args
236274
return errors.New("runner failed")
237275
}

test/taskrun_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func TestTaskRunFailure(t *testing.T) {
9595
Terminated: &corev1.ContainerStateTerminated{
9696
ExitCode: 1,
9797
Reason: "Error",
98+
Message: `[{"key":"Message","value":"exit status 1","resourceRef":{}},{"key":"Reason","value":"exit status 1","resourceRef":{}}]`,
9899
},
99100
},
100101
Name: "unnamed-1",

test/v1alpha1/taskrun_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func TestTaskRunFailure(t *testing.T) {
8686
Terminated: &corev1.ContainerStateTerminated{
8787
ExitCode: 1,
8888
Reason: "Error",
89+
Message: `[{"key":"Message","value":"exit status 1","resourceRef":{}},{"key":"Reason","value":"exit status 1","resourceRef":{}}]`,
8990
},
9091
},
9192
Name: "unnamed-1",

0 commit comments

Comments
 (0)