Skip to content

Commit

Permalink
TEP-0142: Surface step results via termination message
Browse files Browse the repository at this point in the history
This PR surfaces step results (i.e results written to $(step.results.<resultName>.path)) via termination messages.  A followup PR will handle surfacing the results via sidecar logs.
  • Loading branch information
chitrangpatel committed Nov 14, 2023
1 parent f975869 commit 19cf121
Show file tree
Hide file tree
Showing 20 changed files with 837 additions and 40 deletions.
2 changes: 2 additions & 0 deletions cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var (
postFile = flag.String("post_file", "", "If specified, file to write upon completion")
terminationPath = flag.String("termination_path", "/tekton/termination", "If specified, file to write upon termination")
results = flag.String("results", "", "If specified, list of file names that might contain task results")
stepResults = flag.String("step_results", "", "step results if specified")
timeout = flag.Duration("timeout", time.Duration(0), "If specified, sets timeout for step")
stdoutPath = flag.String("stdout_path", "", "If specified, file to copy stdout to")
stderrPath = flag.String("stderr_path", "", "If specified, file to copy stderr to")
Expand Down Expand Up @@ -159,6 +160,7 @@ func main() {
},
PostWriter: &realPostWriter{},
Results: strings.Split(*results, ","),
StepResults: strings.Split(*stepResults, ","),
Timeout: timeout,
BreakpointOnFailure: *breakpointOnFailure,
OnError: *onError,
Expand Down
14 changes: 13 additions & 1 deletion docs/pipeline-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4534,6 +4534,18 @@ string
<td>
</td>
</tr>
<tr>
<td>
<code>results</code><br/>
<em>
<a href="#tekton.dev/v1.TaskRunResult">
[]TaskRunResult
</a>
</em>
</td>
<td>
</td>
</tr>
</tbody>
</table>
<h3 id="tekton.dev/v1.StepTemplate">StepTemplate
Expand Down Expand Up @@ -5052,7 +5064,7 @@ reasons that emerge from underlying resources are not included here</p>
<h3 id="tekton.dev/v1.TaskRunResult">TaskRunResult
</h3>
<p>
(<em>Appears on:</em><a href="#tekton.dev/v1.TaskRunStatusFields">TaskRunStatusFields</a>)
(<em>Appears on:</em><a href="#tekton.dev/v1.StepState">StepState</a>, <a href="#tekton.dev/v1.TaskRunStatusFields">TaskRunStatusFields</a>)
</p>
<div>
<p>TaskRunResult used to describe the results of a task</p>
Expand Down
83 changes: 81 additions & 2 deletions docs/stepactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ A `StepAction` definition supports the following fields:
- cannot be used at the same time as using `command`.
- `env`
- [`params`](#declaring-params)
- [`results`](#declaring-results)
- [`results`](#emitting-results)
- [`securityContext`](#declaring-securitycontext)
- [`volumeMounts`](#declaring-volumemounts)

Expand Down Expand Up @@ -93,7 +93,7 @@ spec:
]
```

### Declaring Results
### Emitting Results

A `StepAction` also declares the results that it will emit.

Expand All @@ -115,6 +115,85 @@ spec:
date | tee $(results.current-date-human-readable.path)
```

In the above example, it is possible that when the same `StepAction` is used multiple times in the same `Task` or if multiple `StepActions` in the same `Task` produce a result that has the same name, resolving the names becomes critical otherwise there could be unexpected outcomes. The `Task` needs to be able to resolve these `Result` names clashes by mapping it to a differen result name. For this reason, we introduce the capability to store results on a `Step` level.

The above `StepAction` can also emit results to `$(step.results.<resultName>.path)`.

```yaml
apiVersion: tekton.dev/v1alpha1
kind: StepAction
metadata:
name: stepaction-declaring-results
spec:
results:
- name: current-date-unix-timestamp
description: The current date in unix timestamp format
- name: current-date-human-readable
description: The current date in human readable format
image: bash:latest
script: |
#!/usr/bin/env bash
date +%s | tee $(step.results.current-date-unix-timestamp.path)
date | tee $(step.results.current-date-human-readable.path)
```

`Results` from the above `StepAciton` can be [fetched by the `Task`](#fetching-emitted-results-from-step-actions) using this `StepAction` via `$(steps.<stepName>.results.<resultName>)`.

#### Fetching Emitted Results from StepActions

A `Task` can fetch `Results` produced by the `StepActions` (i.e. only results emitted to `$(step.results.<resultName>.path)`, `NOT` $(results.<resultName>.path)) using variable replacement syntax. We introduce a field to [`Task Results`](./tasks.md#emitting-results) called `Value` whose value can be set to the variable `$(steps.<stepName>.results.<resultName>)`.

```yaml
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: task-fetching-results
spec:
results:
- name: git-url
description: "url of git repo"
value: $(steps.git-clone.results.url)
- name: registry-url
description: "url of docker registry"
value: $(steps.kaniko.results.url)
steps:
- name: git-clone
ref:
name: clone-step-action
- name: kaniko
ref:
name: kaniko-step-action
```

`Results` emitted to `$(step.results.<resultName>.path)` are not automatically available as `TaskRun Results`. The `Task` must explicitly fetch it from the underlying `Step` referencing `StepActions`.

For example, lets assume that in the previous example, the "kaniko" `StepAction` also produced a result called "digest". In that case, the `Task` should also fetch the "digest" from "kaniko" `Step`.

```yaml
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: task-fetching-results
spec:
results:
- name: git-url
description: "url of git repo"
value: $(steps.git-clone.results.url)
- name: registry-url
description: "url of docker registry"
value: $(steps.kaniko.results.url)
- nane: digest
description: "digest of the image"
value: $(steps.kaniko.results.digest)
steps:
- name: git-clone
ref:
name: clone-step-action
- name: kaniko
ref:
name: kaniko-step-action
```

### Declaring SecurityContext

You can declare `securityContext` in a `StepAction`:
Expand Down
56 changes: 56 additions & 0 deletions examples/v1/taskruns/alpha/stepaction-results.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apiVersion: tekton.dev/v1alpha1
kind: StepAction
metadata:
name: step-action-uri
spec:
results:
- name: uri
params:
- name: uri
image: alpine
script: |
echo $(params.uri) > $(step.results.uri.path)
---
apiVersion: tekton.dev/v1alpha1
kind: StepAction
metadata:
name: step-action-uri-digest
spec:
results:
- name: digest
- name: uri
params:
- name: uri
- name: digest
image: alpine
script: |
echo $(params.digest) > $(step.results.digest.path)
echo $(params.uri) > $(step.results.uri.path)
---
apiVersion: tekton.dev/v1
kind: TaskRun
metadata:
name: step-action-run
spec:
TaskSpec:
results:
- name: step-1-uri
value: $(steps.step1.results.uri)
- name: step-2-uri
value: $(steps.step2.results.uri)
- name: digest
steps:
- name: step1
ref:
name: step-action-uri
params:
- name: uri
value: "https://github.com/tektoncd/pipeline"
- name: step2
ref:
name: step-action-uri-digest
params:
- name: uri
value: "https://github.com/tektoncd/other"
- name: digest
value: "c8381846241cac4c93c30b6a5ac04cac51fa0a6e"
15 changes: 14 additions & 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.

7 changes: 7 additions & 0 deletions pkg/apis/pipeline/v1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,13 @@
"name": {
"type": "string"
},
"results": {
"type": "array",
"items": {
"default": {},
"$ref": "#/definitions/v1.TaskRunResult"
}
},
"running": {
"description": "Details about a running container",
"$ref": "#/definitions/v1.ContainerStateRunning"
Expand Down
7 changes: 4 additions & 3 deletions pkg/apis/pipeline/v1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ func (trs *TaskRunStatus) SetCondition(newCond *apis.Condition) {
// StepState reports the results of running a step in a Task.
type StepState struct {
corev1.ContainerState `json:",inline"`
Name string `json:"name,omitempty"`
Container string `json:"container,omitempty"`
ImageID string `json:"imageID,omitempty"`
Name string `json:"name,omitempty"`
Container string `json:"container,omitempty"`
ImageID string `json:"imageID,omitempty"`
Results []TaskRunResult `json:"results,omitempty"`
}

// SidecarState reports the results of running a sidecar in a Task.
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/pipeline/v1/zz_generated.deepcopy.go

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

27 changes: 23 additions & 4 deletions pkg/entrypoint/entrypointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ type Entrypointer struct {
// PostWriter encapsulates writing files when complete.
PostWriter PostWriter

// StepResults is the set of files that might contain step results
StepResults []string
// Results is the set of files that might contain task results
Results []string
// Timeout is an optional user-specified duration within which the Step must complete
Expand Down Expand Up @@ -147,6 +149,9 @@ func (e Entrypointer) Go() error {
_ = logger.Sync()
}()

if err := os.MkdirAll(filepath.Join(e.StepMetadataDir, "results"), os.ModePerm); err != nil {
return err
}
for _, f := range e.WaitFiles {
if err := e.Waiter.Wait(context.Background(), f, e.WaitFileContent, e.BreakpointOnFailure); err != nil {
// An error happened while waiting, so we bail
Expand Down Expand Up @@ -237,17 +242,30 @@ func (e Entrypointer) Go() error {
if e.ResultsDirectory != "" {
resultPath = e.ResultsDirectory
}
if err := e.readResultsFromDisk(ctx, resultPath); err != nil {
if err := e.readResultsFromDisk(ctx, resultPath, result.TaskRunResultType); err != nil {
logger.Fatalf("Error while handling results: %s", err)
}
}
if len(e.StepResults) >= 1 && e.StepResults[0] != "" {
stepResultPath := filepath.Join(e.StepMetadataDir, "results")
if e.ResultsDirectory != "" {
stepResultPath = e.ResultsDirectory
}
if err := e.readResultsFromDisk(ctx, stepResultPath, result.StepResultType); err != nil {
logger.Fatalf("Error while handling step results: %s", err)
}
}

return err
}

func (e Entrypointer) readResultsFromDisk(ctx context.Context, resultDir string) error {
func (e Entrypointer) readResultsFromDisk(ctx context.Context, resultDir string, resultType result.ResultType) error {
output := []result.RunResult{}
for _, resultFile := range e.Results {
results := e.Results
if resultType == result.StepResultType {
results = e.StepResults
}
for _, resultFile := range results {
if resultFile == "" {
continue
}
Expand All @@ -261,9 +279,10 @@ func (e Entrypointer) readResultsFromDisk(ctx context.Context, resultDir string)
output = append(output, result.RunResult{
Key: resultFile,
Value: string(fileContents),
ResultType: result.TaskRunResultType,
ResultType: resultType,
})
}

if e.SpireWorkloadAPI != nil {
signed, err := e.SpireWorkloadAPI.Sign(ctx, output)
if err != nil {
Expand Down
Loading

0 comments on commit 19cf121

Please sign in to comment.