|
| 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, namespace); |
| 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: sonataflow-psql-postgresql, keys: postgres-username / |
| 34 | + * postgres-password). The e2e infrastructure creates a different layout |
| 35 | + * (secret: backstage-psql-secret, keys: POSTGRES_USER / POSTGRES_PASSWORD). |
| 36 | + * Patch the cloned manifests so the SonataFlow CRs reference the resources |
| 37 | + * that actually exist. |
| 38 | + */ |
| 39 | +async function patchWorkflowPersistence( |
| 40 | + workflowDir: string, |
| 41 | + namespace: string, |
| 42 | +): Promise<void> { |
| 43 | + const result = await $`oc get secrets -n ${namespace} -o name`; |
| 44 | + const secretName = result.stdout |
| 45 | + .trim() |
| 46 | + .split("\n") |
| 47 | + .find((s) => s.includes("backstage-psql")) |
| 48 | + ?.replace("secret/", ""); |
| 49 | + |
| 50 | + if (!secretName) { |
| 51 | + throw new Error( |
| 52 | + `backstage-psql secret not found in namespace ${namespace}`, |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + const replacements: [RegExp, string][] = [ |
| 57 | + [/sonataflow-psql-postgresql/g, secretName], |
| 58 | + [/postgres-username/g, "POSTGRES_USER"], |
| 59 | + [/postgres-password/g, "POSTGRES_PASSWORD"], |
| 60 | + [ |
| 61 | + /databaseName: sonataflow\b/g, |
| 62 | + "databaseName: backstage_plugin_orchestrator", |
| 63 | + ], |
| 64 | + ]; |
| 65 | + |
| 66 | + for (const rel of MANIFEST_DIRS) { |
| 67 | + const dir = join(workflowDir, rel); |
| 68 | + const files = (await readdir(dir)).filter( |
| 69 | + (f) => f.endsWith(".yaml") || f.endsWith(".yml"), |
| 70 | + ); |
| 71 | + for (const file of files) { |
| 72 | + const path = join(dir, file); |
| 73 | + let content = await readFile(path, "utf-8"); |
| 74 | + for (const [pattern, replacement] of replacements) { |
| 75 | + content = content.replace(pattern, replacement); |
| 76 | + } |
| 77 | + await writeFile(path, content); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
22 | 82 | async function waitForWorkflows(namespace: string): Promise<void> { |
23 | 83 | const resourceDeadline = Date.now() + 60_000; |
24 | 84 | while (Date.now() < resourceDeadline) { |
|
0 commit comments