|
| 1 | +import { readdir, readFile, writeFile } from "fs/promises"; |
| 2 | +import { join } from "path"; |
1 | 3 | import { installOrchestrator } from "rhdh-e2e-test-utils/orchestrator"; |
2 | 4 | import { $ } from "rhdh-e2e-test-utils/utils"; |
3 | 5 |
|
4 | 6 | const WORKFLOW_REPO = |
5 | 7 | "https://github.com/rhdhorchestrator/serverless-workflows.git"; |
6 | 8 |
|
| 9 | +const MANIFEST_DIRS = [ |
| 10 | + "workflows/greeting/manifests", |
| 11 | + "workflows/fail-switch/src/main/resources/manifests", |
| 12 | +]; |
| 13 | + |
7 | 14 | export async function deploySonataflow(namespace: string): Promise<void> { |
8 | 15 | await installOrchestrator(namespace); |
9 | 16 |
|
10 | 17 | const workflowDir = `/tmp/serverless-workflows-${process.pid}`; |
11 | 18 | try { |
12 | 19 | await $`git clone --depth=1 ${WORKFLOW_REPO} ${workflowDir}`; |
13 | | - await $`oc apply -n ${namespace} -f ${workflowDir}/workflows/greeting/manifests/`; |
14 | | - await $`oc apply -n ${namespace} -f ${workflowDir}/workflows/fail-switch/src/main/resources/manifests/`; |
| 20 | + await patchWorkflowPersistence(workflowDir); |
| 21 | + for (const rel of MANIFEST_DIRS) { |
| 22 | + await $`oc apply -n ${namespace} -f ${join(workflowDir, rel)}`; |
| 23 | + } |
15 | 24 | } finally { |
16 | 25 | await $`rm -rf ${workflowDir}`.catch(() => {}); |
17 | 26 | } |
18 | 27 |
|
19 | 28 | await waitForWorkflows(namespace); |
20 | 29 | } |
21 | 30 |
|
| 31 | +/** |
| 32 | + * The upstream serverless-workflows manifests expect a Helm-chart-style |
| 33 | + * PostgreSQL (secret & service both named sonataflow-psql-postgresql, |
| 34 | + * keys: postgres-username / postgres-password). The e2e infrastructure |
| 35 | + * creates a different layout (secret: backstage-psql-secret, service: |
| 36 | + * backstage-psql, keys: POSTGRES_USER / POSTGRES_PASSWORD). |
| 37 | + * Patch the cloned manifests so the SonataFlow CRs reference the |
| 38 | + * resources that actually exist. |
| 39 | + */ |
| 40 | +async function patchWorkflowPersistence(workflowDir: string): Promise<void> { |
| 41 | + for (const rel of MANIFEST_DIRS) { |
| 42 | + const dir = join(workflowDir, rel); |
| 43 | + const files = (await readdir(dir)).filter( |
| 44 | + (f) => f.endsWith(".yaml") || f.endsWith(".yml"), |
| 45 | + ); |
| 46 | + for (const file of files) { |
| 47 | + const filePath = join(dir, file); |
| 48 | + let content = await readFile(filePath, "utf-8"); |
| 49 | + content = patchPersistence(content); |
| 50 | + await writeFile(filePath, content); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function patchPersistence(content: string): string { |
| 56 | + // secretRef precedes serviceRef in these manifests; patch it first so |
| 57 | + // the blanket replacement below only touches serviceRef occurrences. |
| 58 | + content = content.replace( |
| 59 | + /(secretRef:)([\s\S]*?)(serviceRef:)/g, |
| 60 | + (_m, pre, block, post) => |
| 61 | + pre + |
| 62 | + block |
| 63 | + .replace( |
| 64 | + /name: sonataflow-psql-postgresql/, |
| 65 | + "name: backstage-psql-secret", |
| 66 | + ) |
| 67 | + .replace(/userKey: postgres-username/, "userKey: POSTGRES_USER") |
| 68 | + .replace( |
| 69 | + /passwordKey: postgres-password/, |
| 70 | + "passwordKey: POSTGRES_PASSWORD", |
| 71 | + ) + |
| 72 | + post, |
| 73 | + ); |
| 74 | + |
| 75 | + // Remaining sonataflow-psql-postgresql refs are in serviceRef blocks. |
| 76 | + content = content.replace(/sonataflow-psql-postgresql/g, "backstage-psql"); |
| 77 | + content = content.replace( |
| 78 | + /databaseName: sonataflow\b/g, |
| 79 | + "databaseName: backstage_plugin_orchestrator", |
| 80 | + ); |
| 81 | + |
| 82 | + return content; |
| 83 | +} |
| 84 | + |
22 | 85 | async function waitForWorkflows(namespace: string): Promise<void> { |
23 | 86 | const resourceDeadline = Date.now() + 60_000; |
24 | 87 | while (Date.now() < resourceDeadline) { |
|
0 commit comments