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 d8a0190
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 19 deletions.
13 changes: 7 additions & 6 deletions cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import (
)

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 +51,7 @@ func main() {
Waiter: &realWaiter{},
Runner: &realRunner{},
PostWriter: &realPostWriter{},
Results: strings.Split(*results, ","),
}
if err := e.Go(); err != nil {
switch t := err.(type) {
Expand Down
2 changes: 1 addition & 1 deletion docs/developers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ 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
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
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
12 changes: 12 additions & 0 deletions pkg/apis/pipeline/v1alpha1/task_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ 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 the given description
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 {
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: "result",
})
}
// 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
53 changes: 46 additions & 7 deletions pkg/pod/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"path/filepath"
"strings"

"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"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 All @@ -39,6 +41,8 @@ const (
readyAnnotation = "tekton.dev/ready"
readyAnnotationValue = "READY"

resultsVolumeName = "tekton-internal-results"

stepPrefix = "step-"
sidecarPrefix = "sidecar-"
)
Expand All @@ -54,6 +58,15 @@ var (
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
}

resultsMount = corev1.VolumeMount{
Name: resultsVolumeName,
MountPath: pipeline.DefaultResultPath,
}
resultsVolume = corev1.Volume{
Name: resultsVolumeName,
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
}

// TODO(#1605): Signal sidecar readiness by injecting entrypoint,
// remove dependency on Downward API.
downwardVolume = corev1.Volume{
Expand Down Expand Up @@ -85,16 +98,26 @@ 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(images pipeline.Images, steps []corev1.Container, results []v1alpha1.TaskResult) ([]corev1.Container, []corev1.Container, error) {
initContainers := []corev1.Container{}
if results != nil && len(results) != 0 {
initContainers = append(initContainers, corev1.Container{
Name: "results-dir-initializer",
Image: images.ShellImage,
Command: []string{"sh"},
Args: []string{"-c", "mkdir -p " + pipeline.DefaultResultPath},
VolumeMounts: []corev1.VolumeMount{resultsMount},
})
}
initContainers = append(initContainers, corev1.Container{
Name: "place-tools",
Image: entrypointImage,
Image: images.EntrypointImage,
Command: []string{"cp", "/ko-app/entrypoint", entrypointBinary},
VolumeMounts: []corev1.VolumeMount{toolsMount},
}
})

if len(steps) == 0 {
return corev1.Container{}, nil, errors.New("No steps specified")
return []corev1.Container{}, nil, errors.New("No steps specified")
}

for i, s := range steps {
Expand All @@ -117,10 +140,11 @@ 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 {
return corev1.Container{}, nil, fmt.Errorf("Step %d did not specify command", i)
return []corev1.Container{}, nil, fmt.Errorf("Step %d did not specify command", i)
}
if len(cmd) > 1 {
args = append(cmd[1:], args...)
Expand All @@ -137,7 +161,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 initContainers, 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 d8a0190

Please sign in to comment.