Skip to content

Commit

Permalink
Adding substitution for result path variable
Browse files Browse the repository at this point in the history
  • Loading branch information
othomann committed Feb 7, 2020
1 parent a3ef085 commit 5811b1c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pkg/reconciler/taskrun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package resources

import (
"fmt"
"path/filepath"

"github.com/tektoncd/pipeline/pkg/workspace"

"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/substitution"
)
Expand Down Expand Up @@ -96,6 +98,17 @@ func ApplyWorkspaces(spec *v1alpha1.TaskSpec, w []v1alpha1.WorkspaceDeclaration,
return ApplyReplacements(spec, stringReplacements, map[string][]string{})
}

// ApplyTaskResults applies the substitution from values in results which are referenced in spec as subitems
// of the replacementStr.
func ApplyTaskResults(spec *v1alpha1.TaskSpec) *v1alpha1.TaskSpec {
stringReplacements := map[string]string{}

for _, result := range spec.Results {
stringReplacements[fmt.Sprintf("results.%s.path", result.Name)] = filepath.Join(pipeline.DefaultResultPath, result.Name)
}
return ApplyReplacements(spec, stringReplacements, map[string][]string{})
}

// ApplyReplacements replaces placeholders for declared parameters with the specified replacements.
func ApplyReplacements(spec *v1alpha1.TaskSpec, stringReplacements map[string]string, arrayReplacements map[string][]string) *v1alpha1.TaskSpec {
spec = spec.DeepCopy()
Expand Down
37 changes: 36 additions & 1 deletion pkg/reconciler/taskrun/resources/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,41 @@ func TestApplyWorkspaces(t *testing.T) {
}}
got := resources.ApplyWorkspaces(ts, w, wb)
if d := cmp.Diff(got, want); d != "" {
t.Errorf("ApplyParameters() got diff %s", d)
t.Errorf("TestApplyWorkspaces() got diff %s", d)
}
}

func TestTaskResults(t *testing.T) {
names.TestingSeed()
ts := &v1alpha1.TaskSpec{
Results: []v1alpha1.TaskResult{{
Name: "current-date-unix-timestamp",
Description: "The current date in unix timestamp format",
}, {
Name: "current-date-human-readable",
Description: "The current date in humand readable format"},
},
Steps: []v1alpha1.Step{
{Container: corev1.Container{
Name: "print-date-unix-timestamp",
Image: "bash:latest",
Args: []string{"$(results.current-date-unix-timestamp.path)"},
},
Script: "#!/usr/bin/env bash\ndate +%s | tee $(results.current-date-unix-timestamp.path)"},
{Container: corev1.Container{
Name: "print-date-humman-readable",
Image: "bash:latest",
},
Script: "#!/usr/bin/env bash\ndate | tee $(results.current-date-human-readable.path)"},
},
}
want := applyMutation(ts, func(spec *v1alpha1.TaskSpec) {
spec.Steps[0].Script = "#!/usr/bin/env bash\ndate +%s | tee /tekton/results/current-date-unix-timestamp"
spec.Steps[0].Args[0] = "/tekton/results/current-date-unix-timestamp"
spec.Steps[1].Script = "#!/usr/bin/env bash\ndate | tee /tekton/results/current-date-human-readable"
})
got := resources.ApplyTaskResults(ts)
if d := cmp.Diff(got, want); d != "" {
t.Errorf("ApplyTaskResults() got diff %s", d)
}
}
3 changes: 3 additions & 0 deletions pkg/reconciler/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@ func (c *Reconciler) createPod(tr *v1alpha1.TaskRun, rtr *resources.ResolvedTask
// Apply workspace resource substitution
ts = resources.ApplyWorkspaces(ts, ts.Workspaces, tr.Spec.Workspaces)

// Apply task result substitution
ts = resources.ApplyTaskResults(ts)

ts, err = workspace.Apply(*ts, tr.Spec.Workspaces)
if err != nil {
c.Logger.Errorf("Failed to create a pod for taskrun: %s due to workspace error %v", tr.Name, err)
Expand Down

0 comments on commit 5811b1c

Please sign in to comment.