Skip to content

Commit

Permalink
feat: support scf publish version and traffic setup
Browse files Browse the repository at this point in the history
  • Loading branch information
yugasun committed Jul 6, 2020
1 parent 2869a95 commit 47522b4
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 63 deletions.
70 changes: 36 additions & 34 deletions docs/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ inputs:
layers:
- name: layerName # layer名称
version: 1 # 版本
traffic: 0.9 # 配置默认流量中 $LATEST 版本比重:0 - 1
functionConf: # 函数配置相关
timeout: 10 # 超时时间,单位秒
memorySize: 128 # 内存大小,单位MB
Expand All @@ -40,6 +41,7 @@ inputs:
vpcId: '' # 私有网络的Id
subnetId: '' # 子网ID
apigatewayConf: # api网关配置
isDisabled: false # 是否禁用自动创建 API 网关功能
enableCORS: true # 允许跨域
customDomains: # 自定义域名绑定
- domain: abc.com # 待绑定的自定义的域名
Expand All @@ -55,13 +57,13 @@ inputs:
- http
- https
environment: test
serviceTimeout: 15
usagePlan: # 用户使用计划
usagePlanId: 1111
usagePlanName: slscmp
usagePlanDesc: sls create
maxRequestNum: 1000
auth: # 密钥
serviceTimeout: 15
secretName: secret
secretIds:
- xxx
Expand All @@ -71,21 +73,20 @@ inputs:
主要的参数
| 参数名称 | 是否必选 | 默认值 | 描述 |
| ---------------------------------------- | :------: | :-------------: | :------------------------------------------------------------------ |
| runtime | 否 | Nodejs10.15 | 执行环境, 目前支持: Nodejs6.10, Nodejs8.9, Nodejs10.15, Nodejs12.16 |
| region | 否 | ap-guangzhou | 项目部署所在区域,默认广州区 |
| functionName | 否 | | 云函数名称 |
| serviceName | 否 | | API 网关服务名称, 默认创建一个新的服务名称 |
| serviceId | 否 | | API 网关服务 ID,如果存在将使用这个 API 网关服务 |
| src | 否 | `process.cwd()` | 默认为当前目录, 如果是对象, 配置参数参考 [执行目录](#src-object) |
| layers | 否 | | 云函数绑定的 layer, 配置参数参考 [层配置](#layer) |
| exclude | 否 | | 不包含的文件 |
| include | 否 | | 包含的文件, 如果是相对路径,是相对于 `serverless.yml`的路径 |
| [functionConf](#funtionConf) | 否 | | 函数配置 |
| [apigatewayConf](#apigatewayConf) | 否 | | API 网关配置 |
| [cloudDNSConf](#cloudDNSConf) | 否 | | DNS 配置 |
| [Region special config](#apigatewayConf) | 否 | | 指定区配置 |
| 参数名称 | 是否必选 | 默认值 | 描述 |
| ------------------------------------ | :------: | :-------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| runtime | 否 | Nodejs10.15 | 执行环境, 目前支持: Nodejs6.10, Nodejs8.9, Nodejs10.15, Nodejs12.16 |
| region | 否 | ap-guangzhou | 项目部署所在区域,默认广州区 |
| functionName | 否 | | 云函数名称 |
| serviceName | 否 | | API 网关服务名称, 默认创建一个新的服务名称 |
| serviceId | 否 | | API 网关服务 ID,如果存在将使用这个 API 网关服务 |
| src | 否 | `process.cwd()` | 默认为当前目录, 如果是对象, 配置参数参考 [执行目录](#执行目录) |
| layers | 否 | | 云函数绑定的 layer, 配置参数参考 [层配置](#层配置) |
| traffic | 否 | 1 | 配置默认流量中 `$LATEST` 版本比重,取值范围:0 ~ 1,比如 80%,可配置成 0.8。注意如果配置灰度流量,需要配置对应的 API 网关触发器的 endpoints 的 `function.functionQualifier` 参数为 `$DEFAULT` (默认流量) |
| [functionConf](#函数配置) | 否 | | 函数配置 |
| [apigatewayConf](#API-网关配置) | 否 | | API 网关配置 |
| [cloudDNSConf](#DNS-配置) | 否 | | DNS 配置 |
| [Region special config](#指定区配置) | 否 | | 指定区配置 |

## 执行目录

Expand Down Expand Up @@ -114,11 +115,11 @@ inputs:

### 指定区配置

| 参数名称 | 是否必选 | 类型 | 默认值 | 函数 |
| --------------------------------- | :------: | ------ | ------ | ------------ |
| [functionConf](#funtionConf) | 否 | Object | | 函数配置 |
| [apigatewayConf](#apigatewayConf) | 否 | Object | | API 网关配置 |
| [cloudDNSConf](#cloudDNSConf) | 否 | Object | | DNS 配置 |
| 参数名称 | 是否必选 | 类型 | 默认值 | 函数 |
| ------------------------------- | :------: | ------ | ------ | ------------ |
| [functionConf](#函数配置) | 否 | Object | | 函数配置 |
| [apigatewayConf](#API-网关配置) | 否 | Object | | API 网关配置 |
| [cloudDNSConf](#DNS-配置) | 否 | Object | | DNS 配置 |

### 函数配置

Expand All @@ -128,8 +129,8 @@ inputs:
| ----------- | :------: | :----: | :----: | :------------------------------------------------------------------------------ |
| timeout | 否 | Number | 3 | 函数最长执行时间,单位为秒,可选值范围 1-900 秒,默认为 3 秒 |
| memorySize | 否 | Number | 128 | 函数运行时内存大小,默认为 128M,可选范围 64、128MB-3072MB,并且以 128MB 为阶梯 |
| environment | 否 | Object | | 函数的环境变量, 参考 [环境变量](#environment) |
| vpcConfig | 否 | Object | | 函数的 VPC 配置, 参考 [VPC 配置](#vpcConfig) |
| environment | 否 | Object | | 函数的环境变量, 参考 [环境变量](#环境变量) |
| vpcConfig | 否 | Object | | 函数的 VPC 配置, 参考 [VPC 配置](#VPC-配置) |

##### 环境变量

Expand All @@ -146,17 +147,18 @@ inputs:

### API 网关配置

| 参数名称 | 是否必选 | 类型 | 默认值 | 描述 |
| ------------ | :------: | :------- | :------- | :--------------------------------------------------------------------------------- |
| protocols | 否 | String[] | ['http'] | 前端请求的类型,如 http,https,http 与 https |
| environment | 否 | String | release | 发布环境. 目前支持三种发布环境: test(测试), prepub(预发布) 与 release(发布). |
| usagePlan | 否 | | | 使用计划配置, 参考 [使用计划](#usagePlan) |
| auth | 否 | | | API 密钥配置, 参考 [API 密钥](#auth) |
| customDomain | 否 | Object[] | | 自定义 API 域名配置, 参考 [自定义域名](#customDomain) |
| isDisabled | 否 | Boolean | false | 关闭自动创建 API 网关功能。默认值为否,即默认自动创建 API 网关。 |
| enableCORS | 否 | Boolean | false | 开启跨域。默认值为否。 |
| 参数名称 | 是否必选 | 类型 | 默认值 | 描述 |
| -------------- | :------: | :------- | :------- | :--------------------------------------------------------------------------------- |
| protocols | 否 | String[] | ['http'] | 前端请求的类型,如 http,https,http 与 https |
| environment | 否 | String | release | 发布环境. 目前支持三种发布环境: test(测试), prepub(预发布) 与 release(发布). |
| usagePlan | 否 | | | 使用计划配置, 参考 [使用计划](#使用计划) |
| auth | 否 | | | API 密钥配置, 参考 [API 密钥](#API-密钥配置) |
| customDomain | 否 | Object[] | | 自定义 API 域名配置, 参考 [自定义域名](#自定义域名) |
| enableCORS | 否 | Boolean | `false` | 开启跨域。默认值为否。 |
| serviceTimeout | 否 | Number | `15` | Api 超时时间,单位: 秒 |
| isDisabled | 否 | Boolean | `false` | 关闭自动创建 API 网关功能。默认值为否,即默认自动创建 API 网关。 |

- 使用计划
##### 使用计划

参考: https://cloud.tencent.com/document/product/628/14947

Expand All @@ -167,7 +169,7 @@ inputs:
| usagePlanDesc | 否 | String | 用户自定义的使用计划描述 |
| maxRequestNum | 否 | Int | 请求配额总数,如果为空,将使用-1 作为默认值,表示不开启 |

- API 密钥配置
##### API 密钥配置

参考: https://cloud.tencent.com/document/product/628/14916

Expand Down
3 changes: 2 additions & 1 deletion example/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
org: orgDemo # (optional) serverless dashboard org. default is the first org you created during signup.
app: appDemo # (optional) serverless dashboard app. default is the same as the name property.
stage: dev # (optional) serverless dashboard stage. default is dev.
component: koa # (required) name of the component. In that case, it's koa.
component: koa@dev # (required) name of the component. In that case, it's koa.
name: koaDemo # (required) name of your koa component instance.

inputs:
Expand All @@ -11,6 +11,7 @@ inputs:
- .env
region: ap-guangzhou
runtime: Nodejs10.15
traffic: 0.8
apigatewayConf:
protocols:
- http
Expand Down
4 changes: 2 additions & 2 deletions serverless.component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ author: Tencent Cloud, Inc.
org: Tencent Cloud, Inc.
description: Deploys a serverless Koa.js application onto Tencent SCF and Tencent APIGateway.
keywords: tencent, serverless, koa
repo: https://github.com/serverless-components/tencent-koa/tree/v2
readme: https://github.com/serverless-components/tencent-koa/tree/v2/README.md
repo: https://github.com/serverless-components/tencent-koa/
readme: https://github.com/serverless-components/tencent-koa/tree/master/README.md
license: MIT
main: ./src
13 changes: 1 addition & 12 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
{
"name": "@serverless/koa",
"main": "./serverless.js",
"publishConfig": {
"access": "public"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint . --fix --cache"
},
"author": "Tencent Cloud, Inc.",
"license": "MIT",
"dependencies": {
"download": "^8.0.0",
"tencent-component-toolkit": "^1.11.4",
"tencent-component-toolkit": "^1.12.8",
"type": "^2.0.0"
}
}
20 changes: 20 additions & 0 deletions src/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ class ServerlessComponent extends Component {
...(this.state[curRegion] ? this.state[curRegion] : {}),
...outputs[curRegion]
}

// default version is $LATEST
outputs[curRegion].lastVersion = scfOutput.LastVersion
? scfOutput.LastVersion
: this.state.lastVersion || '$LATEST'

// default traffic is 1.0, it can also be 0, so we should compare to undefined
outputs[curRegion].traffic = scfOutput.Traffic
? scfOutput.Traffic
: this.state.traffic !== undefined
? this.state.traffic
: 1

if (outputs[curRegion].traffic !== 1 && scfOutput.ConfigTrafficVersion) {
outputs[curRegion].configTrafficVersion = scfOutput.ConfigTrafficVersion
this.state.configTrafficVersion = scfOutput.ConfigTrafficVersion
}

this.state.lastVersion = outputs[curRegion].lastVersion
this.state.traffic = outputs[curRegion].traffic
}
uploadCodeHandler.push(funcDeployer())
}
Expand Down
63 changes: 49 additions & 14 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ const ensureObject = require('type/object/ensure')
const ensureIterable = require('type/iterable/ensure')
const ensureString = require('type/string/ensure')
const download = require('download')
const { TypeError } = require('tencent-component-toolkit/src/utils/error')
const CONFIGS = require('./config')

/*
* Pauses execution for the provided miliseconds
*
* @param ${number} wait - number of miliseconds to wait
*/
const sleep = async (wait) => new Promise((resolve) => setTimeout(() => resolve(), wait))

/*
* Generates a random id
*/
Expand All @@ -21,6 +15,26 @@ const generateId = () =>
.toString(36)
.substring(6)

const getType = (obj) => {
return Object.prototype.toString.call(obj).slice(8, -1)
}

const validateTraffic = (num) => {
if (getType(num) !== 'Number') {
throw new TypeError(
`PARAMETER_${CONFIGS.compName.toUpperCase()}_TRAFFIC`,
'traffic must be a number'
)
}
if (num < 0 || num > 1) {
throw new TypeError(
`PARAMETER_${CONFIGS.compName.toUpperCase()}_TRAFFIC`,
'traffic must be a number between 0 and 1'
)
}
return true
}

const getCodeZipPath = async (instance, inputs) => {
console.log(`Packaging ${CONFIGS.compFullname} application...`)

Expand All @@ -32,9 +46,16 @@ const getCodeZipPath = async (instance, inputs) => {
const filename = 'template'

console.log(`Installing Default ${CONFIGS.compFullname} App...`)
await download(CONFIGS.templateUrl, downloadPath, {
filename: `${filename}.zip`
})
try {
await download(CONFIGS.templateUrl, downloadPath, {
filename: `${filename}.zip`
})
} catch (e) {
throw new TypeError(
`DOWNLOAD_${CONFIGS.compName.toUpperCase()}_TEMPLATE`,
'Download default template failed.'
)
}
zipPath = `${downloadPath}/${filename}.zip`
} else {
zipPath = inputs.code.src
Expand Down Expand Up @@ -199,6 +220,9 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
stateFunctionName ||
`${CONFIGS.compName}_component_${generateId()}`,
region: regionList,
role: ensureString(tempFunctionConf.role ? tempFunctionConf.role : inputs.role, {
default: ''
}),
handler: ensureString(tempFunctionConf.handler ? tempFunctionConf.handler : inputs.handler, {
default: CONFIGS.handler
}),
Expand All @@ -218,8 +242,18 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
fromClientRemark,
layers: ensureIterable(tempFunctionConf.layers ? tempFunctionConf.layers : inputs.layers, {
default: []
})
}),
publish: inputs.publish,
traffic: inputs.traffic,
lastVersion: instance.state.lastVersion
}

// validate traffic
if (inputs.traffic !== undefined) {
validateTraffic(inputs.traffic)
}
functionConf.needSetTraffic = inputs.traffic !== undefined && functionConf.lastVersion

functionConf.tags = ensureObject(tempFunctionConf.tags ? tempFunctionConf.tags : inputs.tag, {
default: null
})
Expand Down Expand Up @@ -264,11 +298,13 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
{
path: '/',
enableCORS: apigatewayConf.enableCORS,
serviceTimeout: apigatewayConf.serviceTimeout,
method: 'ANY',
function: {
isIntegratedResponse: true,
isIntegratedResponse: apigatewayConf.isIntegratedResponse === false ? false : true,
functionName: functionConf.name,
functionNamespace: functionConf.namespace
functionNamespace: functionConf.namespace,
functionQualifier: functionConf.needSetTraffic ? '$DEFAULT' : '$LATEST'
}
}
]
Expand Down Expand Up @@ -353,7 +389,6 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {

module.exports = {
generateId,
sleep,
uploadCodeToCos,
mergeJson,
capitalString,
Expand Down

0 comments on commit 47522b4

Please sign in to comment.