Skip to content

Commit 1f3780b

Browse files
wangpand0508brycewwang
and
brycewwang
authored
fix: support add alisa and publish function version (#302)
Co-authored-by: brycewwang <[email protected]>
1 parent 62fb5ed commit 1f3780b

File tree

7 files changed

+125
-41
lines changed

7 files changed

+125
-41
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tencent-component-toolkit",
3-
"version": "2.27.0",
3+
"version": "2.27.1",
44
"description": "Tencent component toolkit",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/modules/scf/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const CONFIGS = {
66
defaultInitTimeout: 3,
77
waitStatus: ['Creating', 'Updating', 'Publishing', 'Deleting'],
88
failStatus: ['CreateFailed ', 'UpdateFailed', 'PublishFailed', 'DeleteFailed'],
9+
defaultDiskSize: 512,
910
};
1011

1112
export default CONFIGS;

src/modules/scf/entities/alias.ts

+18-13
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ export default class AliasEntity extends BaseEntity {
2020
Name: inputs.aliasName,
2121
Namespace: inputs.namespace || 'default',
2222
Description: inputs.description || 'Published by Serverless Component',
23+
RoutingConfig: {
24+
AdditionalVersionWeights: inputs.additionalVersions
25+
? inputs.additionalVersions?.map((v) => {
26+
return {
27+
Version: v.version,
28+
Weight: v.weight,
29+
};
30+
})
31+
: [],
32+
},
2333
};
24-
if (inputs.lastVersion && inputs.traffic) {
25-
publishInputs.RoutingConfig = {
26-
AdditionalVersionWeights: [
27-
{ Version: inputs.lastVersion, Weight: strip(1 - inputs.traffic) },
28-
],
29-
};
30-
}
3134
const Response = await this.request(publishInputs);
3235
return Response;
3336
}
@@ -43,12 +46,14 @@ export default class AliasEntity extends BaseEntity {
4346
Name: inputs.aliasName || '$DEFAULT',
4447
Namespace: inputs.namespace || 'default',
4548
RoutingConfig: {
46-
AdditionalVersionWeights: inputs.additionalVersions?.map((v) => {
47-
return {
48-
Version: v.version,
49-
Weight: v.weight,
50-
};
51-
}),
49+
AdditionalVersionWeights: inputs.additionalVersions
50+
? inputs.additionalVersions?.map((v) => {
51+
return {
52+
Version: v.version,
53+
Weight: v.weight,
54+
};
55+
})
56+
: [],
5257
},
5358
Description: inputs.description || 'Configured by Serverless Component',
5459
};

src/modules/scf/index.ts

+59-23
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,21 @@ export default class Scf {
280280
const functionName = inputs.name;
281281
const { ignoreTriggers = false } = inputs;
282282

283+
if (inputs?.aliasName) {
284+
if (!inputs?.additionalVersionWeights) {
285+
throw new ApiTypeError(
286+
'PARAMETER_SCF',
287+
'additionalVersionWeights is required when aliasName is setted',
288+
);
289+
}
290+
if (!inputs.publish && !inputs?.aliasFunctionVersion) {
291+
throw new ApiTypeError(
292+
'PARAMETER_SCF',
293+
'aliasFunctionVersion is required when aliasName is setted',
294+
);
295+
}
296+
}
297+
283298
// 在部署前,检查函数初始状态,如果初始为 CreateFailed,尝试先删除,再重新创建
284299
let funcInfo = await this.scf.getInitialStatus({ namespace, functionName });
285300

@@ -311,6 +326,11 @@ export default class Scf {
311326
namespace,
312327
description: inputs.publishDescription,
313328
});
329+
330+
if (inputs.aliasName) {
331+
inputs.aliasFunctionVersion = FunctionVersion;
332+
}
333+
314334
inputs.lastVersion = FunctionVersion;
315335
outputs.LastVersion = FunctionVersion;
316336

@@ -321,13 +341,10 @@ export default class Scf {
321341
});
322342
}
323343

