Skip to content

Commit

Permalink
TEP-0090: Matrix - Implement isFailure for Runs
Browse files Browse the repository at this point in the history
[TEP-0090: Matrix][tep-0090] proposed executing a `PipelineTask` in
parallel `TaskRuns` and `Runs` with substitutions from combinations
of `Parameters` in a `Matrix`.

In this change, we implement the `isFailure` and other member functions
of `ResolvedPipelineRunTask` that `isFailure` uses: `isCancelled` and
`hasRemainingRetries`. `isFailure` evaluates to `true` only when there
is a failure and there are no running `Runs` in the `rprt`.

[tep-0090]: https://github.com/tektoncd/community/blob/main/teps/0090-matrix.md
  • Loading branch information
jerop authored and tekton-robot committed Jun 27, 2022
1 parent 067cbb7 commit ea87e41
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ func (t ResolvedPipelineTask) isFailure() bool {
var isDone bool

switch {
case t.IsCustomTask() && t.IsMatrixed():
if len(t.Runs) == 0 {
return false
}
isDone = true
atLeastOneFailed := false
for _, run := range t.Runs {
isDone = isDone && run.IsDone()
runFailed := run.Status.GetCondition(apis.ConditionSucceeded).IsFalse() && !t.hasRemainingRetries()
atLeastOneFailed = atLeastOneFailed || runFailed
}
return atLeastOneFailed && isDone
case t.IsCustomTask():
if t.Run == nil {
return false
Expand Down Expand Up @@ -183,6 +195,18 @@ func (t ResolvedPipelineTask) isFailure() bool {
func (t ResolvedPipelineTask) hasRemainingRetries() bool {
var retriesDone int
switch {
case t.IsCustomTask() && t.IsMatrixed():
if len(t.Runs) == 0 {
return true
}
// has remaining retries when any Run has a remaining retry
for _, run := range t.Runs {
retriesDone = len(run.Status.RetriesStatus)
if retriesDone < t.PipelineTask.Retries {
return true
}
}
return false
case t.IsCustomTask():
if t.Run == nil {
return true
Expand Down Expand Up @@ -213,6 +237,19 @@ func (t ResolvedPipelineTask) hasRemainingRetries() bool {
// If the PipelineTask has a Matrix, isCancelled returns true if any run is cancelled and all other runs are done.
func (t ResolvedPipelineTask) isCancelled() bool {
switch {
case t.IsCustomTask() && t.IsMatrixed():
if len(t.Runs) == 0 {
return false
}
isDone := true
atLeastOneCancelled := false
for _, run := range t.Runs {
isDone = isDone && run.IsDone()
c := run.Status.GetCondition(apis.ConditionSucceeded)
runCancelled := c.IsFalse() && c.Reason == v1alpha1.RunReasonCancelled
atLeastOneCancelled = atLeastOneCancelled || runCancelled
}
return atLeastOneCancelled && isDone
case t.IsCustomTask():
if t.Run == nil {
return false
Expand Down
135 changes: 135 additions & 0 deletions pkg/reconciler/pipelinerun/resources/pipelinerunresolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,20 +1135,43 @@ func TestIsFailure(t *testing.T) {
PipelineTask: matrixedPipelineTask,
},
want: false,
}, {
name: "matrixed runs not started",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: matrixedPipelineTask,
},
want: false,
}, {
name: "matrixed taskruns running",
rpt: ResolvedPipelineTask{
PipelineTask: matrixedPipelineTask,
TaskRuns: []*v1beta1.TaskRun{makeStarted(trs[0]), makeStarted(trs[1])},
},
want: false,
}, {
name: "matrixed runs running",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: matrixedPipelineTask,
Runs: []*v1alpha1.Run{makeRunStarted(runs[0]), makeRunStarted(runs[1])},
},
want: false,
}, {
name: "one matrixed taskrun running",
rpt: ResolvedPipelineTask{
PipelineTask: matrixedPipelineTask,
TaskRuns: []*v1beta1.TaskRun{makeStarted(trs[0]), makeSucceeded(trs[1])},
},
want: false,
}, {
name: "one matrixed run running",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: matrixedPipelineTask,
Runs: []*v1alpha1.Run{makeRunStarted(runs[0]), makeRunSucceeded(runs[1])},
},
want: false,
}, {
name: "matrixed taskruns succeeded",
rpt: ResolvedPipelineTask{
Expand All @@ -1163,97 +1186,209 @@ func TestIsFailure(t *testing.T) {
TaskRuns: []*v1beta1.TaskRun{makeSucceeded(trs[0]), makeStarted(trs[1])},
},
want: false,
}, {
name: "one matrixed run succeeded",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: matrixedPipelineTask,
Runs: []*v1alpha1.Run{makeRunSucceeded(runs[0]), makeRunStarted(runs[1])},
},
want: false,
}, {
name: "matrixed taskruns failed",
rpt: ResolvedPipelineTask{
PipelineTask: matrixedPipelineTask,
TaskRuns: []*v1beta1.TaskRun{makeFailed(trs[0]), makeFailed(trs[1])},
},
want: true,
}, {
name: "matrixed runs failed",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: matrixedPipelineTask,
Runs: []*v1alpha1.Run{makeRunFailed(runs[0]), makeRunFailed(runs[1])},
},
want: true,
}, {
name: "one matrixed taskrun failed, one matrixed taskrun running",
rpt: ResolvedPipelineTask{
PipelineTask: matrixedPipelineTask,
TaskRuns: []*v1beta1.TaskRun{makeFailed(trs[0]), makeStarted(trs[1])},
},
want: false,
}, {
name: "one matrixed run failed, one matrixed run running",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: matrixedPipelineTask,
Runs: []*v1alpha1.Run{makeRunFailed(runs[0]), makeRunStarted(runs[1])},
},
want: false,
}, {
name: "matrixed taskruns failed: retries remaining",
rpt: ResolvedPipelineTask{
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
TaskRuns: []*v1beta1.TaskRun{makeFailed(trs[0]), makeFailed(trs[1])},
},
want: false,
}, {
name: "matrixed runs failed: retries remaining",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
Runs: []*v1alpha1.Run{makeRunFailed(runs[0]), makeRunFailed(runs[1])},
},
want: false,
}, {
name: "matrixed taskruns failed: one taskrun with retries remaining",
rpt: ResolvedPipelineTask{
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
TaskRuns: []*v1beta1.TaskRun{makeFailed(trs[0]), withRetries(makeFailed(trs[1]))},
},
want: false,
}, {
name: "matrixed runs failed: one run with retries remaining",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
Runs: []*v1alpha1.Run{makeRunFailed(runs[0]), withRunRetries(makeRunFailed(runs[1]))},
},
want: false,
}, {
name: "matrixed taskruns failed: no retries remaining",
rpt: ResolvedPipelineTask{
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
TaskRuns: []*v1beta1.TaskRun{withRetries(makeFailed(trs[0])), withRetries(makeFailed(trs[1]))},
},
want: true,
}, {
name: "matrixed runs failed: no retries remaining",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
Runs: []*v1alpha1.Run{withRunRetries(makeRunFailed(runs[0])), withRunRetries(makeRunFailed(runs[1]))},
},
want: true,
}, {
name: "matrixed taskruns cancelled",
rpt: ResolvedPipelineTask{
PipelineTask: matrixedPipelineTask,
TaskRuns: []*v1beta1.TaskRun{withCancelled(makeFailed(trs[0])), withCancelled(makeFailed(trs[1]))},
},
want: true,
}, {
name: "matrixed runs cancelled",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: matrixedPipelineTask,
Runs: []*v1alpha1.Run{withRunCancelled(makeRunFailed(runs[0])), withRunCancelled(makeRunFailed(runs[1]))},
},
want: true,
}, {
name: "one matrixed taskrun cancelled, one matrixed taskrun running",
rpt: ResolvedPipelineTask{
PipelineTask: matrixedPipelineTask,
TaskRuns: []*v1beta1.TaskRun{withCancelled(makeFailed(trs[0])), makeStarted(trs[1])},
},
want: false,
}, {
name: "one matrixed run cancelled, one matrixed run running",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: matrixedPipelineTask,
Runs: []*v1alpha1.Run{withRunCancelled(makeRunFailed(runs[0])), makeRunStarted(runs[1])},
},
want: false,
}, {
name: "matrixed taskruns cancelled but not failed",
rpt: ResolvedPipelineTask{
PipelineTask: matrixedPipelineTask,
TaskRuns: []*v1beta1.TaskRun{withCancelled(newTaskRun(trs[0])), withCancelled(newTaskRun(trs[1]))},
},
want: false,
}, {
name: "matrixed runs cancelled but not failed",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: matrixedPipelineTask,
Runs: []*v1alpha1.Run{withRunCancelled(newRun(runs[0])), withRunCancelled(newRun(runs[1]))},
},
want: false,
}, {
name: "one matrixed taskrun cancelled but not failed",
rpt: ResolvedPipelineTask{
PipelineTask: matrixedPipelineTask,
TaskRuns: []*v1beta1.TaskRun{withCancelled(newTaskRun(trs[0])), makeStarted(trs[1])},
},
want: false,
}, {
name: "one matrixed run cancelled but not failed",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: matrixedPipelineTask,
Runs: []*v1alpha1.Run{withRunCancelled(newRun(runs[0])), makeRunStarted(runs[1])},
},
want: false,
}, {
name: "matrixed taskruns cancelled: retries remaining",
rpt: ResolvedPipelineTask{
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
TaskRuns: []*v1beta1.TaskRun{withCancelled(makeFailed(trs[0])), withCancelled(makeFailed(trs[1]))},
},
want: true,
}, {
name: "matrixed runs cancelled: retries remaining",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
Runs: []*v1alpha1.Run{withRunCancelled(makeRunFailed(runs[0])), withRunCancelled(makeRunFailed(runs[1]))},
},
want: true,
}, {
name: "one matrixed taskrun cancelled: retries remaining, one matrixed taskrun running",
rpt: ResolvedPipelineTask{
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
TaskRuns: []*v1beta1.TaskRun{withCancelled(makeFailed(trs[0])), makeStarted(trs[1])},
},
want: false,
}, {
name: "one matrixed run cancelled: retries remaining, one matrixed run running",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
Runs: []*v1alpha1.Run{withRunCancelled(makeRunFailed(runs[0])), makeRunStarted(runs[1])},
},
want: false,
}, {
name: "matrixed taskruns cancelled: no retries remaining",
rpt: ResolvedPipelineTask{
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
TaskRuns: []*v1beta1.TaskRun{withCancelled(withRetries(makeFailed(trs[0]))), withCancelled(withRetries(makeFailed(trs[1])))},
},
want: true,
}, {
name: "matrixed runs cancelled: no retries remaining",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
Runs: []*v1alpha1.Run{withRunCancelled(withRunRetries(makeRunFailed(runs[0]))), withRunCancelled(withRunRetries(makeRunFailed(runs[1])))},
},
want: true,
}, {
name: "one matrixed taskrun cancelled: no retries remaining, one matrixed taskrun running",
rpt: ResolvedPipelineTask{
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
TaskRuns: []*v1beta1.TaskRun{withCancelled(withRetries(makeFailed(trs[0]))), makeStarted(trs[1])},
},
want: false,
}, {
name: "one matrixed run cancelled: no retries remaining, one matrixed run running",
rpt: ResolvedPipelineTask{
CustomTask: true,
PipelineTask: withPipelineTaskRetries(*matrixedPipelineTask, 1),
Runs: []*v1alpha1.Run{withRunCancelled(withRunRetries(makeRunFailed(runs[0]))), makeRunStarted(runs[1])},
},
want: false,
}} {
t.Run(tc.name, func(t *testing.T) {
if got := tc.rpt.isFailure(); got != tc.want {
Expand Down

0 comments on commit ea87e41

Please sign in to comment.