205 lines
8.4 KiB
JavaScript
205 lines
8.4 KiB
JavaScript
#!/usr/bin/env node
|
|
// SPEC: PJ2026-01060703 CI/CD branch follower runtime GitOps verify.
|
|
// Responsibility: fail publish before Argo when rendered runtime GitOps violates UniDesk overlay gates.
|
|
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
import { createRequire } from "node:module";
|
|
import path from "node:path";
|
|
|
|
const requireFromScript = createRequire(import.meta.url);
|
|
const requireFromCwd = createRequire(path.join(process.cwd(), "package.json"));
|
|
const YAML = requireYaml();
|
|
const repoDir = process.cwd();
|
|
const overlay = readOverlay();
|
|
const runtimePath = requiredOverlayString("runtimePath");
|
|
const runtimeDir = path.resolve(repoDir, runtimePath);
|
|
const prometheusOperatorKinds = new Set(["ServiceMonitor", "PrometheusRule", "PodMonitor", "Probe"]);
|
|
|
|
if (!existsSync(runtimeDir)) {
|
|
fail("runtime-path-missing", { runtimePath });
|
|
}
|
|
|
|
const checks = [];
|
|
if (overlay?.observability?.prometheusOperator === false) {
|
|
checks.push("prometheus-operator-disabled");
|
|
const refs = findPrometheusOperatorResources();
|
|
if (refs.length > 0) fail("prometheus-operator-resource-present", { runtimePath, refs: refs.slice(0, 12), refCount: refs.length });
|
|
}
|
|
if (overlay?.codeAgentRuntime?.enabled && overlay.codeAgentRuntime.kafkaEventBridge?.enabled) {
|
|
checks.push("code-agent-runtime-kafka-event-bridge-enabled");
|
|
verifyTransactionalProjectionAuthority(overlay.codeAgentRuntime.kafkaEventBridge);
|
|
verifyCodeAgentRuntimeWorkloads();
|
|
}
|
|
|
|
console.error(JSON.stringify({ event: "unidesk-runtime-gitops-verify", ok: true, runtimePath, checks }));
|
|
|
|
function findPrometheusOperatorResources() {
|
|
const refs = [];
|
|
for (const file of listYamlFiles(runtimeDir)) {
|
|
const rel = path.relative(repoDir, file);
|
|
for (const doc of splitYamlDocuments(readFileSync(file, "utf8"))) {
|
|
refs.push(...prometheusOperatorResourceRefs(doc, rel));
|
|
}
|
|
}
|
|
return refs;
|
|
}
|
|
|
|
function verifyTransactionalProjectionAuthority(kafka) {
|
|
const expected = {
|
|
directPublish: false,
|
|
liveKafkaSse: false,
|
|
kafkaRefreshReplay: false,
|
|
transactionalProjector: true,
|
|
projectionOutboxRelay: true,
|
|
projectionRealtime: true,
|
|
};
|
|
const actual = Object.fromEntries(Object.keys(expected).map((name) => [name, kafka.features?.[name]]));
|
|
const invalid = Object.entries(expected).filter(([name, value]) => actual[name] !== value).map(([name]) => name);
|
|
if (invalid.length > 0) fail("kafka-event-bridge-authority-invalid", { invalid, expected, actual });
|
|
}
|
|
|
|
function verifyCodeAgentRuntimeWorkloads() {
|
|
const expectedByWorkload = {
|
|
"hwlab-cloud-api": {
|
|
HWLAB_KAFKA_ENABLED: "true",
|
|
HWLAB_KAFKA_AGENTRUN_EVENT_CONSUME_ENABLED: "true",
|
|
HWLAB_KAFKA_DIRECT_PUBLISH_ENABLED: "false",
|
|
HWLAB_WORKBENCH_LIVE_KAFKA_SSE_ENABLED: "false",
|
|
HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_ENABLED: "false",
|
|
HWLAB_KAFKA_TRANSACTIONAL_PROJECTOR_ENABLED: "true",
|
|
HWLAB_KAFKA_PROJECTION_OUTBOX_RELAY_ENABLED: "true",
|
|
HWLAB_WORKBENCH_PROJECTION_REALTIME_ENABLED: "true",
|
|
},
|
|
"hwlab-cloud-web": {
|
|
HWLAB_WORKBENCH_LIVE_KAFKA_SSE_ENABLED: "false",
|
|
HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_ENABLED: "false",
|
|
HWLAB_WORKBENCH_PROJECTION_REALTIME_ENABLED: "true",
|
|
},
|
|
};
|
|
const found = new Set();
|
|
for (const file of listYamlFiles(runtimeDir)) {
|
|
const rel = path.relative(repoDir, file);
|
|
for (const document of parseStructuredDocuments(readFileSync(file, "utf8"), file)) {
|
|
const items = isKubernetesList(document) ? document.items : [document];
|
|
for (const item of items) {
|
|
const workloadName = String(item?.metadata?.labels?.["app.kubernetes.io/name"] ?? item?.metadata?.name ?? "");
|
|
const expected = expectedByWorkload[workloadName];
|
|
if (!expected) continue;
|
|
const container = item?.spec?.template?.spec?.containers?.find((candidate) => candidate?.name === workloadName);
|
|
if (!container) fail("code-agent-runtime-container-missing", { file: rel, workload: workloadName, container: workloadName });
|
|
if (!Array.isArray(container.env)) fail("code-agent-runtime-env-missing", { file: rel, workload: workloadName, container: workloadName });
|
|
const actual = Object.fromEntries(container.env.filter((item) => typeof item?.name === "string").map((item) => [item.name, item.value]));
|
|
const invalid = Object.entries(expected)
|
|
.filter(([name, value]) => actual[name] !== value)
|
|
.map(([name, value]) => ({ name, expected: value, actual: actual[name] ?? null }));
|
|
if (invalid.length > 0) fail("code-agent-runtime-capability-env-invalid", { file: rel, workload: workloadName, container: workloadName, invalid });
|
|
found.add(workloadName);
|
|
}
|
|
}
|
|
}
|
|
const missing = Object.keys(expectedByWorkload).filter((workloadName) => !found.has(workloadName));
|
|
if (missing.length > 0) fail("code-agent-runtime-workload-missing", { runtimePath, missing });
|
|
}
|
|
|
|
function splitYamlDocuments(text) {
|
|
return text.split(/^---[ \t]*(?:#.*)?$/mu).map((doc) => doc.trim()).filter(Boolean);
|
|
}
|
|
|
|
function prometheusOperatorResourceRefs(text, file) {
|
|
const jsonRefs = prometheusOperatorJsonResourceRefs(text, file);
|
|
if (jsonRefs !== null) return jsonRefs;
|
|
const api = text.match(/^\s*apiVersion:\s*["']?(monitoring\.coreos\.com\/[^"'\s#]+)["']?\s*(?:#.*)?$/mu);
|
|
const kind = text.match(/^\s*kind:\s*["']?([^"'\s#]+)["']?\s*(?:#.*)?$/mu);
|
|
if (!api || !kind || !prometheusOperatorKinds.has(kind[1])) return [];
|
|
const name = text.match(/^\s*name:\s*["']?([^"'\s#]+)["']?\s*(?:#.*)?$/mu);
|
|
return [{ file, kind: kind[1], name: name ? name[1] : null, container: null }];
|
|
}
|
|
|
|
function prometheusOperatorJsonResourceRefs(text, file) {
|
|
const parsed = parseJsonDocument(text);
|
|
if (parsed === null) return null;
|
|
const items = isKubernetesList(parsed) ? parsed.items : [parsed];
|
|
return items.filter(isPrometheusOperatorResource).map((item) => ({
|
|
file,
|
|
kind: item.kind,
|
|
name: typeof item.metadata?.name === "string" ? item.metadata.name : null,
|
|
container: isKubernetesList(parsed) ? "List.items" : null,
|
|
}));
|
|
}
|
|
|
|
function isPrometheusOperatorResource(value) {
|
|
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
return typeof value.apiVersion === "string"
|
|
&& value.apiVersion.startsWith("monitoring.coreos.com/")
|
|
&& typeof value.kind === "string"
|
|
&& prometheusOperatorKinds.has(value.kind);
|
|
}
|
|
|
|
function isKubernetesList(value) {
|
|
return value
|
|
&& typeof value === "object"
|
|
&& !Array.isArray(value)
|
|
&& value.apiVersion === "v1"
|
|
&& value.kind === "List"
|
|
&& Array.isArray(value.items);
|
|
}
|
|
|
|
function parseJsonDocument(text) {
|
|
const trimmed = text.trim();
|
|
if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) return null;
|
|
try {
|
|
return JSON.parse(trimmed);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function parseStructuredDocuments(text, file) {
|
|
const json = parseJsonDocument(text);
|
|
if (json !== null) return [json];
|
|
const documents = YAML.parseAllDocuments(text);
|
|
const errors = documents.flatMap((document) => document.errors);
|
|
if (errors.length > 0) fail("runtime-yaml-invalid", { file: path.relative(repoDir, file), errors: errors.map((error) => error.message) });
|
|
return documents.map((document) => document.toJS()).filter((document) => document !== null);
|
|
}
|
|
|
|
function listYamlFiles(root) {
|
|
const out = [];
|
|
for (const name of readdirSync(root)) {
|
|
const file = path.join(root, name);
|
|
const stat = statSync(file);
|
|
if (stat.isDirectory()) {
|
|
out.push(...listYamlFiles(file));
|
|
} else if (/\.(ya?ml)$/u.test(name)) {
|
|
out.push(file);
|
|
}
|
|
}
|
|
return out.sort();
|
|
}
|
|
|
|
function readOverlay() {
|
|
const file = process.env.UNIDESK_RUNTIME_GITOPS_OVERLAY_FILE;
|
|
if (file) return JSON.parse(readFileSync(file, "utf8"));
|
|
const encoded = process.env.UNIDESK_RUNTIME_GITOPS_OVERLAY_B64;
|
|
if (!encoded) throw new Error("UNIDESK_RUNTIME_GITOPS_OVERLAY_FILE or UNIDESK_RUNTIME_GITOPS_OVERLAY_B64 is required");
|
|
return JSON.parse(Buffer.from(encoded, "base64").toString("utf8"));
|
|
}
|
|
|
|
function requireYaml() {
|
|
try {
|
|
return requireFromScript("yaml");
|
|
} catch {
|
|
return requireFromCwd("yaml");
|
|
}
|
|
}
|
|
|
|
function requiredOverlayString(name) {
|
|
const value = overlay[name];
|
|
if (typeof value !== "string" || value.length === 0) throw new Error(`overlay.${name} is required`);
|
|
return value;
|
|
}
|
|
|
|
function fail(reason, extra = {}) {
|
|
console.error(JSON.stringify({ event: "unidesk-runtime-gitops-verify", ok: false, reason, ...extra }));
|
|
process.exit(48);
|
|
}
|