Skip to content

Commit

Permalink
Add YAML mode for Create TaskRun
Browse files Browse the repository at this point in the history
Similar to Create PipelineRun, add a button to the Create TaskRun
page allowing the user to switch to YAML mode for more advanced
use cases not currently supported by the form-based UI.

Also add the 'Edit and run' action on the TaskRuns page allowing
users to create a new TaskRun based on an existing one.
  • Loading branch information
AlanGreene authored and tekton-robot committed Feb 20, 2023
1 parent c52e8d5 commit 1e10ff7
Show file tree
Hide file tree
Showing 20 changed files with 535 additions and 53 deletions.
7 changes: 2 additions & 5 deletions src/api/pipelineRuns.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getQueryParams,
getTektonAPI,
getTektonPipelinesAPIVersion,
removeSystemLabels,
useCollection,
useResource
} from './utils';
Expand Down Expand Up @@ -197,11 +198,7 @@ export function generateNewPipelineRunPayload({ pipelineRun, rerun }) {
payload.metadata.labels['dashboard.tekton.dev/rerunOf'] = name;
}

Object.keys(payload.metadata.labels).forEach(label => {
if (label.startsWith('tekton.dev/')) {
delete payload.metadata.labels[label];
}
});
removeSystemLabels(payload);

/*
This is used by Tekton Pipelines as part of the conversion between v1beta1
Expand Down
90 changes: 74 additions & 16 deletions src/api/taskRuns.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
getQueryParams,
getTektonAPI,
getTektonPipelinesAPIVersion,
removeSystemAnnotations,
removeSystemLabels,
useCollection,
useResource
} from './utils';
Expand Down Expand Up @@ -76,73 +78,129 @@ export function cancelTaskRun({ name, namespace }) {
return patch(uri, payload);
}

export function createTaskRun({
export function getTaskRunPayload({
kind,
labels,
namespace,
nodeSelector,
params,
serviceAccount,
taskName,
taskRunName = `${taskName}-run-${Date.now()}`,
taskRunName = `${taskName ? `${taskName}-run` : 'run'}-${Date.now()}`,
timeout
}) {
const payload = {
apiVersion: `tekton.dev/${getTektonPipelinesAPIVersion()}`,
kind: 'TaskRun',
metadata: {
name: taskRunName,
namespace,
labels
namespace
},
spec: {
params: [],
taskRef: {
name: taskName,
kind: kind || 'Task'
}
}
};
if (nodeSelector) {
payload.spec.podTemplate = { nodeSelector };
if (labels) {
payload.metadata.labels = labels;
}
if (params) {
payload.spec.params = Object.keys(params).map(name => ({
name,
value: params[name]
}));
}
if (nodeSelector) {
payload.spec.podTemplate = { nodeSelector };
}
if (serviceAccount) {
payload.spec.serviceAccountName = serviceAccount;
}
if (timeout) {
payload.spec.timeout = timeout;
}

return payload;
}

export function createTaskRun({
kind,
labels,
namespace,
nodeSelector,
params,
serviceAccount,
taskName,
taskRunName = `${taskName}-run-${Date.now()}`,
timeout
}) {
const payload = getTaskRunPayload({
kind,
labels,
namespace,
nodeSelector,
params,
serviceAccount,
taskName,
taskRunName,
timeout
});
const uri = getTektonAPI('taskruns', { namespace });
return post(uri, payload).then(({ body }) => body);
}

export function rerunTaskRun(taskRun) {
const { annotations, labels, name, namespace } = taskRun.metadata;
export function createTaskRunRaw({ namespace, payload }) {
const uri = getTektonAPI('taskruns', { namespace });
return post(uri, payload).then(({ body }) => body);
}

export function generateNewTaskRunPayload({ taskRun, rerun }) {
const { annotations, labels, name, namespace, generateName } =
taskRun.metadata;

const payload = deepClone(taskRun);
payload.apiVersion =
payload.apiVersion || `tekton.dev/${getTektonPipelinesAPIVersion()}`;
payload.kind = payload.kind || 'TaskRun';

function getGenerateName() {
if (rerun) {
return getGenerateNamePrefixForRerun(name);
}

return generateName || `${name}-`;
}

payload.metadata = {
annotations,
generateName: getGenerateNamePrefixForRerun(name),
labels: {
...labels,
'dashboard.tekton.dev/rerunOf': name
},
annotations: annotations || {},
generateName: getGenerateName(),
labels: labels || {},
namespace
};
if (rerun) {
payload.metadata.labels['dashboard.tekton.dev/rerunOf'] = name;
}

delete payload.metadata.labels['tekton.dev/task'];
removeSystemAnnotations(payload);
removeSystemLabels(payload);

Object.keys(payload.metadata).forEach(
i => payload.metadata[i] === undefined && delete payload.metadata[i]
);

delete payload.status;

delete payload.spec?.status;
return { namespace, payload };
}

export function rerunTaskRun(taskRun) {
const { namespace, payload } = generateNewTaskRunPayload({
taskRun,
rerun: true
});

const uri = getTektonAPI('taskruns', { namespace });
return post(uri, payload).then(({ body }) => body);
Expand Down
Loading

0 comments on commit 1e10ff7

Please sign in to comment.