fix: isolate production migration database URL

This commit is contained in:
root
2026-07-17 00:13:07 +02:00
parent 3099c5d7d1
commit 26eec181a0
3 changed files with 40 additions and 3 deletions
+18 -1
View File
@@ -8,10 +8,27 @@ import test from "node:test";
import { parse as parseYaml } from "yaml";
import { configString } from "./src/gitops-render/core.mjs";
import { opencodeEgressProxyUrlForProfile } from "./src/gitops-render/runtime-manifests.mjs";
import { externalPostgresConfigForLane, externalPostgresManifest, opencodeEgressProxyUrlForProfile } from "./src/gitops-render/runtime-manifests.mjs";
import { CLOUD_CORE_MIGRATIONS, CLOUD_TRANSACTIONAL_REALTIME_MIGRATION_ID } from "../internal/db/schema.ts";
test("production migration uses its YAML-declared database Secret key", async () => {
const deploy = parseYaml(await readFile("deploy/deploy.yaml", "utf8"));
const config = externalPostgresConfigForLane(deploy, "production", { nodeId: "NC01" });
const manifest = externalPostgresManifest({
profile: "production",
config,
source: { full: "a".repeat(40), short: "a".repeat(12), imageTag: "a".repeat(12) },
deploy,
migrationSources: [{ path: "0001_test.sql", sql: "select 1;" }]
});
const job = manifest.items.find((item) => item.kind === "Job");
assert.deepEqual(job?.spec?.template?.spec?.containers?.[0]?.env?.[0]?.valueFrom?.secretKeyRef, {
name: "hwlab-cloud-api-production-db",
key: "migration-database-url"
});
});
function taskByName(pipeline, name) {
const task = pipeline.spec.tasks.find((item) => item.name === name);
assert.ok(task, `missing task ${name}`);
@@ -737,7 +737,15 @@ function externalPostgresConfigForLane(deploy, profile, args = {}) {
assert.ok(typeof effective.endpointAddress === "string" && effective.endpointAddress.length > 0, `deploy.lanes.${profile}.externalPostgres.endpointAddress is required`);
const port = Number(effective.port ?? 5432);
assert.ok(Number.isInteger(port) && port > 0 && port <= 65535, `deploy.lanes.${profile}.externalPostgres.port must be a valid TCP port`);
return { serviceName: effective.serviceName, endpointAddress: effective.endpointAddress, port };
const migrationSecretName = effective.migrationSecretName;
const migrationSecretKey = effective.migrationSecretKey;
assert.equal(typeof migrationSecretName === "string", typeof migrationSecretKey === "string", `deploy.lanes.${profile}.externalPostgres migration Secret name and key must be declared together`);
return {
serviceName: effective.serviceName,
endpointAddress: effective.endpointAddress,
port,
...(typeof migrationSecretName === "string" ? { migrationSecretName, migrationSecretKey } : {})
};
}
function externalPostgresManifest({ profile = "v03", config, source, deploy = null, migrationSources = [], catalog = null, registryPrefix = defaultRegistryPrefix, useDeployImages = false }) {
@@ -816,7 +824,12 @@ function externalPostgresManifest({ profile = "v03", config, source, deploy = nu
command: ["node", "/opt/hwlab-migrations/run.mjs"],
env: [{
name: "DATABASE_URL",
valueFrom: { secretKeyRef: { name: `hwlab-cloud-api-${profile}-db`, key: "database-url" } }
valueFrom: {
secretKeyRef: {
name: config.migrationSecretName ?? `hwlab-cloud-api-${profile}-db`,
key: config.migrationSecretKey ?? "database-url"
}
}
}],
volumeMounts: [{ name: "migrations", mountPath: "/opt/hwlab-migrations", readOnly: true }]
}],