Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(frontend): Display loop UI for pipelines page #489

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 101 additions & 22 deletions frontend/src/lib/StaticGraphParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/

import * as dagre from 'dagre';
import { graphlib } from 'dagre';
import { color } from '../Css';
import { Constants } from './Constants';
import { parseTaskDisplayName } from './ParserUtils';
import { graphlib } from 'dagre';

export type nodeType = 'container' | 'resource' | 'dag' | 'unknown';

Expand Down Expand Up @@ -87,7 +87,13 @@ export function createGraph(workflow: any): dagre.graphlib.Graph {
return graph;
}

function buildTektonDag(graph: dagre.graphlib.Graph, template: any): void {
function buildTektonDag(
graph: dagre.graphlib.Graph,
template: any,
startloop: string = '',
endloop: string = '',
lastlooptask: string = '',
): void {
const pipeline = template;
const tasks = (pipeline['spec']['pipelineSpec']['tasks'] || []).concat(
pipeline['spec']['pipelineSpec']['finally'] || [],
Expand All @@ -97,7 +103,8 @@ function buildTektonDag(graph: dagre.graphlib.Graph, template: any): void {
(pipeline['spec']['pipelineSpec']['finally'] || []).map((element: any) => {
return element['name'];
}) || [];
// Collect the anyConditions from 'metadata.annotations.anyConditions'
let loopNumber = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loopNumber is always 1. It should be incremented somewhere

let loopTaskList: string[] = [];
let anyConditions = {};
if (
pipeline['metadata'] &&
Expand All @@ -114,9 +121,11 @@ function buildTektonDag(graph: dagre.graphlib.Graph, template: any): void {
// on task output being passed in as parameters
if (task['runAfter'])
task['runAfter'].forEach((depTask: any) => {
if (loopTaskList.includes(depTask)) {
depTask = depTask + '-end';
}
graph.setEdge(depTask, taskName);
});
// Adds dependencies for anySequencers from 'anyCondition' annotation
if (anyTasks.includes(task['name'])) {
for (const depTask of anyConditions[task['name']]) {
graph.setEdge(depTask, taskName);
Expand All @@ -128,8 +137,10 @@ function buildTektonDag(graph: dagre.graphlib.Graph, template: any): void {
const input = condition['input'];
if (input.substring(0, 8) === '$(tasks.' && input.substring(input.length - 1) === ')') {
const paramSplit = input.split('.');
const parentTask = paramSplit[1];

let parentTask = paramSplit[1];
if (loopTaskList.includes(parentTask)) {
parentTask = parentTask + '-end';
}
graph.setEdge(parentTask, taskName);
}
}
Expand All @@ -143,21 +154,36 @@ function buildTektonDag(graph: dagre.graphlib.Graph, template: any): void {
condParam['value'].substring(condParam['value'].length - 1) === ')'
) {
const paramSplit = condParam['value'].split('.');
const parentTask = paramSplit[1];

let parentTask = paramSplit[1];
if (loopTaskList.includes(parentTask)) {
parentTask = parentTask + '-end';
}
graph.setEdge(parentTask, taskName);
}
}
}

for (const param of task['params'] || []) {
if (
param['value'].substring(0, 8) === '$(tasks.' &&
param['value'].substring(param['value'].length - 1) === ')'
) {
const paramSplit = param['value'].split('.');
const parentTask = paramSplit[1];
graph.setEdge(parentTask, taskName);
if (typeof param['value'] === 'string') {
if (
param['value'].substring(0, 8) === '$(tasks.' &&
param['value'].substring(param['value'].length - 1) === ')'
) {
const paramSplit = param['value'].split('.');
const parentTask = paramSplit[1];
graph.setEdge(parentTask, taskName);
}
} else if (typeof param['value'] === 'object') {
for (const paramValue of param['value'] || []) {
if (
paramValue.substring(0, 8) === '$(tasks.' &&
paramValue.substring(param['value'].length - 1) === ')'
) {
const paramSplit = paramValue.split('.');
const parentTask = paramSplit[1];
graph.setEdge(parentTask, taskName);
}
}
}
}

Expand All @@ -172,13 +198,66 @@ function buildTektonDag(graph: dagre.graphlib.Graph, template: any): void {
? 'cornsilk'
: undefined;

graph.setNode(taskName, {
bgColor: bgColor,
height: Constants.NODE_HEIGHT,
info,
label: parseTaskDisplayName(task['taskSpec']) || label,
width: Constants.NODE_WIDTH,
});
if (task['taskSpec']) {
graph.setNode(taskName, {
bgColor: bgColor,
height: Constants.NODE_HEIGHT,
info,
label: parseTaskDisplayName(task['taskSpec']) || label,
width: Constants.NODE_WIDTH,
});
} else if (task['taskRef'] && task['taskRef']['kind'] === 'PipelineLoop') {
// handle the case of loop pipelines
graph.setNode(taskName, {
bgColor: bgColor,
height: Constants.NODE_HEIGHT,
info,
label: 'start-loop-' + loopNumber,
width: Constants.NODE_WIDTH,
});
const loopPipelineName = task['taskRef']['name'];
if (
pipeline['metadata'] &&
pipeline['metadata']['annotations'] &&
pipeline['metadata']['annotations']['tekton.dev/' + loopPipelineName]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any sample pipelines using this annotation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @drewbutlerbb4 , yes, I will modify sdk part test examples for it, but seems I have to suspend this PR due to this logic should be removed after custom task could be defined as taskSpec

) {
const loopPipeline = JSON.parse(
pipeline['metadata']['annotations']['tekton.dev/' + loopPipelineName],
);
const endLoopName = taskName + '-end';
loopTaskList.push(taskName);
graph.setNode(endLoopName, {
bgColor: bgColor,
height: Constants.NODE_HEIGHT,
info,
label: 'end-loop-' + loopNumber++,
width: Constants.NODE_WIDTH,
});
let lastLoopTask = '';
if (
pipeline['metadata'] &&
pipeline['metadata']['labels'] &&
pipeline['metadata']['labels']['last-loop-task']
) {
lastLoopTask = pipeline['metadata']['labels']['last-loop-task'];
}
buildTektonDag(graph, loopPipeline, taskName, endLoopName, lastLoopTask);
}
}
}
if (startloop && endloop) {
if (lastlooptask) {
graph.setEdge(lastlooptask, endloop);
}
for (const looptask of tasks) {
const loopTaskName = looptask['name'];
if (graph.inEdges(loopTaskName)?.length === 0) {
graph.setEdge(startloop, loopTaskName);
}
if (lastlooptask === '' && graph.outEdges(loopTaskName)?.length === 0) {
graph.setEdge(loopTaskName, endloop);
}
}
}
}

Expand Down