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 16, 2020
1 parent 530f084 commit 2ee1b46
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 10 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
1 change: 1 addition & 0 deletions pkg/apis/pipeline/v1alpha1/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type PipelineResourceResult struct {
Key string `json:"key"`
Value string `json:"value"`
ResourceRef PipelineResourceRef `json:"resourceRef,omitempty"`
ResultType string `json:"type,omitempty"`
}

// ResourceFromType returns an instance of the correct PipelineResource object type which can be
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 all possible results of this Task
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.

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

import (
"fmt"
"io/ioutil"
"time"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/logging"
"github.com/tektoncd/pipeline/pkg/termination"
)

const (
// DefaultResultPath is the path for task result
DefaultResultPath = "/tekton/results/"
)

// Entrypointer holds fields for running commands with redirected
// entrypoints.
type Entrypointer struct {
Expand All @@ -51,6 +57,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 +109,39 @@ func (e Entrypointer) Go() error {
// Write the post file *no matter what*
e.WritePostFile(e.PostFile, err)

if e.Results != nil {
if err := e.handleResultFiles(); 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) handleResultFiles() error {
output := []v1alpha1.PipelineResourceResult{}
for _, resultFile := range e.Results {
// check result file
fileContents, err := ioutil.ReadFile(DefaultResultPath + resultFile)
if err != nil {
return err
}
output = append(output, v1alpha1.PipelineResourceResult{
Key: resultFile,
Value: string(fileContents),
ResultType: "result",
})
}
// push output to termination path
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: 22 additions & 1 deletion 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,7 +86,7 @@ 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) {
func orderContainers(entrypointImage string, steps []corev1.Container, results []v1alpha1.TaskResult) (corev1.Container, []corev1.Container, error) {
toolsInit := corev1.Container{
Name: "place-tools",
Image: entrypointImage,
Expand All @@ -109,6 +110,18 @@ func orderContainers(entrypointImage string, steps []corev1.Container) (corev1.C
"-post_file", filepath.Join(mountPoint, fmt.Sprintf("%d", i)),
"-termination_path", terminationPath,
}
if results != nil && len(steps) == 1 {
argsForEntrypoint = append(argsForEntrypoint, "-results", collectResultsName(results))
}
case len(steps) - 1:
argsForEntrypoint = []string{
"-wait_file", filepath.Join(mountPoint, fmt.Sprintf("%d", i-1)),
"-post_file", filepath.Join(mountPoint, fmt.Sprintf("%d", i)),
"-termination_path", terminationPath,
}
if results != nil {
argsForEntrypoint = append(argsForEntrypoint, "-results", collectResultsName(results))
}
default:
// All other steps wait for previous file, write next file.
argsForEntrypoint = []string{
Expand Down Expand Up @@ -140,6 +153,14 @@ func orderContainers(entrypointImage string, steps []corev1.Container) (corev1.C
return toolsInit, steps, nil
}

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
// by projecting the ready annotation via the Downward API.
func UpdateReady(kubeclient kubernetes.Interface, pod corev1.Pod) error {
Expand Down
146 changes: 145 additions & 1 deletion pkg/pod/entrypoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fakek8s "k8s.io/client-go/kubernetes/fake"
Expand Down Expand Up @@ -85,7 +86,7 @@ func TestOrderContainers(t *testing.T) {
VolumeMounts: []corev1.VolumeMount{toolsMount},
TerminationMessagePath: "/tekton/termination",
}}
gotInit, got, err := orderContainers(images.EntrypointImage, steps)
gotInit, got, err := orderContainers(images.EntrypointImage, steps, nil)
if err != nil {
t.Fatalf("orderContainers: %v", err)
}
Expand All @@ -104,6 +105,149 @@ func TestOrderContainers(t *testing.T) {
}
}

func TestEntryPointResults(t *testing.T) {
results := []v1alpha1.TaskResult{{
Name: "sum",
Description: "This is the sum result of the task",
}, {
Name: "sub",
Description: "This is the sub result of the task",
}}

steps := []corev1.Container{{
Image: "step-1",
Command: []string{"cmd"},
Args: []string{"arg1", "arg2"},
}, {
Image: "step-2",
Command: []string{"cmd1", "cmd2", "cmd3"}, // multiple cmd elements
Args: []string{"arg1", "arg2"},
VolumeMounts: []corev1.VolumeMount{volumeMount}, // pre-existing volumeMount
}, {
Image: "step-3",
Command: []string{"cmd"},
Args: []string{"arg1", "arg2"},
}}
want := []corev1.Container{{
Image: "step-1",
Command: []string{entrypointBinary},
Args: []string{
"-wait_file", "/tekton/downward/ready",
"-wait_file_content",
"-post_file", "/tekton/tools/0",
"-termination_path", "/tekton/termination",
"-entrypoint", "cmd", "--",
"arg1", "arg2",
},
VolumeMounts: []corev1.VolumeMount{toolsMount, downwardMount},
TerminationMessagePath: "/tekton/termination",
}, {
Image: "step-2",
Command: []string{entrypointBinary},
Args: []string{
"-wait_file", "/tekton/tools/0",
"-post_file", "/tekton/tools/1",
"-termination_path", "/tekton/termination",
"-entrypoint", "cmd1", "--",
"cmd2", "cmd3",
"arg1", "arg2",
},
VolumeMounts: []corev1.VolumeMount{volumeMount, toolsMount},
TerminationMessagePath: "/tekton/termination",
}, {
Image: "step-3",
Command: []string{entrypointBinary},
Args: []string{
"-wait_file", "/tekton/tools/1",
"-post_file", "/tekton/tools/2",
"-termination_path", "/tekton/termination",
"-results", "sum,sub",
"-entrypoint", "cmd", "--",
"arg1", "arg2",
},
VolumeMounts: []corev1.VolumeMount{toolsMount},
TerminationMessagePath: "/tekton/termination",
}}
_, got, err := orderContainers(images.EntrypointImage, steps, results)
if err != nil {
t.Fatalf("orderContainers: %v", err)
}
if d := cmp.Diff(want, got); d != "" {
t.Errorf("Diff (-want, +got): %s", d)
}
}

func TestEntryPointResultsSingleStep(t *testing.T) {
results := []v1alpha1.TaskResult{{
Name: "sum",
Description: "This is the sum result of the task",
}, {
Name: "sub",
Description: "This is the sub result of the task",
}}

steps := []corev1.Container{{
Image: "step-1",
Command: []string{"cmd"},
Args: []string{"arg1", "arg2"},
}}
want := []corev1.Container{{
Image: "step-1",
Command: []string{entrypointBinary},
Args: []string{
"-wait_file", "/tekton/downward/ready",
"-wait_file_content",
"-post_file", "/tekton/tools/0",
"-termination_path", "/tekton/termination",
"-results", "sum,sub",
"-entrypoint", "cmd", "--",
"arg1", "arg2",
},
VolumeMounts: []corev1.VolumeMount{toolsMount, downwardMount},
TerminationMessagePath: "/tekton/termination",
}}
_, got, err := orderContainers(images.EntrypointImage, steps, results)
if err != nil {
t.Fatalf("orderContainers: %v", err)
}
if d := cmp.Diff(want, got); d != "" {
t.Errorf("Diff (-want, +got): %s", d)
}
}
func TestEntryPointSingleResultsSingleStep(t *testing.T) {
results := []v1alpha1.TaskResult{{
Name: "sum",
Description: "This is the sum result of the task",
}}

steps := []corev1.Container{{
Image: "step-1",
Command: []string{"cmd"},
Args: []string{"arg1", "arg2"},
}}
want := []corev1.Container{{
Image: "step-1",
Command: []string{entrypointBinary},
Args: []string{
"-wait_file", "/tekton/downward/ready",
"-wait_file_content",
"-post_file", "/tekton/tools/0",
"-termination_path", "/tekton/termination",
"-results", "sum",
"-entrypoint", "cmd", "--",
"arg1", "arg2",
},
VolumeMounts: []corev1.VolumeMount{toolsMount, downwardMount},
TerminationMessagePath: "/tekton/termination",
}}
_, got, err := orderContainers(images.EntrypointImage, steps, results)
if err != nil {
t.Fatalf("orderContainers: %v", err)
}
if d := cmp.Diff(want, got); d != "" {
t.Errorf("Diff (-want, +got): %s", d)
}
}
func TestUpdateReady(t *testing.T) {
for _, c := range []struct {
desc string
Expand Down
2 changes: 1 addition & 1 deletion pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func MakePod(images pipeline.Images, taskRun *v1alpha1.TaskRun, taskSpec v1alpha

// Rewrite steps with entrypoint binary. Append the entrypoint init
// container to place the entrypoint binary.
entrypointInit, stepContainers, err := orderContainers(images.EntrypointImage, stepContainers)
entrypointInit, stepContainers, err := orderContainers(images.EntrypointImage, stepContainers, taskSpec.Results)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2ee1b46

Please sign in to comment.