Skip to content

Commit

Permalink
Changes to add results to a task
Browse files Browse the repository at this point in the history
  • Loading branch information
othomann committed Jan 17, 2020
1 parent 78d7e11 commit 8b35cdf
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 13 deletions.
19 changes: 13 additions & 6 deletions cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ import (
"syscall"
"time"

"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/entrypoint"
)

var (
ep = flag.String("entrypoint", "", "Original specified entrypoint to execute")
waitFiles = flag.String("wait_file", "", "Comma-separated list of paths to wait for")
waitFileContent = flag.Bool("wait_file_content", false, "If specified, expect wait_file to have content")
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")

ep = flag.String("entrypoint", "", "Original specified entrypoint to execute")
waitFiles = flag.String("wait_file", "", "Comma-separated list of paths to wait for")
waitFileContent = flag.Bool("wait_file_content", false, "If specified, expect wait_file to have content")
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")
waitPollingInterval = time.Second
)

Expand All @@ -51,6 +52,12 @@ func main() {
Waiter: &realWaiter{},
Runner: &realRunner{},
PostWriter: &realPostWriter{},
Results: strings.Split(*results, ","),
}
if len(e.Results) != 0 {
if err := os.MkdirAll(pipeline.DefaultResultPath, 0755); err != nil {
log.Fatalf("Error creating the results directory: %v", err)
}
}
if err := e.Go(); err != nil {
switch t := err.(type) {
Expand Down
4 changes: 2 additions & 2 deletions docs/developers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ There are known issues with the existing implementation of sidecars:

- When the `nop` image does provide the sidecar's command, the sidecar will continue to
run even after `nop` has been swapped into the sidecar container's image
field. See https://github.com/tektoncd/pipeline/issues/1347 for the issue
tracking this bug. Until this issue is resolved the best way to avoid it is to
field. See [the issue tracking this bug](https://github.com/tektoncd/pipeline/issues/1347)
for the issue tracking this bug. Until this issue is resolved the best way to avoid it is to
avoid overriding the `nop` image when deploying the tekton controller, or
ensuring that the overridden `nop` image contains as few commands as possible.

Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/pipeline/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ package pipeline
const (
// WorkspaceDir is the root directory used for PipelineResources and (by default) Workspaces
WorkspaceDir = "/workspace"
// DefaultResultPath is the path for task result
DefaultResultPath = "/tekton/results"
)
4 changes: 4 additions & 0 deletions pkg/apis/pipeline/v1alpha1/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ type PipelineResourceResult struct {
Key string `json:"key"`
Value string `json:"value"`
ResourceRef PipelineResourceRef `json:"resourceRef,omitempty"`
ResultType ResultType `json:"type,omitempty"`
}

// ResultType used to find out whether a PipelineResourceResult is from a task result or not
type ResultType string

// ResourceFromType returns an instance of the correct PipelineResource object type which can be
// used to add input and output containers as well as volumes to a TaskRun's pod in order to realize
// a PipelineResource in a pod.
Expand Down
18 changes: 18 additions & 0 deletions pkg/apis/pipeline/v1alpha1/task_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2"
)

const (
// TaskRunResultType default task run result value
TaskRunResultType ResultType = "TaskRunResult"
)

func (t *Task) TaskSpec() TaskSpec {
return t.Spec
}
Expand Down Expand Up @@ -64,6 +69,19 @@ type TaskSpec struct {

// Workspaces are the volumes that this Task requires.
Workspaces []WorkspaceDeclaration `json:"workspaces,omitempty"`

// Results are values that this Task can output
Results []TaskResult `json:"results,omitempty"`
}

// TaskResult used to describe the results of a task
type TaskResult struct {
// Name the given name
Name string `json:"name"`

// Description is a human-readable description of the result
// +optional
Description string `json:"description"`
}

// Step embeds the Container type, which allows it to include fields not
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/pipeline/v1alpha1/zz_generated.deepcopy.go

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

39 changes: 39 additions & 0 deletions pkg/entrypoint/entrypointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ package entrypoint

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"

"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/logging"
"github.com/tektoncd/pipeline/pkg/termination"
Expand Down Expand Up @@ -51,6 +55,9 @@ type Entrypointer struct {
Runner Runner
// PostWriter encapsulates writing files when complete.
PostWriter PostWriter

// Results is the set of files that might contain task results
Results []string
}

// Waiter encapsulates waiting for files to exist.
Expand Down Expand Up @@ -100,12 +107,44 @@ func (e Entrypointer) Go() error {
// Write the post file *no matter what*
e.WritePostFile(e.PostFile, err)

if e.Results != nil && len(e.Results) != 0 {
if err := e.readResultsFromDisk(); err != nil {
logger.Fatalf("Error while handling results: %s", err)
}
}
if wErr := termination.WriteMessage(e.TerminationPath, output); wErr != nil {
logger.Fatalf("Error while writing message: %s", wErr)
}
return err
}

func (e Entrypointer) readResultsFromDisk() error {
output := []v1alpha1.PipelineResourceResult{}
for _, resultFile := range e.Results {
// check result file
fileContents, err := ioutil.ReadFile(filepath.Join(pipeline.DefaultResultPath, resultFile))
if os.IsNotExist(err) {
continue
} else if err != nil {
return err
}
// if the file doesn't exist, ignore it
output = append(output, v1alpha1.PipelineResourceResult{
Key: resultFile,
Value: string(fileContents),
ResultType: v1alpha1.TaskRunResultType,
})
}
// push output to termination path
if len(output) != 0 {
if err := termination.WriteMessage(e.TerminationPath, output); err != nil {
return err
}
}
return nil
}

// WritePostFile write the postfile
func (e Entrypointer) WritePostFile(postFile string, err error) {
if err != nil && postFile != "" {
postFile = fmt.Sprintf("%s.err", postFile)
Expand Down
23 changes: 20 additions & 3 deletions pkg/pod/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"strings"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -85,8 +86,8 @@ var (
// method, using entrypoint_lookup.go.
//
// TODO(#1605): Also use entrypoint injection to order sidecar start/stop.
func orderContainers(entrypointImage string, steps []corev1.Container) (corev1.Container, []corev1.Container, error) {
toolsInit := corev1.Container{
func orderContainers(entrypointImage string, steps []corev1.Container, results []v1alpha1.TaskResult) (corev1.Container, []corev1.Container, error) {
initContainer := corev1.Container{
Name: "place-tools",
Image: entrypointImage,
Command: []string{"cp", "/ko-app/entrypoint", entrypointBinary},
Expand Down Expand Up @@ -117,6 +118,7 @@ func orderContainers(entrypointImage string, steps []corev1.Container) (corev1.C
"-termination_path", terminationPath,
}
}
argsForEntrypoint = append(argsForEntrypoint, resultArgument(steps, results)...)

cmd, args := s.Command, s.Args
if len(cmd) == 0 {
Expand All @@ -137,7 +139,22 @@ func orderContainers(entrypointImage string, steps []corev1.Container) (corev1.C
// Mount the Downward volume into the first step container.
steps[0].VolumeMounts = append(steps[0].VolumeMounts, downwardMount)

return toolsInit, steps, nil
return initContainer, steps, nil
}

func resultArgument(steps []corev1.Container, results []v1alpha1.TaskResult) []string {
if results == nil || len(results) == 0 {
return nil
}
return []string{"-results", collectResultsName(results)}
}

func collectResultsName(results []v1alpha1.TaskResult) string {
var resultNames []string
for _, r := range results {
resultNames = append(resultNames, r.Name)
}
return strings.Join(resultNames, ",")
}

// UpdateReady updates the Pod's annotations to signal the first step to start
Expand Down
Loading

0 comments on commit 8b35cdf

Please sign in to comment.