Skip to content

Commit 6a3155e

Browse files
authored
feat: enable support for container runtime (#48)
feat: enable support for container runtime Refs: #40 --------- Signed-off-by: seven <[email protected]>
1 parent c4ae761 commit 6a3155e

File tree

10 files changed

+197
-77
lines changed

10 files changed

+197
-77
lines changed

samples/aliyun-poc-fc-gpu.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,10 @@ tags:
2222

2323
functions:
2424
insight_poc_fn:
25-
name: insight-poc-gpu-fns
26-
code:
27-
runtime: nodejs18
28-
handler: ${vars.handler}
29-
path: tests/fixtures/artifacts/artifact.zip
25+
name: insight-poc-gpu-fn
3026
container:
31-
image: registry.cn-hangzhou.aliyuncs.com/aliyunfc/runtime/nodejs18:1.8.0
32-
command: [node, index.handler]
33-
entrypoint: [node]
27+
image: registry.cn-chengdu.aliyuncs.com/geek-fun/meke-api:latest
28+
cmd: "npm start"
3429
port: 9000
3530
memory: 512
3631
timeout: 10

src/parser/functionParser.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ export const parseFunction = (functions?: {
1010
return Object.entries(functions).map(([key, func]) => ({
1111
key,
1212
name: func.name,
13-
runtime: func.runtime,
14-
handler: func.handler,
13+
code: func.code,
14+
container: func.container,
1515
memory: func.memory,
1616
timeout: func.timeout,
1717
environment: func.environment,
18-
code: func.code,
1918
log: func.log,
2019
network: func.network,
2120
storage: {

src/stack/rfsStack/function.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ resource "huaweicloud_fgs_application" "${service}_app" {
1313
const fgsFunction = (fn: FunctionDomain, context: ActionContext, service: string) => `
1414
resource "huaweicloud_fgs_function" "${fn.key}" {
1515
name = "${fn.name}"
16-
handler = "${fn.handler}"
17-
runtime = "${fn.runtime}"
16+
handler = "${fn.code!.handler}"
17+
runtime = "${fn.code!.runtime}"
1818
memory_size = ${fn.memory}
1919
timeout = ${fn.timeout}
2020
environment = ${JSON.stringify(fn.environment)}
2121
code_type = "inline"
22-
func_code = "${resolveCode(fn.code)}"
22+
func_code = "${resolveCode(fn.code!.path)}"
2323
app = "huaweicloud_fgs_application.${service}_app.id"
2424
}
2525
`;

src/stack/rosStack/function.ts

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import * as ros from '@alicloud/ros-cdk-core';
1414
import * as sls from '@alicloud/ros-cdk-sls';
1515
import * as nas from '@alicloud/ros-cdk-nas';
1616
import * as ecs from '@alicloud/ros-cdk-ecs';
17-
import { RosFunction } from '@alicloud/ros-cdk-fc3/lib/fc3.generated';
17+
import { RosFunction, RosFunctionProps } from '@alicloud/ros-cdk-fc3/lib/fc3.generated';
1818

1919
const storageClassMap = {
2020
[NasStorageClassEnum.STANDARD_CAPACITY]: { fileSystemType: 'standard', storageType: 'Capacity' },
@@ -99,11 +99,11 @@ export const resolveFunctions = (
9999
}
100100

101101
const fileSources = functions
102-
?.filter(({ code }) => readCodeSize(code) > CODE_ZIP_SIZE_LIMIT)
102+
?.filter(({ code }) => code?.path && readCodeSize(code.path) > CODE_ZIP_SIZE_LIMIT)
103103
.map(({ code, name }) => {
104104
const fcName = replaceReference(name, context);
105105

106-
return { fcName, ...getFileSource(fcName, code) };
106+
return { fcName, ...getFileSource(fcName, code!.path) };
107107
});
108108

109109
const destinationBucketName = ros.Fn.sub(
@@ -125,17 +125,48 @@ export const resolveFunctions = (
125125
true,
126126
);
127127
}
128+
128129
functions?.forEach((fnc) => {
129-
const storeInBucket = readCodeSize(fnc.code) > CODE_ZIP_SIZE_LIMIT;
130-
let code: fc.RosFunction.CodeProperty = {
131-
zipFile: resolveCode(fnc.code),
132-
};
133-
if (storeInBucket) {
134-
code = {
135-
ossBucketName: destinationBucketName,
136-
ossObjectName: fileSources?.find(
137-
({ fcName }) => fcName === replaceReference(fnc.name, context),
138-
)?.objectKey,
130+
let runtimeConfig:
131+
| {
132+
customContainerConfig: RosFunctionProps['customContainerConfig'];
133+
runtime: RosFunctionProps['runtime'];
134+
handler: RosFunctionProps['handler'];
135+
}
136+
| {
137+
code: RosFunctionProps['code'];
138+
runtime: RosFunctionProps['runtime'];
139+
handler: RosFunctionProps['handler'];
140+
};
141+
142+
const storeInBucket = !!fnc.code?.path && readCodeSize(fnc.code.path) > CODE_ZIP_SIZE_LIMIT;
143+
144+
if (fnc.container) {
145+
runtimeConfig = {
146+
runtime: 'custom-container',
147+
handler: 'index.handler',
148+
customContainerConfig: {
149+
image: fnc.container.image,
150+
command: fnc.container.cmd?.split(' '),
151+
port: fnc.container.port,
152+
},
153+
};
154+
} else {
155+
let code: fc.RosFunction.CodeProperty = {
156+
zipFile: resolveCode(fnc.code!.path),
157+
};
158+
if (storeInBucket) {
159+
code = {
160+
ossBucketName: destinationBucketName,
161+
ossObjectName: fileSources?.find(
162+
({ fcName }) => fcName === replaceReference(fnc.name, context),
163+
)?.objectKey,
164+
};
165+
}
166+
runtimeConfig = {
167+
code,
168+
handler: replaceReference(fnc.code!.handler, context),
169+
runtime: replaceReference(fnc.code!.runtime, context),
139170
};
140171
}
141172

@@ -216,15 +247,13 @@ export const resolveFunctions = (
216247
fnc.key,
217248
{
218249
functionName: replaceReference(fnc.name, context),
219-
handler: replaceReference(fnc.handler, context),
220-
runtime: replaceReference(fnc.runtime, context),
221250
memorySize: replaceReference(fnc.memory, context),
222251
timeout: replaceReference(fnc.timeout, context),
223252
diskSize: fnc.storage?.disk,
224253
environmentVariables: replaceReference(fnc.environment, context),
225-
code,
226254
logConfig,
227255
vpcConfig,
256+
...runtimeConfig,
228257
nasConfig: fcNas?.length
229258
? {
230259
mountPoints: fcNas?.map(({ nasMount, mountDir }) => ({

src/types/domains/function.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
export type FunctionRaw = {
22
name: string;
3-
runtime: string;
4-
handler: string;
5-
code: string;
3+
code?: {
4+
runtime: string;
5+
handler: string;
6+
path: string;
7+
};
8+
container?: {
9+
image: string;
10+
cmd?: string;
11+
port: number;
12+
};
613
memory: number;
714
timeout: number;
815
log?: boolean;
@@ -30,9 +37,16 @@ export type FunctionRaw = {
3037
export type FunctionDomain = {
3138
key: string;
3239
name: string;
33-
runtime: string;
34-
handler: string;
35-
code: string;
40+
code?: {
41+
runtime: string;
42+
handler: string;
43+
path: string;
44+
};
45+
container?: {
46+
image: string;
47+
cmd?: string;
48+
port: number;
49+
};
3650
memory: number;
3751
timeout: number;
3852
log?: boolean;

src/validator/functionSchema.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,47 @@ export const functionSchema = {
44
patternProperties: {
55
'.*': {
66
type: 'object',
7-
required: ['name', 'runtime', 'handler', 'code'],
7+
required: ['name'],
88
properties: {
99
name: { type: 'string' },
10-
runtime: {
11-
type: 'string',
12-
enum: [
13-
'nodejs20',
14-
'nodejs18',
15-
'nodejs16',
16-
'nodejs14',
17-
'nodejs12',
18-
'nodejs10',
19-
'nodejs8',
20-
'python3.10',
21-
'python3.9',
22-
'python3',
23-
'PHP 7.2',
24-
'Java 11',
25-
'.NET Core 3.1',
26-
'Go 1.x',
27-
],
10+
code: {
11+
type: 'object',
12+
required: ['runtime', 'handler', 'path'],
13+
additionalProperties: false,
14+
properties: {
15+
runtime: {
16+
type: 'string',
17+
enum: [
18+
'nodejs20',
19+
'nodejs18',
20+
'nodejs16',
21+
'nodejs14',
22+
'nodejs12',
23+
'nodejs10',
24+
'nodejs8',
25+
'python3.10',
26+
'python3.9',
27+
'python3',
28+
'PHP 7.2',
29+
'Java 11',
30+
'.NET Core 3.1',
31+
'Go 1.x',
32+
],
33+
},
34+
handler: { type: 'string' },
35+
path: { type: 'string' },
36+
},
37+
},
38+
container: {
39+
type: 'object',
40+
required: ['image', 'port'],
41+
additionalProperties: false,
42+
properties: {
43+
image: { type: 'string' },
44+
cmd: { type: 'string' },
45+
port: { type: 'number' },
46+
},
2847
},
29-
handler: { type: 'string' },
30-
code: { type: 'string' },
3148
memory: { type: 'number' },
3249
timeout: { type: 'number' },
3350
log: { type: 'boolean' },

tests/fixtures/deployFixture.ts

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const oneFcOneGatewayIac = {
77
version: '0.0.1',
88
provider: {
99
name: 'aliyun' as ProviderEnum,
10-
region: "cn-hangzhou'",
10+
region: 'cn-hangzhou',
1111
},
1212
vars: {
1313
account_id: 1234567890,
@@ -22,9 +22,11 @@ export const oneFcOneGatewayIac = {
2222
{
2323
key: 'hello_fn',
2424
name: 'hello_fn',
25-
runtime: 'nodejs18',
26-
handler: 'index.handler',
27-
code: 'tests/fixtures/artifacts/artifact.zip',
25+
code: {
26+
runtime: 'nodejs18',
27+
handler: 'index.handler',
28+
path: 'tests/fixtures/artifacts/artifact.zip',
29+
},
2830
memory: 128,
2931
timeout: 10,
3032
log: true,
@@ -255,6 +257,7 @@ export const oneFcOneGatewayRos = {
255257
},
256258
},
257259
};
260+
258261
export const referredServiceIac = set(
259262
cloneDeep(oneFcOneGatewayIac),
260263
'service',
@@ -462,9 +465,11 @@ export const minimumIac = {
462465
{
463466
key: 'hello_fn',
464467
name: 'hello_fn',
465-
runtime: 'nodejs18',
466-
handler: 'index.handler',
467-
code: 'tests/fixtures/artifacts/artifact.zip',
468+
code: {
469+
runtime: 'nodejs18',
470+
handler: 'index.handler',
471+
path: 'tests/fixtures/artifacts/artifact.zip',
472+
},
468473
},
469474
],
470475
} as ServerlessIac;
@@ -544,9 +549,11 @@ export const oneFcIac = {
544549
{
545550
key: 'hello_fn',
546551
name: 'hello_fn',
547-
runtime: 'nodejs18',
548-
handler: 'index.handler',
549-
code: 'tests/fixtures/artifacts/artifact.zip',
552+
code: {
553+
runtime: 'nodejs18',
554+
handler: 'index.handler',
555+
path: 'tests/fixtures/artifacts/artifact.zip',
556+
},
550557
memory: 128,
551558
timeout: 10,
552559
log: true,
@@ -688,6 +695,7 @@ export const oneFcIacWithNas = {
688695
},
689696
],
690697
} as ServerlessIac;
698+
691699
export const oneFcIacWithNasRos = {
692700
...oneFcRos,
693701
Resources: {
@@ -838,9 +846,11 @@ export const oneFcIacWithStage = {
838846
{
839847
key: 'hello_fn',
840848
name: 'hello_fn',
841-
runtime: 'nodejs18',
842-
handler: 'index.handler',
843-
code: 'tests/fixtures/artifacts/artifact.zip',
849+
code: {
850+
runtime: 'nodejs18',
851+
handler: 'index.handler',
852+
path: 'tests/fixtures/artifacts/artifact.zip',
853+
},
844854
memory: 128,
845855
timeout: 10,
846856
log: true,
@@ -948,6 +958,42 @@ export const oneFcWithStageRos = {
948958
},
949959
},
950960
};
961+
962+
export const oneFcWithContainerIac = {
963+
...oneFcIac,
964+
functions: [
965+
{
966+
...((oneFcIac.functions && oneFcIac.functions[0]) ?? {}),
967+
code: undefined,
968+
container: {
969+
image: 'registry.cn-hangzhou.aliyuncs.com/aliyunfc/abcd:1.6.0',
970+
cmd: 'npm start',
971+
port: 9200,
972+
},
973+
},
974+
],
975+
} as ServerlessIac;
976+
977+
export const oneFcWithContainerRos = {
978+
...oneFcRos,
979+
Resources: {
980+
...oneFcRos.Resources,
981+
hello_fn: {
982+
...oneFcRos.Resources.hello_fn,
983+
Properties: {
984+
...oneFcRos.Resources.hello_fn.Properties,
985+
Code: undefined,
986+
Runtime: 'custom-container',
987+
CustomContainerConfig: {
988+
Command: ['npm', 'start'],
989+
Image: 'registry.cn-hangzhou.aliyuncs.com/aliyunfc/abcd:1.6.0',
990+
Port: 9200,
991+
},
992+
},
993+
},
994+
},
995+
};
996+
951997
export const largeCodeRos = {
952998
Description: 'my-demo-service stack',
953999
Mappings: {

tests/fixtures/serverless-insight-huawei.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ tags:
2323
functions:
2424
insight_poc_fn:
2525
name: insight-poc-fn
26-
runtime: nodejs18
27-
handler: ${vars.handler}
28-
code: tests/fixtures/artifacts/artifact.zip
26+
code:
27+
runtime: nodejs18
28+
handler: ${vars.handler}
29+
path: tests/fixtures/artifacts/artifact.zip
2930
memory: 512
3031
timeout: 10
3132
environment:

0 commit comments

Comments
 (0)