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

feat: cli创建函数支持sfType字段 #295

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tencent-component-toolkit",
"version": "2.24.2",
"version": "2.24.3",
"description": "Tencent component toolkit",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/modules/scf/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const ACTIONS = [
'DeleteProvisionedConcurrencyConfig',
'GetReservedConcurrencyConfig',
'GetProvisionedConcurrencyConfig',
'GetRequestStatus'
'GetRequestStatus',
'PutGpuReservedQuota',
] as const;

export type ActionType = typeof ACTIONS[number];
Expand Down
26 changes: 24 additions & 2 deletions src/modules/scf/entities/scf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
FunctionInfo,
GetLogOptions,
GetRequestStatusOptions,
GpuReservedQuota,
ScfCreateFunctionInputs,
UpdateFunctionCodeOptions,
} from '../interface';
Expand Down Expand Up @@ -150,8 +151,12 @@ export default class ScfEntity extends BaseEntity {
console.log(`Creating function ${inputs.name}, region ${this.region}`);
const inp = formatInputs(inputs);
const functionInputs = { Action: 'CreateFunction' as const, ...inp };
await this.request(functionInputs);
return true;
try {
await this.request(functionInputs);
return true;
} catch (error) {
return false;
}
}

// 更新函数代码
Expand Down Expand Up @@ -476,4 +481,21 @@ export default class ScfEntity extends BaseEntity {
console.log(e);
}
}

// 设置Gpu函数独占配额
async updateGpuReservedQuota(inputs: GpuReservedQuota) {
console.log(`PutGpuReservedQuota functionName ${inputs.functionName}, region ${this.region},gpuReservedQuota ${inputs.gpuReservedQuota}`);
const quotaInputs = {
Action: 'PutGpuReservedQuota' as const,
Version: '2018-04-16',
FunctionName:inputs?.functionName,
GpuReservedQuota: inputs?.gpuReservedQuota
};
try {
await this.request(quotaInputs);
return true;
} catch (error) {
return false;
}
}
}
10 changes: 9 additions & 1 deletion src/modules/scf/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ScfDeployOutputs,
OriginTriggerType,
GetLogOptions,
SF_TYPE_ENUM,
} from './interface';
import ScfEntity from './entities/scf';
import AliasEntity from './entities/alias';
Expand Down Expand Up @@ -283,7 +284,14 @@ export default class Scf {

// 检查函数是否存在,不存在就创建,存在就更新
if (!funcInfo) {
await this.scf.create(inputs);
let result = await this.scf.create(inputs);
//创建函数成功后,设置Gpu函数独占配额,如果当前是sd函数、gpuReservedQuota大于0,则执行该操作。
if(result && [SF_TYPE_ENUM.SD_API,SF_TYPE_ENUM.SD_WEB_UI].includes(inputs?.sFType as any) && !!inputs?.gpuReservedQuota){
await this.scf.updateGpuReservedQuota({
functionName: inputs?.name,
gpuReservedQuota: inputs?.gpuReservedQuota
});
}
} else {
await this.scf.updateCode(inputs, funcInfo);

Expand Down
42 changes: 37 additions & 5 deletions src/modules/scf/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface FunctionCode {
RegistryId?: string;
Command?: string;
Args?: string;
ContainerImageAccelerate?: boolean;
};
}

Expand Down Expand Up @@ -67,6 +68,13 @@ export interface BaseFunctionConfig {
ProtocolParams?: ProtocolParams;
NodeType?: string;
NodeSpec?: string;
SFType?: string;
GpuReservedQuota?: number;
// 请求并发
InstanceConcurrencyConfig?: {
DynamicEnabled?: boolean;
MaxConcurrency?: number;
};
}

export interface TriggerType {
Expand Down Expand Up @@ -228,6 +236,8 @@ export interface ScfCreateFunctionInputs {
command?: string;
// 启动命令参数
args?: string;
// 镜像加速
containerImageAccelerate?: boolean;
};

// 异步调用重试配置
Expand All @@ -236,6 +246,16 @@ export interface ScfCreateFunctionInputs {

protocolType?: string;
protocolParams?: ProtocolParams;

// sd应用类型
sFType?: string;
// gpu并发数,默认是0
gpuReservedQuota?: number;
// 请求并发配置
instanceConcurrencyConfig?: {
dynamicEnabled?: boolean;
maxConcurrency?: number;
};
}

export interface ScfUpdateAliasTrafficInputs {
Expand Down Expand Up @@ -391,25 +411,37 @@ export interface GetRequestStatusOptions {
/**
* 函数名称
*/
functionName: string
functionName: string;

/**
* 需要查询状态的请求id
*/
functionRequestId: string
functionRequestId: string;

/**
* 函数的所在的命名空间
*/
namespace?: string
namespace?: string;

/**
* 查询的开始时间,例如:2017-05-16 20:00:00,不填默认为当前时间 - 15min
*/
startTime?: string
startTime?: string;

/**
* 查询的结束时间,例如:2017-05-16 20:59:59,不填默认为当前时间。EndTime 需要晚于 StartTime。
*/
endTime?: string
endTime?: string;
}

export const SF_TYPE_ENUM = {
SD_WEB_UI: 'SD WebUI',
SD_API: 'SD API',
};

export interface GpuReservedQuota {
functionName: string
gpuReservedQuota: number
region?: string
namespace?: string
}
21 changes: 21 additions & 0 deletions src/modules/scf/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export const formatInputs = (inputs: ScfCreateFunctionInputs) => {
if (imageConfig.args) {
functionInputs.Code!.ImageConfig!.Args = imageConfig.args;
}

if (imageConfig?.containerImageAccelerate !== undefined) {
functionInputs.Code!.ImageConfig!.ContainerImageAccelerate =
imageConfig?.containerImageAccelerate;
}
} else {
// 基于 COS 代码部署
functionInputs.Code = {
Expand Down Expand Up @@ -85,6 +90,22 @@ export const formatInputs = (inputs: ScfCreateFunctionInputs) => {
}
}

// sd应用类型
if (inputs?.sFType) {
functionInputs.SFType = inputs?.sFType;
}
// sd gpu并发数
if (inputs?.gpuReservedQuota) {
functionInputs.GpuReservedQuota = inputs?.gpuReservedQuota;
}
// 请求并发
if (inputs?.instanceConcurrencyConfig) {
functionInputs.InstanceConcurrencyConfig = {
DynamicEnabled: inputs?.instanceConcurrencyConfig?.dynamicEnabled,
MaxConcurrency: inputs?.instanceConcurrencyConfig?.maxConcurrency,
};
}

if (inputs.role) {
functionInputs.Role = inputs.role;
}
Expand Down