Skip to content

Commit 52f563e

Browse files
committed
refactoring get condition status for pipeline
GetPipelineConditionStatus based on a new structure PipelineRunFacts PR # 3254 applied refactoring and introduced PipelineRunFacts to simplify PipelineRun. This is a follow up PR to further simplify GetPipelineConditionStatus.
1 parent 4f5a71c commit 52f563e

File tree

1 file changed

+54
-51
lines changed

1 file changed

+54
-51
lines changed

pkg/reconciler/pipelinerun/resources/pipelinerunstate.go

+54-51
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package resources
1818

1919
import (
2020
"fmt"
21-
"reflect"
2221

2322
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
2423
"github.com/tektoncd/pipeline/pkg/reconciler/pipeline/dag"
@@ -203,53 +202,31 @@ func (facts *PipelineRunFacts) GetPipelineConditionStatus(pr *v1beta1.PipelineRu
203202
}
204203
}
205204

206-
allTasks := []string{}
207-
withStatusTasks := []string{}
208-
skipTasks := []v1beta1.SkippedTask{}
209-
failedTasks := int(0)
210-
cancelledTasks := int(0)
211-
reason := v1beta1.PipelineRunReasonSuccessful.String()
212-
213-
// Check to see if all tasks are success or skipped
214-
//
215-
// The completion reason is also calculated here, but it will only be used
216-
// if all tasks are completed.
217-
//
218-
// The pipeline run completion reason is set from the taskrun completion reason
219-
// according to the following logic:
220-
//
221-
// - All successful: ReasonSucceeded
222-
// - Some successful, some skipped: ReasonCompleted
223-
// - Some cancelled, none failed: ReasonCancelled
224-
// - At least one failed: ReasonFailed
225-
for _, rprt := range facts.State {
226-
allTasks = append(allTasks, rprt.PipelineTask.Name)
227-
switch {
228-
case rprt.IsSuccessful():
229-
withStatusTasks = append(withStatusTasks, rprt.PipelineTask.Name)
230-
case rprt.Skip(facts):
231-
withStatusTasks = append(withStatusTasks, rprt.PipelineTask.Name)
232-
skipTasks = append(skipTasks, v1beta1.SkippedTask{Name: rprt.PipelineTask.Name})
233-
// At least one is skipped and no failure yet, mark as completed
234-
if reason == v1beta1.PipelineRunReasonSuccessful.String() {
235-
reason = v1beta1.PipelineRunReasonCompleted.String()
236-
}
237-
case rprt.IsCancelled():
238-
cancelledTasks++
239-
withStatusTasks = append(withStatusTasks, rprt.PipelineTask.Name)
240-
if reason != v1beta1.PipelineRunReasonFailed.String() {
241-
reason = v1beta1.PipelineRunReasonCancelled.String()
242-
}
243-
case rprt.IsFailure():
244-
withStatusTasks = append(withStatusTasks, rprt.PipelineTask.Name)
245-
failedTasks++
246-
reason = v1beta1.PipelineRunReasonFailed.String()
247-
}
248-
}
205+
// report the count in PipelineRun Status
206+
// get the count of successful tasks, failed tasks, cancelled tasks, skipped task, and incomplete tasks
207+
sTasks, fTasks, cTasks, skTasks, iTasks := facts.getPipelineTasksCount()
208+
// completed task is a collection of successful, failed, cancelled tasks (skipped tasks are reported separately)
209+
cmTasks := sTasks + fTasks + cTasks
249210