324-
const aliasAddionalVersion = inputs.aliasAddionalVersion || inputs.lastVersion;
325-
const needSetTraffic =
326-
inputs.traffic != null && aliasAddionalVersion && aliasAddionalVersion !== '$LATEST';
327-
const needSetAlias = (inputs.aliasName && inputs.aliasName !== '$DEFAULT') || needSetTraffic;
328-
if (needSetAlias) {
344+
// 检测配置的别名是否存在,不存在就创建,存在的话就设置流量
345+
if (inputs.aliasName) {
329346
let needCreateAlias = false;
330-
if (inputs.aliasName && inputs.aliasName !== '$DEFAULT') {
347+
if (inputs.aliasName !== '$DEFAULT') {
331348
try {
332349
const aliasInfo = await this.alias.get({
333350
namespace,
@@ -347,32 +364,51 @@ export default class Scf {
347364
}
348365
}
349366
}
350-
if (needCreateAlias) {
351-
await this.alias.create({
352-
namespace,
353-
functionName,
354-
functionVersion: inputs.aliasFunctionVersion || funcInfo?.Qualifier,
355-
aliasName: inputs.aliasName!,
356-
lastVersion: aliasAddionalVersion!,
357-
traffic: inputs.traffic!,
358-
description: inputs.aliasDescription,
359-
});
360-
} else {
367+
try {
368+
// 创建别名
369+
if (needCreateAlias) {
370+
await this.alias.create({
371+
namespace,
372+
functionName,
373+
functionVersion: inputs.aliasFunctionVersion || funcInfo?.Qualifier,
374+
aliasName: inputs.aliasName!,
375+
description: inputs.aliasDescription,
376+
additionalVersions: inputs.additionalVersionWeights,
377+
});
378+
} else {
379+
// 更新别名
380+
await this.alias.update({
381+
namespace,
382+
functionName,
383+
functionVersion: inputs.aliasFunctionVersion || funcInfo?.Qualifier,
384+
additionalVersions: inputs.additionalVersionWeights,
385+
region: this.region,
386+
aliasName: inputs.aliasName,
387+
description: inputs.aliasDescription,
388+
});
389+
}
390+
} catch (error) {
391+
const errorType = needCreateAlias ? 'CREATE_ALIAS_SCF' : 'UPDATE_ALIAS_SCF';
392+
throw new ApiTypeError(errorType, error.message);
393+
}
394+
} else {
395+
// 兼容旧逻辑,即给默认版本$LATEST设置traffic比例的流量,给lastVersion版本设置(1-traffic)比例的流量。
396+
const needSetTraffic =
397+
inputs.traffic != null && inputs.lastVersion && inputs.lastVersion !== '$LATEST';
398+
if (needSetTraffic) {
361399
await this.alias.update({
362400
namespace,
363401
functionName,
364-
functionVersion: inputs.aliasFunctionVersion || funcInfo?.Qualifier,
402+
region: this.region,
365403
additionalVersions: needSetTraffic
366-
? [{ weight: strip(1 - inputs.traffic!), version: aliasAddionalVersion! }]
404+
? [{ weight: strip(1 - inputs.traffic!), version: inputs.lastVersion! }]
367405
: [],
368-
region: this.region,
369406
aliasName: inputs.aliasName,
370407
description: inputs.aliasDescription,
371408
});
409+
outputs.Traffic = inputs.traffic;
410+
outputs.ConfigTrafficVersion = inputs.lastVersion;
372411
}
373-
374-
outputs.Traffic = inputs.traffic;
375-
outputs.ConfigTrafficVersion = inputs.lastVersion;
376412
}
377413

378414
// get default alias

src/modules/scf/interface.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface BaseFunctionConfig {
3535
Timeout?: number;
3636
InitTimeout?: number;
3737
MemorySize?: number;
38+
DiskSize?: number;
3839
Type?: 'HTTP' | 'Event';
3940
DeployMode?: 'code' | 'image';
4041
PublicNetConfig?: {
@@ -69,6 +70,7 @@ export interface BaseFunctionConfig {
6970
ProtocolParams?: ProtocolParams;
7071
NodeType?: string;
7172
NodeSpec?: string;
73+
InstanceConcurrencyConfig?: { DynamicEnabled: 'TRUE' | 'FALSE'; MaxConcurrency?: number };
7274
}
7375

7476
export interface TriggerType {
@@ -145,9 +147,10 @@ export interface ScfCreateAlias {
145147
functionVersion?: string;
146148
aliasName: string;
147149
namespace?: string;
148-
lastVersion: string;
149-
traffic: number;
150+
lastVersion?: string;
151+
traffic?: number;
150152
description?: string;
153+
additionalVersions?: { version: string; weight: number }[];
151154
}
152155

153156
export interface ScfCreateFunctionInputs {
@@ -167,6 +170,7 @@ export interface ScfCreateFunctionInputs {
167170
timeout?: number;
168171
initTimeout?: number;
169172
memorySize?: number;
173+
diskSize?: number;
170174
publicAccess?: boolean;
171175
eip?: boolean;
172176
l5Enable?: boolean;
@@ -245,6 +249,13 @@ export interface ScfCreateFunctionInputs {
245249

246250
protocolType?: string;
247251
protocolParams?: ProtocolParams;
252+
253+
// 请求多并发配置
254+
instanceConcurrencyConfig?: {
255+
enable: boolean; // 是否开启多并发
256+
dynamicEnabled: boolean; // 是否开启动态配置
257+
maxConcurrency: number; // 最大并发数
258+
};
248259
}
249260

250261
export interface ScfUpdateAliasTrafficInputs {
@@ -270,17 +281,18 @@ export interface ScfDeployInputs extends ScfCreateFunctionInputs {
270281
enableRoleAuth?: boolean;
271282
region?: string;
272283

284+
// 版本相关配置
273285
lastVersion?: string;
274286
publish?: boolean;
275287
publishDescription?: string;
276-
277288
needSetTraffic?: boolean;
278289
traffic?: number;
279290

291+
// 别名相关配置
280292
aliasName?: string;
281293
aliasDescription?: string;
282294
aliasFunctionVersion?: string;
283-
aliasAddionalVersion?: string;
295+
additionalVersionWeights?: { version: string; weight: number }[];
284296

285297
tags?: Record<string, string>;
286298

src/modules/scf/utils.ts

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const formatInputs = (inputs: ScfCreateFunctionInputs) => {
2121
},
2222
L5Enable: inputs.l5Enable === true ? 'TRUE' : 'FALSE',
2323
InstallDependency: inputs.installDependency === true ? 'TRUE' : 'FALSE',
24+
DiskSize: +(inputs.diskSize || CONFIGS.defaultDiskSize),
2425
};
2526

2627
if (inputs.nodeType) {
@@ -96,6 +97,19 @@ export const formatInputs = (inputs: ScfCreateFunctionInputs) => {
9697
functionInputs.ProtocolParams = protocolParams;
9798
}
9899
}
100+
// 仅web函数支持单实例请求多并发,instanceConcurrencyConfig.enable:true,启用多并发;instanceConcurrencyConfig.enable:false,关闭多并发
101+
if (inputs.instanceConcurrencyConfig) {
102+
if (inputs.instanceConcurrencyConfig.enable) {
103+
functionInputs.InstanceConcurrencyConfig = {
104+
DynamicEnabled: inputs.instanceConcurrencyConfig.dynamicEnabled ? 'TRUE' : 'FALSE',
105+
MaxConcurrency: inputs.instanceConcurrencyConfig.maxConcurrency || 2,
106+
};
107+
} else {
108+
functionInputs.InstanceConcurrencyConfig = {
109+
DynamicEnabled: '' as any,
110+
};
111+
}
112+
}
99113
}
100114

101115
if (inputs.role) {

src/utils/index.ts

+16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ export function isArray<T>(obj: T[] | T): obj is T[] {
3131
return Object.prototype.toString.call(obj) == '[object Array]';
3232
}
3333

34+
/**
35+
* is positive integer(正整数)
36+
* @param obj object
37+
*/
38+
export function isPositiveInteger(value: string | number) {
39+
return +value > 0 && Number.isInteger(+value);
40+
}
41+
42+
/**
43+
* is number(数字)
44+
* @param obj object
45+
*/
46+
export function isNumber(value: string | number) {
47+
return !Number.isNaN(+value);
48+
}
49+
3450
/**
3551
* is object
3652
* @param obj object

0 commit comments

Comments
 (0)