-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get-location should output the s3 archive workflow directory TDE…
- Loading branch information
1 parent
e34d93d
commit af92c04
Showing
7 changed files
with
150 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import fs from 'node:fs'; | ||
|
||
/** | ||
* Extract the script from a workflow template file located as its `source`. | ||
* | ||
* @param path to the workflow template file | ||
* @param stopDelimiter optional delimiter to stop the extraction | ||
* @returns the script as a string | ||
*/ | ||
function getFunctionFromScript(path: string, stopDelimiter?: string): string { | ||
let func = fs.readFileSync(path, 'utf-8').split('source: |')[1]; | ||
if (stopDelimiter) { | ||
func = func?.split(stopDelimiter)[0]; | ||
} | ||
if (!func) { | ||
throw new Error('No script found in the workflow'); | ||
} | ||
return func; | ||
} | ||
|
||
/** | ||
* Data to replace in the script | ||
*/ | ||
type FunctionData = { toReplace: string; replaceWith: string }; | ||
|
||
/** | ||
* Create and run a function from a script located in a workflowTemplate file. | ||
* | ||
* @param workflowPath path to the workflow template file | ||
* @param data data to replace in the script | ||
* @param stopDelimiter optional delimiter to stop the extraction of the script | ||
*/ | ||
export function runTestFunction(workflowPath: string, data: FunctionData[], stopDelimiter?: string): void { | ||
let func = getFunctionFromScript(workflowPath, stopDelimiter); | ||
data.forEach((d) => { | ||
func = func.replace(d.toReplace, d.replaceWith); | ||
}); | ||
// eslint-disable-next-line @typescript-eslint/no-implied-eval | ||
new Function(func)(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import assert from 'node:assert'; | ||
import { describe, it } from 'node:test'; | ||
|
||
import { runTestFunction } from './function.helper.js'; | ||
|
||
describe('get-location script template', () => { | ||
it('should output workflow artifact location', (t) => { | ||
const spy = t.mock.method(console, 'log'); | ||
runTestFunction( | ||
'./templates/common/get.location.yml', | ||
[ | ||
{ | ||
toReplace: '/tmp/', | ||
replaceWith: 'memory://tmp/', | ||
}, | ||
{ | ||
toReplace: `JSON.parse(process.env['ARGO_TEMPLATE'])`, | ||
replaceWith: JSON.stringify({ | ||
archiveLocation: { | ||
s3: { | ||
key: '2024-10/02-test-get-location-29l4x/test-get-location-29l4x-get-location-3125883809', | ||
bucket: 'linz-workflows-scratch', | ||
}, | ||
}, | ||
}), | ||
}, | ||
], | ||
'// Write outputs', | ||
); | ||
assert.equal(spy.mock.callCount(), 3); | ||
|
||
const location = String(spy.mock.calls[0]?.arguments[0]); | ||
assert.equal(location, 'Location: s3://linz-workflows-scratch/2024-10/02-test-get-location-29l4x/'); | ||
|
||
const bucket = String(spy.mock.calls[1]?.arguments[0]); | ||
assert.equal(bucket, 'Bucket: linz-workflows-scratch'); | ||
|
||
const key = String(spy.mock.calls[2]?.arguments[0]); | ||
assert.equal(key, 'Key: 2024-10/02-test-get-location-29l4x'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# yaml-language-server: $schema=https://raw.githubusercontent.com/argoproj/argo-workflows/v3.5.5/api/jsonschema/schema.json | ||
|
||
apiVersion: argoproj.io/v1alpha1 | ||
kind: WorkflowTemplate | ||
metadata: | ||
# Template to output the locaton of the workflow S3 artifact root directory. | ||
name: tpl-get-location | ||
spec: | ||
templateDefaults: | ||
container: | ||
imagePullPolicy: Always | ||
image: '' | ||
entrypoint: main | ||
templates: | ||
- name: main | ||
inputs: | ||
parameters: | ||
- name: version_argo_tasks | ||
description: Version of argo-tasks to use | ||
default: 'v4' | ||
outputs: | ||
parameters: | ||
- name: location | ||
valueFrom: | ||
path: '/tmp/location' | ||
- name: bucket | ||
valueFrom: | ||
path: '/tmp/bucket' | ||
- name: key | ||
valueFrom: | ||
path: '/tmp/key' | ||
script: | ||
image: '019359803926.dkr.ecr.ap-southeast-2.amazonaws.com/argo-tasks:{{=sprig.trim(inputs.parameters.version_argo_tasks)}}' | ||
command: [node] | ||
source: | | ||
const argoTemplate = JSON.parse(process.env['ARGO_TEMPLATE']); | ||
const podArchiveLoc = argoTemplate.archiveLocation.s3; | ||
const workflowArchiveKey = podArchiveLoc.key.substr(0, podArchiveLoc.key.lastIndexOf('/')); | ||
const location = `s3://${podArchiveLoc.bucket}/${workflowArchiveKey}/` | ||
console.log(`Location: ${location}`); | ||
console.log(`Bucket: ${podArchiveLoc.bucket}`); | ||
console.log(`Key: ${workflowArchiveKey}`); | ||
// Write outputs | ||
const fs = require('fs'); | ||
fs.writeFileSync('/tmp/location', location); | ||
fs.writeFileSync('/tmp/bucket', `${podArchiveLoc.bucket}`); | ||
fs.writeFileSync('/tmp/key', `${workflowArchiveKey}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters