Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend allowed memory for GCP functions #339

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions packages/gcp-functions/src/executors/deploy/deploy.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { buildCommand, execCommand } from '@nx-extend/core'
import { join } from 'path'

import { getValidSecrets } from '../../utils/get-valid-secrets'
import { Gen, ValidMemory } from '../../types'
import { validateResources } from '../../utils/validate-resources'

export interface DeployExecutorSchema {
functionName: string
runtime?: 'nodejs16' | 'nodejs18' | 'nodejs20' | 'recommended'
entryPoint?: string
serviceAccount?: string
memory?: '128MB' | '256MB' | '512MB' | '1024MB' | '2048MB' | '4096MB'
memory?: ValidMemory
region: string
envVarsFile?: string
envVars?: Record<string, string>
Expand All @@ -28,7 +30,7 @@ export interface DeployExecutorSchema {
secrets?: string[] | Record<string, string>

// Gen 2 options
gen?: 1 | 2
gen?: Gen
concurrency?: number
timeout?: number
cloudSqlInstance?: string
Expand Down Expand Up @@ -81,6 +83,8 @@ export async function deployExecutor(
allowUnauthenticated = trigger === 'http'
} = options

validateResources({ cpu, memory, gen })

let correctMemory = memory as string
let correctCPU = cpu as number

Expand Down
5 changes: 4 additions & 1 deletion packages/gcp-functions/src/executors/deploy/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@
"512MB",
"1024MB",
"2048MB",
"4096MB"
"4096MB",
"8192MB",
"16384MB",
"32768MB"
]
},
"allowUnauthenticated": {
Expand Down
12 changes: 12 additions & 0 deletions packages/gcp-functions/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type ValidMemory =
| '128MB'
| '256MB'
| '512MB'
| '1024MB'
| '2048MB'
| '4096MB'
| '8192MB'
| '16384MB'
| '32768MB'

export type Gen = 1 | 2
64 changes: 64 additions & 0 deletions packages/gcp-functions/src/utils/validate-resources.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { validateResources } from './validate-resources'
import { Gen, ValidMemory } from '../types'

describe('validateResources', () => {
it('should throw an error if memory is gen2 and gen is 1', () => {
const resources = {
cpu: 4,
memory: '16384MB' as const,
gen: 1 as const
}

expect(() => validateResources(resources)).toThrow()
})

it('should throw an error if CPU is less than minimum required for memory', () => {
const resources = {
cpu: 0.5,
memory: '1024MB' as const,
gen: 2 as const
}

expect(() => validateResources(resources)).toThrow()
})

it('should not throw an error for valid resources', () => {
const resources = {
cpu: 1,
memory: '1024MB' as ValidMemory,
gen: 2 as Gen
}

expect(() => validateResources(resources)).not.toThrow()
})

it('should not throw an error for gen2 memory and gen 2', () => {
const resources = {
cpu: 4,
memory: '16384MB' as const,
gen: 2 as Gen
}

expect(() => validateResources(resources)).not.toThrow()
})

it('should not throw an error for minimum CPU and memory', () => {
const resources = {
cpu: 0.083,
memory: '128MB' as const,
gen: 2 as const
}

expect(() => validateResources(resources)).not.toThrow()
})

it('should not throw an error if CPU is higher than minimum required for memory', () => {
const resources = {
cpu: 1,
memory: '256MB' as const,
gen: 1 as const
}

expect(() => validateResources(resources)).not.toThrow()
})
})
43 changes: 43 additions & 0 deletions packages/gcp-functions/src/utils/validate-resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Gen, ValidMemory } from '../types'

const gen2Memory: ValidMemory[] = ['16384MB', '32768MB']
const minimumCPU: Record<ValidMemory, number> = {
'128MB': 0.083,
'256MB': 0.167,
'512MB': 0.333,
'1024MB': 0.583,
'2048MB': 1,
'4096MB': 2,
'8192MB': 2,
'16384MB': 4,
'32768MB': 8
}
const docsLink = 'https://cloud.google.com/functions/docs/configuring/memory'

export const validateResources = ({
cpu,
memory,
gen
}: {
cpu: number
memory: ValidMemory
gen: Gen
}) => {
if (gen2Memory.includes(memory) && gen === 1) {
throw new Error(
`Setting memory to "${memory}" is not supported when gen is 2.

See ${docsLink} for more information.
`
)
}

if (cpu < minimumCPU[memory]) {
throw new Error(
`CPU value of ${cpu} is not enough for memory of ${memory}.

See ${docsLink} for more information
`
)
}
}
Loading