Skip to content

Commit

Permalink
feat: support cfs
Browse files Browse the repository at this point in the history
  • Loading branch information
yugasun committed Aug 25, 2020
1 parent 2c2315e commit dd12119
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
23 changes: 20 additions & 3 deletions docs/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ inputs:
vpcConfig: # 私有网络配置
vpcId: '' # 私有网络的Id
subnetId: '' # 子网ID
cfs: # cfs配置
- cfsId: cfs-123
mountInsId: cfs-123
localMountDir: /mnt/
remoteMountDir: /
deadLetter: # 死信队列配置
type: deadLetterType
name: deadLetterName
Expand All @@ -71,7 +76,7 @@ inputs:
- apigw: # api网关触发器,已有apigw服务,配置触发器
name: #触发器名称,默认apigw-${name}-${stage}
parameters:
serviceName: apigw-service-xxxx
serviceName: serverless
serviceId: service-8dsikiq6
protocols:
- http
Expand Down Expand Up @@ -176,6 +181,7 @@ inputs:
| [cls](#函数日子) | 否 | | 函数日志配置,配置参数参考[函数日志](#函数日志) |
| eip | 否 | `false` | 固定出口 IP。默认为 false,即不启用。 |
| tags | 否 | | 标签设置。可设置多对 key-value 的键值对 |
| [cfs](文件系统) | 否 | | 文件系统挂载配置,用于云函数挂载文件系统。配置参数参考[文件系统](#文件系统)。 |
| [events](#触发器) | 否 | | 触发器数组。支持以下几种触发器:timer(定时触发器)、apigw(网关触发器)、cos(COS 触发器)、cmq(CMQ Topic 触发器)、ckafka(CKafka 触发器)配置参数参考[触发器](#触发器)。 |

### 执行目录
Expand Down Expand Up @@ -213,7 +219,7 @@ inputs:
| 参数名称 | 是否必选 | 类型 | 描述 |
| -------- | :------: | :----: | :------- |
| name | 是 | string | 层名称 |
| version | 是 | string | 层版本号 |
| version | 是 | number | 层版本号 |

### 函数日志

Expand All @@ -222,6 +228,17 @@ inputs:
| logsetId | 否 | string | 函数日志投递到的 CLS LogsetID |
| topicId | 否 | string | 函数日志投递到的 CLS TopicID |

### 文件系统

使用文件系统必须配置[私有网络](#私有网络),并保证 cfs 文件系统与云函数在同一个私有网络下。

| 参数名称 | 是否必选 | 类型 | 描述 |
| -------------- | :------: | :----: | :---------------- |
| cfsId | 是 | String | 文件系统实例 id |
| mountInsId | 是 | String | 文件系统挂载点 id |
| localMountDir | 是 | String | 本地挂载点 |
| remoteMountDir | 是 | String | 远程挂载点 |

### 触发器

参考: https://cloud.tencent.com/document/product/583/39901
Expand Down Expand Up @@ -277,7 +294,7 @@ inputs:

| 参数名称 | 是否必选 | 类型 | 默认值 | 描述 |
| ----------- | -------- | :------: | :---------- | :----------------------------------------------------------------------------- |
| environment | | string | `release` | 发布的环境,填写 `release`、`test` 或 `prepub`,不填写默认为`release` |
| environment | | string | `release` | 发布的环境,填写 `release`、`test` 或 `prepub`,不填写默认为`release` |
| serviceId | 否 | string | | 网关 Service ID(不传入则新建一个 Service) |
| protocols | 否 | string[] | `['http']` | 前端请求的类型,如 http,https,http 与 https |
| netTypes | 否 | string[] | `['OUTER']` | 网络类型,如 `['OUTER']`, `['INNER']` 与`['OUTER', 'INNER']` |
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "@serverless/scf",
"version": "0.1.3",
"main": "src/serverless.js",
"publishConfig": {
"access": "public"
Expand Down
11 changes: 11 additions & 0 deletions serverless.component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ actions:
keys:
variables:
type: object
layers:
type: array
items:
- type: object
keys:
name:
type: string
required: true
version:
type: number
required: true
vpcConfig:
type: object
keys:
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dependencies": {
"download": "^8.0.0",
"tencent-component-toolkit": "^1.15.1",
"tencent-component-toolkit": "^1.15.6",
"type": "^2.0.0"
}
}
20 changes: 10 additions & 10 deletions src/serverless.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Component } = require('@serverless/core')
const { Scf } = require('tencent-component-toolkit')
const { TypeError } = require('tencent-component-toolkit/src/utils/error')
const { prepareInputs, getType } = require('./utils')
const { prepareInputs, getType, getDefaultProtocol } = require('./utils')
const CONFIGS = require('./config')

class ServerlessComponent extends Component {
Expand All @@ -26,13 +26,6 @@ class ServerlessComponent extends Component {
return this.credentials.tencent.tmpSecrets.appId
}

getDefaultProtocol(protocols) {
if (String(protocols).includes('https')) {
return 'https'
}
return 'http'
}

async deploy(inputs) {
console.log(`Deploying Tencent ${CONFIGS.compFullname}...`)

Expand Down Expand Up @@ -63,6 +56,13 @@ class ServerlessComponent extends Component {
memorySize: scfOutput.MemorySize
}

if (scfOutput.Layers && scfOutput.Layers.length > 0) {
outputs.layers = scfOutput.Layers.map((item) => ({
name: item.LayerName,
version: item.LayerVersion
}))
}

// default version is $LATEST
outputs.lastVersion = scfOutput.LastVersion
? scfOutput.LastVersion
Expand Down Expand Up @@ -94,14 +94,14 @@ class ServerlessComponent extends Component {
if (getType(apigwTrigger.subDomain) === 'Array') {
apigwTrigger.subDomain.forEach((item) => {
triggers['apigw'].push(
`${this.getDefaultProtocol(apigwTrigger.protocols)}://${item}/${
`${getDefaultProtocol(apigwTrigger.protocols)}://${item}/${
apigwTrigger.environment
}${endpoint.path}`
)
})
} else {
triggers['apigw'].push(
`${this.getDefaultProtocol(apigwTrigger.protocols)}://${apigwTrigger.subDomain}/${
`${getDefaultProtocol(apigwTrigger.protocols)}://${apigwTrigger.subDomain}/${
apigwTrigger.environment
}${endpoint.path}`
)
Expand Down
17 changes: 11 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const validateTraffic = (num) => {
return true
}

const getDefaultProtocol = (protocols) => {
return String(protocols).includes('https') ? 'https' : 'http'
}

const getDefaultFunctionName = (instance) => {
return `${instance.name}-${instance.stage}-${instance.app}`
}
Expand All @@ -39,12 +43,12 @@ const getDefaultTriggerName = (type, instance) => {
return `${type}-${instance.name}-${instance.stage}`
}

const getDefaultServiceName = (instance) => {
return `${instance.name}_${instance.stage}`
const getDefaultServiceName = () => {
return 'serverless'
}

const getDefaultServiceDescription = (instance) => {
return `${instance.name}-${instance.stage}-${instance.app}`
return `The service of serverless scf: ${instance.name}-${instance.stage}-${instance.app}`
}

/**
Expand Down Expand Up @@ -183,11 +187,11 @@ const prepareInputs = async (instance, credentials, appId, inputs) => {
currentEvent.parameters.description =
currentEvent.parameters.description || getDefaultServiceDescription(instance)
currentEvent.name = currentEvent.name || getDefaultTriggerName(eventType, instance)
if (stateApigw && stateApigw[currentEvent.name]) {
if (stateApigw && stateApigw[currentEvent.parameters.serviceName]) {
currentEvent.parameters.serviceId =
currentEvent.parameters.serviceId || stateApigw[currentEvent.name]
currentEvent.parameters.serviceId || stateApigw[currentEvent.parameters.serviceName]
}
apigwName.push(currentEvent.name)
apigwName.push(currentEvent.parameters.serviceName)
}
existApigwTrigger = true
} else {
Expand Down Expand Up @@ -227,6 +231,7 @@ const prepareInputs = async (instance, credentials, appId, inputs) => {

module.exports = {
getType,
getDefaultProtocol,
generateId,
prepareInputs,
getDefaultZipPath
Expand Down

0 comments on commit dd12119

Please sign in to comment.