fix: improve hwlab v02 status targeting
This commit is contained in:
+90
-17
@@ -91,6 +91,16 @@ interface G14ControlPlaneOptions {
|
||||
timeoutSeconds: number;
|
||||
minAgeMinutes: number;
|
||||
limit: number;
|
||||
sourceCommit?: string;
|
||||
pipelineRun?: string;
|
||||
}
|
||||
|
||||
type V02StatusTargetMode = "latest-source-head" | "source-commit" | "pipeline-run";
|
||||
|
||||
interface V02ControlPlaneStatusTarget {
|
||||
sourceCommit?: string | null;
|
||||
pipelineRun?: string | null;
|
||||
mode?: V02StatusTargetMode;
|
||||
}
|
||||
|
||||
interface G14ToolsImageOptions {
|
||||
@@ -193,6 +203,16 @@ function optionValue(args: string[], name: string): string | undefined {
|
||||
return value;
|
||||
}
|
||||
|
||||
function validateFullShaOption(value: string, name: string): string {
|
||||
if (!/^[0-9a-f]{40}$/iu.test(value)) throw new Error(`${name} must be a full 40-character git SHA`);
|
||||
return value.toLowerCase();
|
||||
}
|
||||
|
||||
function validateV02PipelineRunOption(value: string): string {
|
||||
if (!/^hwlab-v02-ci-poll-[0-9a-f]{7,40}$/iu.test(value)) throw new Error("--pipeline-run must be a hwlab-v02-ci-poll-<sha> PipelineRun name");
|
||||
return value.toLowerCase();
|
||||
}
|
||||
|
||||
function parseRecordRolloutOptions(args: string[]): G14RecordRolloutOptions {
|
||||
const prRaw = optionValue(args, "--pr") ?? optionValue(args, "--number");
|
||||
const prNumber = Number(prRaw);
|
||||
@@ -236,6 +256,12 @@ function parseControlPlaneOptions(args: string[]): G14ControlPlaneOptions {
|
||||
const allowLiveDbRead = args.includes("--allow-live-db-read");
|
||||
if (allowLiveDbRead && confirm) throw new Error("control-plane runtime-migration accepts --allow-live-db-read only with dry-run/source-check mode, not --confirm");
|
||||
if (allowLiveDbRead && actionRaw !== "runtime-migration") throw new Error("--allow-live-db-read is only valid for control-plane runtime-migration");
|
||||
const sourceCommitRaw = optionValue(args, "--source-commit");
|
||||
const pipelineRunRaw = optionValue(args, "--pipeline-run");
|
||||
if ((sourceCommitRaw !== undefined || pipelineRunRaw !== undefined) && actionRaw !== "status") {
|
||||
throw new Error("--source-commit and --pipeline-run are only valid for control-plane status");
|
||||
}
|
||||
if (sourceCommitRaw !== undefined && pipelineRunRaw !== undefined) throw new Error("control-plane status accepts only one of --source-commit or --pipeline-run");
|
||||
return {
|
||||
action: actionRaw,
|
||||
lane,
|
||||
@@ -246,6 +272,8 @@ function parseControlPlaneOptions(args: string[]): G14ControlPlaneOptions {
|
||||
timeoutSeconds: positiveIntegerOption(args, "--timeout-seconds", 120, 600),
|
||||
minAgeMinutes: positiveIntegerOption(args, "--min-age-minutes", 60, 10080),
|
||||
limit: positiveIntegerOption(args, "--limit", 20, 200),
|
||||
sourceCommit: sourceCommitRaw === undefined ? undefined : validateFullShaOption(sourceCommitRaw, "--source-commit"),
|
||||
pipelineRun: pipelineRunRaw === undefined ? undefined : validateV02PipelineRunOption(pipelineRunRaw),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -717,15 +745,26 @@ function shellSectionOk(section: ShellSection | undefined): boolean {
|
||||
return section?.exitCode === 0;
|
||||
}
|
||||
|
||||
function v02ControlPlaneStatusBundle(sourceCommit: string | null | undefined): CommandJsonResult {
|
||||
const sourceCommitLine = sourceCommit === undefined
|
||||
? `source_commit=$(git --git-dir=${shellQuote(V02_CICD_REPO)} rev-parse refs/remotes/origin/v0.2 2>/dev/null || true)`
|
||||
: `source_commit=${shellQuote(sourceCommit ?? "")}`;
|
||||
function v02ControlPlaneStatusBundle(target: V02ControlPlaneStatusTarget = {}): CommandJsonResult {
|
||||
const targetMode: V02StatusTargetMode = target.mode
|
||||
?? (target.pipelineRun !== undefined && target.pipelineRun !== null ? "pipeline-run" : target.sourceCommit !== undefined ? "source-commit" : "latest-source-head");
|
||||
const targetInit = target.pipelineRun !== undefined && target.pipelineRun !== null
|
||||
? [
|
||||
`pipeline_run=${shellQuote(target.pipelineRun)}`,
|
||||
`source_commit=$(kubectl get pipelinerun -n ${shellQuote(CI_NAMESPACE)} "$pipeline_run" -o 'jsonpath={.metadata.labels.hwlab\\.pikastech\\.local/source-commit}' 2>/dev/null || true)`,
|
||||
`if [ -z "$source_commit" ]; then source_commit=$(printf '%s' "$pipeline_run" | cut -d- -f5-); fi`,
|
||||
].join("\n")
|
||||
: [
|
||||
target.sourceCommit === undefined
|
||||
? `source_commit=$(git --git-dir=${shellQuote(V02_CICD_REPO)} rev-parse refs/remotes/origin/v0.2 2>/dev/null || true)`
|
||||
: `source_commit=${shellQuote(target.sourceCommit ?? "")}`,
|
||||
"pipeline_run=",
|
||||
].join("\n");
|
||||
const script = [
|
||||
"set +e",
|
||||
sourceCommitLine,
|
||||
"pipeline_run=",
|
||||
`if [ -n "$source_commit" ]; then pipeline_run="${V02_PIPELINERUN_PREFIX}-$(printf '%s' "$source_commit" | cut -c1-12)"; fi`,
|
||||
targetInit,
|
||||
`target_mode=${shellQuote(targetMode)}`,
|
||||
`if [ -z "$pipeline_run" ] && [ -n "$source_commit" ]; then pipeline_run="${V02_PIPELINERUN_PREFIX}-$(printf '%s' "$source_commit" | cut -c1-12)"; fi`,
|
||||
"section() {",
|
||||
" name=\"$1\"",
|
||||
" shift",
|
||||
@@ -734,7 +773,9 @@ function v02ControlPlaneStatusBundle(sourceCommit: string | null | undefined): C
|
||||
" code=$?",
|
||||
" printf '\\n__UNIDESK_SECTION_END__ %s exit=%s\\n' \"$name\" \"$code\"",
|
||||
"}",
|
||||
"section statusTarget printf 'mode\\t%s\\nsourceCommit\\t%s\\npipelineRun\\t%s\\n' \"$target_mode\" \"$source_commit\" \"$pipeline_run\"",
|
||||
"section sourceCommit printf '%s\\n' \"$source_commit\"",
|
||||
"section pipelineRunName printf '%s\\n' \"$pipeline_run\"",
|
||||
`section sourceHeads sh -c ${shellQuote(v02SourceHeadsProbeScript())}`,
|
||||
"section queryNow date -u +%Y-%m-%dT%H:%M:%SZ",
|
||||
`section controlPlane kubectl get pipeline,role,rolebinding,serviceaccount -n ${shellQuote(CI_NAMESPACE)} -l hwlab.pikastech.local/gitops-target=v02 -o name`,
|
||||
@@ -1805,11 +1846,15 @@ function deleteV02PipelineRun(pipelineRun: string): CommandJsonResult {
|
||||
return g14K3s(["kubectl", "delete", "pipelinerun", "-n", CI_NAMESPACE, pipelineRun, "--ignore-not-found=true"], 60_000);
|
||||
}
|
||||
|
||||
function v02ControlPlaneStatus(sourceCommitInput?: string | null): Record<string, unknown> {
|
||||
const bundle = v02ControlPlaneStatusBundle(sourceCommitInput);
|
||||
function v02ControlPlaneStatus(target: V02ControlPlaneStatusTarget = {}): Record<string, unknown> {
|
||||
const targetMode: V02StatusTargetMode = target.mode
|
||||
?? (target.pipelineRun !== undefined && target.pipelineRun !== null ? "pipeline-run" : target.sourceCommit !== undefined ? "source-commit" : "latest-source-head");
|
||||
const strictHeadAlignment = targetMode === "latest-source-head";
|
||||
const bundle = v02ControlPlaneStatusBundle({ ...target, mode: targetMode });
|
||||
const sections = parseShellSections(statusText(bundle));
|
||||
const sourceCommit = stringOrNull(sections.sourceCommit?.stdout) ?? null;
|
||||
const pipelineRun = sourceCommit === null ? null : v02PipelineRunName(sourceCommit);
|
||||
const pipelineRun = stringOrNull(sections.pipelineRunName?.stdout) ?? (sourceCommit === null ? null : v02PipelineRunName(sourceCommit));
|
||||
const statusTargetFields = keyValueLinesFromText(sections.statusTarget?.stdout ?? "");
|
||||
const queryNowMs = timestampMs(sections.queryNow?.stdout) ?? Date.now();
|
||||
const sourceHeadsSection = sections.sourceHeads;
|
||||
const controlPlane = sections.controlPlane;
|
||||
@@ -1895,14 +1940,38 @@ function v02ControlPlaneStatus(sourceCommitInput?: string | null): Record<string
|
||||
webAssets,
|
||||
});
|
||||
const baseOk = sourceCommit !== null && isCommandSuccess(bundle) && shellSectionOk(controlPlane) && shellSectionOk(argo);
|
||||
const targetPipelineRunOk = strictHeadAlignment
|
||||
? true
|
||||
: pipelineRunInfo !== null && pipelineRunInfo.ok === true && pipelineRunInfo.exists !== false;
|
||||
const targetDegradedReason = !strictHeadAlignment && !targetPipelineRunOk ? "target-pipelinerun-not-found-or-unreadable" : undefined;
|
||||
const statusCommand = targetMode === "pipeline-run" && pipelineRun !== null
|
||||
? `hwlab g14 control-plane status --lane v02 --pipeline-run ${pipelineRun}`
|
||||
: targetMode === "source-commit" && sourceCommit !== null
|
||||
? `hwlab g14 control-plane status --lane v02 --source-commit ${sourceCommit}`
|
||||
: "hwlab g14 control-plane status --lane v02";
|
||||
const state = strictHeadAlignment ? commitAlignment.state : `target-${targetMode}`;
|
||||
const sourceCommitSource = targetMode === "pipeline-run"
|
||||
? "PipelineRun metadata label hwlab.pikastech.local/source-commit"
|
||||
: targetMode === "source-commit"
|
||||
? "explicit --source-commit"
|
||||
: "G14 CI/CD dedicated bare repo refs/remotes/origin/v0.2; workspace checkout is observable but isolated";
|
||||
return {
|
||||
ok: baseOk && commitAlignment.aligned !== false,
|
||||
command: "hwlab g14 control-plane status --lane v02",
|
||||
ok: baseOk && targetPipelineRunOk && (!strictHeadAlignment || commitAlignment.aligned !== false),
|
||||
command: statusCommand,
|
||||
lane: "v02",
|
||||
state: commitAlignment.state,
|
||||
degradedReason: commitAlignment.aligned === false ? commitAlignment.state : undefined,
|
||||
state,
|
||||
degradedReason: strictHeadAlignment && commitAlignment.aligned === false ? commitAlignment.state : targetDegradedReason,
|
||||
statusTarget: {
|
||||
mode: statusTargetFields.mode || targetMode,
|
||||
sourceCommit,
|
||||
pipelineRun,
|
||||
strictHeadAlignment,
|
||||
note: strictHeadAlignment
|
||||
? "default status validates the latest v0.2 source head alignment"
|
||||
: "targeted status inspects the requested PipelineRun/source commit without failing merely because origin/v0.2 advanced later",
|
||||
},
|
||||
sourceCommit,
|
||||
sourceCommitSource: "G14 CI/CD dedicated bare repo refs/remotes/origin/v0.2; workspace checkout is observable but isolated",
|
||||
sourceCommitSource,
|
||||
expected: {
|
||||
sourceRepo: V02_CICD_REPO,
|
||||
workspace: V02_WORKSPACE,
|
||||
@@ -1956,12 +2025,14 @@ function v02ControlPlaneStatus(sourceCommitInput?: string | null): Record<string
|
||||
function runV02ControlPlane(options: G14ControlPlaneOptions): Record<string, unknown> {
|
||||
if (options.action === "cleanup-runs") return runControlPlaneCleanup(options);
|
||||
if (options.action === "cleanup-released-pvs") return runControlPlaneReleasedPvCleanup(options);
|
||||
if (options.action === "status" && options.pipelineRun !== undefined) return v02ControlPlaneStatus({ pipelineRun: options.pipelineRun, mode: "pipeline-run" });
|
||||
if (options.action === "status" && options.sourceCommit !== undefined) return v02ControlPlaneStatus({ sourceCommit: options.sourceCommit, mode: "source-commit" });
|
||||
const sourceCommit = getV02Head();
|
||||
if (sourceCommit === null) {
|
||||
return { ok: false, command: `hwlab g14 control-plane ${options.action} --lane v02`, degradedReason: "v02-head-unresolved", sourceRepo: V02_CICD_REPO, workspace: V02_WORKSPACE };
|
||||
}
|
||||
if (options.action === "runtime-migration") return runV02RuntimeMigration(options, sourceCommit);
|
||||
if (options.action === "status") return v02ControlPlaneStatus(sourceCommit);
|
||||
if (options.action === "status") return v02ControlPlaneStatus({ sourceCommit, mode: "latest-source-head" });
|
||||
if (options.action === "apply") {
|
||||
const render = runV02RenderToTemp(sourceCommit);
|
||||
if (!isCommandSuccess(render)) {
|
||||
@@ -1984,7 +2055,7 @@ function runV02ControlPlane(options: G14ControlPlaneOptions): Record<string, unk
|
||||
render: commandData(render),
|
||||
apply: compactCommandResult(apply),
|
||||
cleanupObsoleteCronJobs: compactCommandResult(options.dryRun ? deleteV02ObsoleteCronJobs(true) : deleteV02ObsoleteCronJobs(false)),
|
||||
status: v02ControlPlaneStatus(sourceCommit),
|
||||
status: v02ControlPlaneStatus({ sourceCommit, mode: "latest-source-head" }),
|
||||
next: options.dryRun
|
||||
? { apply: "bun scripts/cli.ts hwlab g14 control-plane apply --lane v02 --confirm" }
|
||||
: { triggerCurrent: "bun scripts/cli.ts hwlab g14 control-plane trigger-current --lane v02 --confirm" },
|
||||
@@ -3588,6 +3659,8 @@ export function hwlabG14Help(): Record<string, unknown> {
|
||||
"bun scripts/cli.ts hwlab g14 monitor-prs --once --dry-run",
|
||||
"bun scripts/cli.ts hwlab g14 record-rollout --pr <number> [--source-commit sha]",
|
||||
"bun scripts/cli.ts hwlab g14 control-plane status --lane v02",
|
||||
"bun scripts/cli.ts hwlab g14 control-plane status --lane v02 --pipeline-run hwlab-v02-ci-poll-<short-sha>",
|
||||
"bun scripts/cli.ts hwlab g14 control-plane status --lane v02 --source-commit <full-sha>",
|
||||
"bun scripts/cli.ts hwlab g14 control-plane apply --lane v02 --dry-run",
|
||||
"bun scripts/cli.ts hwlab g14 control-plane apply --lane v02 --confirm",
|
||||
"bun scripts/cli.ts hwlab g14 control-plane trigger-current --lane v02 --confirm",
|
||||
|
||||
Reference in New Issue
Block a user