Skip to content

Commit

Permalink
feat: support deploy and remove command only with asyncInvokeConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
rsonghuster committed Apr 11, 2024
1 parent 03b3ed7 commit 344c32e
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 70 deletions.
27 changes: 22 additions & 5 deletions __tests__/e2e/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ s remove -y
rm -rf ./code/target
cd ..


echo "test java runtime"
cd java
export fc_component_function_name=java-$(uname)-$(uname -m)-$RANDSTR
Expand All @@ -31,7 +30,6 @@ s remove -y
rm -rf ./target
cd ..


echo "test custom go runtime ..."
cd custom
rm -rf ./go/code/go.sum
Expand All @@ -43,7 +41,6 @@ s remove -y -t ./go/s.yaml
rm -rf ./go/code/target
cd ..


echo "test nodejs runtime with auto ..."
cd nodejs
export fc_component_function_name=nodejs14-$(uname)-$(uname -m)-$RANDSTR
Expand All @@ -53,15 +50,35 @@ s info -y -t ./s_auto.yaml
s remove -y -t ./s_auto.yaml
cd ..

echo "test deploy with alias"
cd nodejs
export fc_component_function_name=nodejs14-$(uname)-$(uname -m)-$RANDSTR
s deploy --function -t s2.yaml
versionId=$(s version publish -t s2.yaml --silent -o json | jq -r '."versionId"')
echo "latest version = $versionId"
if [[ "$versionId" -gt 1 ]]; then
mainVersion=$((versionId - 1))
echo "main version = $mainVersion"
s alias publish --alias-name test --version-id $mainVersion --vw "{\"$versionId\": 0.2}" -t s2.yaml
else
s alias publish --alias-name test --version-id $versionId -t s2.yaml
fi

s deploy --trigger -t s2.yaml
s deploy --async-invoke-config -t s2.yaml
s info -t s2.yaml
s remove -y -t s2.yaml
cd ..

echo "test http trigger with jwt ..."
cd trigger/jwt
export fc_component_function_name=nodejs16-$(uname)-$(uname -m)-$RANDSTR
s deploy -y -t ./s.yaml
s invoke -e '{"hello":"fc http trigger with jwt"}' -t ./s.yaml
rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y jq
url1=$(s info -y -t ./s.yaml --silent -o json | jq -r '.hello_world.url.system_url')
url2=$(s info -y -t ./s.yaml --silent -o json | jq -r '.hello_world_2.url.system_url')
url1=$(s info -y -t ./s.yaml --silent -o json | jq -r '.hello_world.url.system_url')
url2=$(s info -y -t ./s.yaml --silent -o json | jq -r '.hello_world_2.url.system_url')
echo $url1
echo $url2
curl -XPOST $url1/black1/aa -d '{"test":"jwt"}'
Expand Down
15 changes: 1 addition & 14 deletions __tests__/e2e/nodejs/s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,4 @@ resources:
instanceLifecycleConfig:
initializer:
handler: event.initializer
timeout: 10

# vpcConfig: auto
# nasConfig: auto
# logConfig: auto

asyncInvokeConfig:
destinationConfig:
onFailure:
destination: acs:mns:${vars.region}::/topics/serverless-devs-fc3-ci-test/messages
onSuccess:
destination: acs:fc:${vars.region}::functions/serverless-devs-ci-async-invoke-config-succ
maxAsyncEventAgeInSeconds: 360
maxAsyncRetryAttempts: 3
timeout: 10
44 changes: 44 additions & 0 deletions __tests__/e2e/nodejs/s2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
edition: 3.0.0 # 命令行YAML规范版本,遵循语义化版本(Semantic Versioning)规范
name: test-node-app # 项目名称
access: quanxi

vars:
region: ${env('REGION', 'cn-huhehaote')}

resources:
fcDemo: # 业务名称/模块名称
component: ${env('fc_component_version', path('../../../'))}
props: # 组件的属性值
region: ${vars.region}
functionName: fc3-event-part-${env('fc_component_function_name', 'nodejs14')}
runtime: ${env('fc_component_runtime', 'nodejs14')}
code: ./code
handler: event.handler
memorySize: 128
timeout: 30
instanceLifecycleConfig:
initializer:
handler: event.initializer
timeout: 10

