fix(hwlab): allow targeted active run cleanup
This commit is contained in:
@@ -150,6 +150,7 @@ interface NodeRuntimeCleanupOptions {
|
|||||||
sourceCommit?: string;
|
sourceCommit?: string;
|
||||||
pipelineRun?: string;
|
pipelineRun?: string;
|
||||||
targetPipelineRun?: string;
|
targetPipelineRun?: string;
|
||||||
|
includeActive: boolean;
|
||||||
dryRun: boolean;
|
dryRun: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1963,6 +1964,7 @@ function parseNodeRuntimeCleanupOptions(scoped: ReturnType<typeof parseNodeScope
|
|||||||
sourceCommit,
|
sourceCommit,
|
||||||
pipelineRun,
|
pipelineRun,
|
||||||
targetPipelineRun: pipelineRun ?? (sourceCommit === undefined ? undefined : nodeRuntimePipelineRunName(scoped.spec, sourceCommit)),
|
targetPipelineRun: pipelineRun ?? (sourceCommit === undefined ? undefined : nodeRuntimePipelineRunName(scoped.spec, sourceCommit)),
|
||||||
|
includeActive: scoped.originalArgs.includes("--include-active"),
|
||||||
dryRun: scoped.dryRun || !scoped.confirm,
|
dryRun: scoped.dryRun || !scoped.confirm,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -1996,6 +1998,7 @@ function nodeRuntimeCleanupRuns(scoped: ReturnType<typeof parseNodeScopedDelegat
|
|||||||
limit: options.limit,
|
limit: options.limit,
|
||||||
sourceCommit: options.sourceCommit,
|
sourceCommit: options.sourceCommit,
|
||||||
pipelineRun: options.pipelineRun,
|
pipelineRun: options.pipelineRun,
|
||||||
|
includeActive: options.includeActive,
|
||||||
candidates,
|
candidates,
|
||||||
candidateCount: candidates.length,
|
candidateCount: candidates.length,
|
||||||
selectedPipelineRuns,
|
selectedPipelineRuns,
|
||||||
@@ -2010,6 +2013,7 @@ function nodeRuntimeCleanupRuns(scoped: ReturnType<typeof parseNodeScopedDelegat
|
|||||||
`--limit ${options.limit}`,
|
`--limit ${options.limit}`,
|
||||||
options.pipelineRun === undefined ? "" : `--pipeline-run ${options.pipelineRun}`,
|
options.pipelineRun === undefined ? "" : `--pipeline-run ${options.pipelineRun}`,
|
||||||
options.sourceCommit === undefined ? "" : `--source-commit ${options.sourceCommit}`,
|
options.sourceCommit === undefined ? "" : `--source-commit ${options.sourceCommit}`,
|
||||||
|
options.includeActive ? "--include-active" : "",
|
||||||
"--confirm",
|
"--confirm",
|
||||||
"--wait",
|
"--wait",
|
||||||
].filter(Boolean).join(" "),
|
].filter(Boolean).join(" "),
|
||||||
@@ -2029,6 +2033,7 @@ function nodeRuntimeCleanupRuns(scoped: ReturnType<typeof parseNodeScopedDelegat
|
|||||||
limit: options.limit,
|
limit: options.limit,
|
||||||
sourceCommit: options.sourceCommit,
|
sourceCommit: options.sourceCommit,
|
||||||
pipelineRun: options.pipelineRun,
|
pipelineRun: options.pipelineRun,
|
||||||
|
includeActive: options.includeActive,
|
||||||
deletedPipelineRuns: selectedPipelineRuns,
|
deletedPipelineRuns: selectedPipelineRuns,
|
||||||
deletedPipelineRunCount: selectedPipelineRuns.length,
|
deletedPipelineRunCount: selectedPipelineRuns.length,
|
||||||
ownedResourcesBefore: ownedResources,
|
ownedResourcesBefore: ownedResources,
|
||||||
@@ -2047,6 +2052,26 @@ function nodeRuntimeCleanupRuns(scoped: ReturnType<typeof parseNodeScopedDelegat
|
|||||||
}
|
}
|
||||||
|
|
||||||
function listNodeRuntimeCleanupPipelineRuns(spec: HwlabRuntimeLaneSpec, options: NodeRuntimeCleanupOptions): NodeRuntimeCleanupPipelineRunRow[] {
|
function listNodeRuntimeCleanupPipelineRuns(spec: HwlabRuntimeLaneSpec, options: NodeRuntimeCleanupOptions): NodeRuntimeCleanupPipelineRunRow[] {
|
||||||
|
if (options.targetPipelineRun !== undefined) {
|
||||||
|
const target = getNodeRuntimeCleanupPipelineRun(spec, options.targetPipelineRun);
|
||||||
|
if (target === null) {
|
||||||
|
return [{
|
||||||
|
name: options.targetPipelineRun,
|
||||||
|
createdAt: null,
|
||||||
|
ageMinutes: null,
|
||||||
|
status: null,
|
||||||
|
reason: "target-pipelinerun-not-found",
|
||||||
|
selected: false,
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
if (target.status !== "True" && target.status !== "False" && !options.includeActive) {
|
||||||
|
return [{ ...target, selected: false, selectedReason: "target-pipelinerun-not-terminal" }];
|
||||||
|
}
|
||||||
|
if ((target.status === "True" || target.status === "False") && (target.ageMinutes === null || target.ageMinutes < options.minAgeMinutes)) {
|
||||||
|
return [{ ...target, selected: false, selectedReason: target.ageMinutes === null ? "missing-creation-timestamp" : "below-min-age" }];
|
||||||
|
}
|
||||||
|
return [target];
|
||||||
|
}
|
||||||
const result = runNodeK3sArgs(spec, [
|
const result = runNodeK3sArgs(spec, [
|
||||||
"kubectl",
|
"kubectl",
|
||||||
"-n",
|
"-n",
|
||||||
@@ -2058,26 +2083,6 @@ function listNodeRuntimeCleanupPipelineRuns(spec: HwlabRuntimeLaneSpec, options:
|
|||||||
], 60);
|
], 60);
|
||||||
if (!isCommandSuccess(result)) throw new Error(`failed to list ${HWLAB_CI_NAMESPACE} PipelineRuns: ${result.stderr.trim().slice(0, 1000)}`);
|
if (!isCommandSuccess(result)) throw new Error(`failed to list ${HWLAB_CI_NAMESPACE} PipelineRuns: ${result.stderr.trim().slice(0, 1000)}`);
|
||||||
const rows = nodeRuntimeCleanupPipelineRunRowsFromText(result.stdout);
|
const rows = nodeRuntimeCleanupPipelineRunRowsFromText(result.stdout);
|
||||||
if (options.targetPipelineRun !== undefined) {
|
|
||||||
const target = rows.find((item) => item.name === options.targetPipelineRun);
|
|
||||||
if (target === undefined) {
|
|
||||||
return [{
|
|
||||||
name: options.targetPipelineRun,
|
|
||||||
createdAt: null,
|
|
||||||
ageMinutes: null,
|
|
||||||
status: null,
|
|
||||||
reason: "target-pipelinerun-not-found",
|
|
||||||
selected: false,
|
|
||||||
}];
|
|
||||||
}
|
|
||||||
if (target.status !== "True" && target.status !== "False") {
|
|
||||||
return [{ ...target, selected: false, selectedReason: "target-pipelinerun-not-terminal" }];
|
|
||||||
}
|
|
||||||
if (target.ageMinutes === null || target.ageMinutes < options.minAgeMinutes) {
|
|
||||||
return [{ ...target, selected: false, selectedReason: target.ageMinutes === null ? "missing-creation-timestamp" : "below-min-age" }];
|
|
||||||
}
|
|
||||||
return [target];
|
|
||||||
}
|
|
||||||
const prefix = `${spec.pipelineRunPrefix}-`;
|
const prefix = `${spec.pipelineRunPrefix}-`;
|
||||||
const terminalRuns = rows
|
const terminalRuns = rows
|
||||||
.filter((item) => item.name.startsWith(prefix))
|
.filter((item) => item.name.startsWith(prefix))
|
||||||
@@ -2092,6 +2097,21 @@ function listNodeRuntimeCleanupPipelineRuns(spec: HwlabRuntimeLaneSpec, options:
|
|||||||
.slice(0, options.limit);
|
.slice(0, options.limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getNodeRuntimeCleanupPipelineRun(spec: HwlabRuntimeLaneSpec, pipelineRun: string): NodeRuntimeCleanupPipelineRunRow | null {
|
||||||
|
const result = runNodeK3sArgs(spec, [
|
||||||
|
"kubectl",
|
||||||
|
"-n",
|
||||||
|
HWLAB_CI_NAMESPACE,
|
||||||
|
"get",
|
||||||
|
"pipelinerun",
|
||||||
|
pipelineRun,
|
||||||
|
"-o",
|
||||||
|
'jsonpath={.metadata.name}{"\\t"}{.metadata.creationTimestamp}{"\\t"}{.status.conditions[0].status}{"\\t"}{.status.conditions[0].reason}{"\\n"}',
|
||||||
|
], 60);
|
||||||
|
if (result.exitCode !== 0) return null;
|
||||||
|
return nodeRuntimeCleanupPipelineRunRowsFromText(result.stdout)[0] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
function nodeRuntimeCleanupPipelineRunRowsFromText(text: string): NodeRuntimeCleanupPipelineRunRow[] {
|
function nodeRuntimeCleanupPipelineRunRowsFromText(text: string): NodeRuntimeCleanupPipelineRunRow[] {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
return text.split(/\r?\n/u).map((line) => {
|
return text.split(/\r?\n/u).map((line) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user