diff --git a/src/components/utils/__tests__/pipeline-utils.spec.ts b/src/components/utils/__tests__/pipeline-utils.spec.ts index 0d5a47e37..7d0cfdc4c 100644 --- a/src/components/utils/__tests__/pipeline-utils.spec.ts +++ b/src/components/utils/__tests__/pipeline-utils.spec.ts @@ -21,7 +21,7 @@ import { taskRunWithSBOMResult, taskRunWithSBOMResultExternalLink, } from '../../../test-data/taskrun-test-data'; -import { ComputedStatus, ContainerStatus } from '../../../types'; +import { ComputedStatus, ContainerStatus, TaskRunKind } from '../../../types'; import { LatestPipelineRunStatus, appendPipelineRunStatus, @@ -380,6 +380,163 @@ describe('pipeline-utils ', () => { ).toHaveLength(2); }); + it('should pick the taskrun of the current pipelineRun when a stale taskrun with the same task label is present', () => { + const { pipeline, pipelineRuns } = + pipelineTestData[PipelineExampleNames.SIMPLE_PIPELINE]; + const pipelineRun = _.cloneDeep(pipelineRuns[DataState.SUCCESS]); + pipelineRun.metadata.uid = 'current-plr-uid'; + // Tekton v1 no longer populates status.taskRuns (only childReferences), so + // the task status must be derived from the TaskRun list, as in the console + delete pipelineRun.status.taskRuns; + const pipelineTaskName = pipeline.spec.tasks[0].name; + + const staleTaskRun = { + apiVersion: 'tekton.dev/v1', + kind: 'TaskRun', + metadata: { + name: `${pipelineRun.metadata.name}-${pipelineTaskName}`, + uid: 'stale-tr-uid', + labels: { + [TektonResourceLabel.pipelinerun]: pipelineRun.metadata.name, + [TektonResourceLabel.pipelineRunUid]: 'stale-plr-uid', + [TektonResourceLabel.pipelineTask]: pipelineTaskName, + }, + }, + spec: {}, + status: { + conditions: [ + { + type: 'Succeeded', + status: 'False', + reason: ComputedStatus.Failed, + }, + ], + steps: [{ terminated: { exitCode: 1, name: 'step' } }], + }, + } as unknown as TaskRunKind; + + const currentTaskRun = { + apiVersion: 'tekton.dev/v1', + kind: 'TaskRun', + metadata: { + name: `${pipelineRun.metadata.name}-${pipelineTaskName}`, + uid: 'current-tr-uid', + labels: { + [TektonResourceLabel.pipelinerun]: pipelineRun.metadata.name, + [TektonResourceLabel.pipelineRunUid]: 'current-plr-uid', + [TektonResourceLabel.pipelineTask]: pipelineTaskName, + }, + }, + spec: {}, + status: { + conditions: [ + { + type: 'Succeeded', + status: 'True', + reason: ComputedStatus.Succeeded, + }, + ], + steps: [{ terminated: { exitCode: 0, name: 'step' } }], + }, + } as unknown as TaskRunKind; + + // the stale record comes first: it used to win the `_.find` lookup + const taskList = appendPipelineRunStatus(pipeline, pipelineRun, [ + staleTaskRun, + currentTaskRun, + ]); + const firstTask = taskList.find((t) => t.name === pipelineTaskName); + + expect(firstTask.status.reason).toBe(ComputedStatus.Succeeded); + expect(firstTask.status.steps[0].terminated.exitCode).toBe(0); + }); + + it('should fall back to the first taskrun matching the task label when the pipelineRun uid is not labelled on the taskrun', () => { + const { pipeline, pipelineRuns } = + pipelineTestData[PipelineExampleNames.SIMPLE_PIPELINE]; + const pipelineRun = _.cloneDeep(pipelineRuns[DataState.SUCCESS]); + pipelineRun.metadata.uid = 'current-plr-uid'; + delete pipelineRun.status.taskRuns; + const pipelineTaskName = pipeline.spec.tasks[0].name; + + const taskRunsWithoutUidLabel = [ + { + apiVersion: 'tekton.dev/v1', + kind: 'TaskRun', + metadata: { + name: `${pipelineRun.metadata.name}-${pipelineTaskName}`, + uid: 'legacy-tr-uid', + labels: { + [TektonResourceLabel.pipelineTask]: pipelineTaskName, + }, + }, + spec: {}, + status: { + conditions: [ + { + type: 'Succeeded', + status: 'True', + reason: ComputedStatus.Succeeded, + }, + ], + }, + } as unknown as TaskRunKind, + ]; + + const taskList = appendPipelineRunStatus( + pipeline, + pipelineRun, + taskRunsWithoutUidLabel, + ); + const firstTask = taskList.find((t) => t.name === pipelineTaskName); + + expect(firstTask.status.reason).toBe(ComputedStatus.Succeeded); + }); + + it('should keep the taskrun status when the taskrun is labelled with a different pipelineRun uid (multi-cluster hub/spoke)', () => { + const { pipeline, pipelineRuns } = + pipelineTestData[PipelineExampleNames.SIMPLE_PIPELINE]; + const pipelineRun = _.cloneDeep(pipelineRuns[DataState.SUCCESS]); + // the PipelineRun is the hub copy + pipelineRun.metadata.uid = 'hub-plr-uid'; + delete pipelineRun.status.taskRuns; + const pipelineTaskName = pipeline.spec.tasks[0].name; + + // the TaskRun comes from the spoke through the multi-cluster proxy, so its + // `tekton.dev/pipelineRunUID` label is the spoke PipelineRun UID + const spokeTaskRun = { + apiVersion: 'tekton.dev/v1', + kind: 'TaskRun', + metadata: { + name: `${pipelineRun.metadata.name}-${pipelineTaskName}`, + uid: 'spoke-tr-uid', + labels: { + [TektonResourceLabel.pipelinerun]: pipelineRun.metadata.name, + [TektonResourceLabel.pipelineRunUid]: 'spoke-plr-uid', + [TektonResourceLabel.pipelineTask]: pipelineTaskName, + }, + }, + spec: {}, + status: { + conditions: [ + { + type: 'Succeeded', + status: 'True', + reason: ComputedStatus.Succeeded, + }, + ], + }, + } as unknown as TaskRunKind; + + const taskList = appendPipelineRunStatus(pipeline, pipelineRun, [ + spokeTaskRun, + ]); + const firstTask = taskList.find((t) => t.name === pipelineTaskName); + + // returning no TaskRun here would leave the task without its real status + expect(firstTask.status.reason).toBe(ComputedStatus.Succeeded); + }); + it('should append pipelineRun pending status for all the tasks if taskruns are not present and pipelinerun status is PipelineRunPending', () => { const { pipeline, pipelineRuns } = pipelineTestData[PipelineExampleNames.SIMPLE_PIPELINE]; diff --git a/src/components/utils/pipeline-utils.ts b/src/components/utils/pipeline-utils.ts index bd541f784..90535b375 100644 --- a/src/components/utils/pipeline-utils.ts +++ b/src/components/utils/pipeline-utils.ts @@ -127,6 +127,51 @@ export const PipelineResourceListFilterLabels = { export const INSTANCE_LABEL = 'app.kubernetes.io/instance'; +/** + * Returns the TaskRun that belongs to the given PipelineRun. + * + * Several TaskRuns can carry the same `tekton.dev/pipelineTask` label: Tekton + * Results keeps the records of previous PipelineRuns that used the same name, + * and the merge of the k8s and Results lists only de-duplicates by UID. When + * the PipelineRun UID is known, the TaskRun created by that very PipelineRun + * is preferred, so that the status of a stale record is never displayed. + * + * The fallback to the first match is intentional and must stay: the UID label + * of the TaskRun is not always equal to the UID of the PipelineRun being shown. + * On a multi-cluster setup the PipelineRun exists on the hub and on the spoke + * with different UIDs (see the `uniqBy` by name in `useTaskRuns`), and TaskRuns + * created by older Tekton versions carry no `tekton.dev/pipelineRunUID` label at + * all. Dropping the fallback would leave those tasks without any status. + * + * @param taskRuns the TaskRuns to pick from + * @param pipelineTaskName the `tekton.dev/pipelineTask` label value to match + * @param pipelineRunUid the UID of the PipelineRun being displayed, if known + */ +export const getTaskRunForPipelineTask = ( + taskRuns: TaskRunKind[], + pipelineTaskName: string, + pipelineRunUid?: string, +): TaskRunKind | undefined => { + const candidates = (taskRuns || []).filter( + (tr) => + tr.metadata?.labels?.[TektonResourceLabel.pipelineTask] === + pipelineTaskName, + ); + if (pipelineRunUid) { + const currentTaskRun = candidates.find( + (tr) => + tr.metadata?.labels?.[TektonResourceLabel.pipelineRunUid] === + pipelineRunUid, + ); + if (currentTaskRun) { + return currentTaskRun; + } + // No TaskRun is labelled with this UID. Keep the previous behaviour + // instead of returning nothing, see the note above. + } + return candidates[0]; +}; + /** * Appends the pipeline run status to each tasks in the pipeline. * @param pipeline @@ -167,10 +212,10 @@ export const appendPipelineRunStatus = ( return { ...task, status: { reason: ComputedStatus.Pending } }; } - const taskRun = _.find( + const taskRun = getTaskRunForPipelineTask( taskRuns, - (tr) => - tr.metadata.labels[TektonResourceLabel.pipelineTask] === task.name, + task.name, + pipelineRun.metadata?.uid, ); const childPipelineRun = _.find(