Skip to content
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
11 changes: 3 additions & 8 deletions samples/aliyun-poc-fc-gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ tags:

functions:
insight_poc_fn:
name: insight-poc-gpu-fns
code:
runtime: nodejs18
handler: ${vars.handler}
path: tests/fixtures/artifacts/artifact.zip
name: insight-poc-gpu-fn
container:
image: registry.cn-hangzhou.aliyuncs.com/aliyunfc/runtime/nodejs18:1.8.0
command: [node, index.handler]
entrypoint: [node]
image: registry.cn-chengdu.aliyuncs.com/geek-fun/meke-api:latest
cmd: "npm start"
port: 9000
memory: 512
timeout: 10
Expand Down
5 changes: 2 additions & 3 deletions src/parser/functionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ export const parseFunction = (functions?: {
return Object.entries(functions).map(([key, func]) => ({
key,
name: func.name,
runtime: func.runtime,
handler: func.handler,
code: func.code,
container: func.container,
memory: func.memory,
timeout: func.timeout,
environment: func.environment,
code: func.code,
log: func.log,
network: func.network,
storage: {
Expand Down
6 changes: 3 additions & 3 deletions src/stack/rfsStack/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ resource "huaweicloud_fgs_application" "${service}_app" {
const fgsFunction = (fn: FunctionDomain, context: ActionContext, service: string) => `
resource "huaweicloud_fgs_function" "${fn.key}" {
name = "${fn.name}"
handler = "${fn.handler}"
runtime = "${fn.runtime}"
handler = "${fn.code!.handler}"
runtime = "${fn.code!.runtime}"
memory_size = ${fn.memory}
timeout = ${fn.timeout}
environment = ${JSON.stringify(fn.environment)}
code_type = "inline"
func_code = "${resolveCode(fn.code)}"
func_code = "${resolveCode(fn.code!.path)}"
app = "huaweicloud_fgs_application.${service}_app.id"
}
`;
Expand Down
61 changes: 45 additions & 16 deletions src/stack/rosStack/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as ros from '@alicloud/ros-cdk-core';
import * as sls from '@alicloud/ros-cdk-sls';
import * as nas from '@alicloud/ros-cdk-nas';
import * as ecs from '@alicloud/ros-cdk-ecs';
import { RosFunction } from '@alicloud/ros-cdk-fc3/lib/fc3.generated';
import { RosFunction, RosFunctionProps } from '@alicloud/ros-cdk-fc3/lib/fc3.generated';

const storageClassMap = {
[NasStorageClassEnum.STANDARD_CAPACITY]: { fileSystemType: 'standard', storageType: 'Capacity' },
Expand Down Expand Up @@ -99,11 +99,11 @@ export const resolveFunctions = (
}

const fileSources = functions
?.filter(({ code }) => readCodeSize(code) > CODE_ZIP_SIZE_LIMIT)
?.filter(({ code }) => code?.path && readCodeSize(code.path) > CODE_ZIP_SIZE_LIMIT)
.map(({ code, name }) => {
const fcName = replaceReference(name, context);

return { fcName, ...getFileSource(fcName, code) };
return { fcName, ...getFileSource(fcName, code!.path) };
});

const destinationBucketName = ros.Fn.sub(
Expand All @@ -125,17 +125,48 @@ export const resolveFunctions = (
true,
);
}

functions?.forEach((fnc) => {
const storeInBucket = readCodeSize(fnc.code) > CODE_ZIP_SIZE_LIMIT;
let code: fc.RosFunction.CodeProperty = {
zipFile: resolveCode(fnc.code),
};
if (storeInBucket) {
code = {
ossBucketName: destinationBucketName,
ossObjectName: fileSources?.find(
({ fcName }) => fcName === replaceReference(fnc.name, context),
)?.objectKey,
let runtimeConfig:
| {
customContainerConfig: RosFunctionProps['customContainerConfig'];
runtime: RosFunctionProps['runtime'];
handler: RosFunctionProps['handler'];
}
| {
code: RosFunctionProps['code'];
runtime: RosFunctionProps['runtime'];
handler: RosFunctionProps['handler'];
};

const storeInBucket = !!fnc.code?.path && readCodeSize(fnc.code.path) > CODE_ZIP_SIZE_LIMIT;

if (fnc.container) {
runtimeConfig = {
runtime: 'custom-container',
handler: 'index.handler',
customContainerConfig: {
image: fnc.container.image,
command: fnc.container.cmd?.split(' '),
port: fnc.container.port,
},
};
} else {
let code: fc.RosFunction.CodeProperty = {
zipFile: resolveCode(fnc.code!.path),
};
if (storeInBucket) {
code = {
ossBucketName: destinationBucketName,
ossObjectName: fileSources?.find(
({ fcName }) => fcName === replaceReference(fnc.name, context),
)?.objectKey,
};
}
runtimeConfig = {
code,
handler: replaceReference(fnc.code!.handler, context),
runtime: replaceReference(fnc.code!.runtime, context),
};
}

Expand Down Expand Up @@ -216,15 +247,13 @@ export const resolveFunctions = (
fnc.key,
{
functionName: replaceReference(fnc.name, context),
handler: replaceReference(fnc.handler, context),
runtime: replaceReference(fnc.runtime, context),
memorySize: replaceReference(fnc.memory, context),
timeout: replaceReference(fnc.timeout, context),
diskSize: fnc.storage?.disk,
environmentVariables: replaceReference(fnc.environment, context),
code,
logConfig,
vpcConfig,
...runtimeConfig,
nasConfig: fcNas?.length
? {
mountPoints: fcNas?.map(({ nasMount, mountDir }) => ({
Expand Down
26 changes: 20 additions & 6 deletions src/types/domains/function.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export type FunctionRaw = {
name: string;
runtime: string;
handler: string;
code: string;
code?: {
runtime: string;
handler: string;
path: string;
};
container?: {
image: string;
cmd?: string;
port: number;
};
memory: number;
timeout: number;
log?: boolean;
Expand Down Expand Up @@ -30,9 +37,16 @@ export type FunctionRaw = {
export type FunctionDomain = {
key: string;
name: string;
runtime: string;
handler: string;
code: string;
code?: {
runtime: string;
handler: string;
path: string;
};
container?: {
image: string;
cmd?: string;
port: number;
};
memory: number;
timeout: number;
log?: boolean;
Expand Down
59 changes: 38 additions & 21 deletions src/validator/functionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,47 @@ export const functionSchema = {
patternProperties: {
'.*': {
type: 'object',
required: ['name', 'runtime', 'handler', 'code'],
required: ['name'],
properties: {
name: { type: 'string' },
runtime: {
type: 'string',
enum: [
'nodejs20',
'nodejs18',
'nodejs16',
'nodejs14',
'nodejs12',
'nodejs10',
'nodejs8',
'python3.10',
'python3.9',
'python3',
'PHP 7.2',
'Java 11',
'.NET Core 3.1',
'Go 1.x',
],
code: {
type: 'object',
required: ['runtime', 'handler', 'path'],
additionalProperties: false,
properties: {
runtime: {
type: 'string',
enum: [
'nodejs20',
'nodejs18',
'nodejs16',
'nodejs14',
'nodejs12',
'nodejs10',
'nodejs8',
'python3.10',
'python3.9',
'python3',
'PHP 7.2',
'Java 11',
'.NET Core 3.1',
'Go 1.x',
],
},
handler: { type: 'string' },
path: { type: 'string' },
},
},
container: {
type: 'object',
required: ['image', 'port'],
additionalProperties: false,
properties: {
image: { type: 'string' },
cmd: { type: 'string' },
port: { type: 'number' },
},
},
handler: { type: 'string' },
code: { type: 'string' },
memory: { type: 'number' },
timeout: { type: 'number' },
log: { type: 'boolean' },
Expand Down
72 changes: 59 additions & 13 deletions tests/fixtures/deployFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const oneFcOneGatewayIac = {
version: '0.0.1',
provider: {
name: 'aliyun' as ProviderEnum,
region: "cn-hangzhou'",
region: 'cn-hangzhou',
},
vars: {
account_id: 1234567890,
Expand All @@ -22,9 +22,11 @@ export const oneFcOneGatewayIac = {
{
key: 'hello_fn',
name: 'hello_fn',
runtime: 'nodejs18',
handler: 'index.handler',
code: 'tests/fixtures/artifacts/artifact.zip',
code: {
runtime: 'nodejs18',
handler: 'index.handler',
path: 'tests/fixtures/artifacts/artifact.zip',
},
memory: 128,
timeout: 10,
log: true,
Expand Down Expand Up @@ -255,6 +257,7 @@ export const oneFcOneGatewayRos = {
},
},
};

export const referredServiceIac = set(
cloneDeep(oneFcOneGatewayIac),
'service',
Expand Down Expand Up @@ -462,9 +465,11 @@ export const minimumIac = {
{
key: 'hello_fn',
name: 'hello_fn',
runtime: 'nodejs18',
handler: 'index.handler',
code: 'tests/fixtures/artifacts/artifact.zip',
code: {
runtime: 'nodejs18',
handler: 'index.handler',
path: 'tests/fixtures/artifacts/artifact.zip',
},
},
],
} as ServerlessIac;
Expand Down Expand Up @@ -544,9 +549,11 @@ export const oneFcIac = {
{
key: 'hello_fn',
name: 'hello_fn',
runtime: 'nodejs18',
handler: 'index.handler',
code: 'tests/fixtures/artifacts/artifact.zip',
code: {
runtime: 'nodejs18',
handler: 'index.handler',
path: 'tests/fixtures/artifacts/artifact.zip',
},
memory: 128,
timeout: 10,
log: true,
Expand Down Expand Up @@ -688,6 +695,7 @@ export const oneFcIacWithNas = {
},
],
} as ServerlessIac;

export const oneFcIacWithNasRos = {
...oneFcRos,
Resources: {
Expand Down Expand Up @@ -838,9 +846,11 @@ export const oneFcIacWithStage = {
{
key: 'hello_fn',
name: 'hello_fn',
runtime: 'nodejs18',
handler: 'index.handler',
code: 'tests/fixtures/artifacts/artifact.zip',
code: {
runtime: 'nodejs18',
handler: 'index.handler',
path: 'tests/fixtures/artifacts/artifact.zip',
},
memory: 128,
timeout: 10,
log: true,
Expand Down Expand Up @@ -948,6 +958,42 @@ export const oneFcWithStageRos = {
},
},
};

export const oneFcWithContainerIac = {
...oneFcIac,
functions: [
{
...((oneFcIac.functions && oneFcIac.functions[0]) ?? {}),
code: undefined,
container: {
image: 'registry.cn-hangzhou.aliyuncs.com/aliyunfc/abcd:1.6.0',
cmd: 'npm start',
port: 9200,
},
},
],
} as ServerlessIac;

export const oneFcWithContainerRos = {
...oneFcRos,
Resources: {
...oneFcRos.Resources,
hello_fn: {
...oneFcRos.Resources.hello_fn,
Properties: {
...oneFcRos.Resources.hello_fn.Properties,
Code: undefined,
Runtime: 'custom-container',
CustomContainerConfig: {
Command: ['npm', 'start'],
Image: 'registry.cn-hangzhou.aliyuncs.com/aliyunfc/abcd:1.6.0',
Port: 9200,
},
},
},
},
};

export const largeCodeRos = {
Description: 'my-demo-service stack',
Mappings: {
Expand Down
7 changes: 4 additions & 3 deletions tests/fixtures/serverless-insight-huawei.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ tags:
functions:
insight_poc_fn:
name: insight-poc-fn
runtime: nodejs18
handler: ${vars.handler}
code: tests/fixtures/artifacts/artifact.zip
code:
runtime: nodejs18
handler: ${vars.handler}
path: tests/fixtures/artifacts/artifact.zip
memory: 512
timeout: 10
environment:
Expand Down
Loading