Merge pull request #838 from pikasTech/fix/hwlab-cleanup-target-active-20260624
fix(hwlab): allow targeted active run cleanup
This commit is contained in:
@@ -150,6 +150,7 @@ interface NodeRuntimeCleanupOptions {
|
||||
sourceCommit?: string;
|
||||
pipelineRun?: string;
|
||||
targetPipelineRun?: string;
|
||||
includeActive: boolean;
|
||||
dryRun: boolean;
|
||||
}
|
||||
|
||||
@@ -1963,6 +1964,7 @@ function parseNodeRuntimeCleanupOptions(scoped: ReturnType<typeof parseNodeScope
|
||||
sourceCommit,
|
||||
pipelineRun,
|
||||
targetPipelineRun: pipelineRun ?? (sourceCommit === undefined ? undefined : nodeRuntimePipelineRunName(scoped.spec, sourceCommit)),
|
||||
includeActive: scoped.originalArgs.includes("--include-active"),
|
||||
dryRun: scoped.dryRun || !scoped.confirm,
|
||||
};
|
||||
}
|
||||
@@ -1996,6 +1998,7 @@ function nodeRuntimeCleanupRuns(scoped: ReturnType<typeof parseNodeScopedDelegat
|
||||
limit: options.limit,
|
||||
sourceCommit: options.sourceCommit,
|
||||
pipelineRun: options.pipelineRun,
|
||||
includeActive: options.includeActive,
|
||||
candidates,
|
||||
candidateCount: candidates.length,
|
||||
selectedPipelineRuns,
|
||||
@@ -2010,6 +2013,7 @@ function nodeRuntimeCleanupRuns(scoped: ReturnType<typeof parseNodeScopedDelegat
|
||||
`--limit ${options.limit}`,
|
||||
options.pipelineRun === undefined ? "" : `--pipeline-run ${options.pipelineRun}`,
|
||||
options.sourceCommit === undefined ? "" : `--source-commit ${options.sourceCommit}`,
|
||||
options.includeActive ? "--include-active" : "",
|
||||
"--confirm",
|
||||
"--wait",
|
||||
].filter(Boolean).join(" "),
|
||||
@@ -2029,6 +2033,7 @@ function nodeRuntimeCleanupRuns(scoped: ReturnType<typeof parseNodeScopedDelegat
|
||||
limit: options.limit,
|
||||
sourceCommit: options.sourceCommit,
|
||||
pipelineRun: options.pipelineRun,
|
||||
includeActive: options.includeActive,
|
||||
deletedPipelineRuns: selectedPipelineRuns,
|
||||
deletedPipelineRunCount: selectedPipelineRuns.length,
|
||||
ownedResourcesBefore: ownedResources,
|
||||
@@ -2047,6 +2052,26 @@ function nodeRuntimeCleanupRuns(scoped: ReturnType<typeof parseNodeScopedDelegat
|
||||
}
|
||||
|
||||
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, [
|
||||
"kubectl",
|
||||
"-n",
|
||||
@@ -2058,26 +2083,6 @@ function listNodeRuntimeCleanupPipelineRuns(spec: HwlabRuntimeLaneSpec, options:
|
||||
], 60);
|
||||
if (!isCommandSuccess(result)) throw new Error(`failed to list ${HWLAB_CI_NAMESPACE} PipelineRuns: ${result.stderr.trim().slice(0, 1000)}`);
|
||||
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 terminalRuns = rows
|
||||
.filter((item) => item.name.startsWith(prefix))
|
||||
@@ -2092,6 +2097,21 @@ function listNodeRuntimeCleanupPipelineRuns(spec: HwlabRuntimeLaneSpec, options:
|
||||
.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[] {
|
||||
const now = Date.now();
|
||||
return text.split(/\r?\n/u).map((line) => {
|
||||
|
||||
Reference in New Issue
Block a user