Skip to content

Commit

Permalink
When Expressions Status
Browse files Browse the repository at this point in the history
When a Task is skipped because its When Expressions evaluated to false,
its resolved When Expressions are listed alongside the PipelineTaskName
in the SkippedTasks section of the PipelineRunStatus.

When a Task is executed because its When Expressions evaluated to true,
its resolved When Expressions are listed in the TaskRuns section of the
PipelineRunStatus.

Further details in https://github.com/tektoncd/community/blob/master/teps/0007-conditions-beta.md#status-1

Closes tektoncd#3139
  • Loading branch information
jerop committed Oct 5, 2020
1 parent 3a8d414 commit a068915
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 7 deletions.
18 changes: 16 additions & 2 deletions docs/pipelineruns.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,8 @@ False|PipelineRunTimeout|Yes|The `PipelineRun` timed out.
When a `PipelineRun` changes status, [events](events.md#pipelineruns) are triggered accordingly.

When a `PipelineRun` has `Tasks` with [WhenExpressions](pipelines.md#guard-task-execution-using-whenexpressions):
- If the `WhenExpressions` evaluate to `true`, the `Task` is executed and the `TaskRun` will be listed in the `Task Runs` section of the `status` of the `PipelineRun`.
- If the `WhenExpressions` evaluate to `false`, the `Task` is skipped and it is listed in the `Skipped Tasks` section of the `status` of the `PipelineRun`.
- If the `WhenExpressions` evaluate to `true`, the `Task` is executed then the `TaskRun` and its resolved `WhenExpressions` will be listed in the `Task Runs` section of the `status` of the `PipelineRun`.
- If the `WhenExpressions` evaluate to `false`, the `Task` is skipped then its name and its resolved `WhenExpressions` will be listed in the `Skipped Tasks` section of the `status` of the `PipelineRun`.

```yaml
Conditions:
Expand All @@ -470,11 +470,25 @@ Conditions:
Type: Succeeded
Skipped Tasks:
Name: skip-this-task
When Expressions:
Input: foo
Operator: in
Values:
bar
Input: foo
Operator: notin
Values:
foo
Task Runs:
pipelinerun-to-skip-task-run-this-task-r2djj:
Pipeline Task Name: run-this-task
Status:
...
When Expressions:
Input: foo
Operator: in
Values:
foo
```

## Cancelling a `PipelineRun`
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipelinerun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ type PipelineRunStatusFields struct {
type SkippedTask struct {
// Name is the Pipeline Task name
Name string `json:"name"`
// WhenExpressions is the list of checks guarding the execution of the PipelineTask
// +optional
WhenExpressions []WhenExpression `json:"whenExpressions,omitempty"`
}

// PipelineRunResult used to describe the results of a pipeline
Expand All @@ -363,6 +366,9 @@ type PipelineRunTaskRunStatus struct {
// ConditionChecks maps the name of a condition check to its Status
// +optional
ConditionChecks map[string]*PipelineRunConditionCheckStatus `json:"conditionChecks,omitempty"`
// WhenExpressions is the list of checks guarding the execution of the PipelineTask
// +optional
WhenExpressions []WhenExpression `json:"whenExpressions,omitempty"`
}

// PipelineRunConditionCheckStatus returns the condition check status
Expand Down
18 changes: 17 additions & 1 deletion pkg/apis/pipeline/v1beta1/zz_generated.deepcopy.go

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

52 changes: 50 additions & 2 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,12 @@ func TestUpdateTaskRunsState(t *testing.T) {
// from a TaskRun associated to the PipelineRun
pr := tb.PipelineRun("test-pipeline-run", tb.PipelineRunNamespace("foo"), tb.PipelineRunSpec("test-pipeline"))
pipelineTask := v1beta1.PipelineTask{
Name: "unit-test-1",
Name: "unit-test-1",
WhenExpressions: []v1beta1.WhenExpression{{
Input: "foo",
Operator: selection.In,
Values: []string{"foo", "bar"},
}},
TaskRef: &v1beta1.TaskRef{Name: "unit-test-task"},
}
task := tb.Task("unit-test-task", tb.TaskSpec(
Expand Down Expand Up @@ -792,6 +797,11 @@ func TestUpdateTaskRunsState(t *testing.T) {
Conditions: []apis.Condition{{Type: apis.ConditionSucceeded}},
},
},
WhenExpressions: []v1beta1.WhenExpression{{
Input: "foo",
Operator: selection.In,
Values: []string{"foo", "bar"},
}},
}
expectedPipelineRunStatus := v1beta1.PipelineRunStatus{
PipelineRunStatusFields: v1beta1.PipelineRunStatusFields{
Expand All @@ -809,7 +819,7 @@ func TestUpdateTaskRunsState(t *testing.T) {
}}
pr.Status.InitializeConditions()
status := state.GetTaskRunsStatus(pr)
if d := cmp.Diff(status, expectedPipelineRunStatus.TaskRuns); d != "" {
if d := cmp.Diff(expectedPipelineRunStatus.TaskRuns, status); d != "" {
t.Fatalf("Expected PipelineRun status to match TaskRun(s) status, but got a mismatch: %s", diff.PrintWantGot(d))
}

Expand Down Expand Up @@ -2076,9 +2086,28 @@ func TestReconcileWithWhenExpressionsWithParameters(t *testing.T) {
t.Errorf("expected to see TaskRun %v created. Diff %s", expectedTaskRunName, diff.PrintWantGot(d))
}

actualWhenExpressionsInTaskRun := pipelineRun.Status.PipelineRunStatusFields.TaskRuns[expectedTaskRunName].WhenExpressions
expectedWhenExpressionsInTaskRun := []v1beta1.WhenExpression{{
Input: "foo",
Operator: "notin",
Values: []string{"bar"},
}, {
Input: "yes",
Operator: "in",
Values: []string{"yes"},
}}
if d := cmp.Diff(expectedWhenExpressionsInTaskRun, actualWhenExpressionsInTaskRun); d != "" {
t.Errorf("expected to see When Expressions %v created. Diff %s", expectedTaskRunName, diff.PrintWantGot(d))
}

actualSkippedTasks := pipelineRun.Status.SkippedTasks
expectedSkippedTasks := []v1beta1.SkippedTask{{
Name: "hello-world-2",
WhenExpressions: v1beta1.WhenExpressions{{
Input: "yes",
Operator: "notin",
Values: []string{"yes"},
}},
}}
if d := cmp.Diff(actualSkippedTasks, expectedSkippedTasks); d != "" {
t.Errorf("expected to find Skipped Tasks %v. Diff %s", expectedSkippedTasks, diff.PrintWantGot(d))
Expand Down Expand Up @@ -2197,9 +2226,28 @@ func TestReconcileWithWhenExpressionsWithTaskResults(t *testing.T) {
t.Errorf("expected to see TaskRun %v created. Diff %s", expectedTaskRunName, diff.PrintWantGot(d))
}

actualWhenExpressionsInTaskRun := pipelineRun.Status.PipelineRunStatusFields.TaskRuns[expectedTaskRunName].WhenExpressions
expectedWhenExpressionsInTaskRun := []v1beta1.WhenExpression{{
Input: "aResultValue",
Operator: "in",
Values: []string{"aResultValue"},
}, {
Input: "aResultValue",
Operator: "in",
Values: []string{"aResultValue"},
}}
if d := cmp.Diff(expectedWhenExpressionsInTaskRun, actualWhenExpressionsInTaskRun); d != "" {
t.Errorf("expected to see When Expressions %v created. Diff %s", expectedTaskRunName, diff.PrintWantGot(d))
}

actualSkippedTasks := pipelineRun.Status.SkippedTasks
expectedSkippedTasks := []v1beta1.SkippedTask{{
Name: "c-task",
WhenExpressions: v1beta1.WhenExpressions{{
Input: "aResultValue",
Operator: "in",
Values: []string{"missing"},
}},
}, {
Name: "d-task",
}}
Expand Down
9 changes: 7 additions & 2 deletions pkg/reconciler/pipelinerun/resources/pipelinerunstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (state PipelineRunState) GetTaskRunsStatus(pr *v1beta1.PipelineRun) map[str
if prtrs == nil {
prtrs = &v1beta1.PipelineRunTaskRunStatus{
PipelineTaskName: rprt.PipelineTask.Name,
WhenExpressions: rprt.PipelineTask.WhenExpressions,
}
}

Expand Down Expand Up @@ -281,10 +282,14 @@ func (facts *PipelineRunFacts) GetPipelineConditionStatus(pr *v1beta1.PipelineRu
}

func (facts *PipelineRunFacts) GetSkippedTasks() []v1beta1.SkippedTask {
skipped := []v1beta1.SkippedTask{}
var skipped []v1beta1.SkippedTask
for _, rprt := range facts.State {
if rprt.Skip(facts) {
skipped = append(skipped, v1beta1.SkippedTask{Name: rprt.PipelineTask.Name})
skippedTask := v1beta1.SkippedTask{
Name: rprt.PipelineTask.Name,
WhenExpressions: rprt.PipelineTask.WhenExpressions,
}
skipped = append(skipped, skippedTask)
}
}
return skipped
Expand Down

0 comments on commit a068915

Please sign in to comment.