From d1f237d3f697213b45e02bdde5b70614452862e8 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Tue, 5 Sep 2023 17:13:29 +0200 Subject: [PATCH 1/2] feat: support go provided.al2 runtime --- src/feature_flags.ts | 3 +++ src/runtimes/go/index.ts | 10 ++++++++-- src/runtimes/runtime.ts | 1 + src/utils/format_result.ts | 2 ++ tests/main.test.ts | 10 ++++++++-- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/feature_flags.ts b/src/feature_flags.ts index 0a3d33a21..40c15b5f0 100644 --- a/src/feature_flags.ts +++ b/src/feature_flags.ts @@ -26,6 +26,9 @@ export const defaultFlags = { // If multiple glob stars are in includedFiles, fail the build instead of warning. zisi_esbuild_fail_double_glob: false, + + // Bundle for the provided.AL2 runtime for Go functions. + zisi_golang_use_al2: false, } as const export type FeatureFlags = Partial> diff --git a/src/runtimes/go/index.ts b/src/runtimes/go/index.ts index 56d4a523c..3f6cc7f9f 100644 --- a/src/runtimes/go/index.ts +++ b/src/runtimes/go/index.ts @@ -113,6 +113,7 @@ const zipFunction: ZipFunction = async function ({ srcPath, stat, isInternal, + featureFlags, }) { const destPath = join(destFolder, filename) const isSource = extname(mainFile) === '.go' @@ -139,13 +140,18 @@ const zipFunction: ZipFunction = async function ({ const zipPath = `${destPath}.zip` const zipOptions = { destPath: zipPath, - filename: basename(destPath), + filename: featureFlags.zisi_golang_use_al2 ? 'bootstrap' : basename(destPath), runtime, } await zipBinary({ ...zipOptions, srcPath: binary.path, stat: binary.stat }) - return { config, path: zipPath, entryFilename: zipOptions.filename } + return { + config, + path: zipPath, + entryFilename: zipOptions.filename, + goUseAL2Runtime: featureFlags.zisi_golang_use_al2, + } } // We don't need to zip the binary, so we can just copy it to the right path. diff --git a/src/runtimes/runtime.ts b/src/runtimes/runtime.ts index c5571a550..56cdcf60f 100644 --- a/src/runtimes/runtime.ts +++ b/src/runtimes/runtime.ts @@ -54,6 +54,7 @@ export interface ZipFunctionResult { path: string runtimeVersion?: string entryFilename: string + goUseAL2Runtime?: boolean } export type ZipFunction = ( diff --git a/src/utils/format_result.ts b/src/utils/format_result.ts index 106a1834a..f69eee8a4 100644 --- a/src/utils/format_result.ts +++ b/src/utils/format_result.ts @@ -9,6 +9,7 @@ export type FunctionResult = Omit & { runtime: RuntimeName schedule?: string runtimeAPIVersion?: number + goUseAL2Runtime?: boolean } // Takes the result of zipping a function and formats it for output. @@ -20,6 +21,7 @@ export const formatZipResult = (archive: FunctionArchive) => { runtime: archive.runtime.name, schedule: archive.inSourceConfig?.schedule ?? archive?.config?.schedule, runtimeAPIVersion: archive.inSourceConfig?.runtimeAPIVersion, + goUseAL2Runtime: archive.goUseAL2Runtime, } return removeUndefined(functionResult) diff --git a/tests/main.test.ts b/tests/main.test.ts index 7332ee7ad..4bb717303 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -1748,6 +1748,9 @@ describe('zip-it-and-ship-it', () => { zipGo: true, }, }, + featureFlags: { + zisi_golang_use_al2: true, + }, }, }) const binaryPath = join(FIXTURES_DIR, fixtureName, 'test') @@ -1759,7 +1762,7 @@ describe('zip-it-and-ship-it', () => { expect(func.path.endsWith('.zip')).toBe(true) const unzippedFunctions = await unzipFiles([func]) - const unzippedBinaryPath = join(unzippedFunctions[0].unzipPath, 'test') + const unzippedBinaryPath = join(unzippedFunctions[0].unzipPath, 'bootstrap') await expect(unzippedBinaryPath).toPathExist() @@ -1784,6 +1787,9 @@ describe('zip-it-and-ship-it', () => { zipGo: true, }, }, + featureFlags: { + zisi_golang_use_al2: true, + }, }, }) const [func] = files @@ -1796,7 +1802,7 @@ describe('zip-it-and-ship-it', () => { const unzippedFunctions = await unzipFiles([func]) - const unzippedBinaryPath = join(unzippedFunctions[0].unzipPath, 'go-func-1') + const unzippedBinaryPath = join(unzippedFunctions[0].unzipPath, 'bootstrap') const unzippedBinaryContents = await readFile(unzippedBinaryPath, 'utf8') expect(mockSource).toBe(unzippedBinaryContents) From 80a3c66ba1ddb49d6867b0de7debdc1eddab4ddb Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 6 Sep 2023 13:38:12 +0200 Subject: [PATCH 2/2] refactor: write into runtimeVersion --- src/runtimes/go/index.ts | 2 +- src/runtimes/runtime.ts | 1 - src/utils/format_result.ts | 2 -- tests/main.test.ts | 16 ++++++++++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/runtimes/go/index.ts b/src/runtimes/go/index.ts index 3f6cc7f9f..1b04cb160 100644 --- a/src/runtimes/go/index.ts +++ b/src/runtimes/go/index.ts @@ -150,7 +150,7 @@ const zipFunction: ZipFunction = async function ({ config, path: zipPath, entryFilename: zipOptions.filename, - goUseAL2Runtime: featureFlags.zisi_golang_use_al2, + runtimeVersion: featureFlags.zisi_golang_use_al2 ? 'provided.al2' : undefined, } } diff --git a/src/runtimes/runtime.ts b/src/runtimes/runtime.ts index 56cdcf60f..c5571a550 100644 --- a/src/runtimes/runtime.ts +++ b/src/runtimes/runtime.ts @@ -54,7 +54,6 @@ export interface ZipFunctionResult { path: string runtimeVersion?: string entryFilename: string - goUseAL2Runtime?: boolean } export type ZipFunction = ( diff --git a/src/utils/format_result.ts b/src/utils/format_result.ts index f69eee8a4..106a1834a 100644 --- a/src/utils/format_result.ts +++ b/src/utils/format_result.ts @@ -9,7 +9,6 @@ export type FunctionResult = Omit & { runtime: RuntimeName schedule?: string runtimeAPIVersion?: number - goUseAL2Runtime?: boolean } // Takes the result of zipping a function and formats it for output. @@ -21,7 +20,6 @@ export const formatZipResult = (archive: FunctionArchive) => { runtime: archive.runtime.name, schedule: archive.inSourceConfig?.schedule ?? archive?.config?.schedule, runtimeAPIVersion: archive.inSourceConfig?.runtimeAPIVersion, - goUseAL2Runtime: archive.goUseAL2Runtime, } return removeUndefined(functionResult) diff --git a/tests/main.test.ts b/tests/main.test.ts index 4bb717303..4df394e23 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -1740,9 +1740,13 @@ describe('zip-it-and-ship-it', () => { }) test('Zips Go function binaries if the `zipGo` config property is set', async () => { + const { path: tmpDir } = await getTmpDir({ prefix: 'zip-it-test' }) + const manifestPath = join(tmpDir, 'manifest.json') + const fixtureName = 'go-simple' const { files } = await zipFixture(fixtureName, { opts: { + manifest: manifestPath, config: { '*': { zipGo: true, @@ -1761,6 +1765,10 @@ describe('zip-it-and-ship-it', () => { expect(func.runtime).toBe('go') expect(func.path.endsWith('.zip')).toBe(true) + const manifest = JSON.parse(await readFile(manifestPath, 'utf-8')) + + expect(manifest.functions[0].runtimeVersion).toEqual('provided.al2') + const unzippedFunctions = await unzipFiles([func]) const unzippedBinaryPath = join(unzippedFunctions[0].unzipPath, 'bootstrap') @@ -1779,9 +1787,13 @@ describe('zip-it-and-ship-it', () => { return {} as any }) + const { path: manifestTmpDir } = await getTmpDir({ prefix: 'zip-it-test' }) + const manifestPath = join(manifestTmpDir, 'manifest.json') + const fixtureName = 'go-source' const { files, tmpDir } = await zipFixture(fixtureName, { opts: { + manifest: manifestPath, config: { '*': { zipGo: true, @@ -1797,6 +1809,10 @@ describe('zip-it-and-ship-it', () => { expect(func.runtime).toBe('go') expect(func.path.endsWith('.zip')).toBe(true) + const manifest = JSON.parse(await readFile(manifestPath, 'utf-8')) + + expect(manifest.functions[0].runtimeVersion).toEqual('provided.al2') + // remove the binary before unzipping await rm(join(tmpDir, 'go-func-1'), { maxRetries: 10 })