Skip to content

Commit

Permalink
Add granular termination reason in container termination message
Browse files Browse the repository at this point in the history
Related with tektoncd#7223.

To report specific Steps termination reasons we need to know why its continer finished; we use the termination message to store a new "state" with this information. We evaluated changing the container `reason` directly, but looks like k8s doesn't allow this.
  • Loading branch information
renzodavid9 committed Nov 17, 2023
1 parent 97184c3 commit 2514909
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
31 changes: 19 additions & 12 deletions pkg/entrypoint/entrypointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,28 +189,22 @@ func (e Entrypointer) Go() error {
}
}()
err = e.Runner.Run(ctx, e.Command...)
if errors.Is(err, ErrContextDeadlineExceeded) {
output = append(output, result.RunResult{
Key: "Reason",
Value: "TimeoutExceeded",
ResultType: result.InternalTektonResultType,
})
}
}

var ee *exec.ExitError
switch {
case err != nil && errors.Is(err, ErrContextCanceled):
logger.Info("Step was canceling")
output = append(output, result.RunResult{
Key: "Reason",
Value: "Cancelled",
ResultType: result.InternalTektonResultType,
})
e.WritePostFile(e.PostFile, ErrContextCanceled)
e.WriteExitCodeFile(e.StepMetadataDir, syscall.SIGKILL.String())
output = append(output, e.getTerminationReason(result.TerminationReasonCancelled))

case errors.Is(err, ErrContextDeadlineExceeded):
output = append(output, e.getTerminationReason(result.TerminationReasonTimeoutExceeded))

case err != nil && e.BreakpointOnFailure:
logger.Info("Skipping writing to PostFile")

case e.OnError == ContinueOnError && errors.As(err, &ee):
// with continue on error and an ExitError, write non-zero exit code and a post file
exitCode := strconv.Itoa(ee.ExitCode())
Expand All @@ -221,13 +215,18 @@ func (e Entrypointer) Go() error {
})
e.WritePostFile(e.PostFile, nil)
e.WriteExitCodeFile(e.StepMetadataDir, exitCode)
output = append(output, e.getTerminationReason(result.TerminationReasonContinued))

case err == nil:
// if err is nil, write zero exit code and a post file
e.WritePostFile(e.PostFile, nil)
e.WriteExitCodeFile(e.StepMetadataDir, "0")
output = append(output, e.getTerminationReason(result.TerminationReasonCompleted))

default:
// for a step without continue on error and any error, write a post file with .err
e.WritePostFile(e.PostFile, err)
output = append(output, e.getTerminationReason(result.TerminationReasonError))
}

// strings.Split(..) with an empty string returns an array that contains one element, an empty string.
Expand Down Expand Up @@ -317,3 +316,11 @@ func (e Entrypointer) waitingCancellation(ctx context.Context, cancel context.Ca
cancel()
return nil
}

func (e Entrypointer) getTerminationReason(terminationReason string) result.RunResult {
return result.RunResult{
Key: "Reason",
Value: terminationReason,
ResultType: result.InternalTektonResultType,
}
}
13 changes: 13 additions & 0 deletions pkg/result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ const (
InternalTektonResultType = 3
// UnknownResultType default unknown result type value
UnknownResultType = 10

// TerminationReasonCompleted indicates a step finished successfully.
TerminationReasonCompleted = "Completed"
// TerminationReasonContinued indicates a step errored but was ignored since onError was set to continue.
TerminationReasonContinued = "Continued"
// TerminationReasonError indicates a step failed with a non-zero exit code.
TerminationReasonError = "Error"
// TerminationReasonTimeoutExceeded indicates a step execution timed out.
TerminationReasonTimeoutExceeded = "TimeoutExceeded"
// TerminationReasonSkipped indicates a step execution was skipped due to previous step failed.
TerminationReasonSkipped = "Skipped"
// TerminationReasonCancelled indicates a step was cancelled by user.
TerminationReasonCancelled = "Cancelled"
)

// RunResult is used to write key/value pairs to TaskRun pod termination messages.
Expand Down

0 comments on commit 2514909

Please sign in to comment.