triggers:
- triggerName: httpTrigger # 触发器名称
triggerType: http # 触发器类型
description: 'xxxx'
qualifier: test # 触发服务的版本
triggerConfig:
authType: anonymous # 鉴权类型,可选值:anonymous、function
disableURLInternet: false # 是否禁用公网访问 URL
methods: # HTTP 触发器支持的访问方法,可选值:GET、POST、PUT、DELETE、HEAD
- GET
- POST

asyncInvokeConfig:
destinationConfig:
onFailure:
destination: acs:mns:${vars.region}::/topics/serverless-devs-fc3-ci-test/messages
onSuccess:
destination: acs:fc:${vars.region}::functions/serverless-devs-ci-async-invoke-config-succ
maxAsyncEventAgeInSeconds: 360
maxAsyncRetryAttempts: 3
qualifier: test
17 changes: 17 additions & 0 deletions __tests__/e2e/nodejs/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#! /bin/bash
# s remove -y -t s2.yaml
s deploy --function -t s2.yaml
versionId=$(s version publish -t s2.yaml --silent -o json | jq -r '."versionId"')
echo "latest version = $versionId"
if [[ "$versionId" -gt 1 ]]; then
mainVersion=$((versionId - 1))
echo "main version = $mainVersion"
s alias publish --alias-name test --version-id $mainVersion --vw "{\"$versionId\": 0.2}" -t s2.yaml
else
s alias publish --alias-name test --version-id $versionId -t s2.yaml
fi

s deploy --trigger -t s2.yaml
s deploy --async-invoke-config -t s2.yaml
s info -t s2.yaml
s alias list -t s2.yaml
1 change: 1 addition & 0 deletions src/interface/async_invoke_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface IAsyncInvokeConfig {
destinationConfig?: IDestinationConfig;
maxAsyncEventAgeInSeconds?: number;
maxAsyncRetryAttempts?: number;
qualifier?: string;
}
18 changes: 13 additions & 5 deletions src/subCommands/deploy/impl/async_invoke_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ export default class AsyncInvokeConfig extends Base {
super(inputs, opts.yes);
this.functionName = inputs.props?.functionName;

this.local = _.cloneDeep(_.get(inputs, 'props.asyncInvokeConfig', {})) as IAsyncInvokeConfig;
logger.debug(`need deploy asyncInvokeConfig: ${JSON.stringify(this.local)}`);
const asyncInvokeConfig = _.get(inputs, 'props.asyncInvokeConfig', {});
this.local = _.cloneDeep(asyncInvokeConfig) as IAsyncInvokeConfig;
logger.debug(`need deploy asyncInvokeConfig: ${JSON.stringify(asyncInvokeConfig)}`);
}