250-
if reflect.DeepEqual(allTasks, withStatusTasks) {
211+
// The completion reason is set from the TaskRun completion reason
212+
// by default, set it to ReasonRunning
213+
reason := v1beta1.PipelineRunReasonRunning.String()
214+
215+
// check if the pipeline is finished executing all tasks i.e. no incomplete tasks
216+
if iTasks == 0 {
251217
status := corev1.ConditionTrue
252-
if failedTasks > 0 || cancelledTasks > 0 {
218+
reason := v1beta1.PipelineRunReasonSuccessful.String()
219+
// Set reason to ReasonCompleted - At least one is skipped
220+
if skTasks > 0 {
221+
reason = v1beta1.PipelineRunReasonCompleted.String()
222+
}
223+
// Set reason to ReasonFailed - At least one failed
224+
if fTasks > 0 {
225+
reason = v1beta1.PipelineRunReasonFailed.String()
226+
status = corev1.ConditionFalse
227+
// Set reason to ReasonCancelled - At least one is cancelled and no failure yet
228+
} else if cTasks > 0 {
229+
reason = v1beta1.PipelineRunReasonCancelled.String()
253230
status = corev1.ConditionFalse
254231
}
255232
logger.Infof("All TaskRuns have finished for PipelineRun %s so it has finished", pr.Name)
@@ -258,28 +235,29 @@ func (facts *PipelineRunFacts) GetPipelineConditionStatus(pr *v1beta1.PipelineRu
258235
Status: status,
259236
Reason: reason,
260237
Message: fmt.Sprintf("Tasks Completed: %d (Failed: %d, Cancelled %d), Skipped: %d",
261-
len(allTasks)-len(skipTasks), failedTasks, cancelledTasks, len(skipTasks)),
238+
cmTasks, fTasks, cTasks, skTasks),
262239
}
263240
}
264241

265242
// Hasn't timed out; not all tasks have finished.... Must keep running then....
266243
// transition pipeline into stopping state when one of the tasks(dag/final) cancelled or one of the dag tasks failed
267244
// for a pipeline with final tasks, single dag task failure does not transition to interim stopping state
268245
// pipeline stays in running state until all final tasks are done before transitioning to failed state
269-
if cancelledTasks > 0 || (failedTasks > 0 && facts.checkFinalTasksDone()) {
246+
if cTasks > 0 || (fTasks > 0 && facts.checkFinalTasksDone()) {
270247
reason = v1beta1.PipelineRunReasonStopping.String()
271-
} else {
272-
reason = v1beta1.PipelineRunReasonRunning.String()
273248
}
249+
250+
// return the status
274251
return &apis.Condition{
275252
Type: apis.ConditionSucceeded,
276253
Status: corev1.ConditionUnknown,
277254
Reason: reason,
278255
Message: fmt.Sprintf("Tasks Completed: %d (Failed: %d, Cancelled %d), Incomplete: %d, Skipped: %d",
279-
len(withStatusTasks)-len(skipTasks), failedTasks, cancelledTasks, len(allTasks)-len(withStatusTasks), len(skipTasks)),
256+
cmTasks, fTasks, cTasks, iTasks, skTasks),
280257
}
281258
}
282259

260+
// GetSkippedTasks constructs a list of SkippedTask struct to be included in the PipelineRun Status
283261
func (facts *PipelineRunFacts) GetSkippedTasks() []v1beta1.SkippedTask {
284262
skipped := []v1beta1.SkippedTask{}
285263
for _, rprt := range facts.State {
@@ -336,6 +314,31 @@ func (facts *PipelineRunFacts) checkFinalTasksDone() bool {
336314
return facts.checkTasksDone(facts.FinalTasksGraph)
337315
}
338316

317+
// getPipelineTasksCount returns the count of successful tasks, failed tasks, cancelled tasks, skipped task, and incomplete tasks
318+
func (facts *PipelineRunFacts) getPipelineTasksCount() (int, int, int, int, int) {
319+
s, f, c, sk, i := 0, 0, 0, 0, 0
320+
for _, t := range facts.State {
321+
switch {
322+
// increment success counter since the task is successful
323+
case t.IsSuccessful():
324+
s++
325+
// increment failure counter since the task has failed
326+
case t.IsFailure():
327+
f++
328+
// increment cancelled counter since the task is cancelled
329+
case t.IsCancelled():
330+
c++
331+
// increment skip counter since the task is skipped
332+
case t.Skip(facts):
333+
sk++
334+
// increment incomplete counter since the task is pending and not executed yet
335+
default:
336+
i++
337+
}
338+
}
339+
return s, f, c, sk, i
340+
}
341+
339342
// check if a specified pipelineTask is defined under tasks(DAG) section
340343
func (facts *PipelineRunFacts) isDAGTask(pipelineTaskName string) bool {
341344
if _, ok := facts.TasksGraph.Nodes[pipelineTaskName]; ok {

0 commit comments

Comments
 (0)