Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

feat: support go provided.al2 runtime #1559

Merged
merged 3 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/feature_flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<keyof typeof defaultFlags, boolean>>
Expand Down
10 changes: 8 additions & 2 deletions src/runtimes/go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const zipFunction: ZipFunction = async function ({
srcPath,
stat,
isInternal,
featureFlags,
}) {
const destPath = join(destFolder, filename)
const isSource = extname(mainFile) === '.go'
Expand All @@ -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,
runtimeVersion: featureFlags.zisi_golang_use_al2 ? 'provided.al2' : undefined,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for consistency with what we do in Node.js, it might be nice to always return the runtime version, even if it's the old one.

Not a big deal, though.

Suggested change
runtimeVersion: featureFlags.zisi_golang_use_al2 ? 'provided.al2' : undefined,
runtimeVersion: featureFlags.zisi_golang_use_al2 ? 'provided.al2' : 'go1.x',

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about that as well, but I didn't want to introduce changes when the flag is off. undefined should do that.

}
}

// We don't need to zip the binary, so we can just copy it to the right path.
Expand Down
26 changes: 24 additions & 2 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1740,14 +1740,21 @@ 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,
},
},
featureFlags: {
zisi_golang_use_al2: true,
},
},
})
const binaryPath = join(FIXTURES_DIR, fixtureName, 'test')
Expand All @@ -1758,8 +1765,12 @@ 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, 'test')
const unzippedBinaryPath = join(unzippedFunctions[0].unzipPath, 'bootstrap')

await expect(unzippedBinaryPath).toPathExist()

Expand All @@ -1776,27 +1787,38 @@ 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,
},
},
featureFlags: {
zisi_golang_use_al2: true,
},
},
})
const [func] = files

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 })

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)
Expand Down