async before() {
Expand All @@ -35,16 +36,18 @@ export default class AsyncInvokeConfig extends Base {
const localConfig = this.local;

const id = `${this.functionName}/asyncInvokeConfig`;
const asyncInvokeConfig = _.get(this.inputs, 'props.asyncInvokeConfig', {});
const qualifier = _.get(asyncInvokeConfig, 'qualifier', 'LATEST');
if (!_.isEmpty(localConfig)) {
localConfig.destinationConfig = localConfig.destinationConfig || {};
if (this.needDeploy) {
await this.fcSdk.putAsyncInvokeConfig(this.functionName, 'LATEST', localConfig);
await this.fcSdk.putAsyncInvokeConfig(this.functionName, qualifier, localConfig);
} else if (_.isEmpty(remoteConfig)) {
// 如果不需要部署,但是远端资源不存在,则尝试创建一下
logger.debug(
`Online asyncInvokeConfig does not exist, specified not to deploy, attempting to create ${id}`,
);
await this.fcSdk.putAsyncInvokeConfig(this.functionName, 'LATEST', localConfig);
await this.fcSdk.putAsyncInvokeConfig(this.functionName, qualifier, localConfig);
} else {
logger.debug(
`Online asyncInvokeConfig exists, specified not to deploy, skipping deployment ${id}`,
Expand All @@ -55,12 +58,17 @@ export default class AsyncInvokeConfig extends Base {
}

private async getRemote() {
const asyncInvokeConfig = _.get(this.inputs, 'props.asyncInvokeConfig', {});
const qualifier = _.get(asyncInvokeConfig, 'qualifier', 'LATEST');
try {
const result = await this.fcSdk.getAsyncInvokeConfig(
this.functionName,
'LATEST',
qualifier,
GetApiType.simpleUnsupported,
);
if (result) {
result.qualifier = qualifier;
}
this.remote = result;
} catch (ex) {
logger.debug(`Get remote asyncInvokeConfig of ${this.functionName} error: ${ex.message}`);
Expand Down
16 changes: 12 additions & 4 deletions src/subCommands/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,34 @@ export default class Deploy {
alias: {
'assume-yes': 'y',
},
boolean: ['skip-push'],
boolean: ['skip-push', 'async_invoke_config'],
});

// TODO: 更完善的验证
verify(inputs.props);

const { function: type, trigger, 'assume-yes': yes, 'skip-push': skipPush } = this.opts;
const {
function: type,
trigger,
'async-invoke-config': async_invoke_config,
'assume-yes': yes,
'skip-push': skipPush,
} = this.opts;
logger.debug('parse argv:');
logger.debug(this.opts);
// 初始化部署实例
const deployAll = !type && !trigger;
const deployAll = !type && !trigger && !async_invoke_config;
logger.debug(`Deploy all resources: ${deployAll}`);
if (deployAll || type) {
this.service = new Service(inputs, { type, yes, skipPush }); // function
}
if (deployAll || trigger) {
this.trigger = new Trigger(inputs, { yes, trigger });
}
if (deployAll) {
if (deployAll || async_invoke_config) {
this.asyncInvokeConfig = new AsyncInvokeConfig(inputs, { yes });
}
if (deployAll) {
this.vpcBinding = new VpcBinding(inputs, { yes });
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/subCommands/info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,17 @@ export default class Info {
return {};
}
try {
return await this.fcSdk.getAsyncInvokeConfig(this.functionName, 'LATEST', this.getApiType);
const asyncInvokeConfig = _.get(this.inputs, 'props.asyncInvokeConfig', {});
const qualifier = _.get(asyncInvokeConfig, 'qualifier', 'LATEST');
const result = await this.fcSdk.getAsyncInvokeConfig(
this.functionName,
qualifier,
this.getApiType,
);
if (result) {
result.qualifier = qualifier;
}
return result;
} catch (ex) {
logger.debug(`Get AsyncInvokeConfig ${this.functionName} error: ${ex}`);
return {
Expand Down
12 changes: 10 additions & 2 deletions src/subCommands/plan/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,17 @@ export default class Plan {

private async planAsyncInvokeConfig() {
let remote = {};
const asyncInvokeConfig = _.get(this.inputs, 'props.asyncInvokeConfig', {});
const qualifier = _.get(asyncInvokeConfig, 'qualifier', 'LATEST');
try {
const result = await this.fcSdk.getAsyncInvokeConfig(
this.functionName,
'LATEST',
qualifier,
GetApiType.simpleUnsupported,
);
if (result) {
result.qualifier = qualifier;
}
remote = result;
} catch (ex) {
logger.debug(
Expand All @@ -140,7 +145,10 @@ export default class Plan {
}

// eslint-disable-next-line prefer-const
let local = _.cloneDeep(_.get(this.inputs.props, 'asyncInvokeConfig', {}));
let local = _.cloneDeep(_.get(this.inputs.props, 'asyncInvokeConfig', {} as any));
if (local) {
local.qualifier = qualifier;
}
return diffConvertPlanYaml(remote, local, { deep: 1, complete: true });
}

Expand Down
Loading

0 comments on commit 344c32e

Please sign in to comment.