Skip to content

Commit

Permalink
Merge pull request #46 from devsapp/create-support-tags
Browse files Browse the repository at this point in the history
fix:tags update or delete
  • Loading branch information
rsonghuster authored Feb 8, 2025
2 parents 2e9524e + 13113fe commit bb67aec
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 11 deletions.
7 changes: 7 additions & 0 deletions __tests__/e2e/ci-mac-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ s invoke -e '{"hello":"fc nodejs with more vpc and nas auto"}' -t ./s_lock_auto.
s info -y -t ./s_lock_auto.yaml
s remove -y -t ./s_lock_auto.yaml

echo "test nodejs runtime with tags ..."
export fc_component_function_name=nodejs16-$(uname)-$(uname -m)-$RANDSTR
s deploy -y -t ./s_tags.yaml
s deploy -y -t ./s_tags2.yaml
s deploy -y -t ./s_tags3.yaml
s remove -y -t ./s_tags3.yaml

echo "test deploy with alias"
export fc_component_function_name=nodejs18-$(uname)-$(uname -m)-$RANDSTR
s deploy --function -t s2.yaml
Expand Down
9 changes: 9 additions & 0 deletions __tests__/e2e/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ s info -y -t ./s_lock_auto.yaml
s remove -y -t ./s_lock_auto.yaml
cd ..

echo "test nodejs runtime with tags ..."
cd nodejs
export fc_component_function_name=nodejs18-$(uname)-$(uname -m)-$RANDSTR
s deploy -y -t ./s_tags.yaml
s deploy -y -t ./s_tags2.yaml
s deploy -y -t ./s_tags3.yaml
s remove -y -t ./s_tags3.yaml
cd ..

echo "test deploy with alias"
cd nodejs
export fc_component_function_name=nodejs18-$(uname)-$(uname -m)-$RANDSTR
Expand Down
6 changes: 0 additions & 6 deletions __tests__/e2e/nodejs/s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ resources:
concurrencyConfig:
reservedConcurrency: 1

tags:
- key: test1
value: value1
- key: test2
value: value2

customDomain:
domainName: auto
protocol: HTTP
Expand Down
5 changes: 1 addition & 4 deletions __tests__/e2e/nodejs/s2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,4 @@ resources:
destination: acs:fc:${vars.region}::functions/serverless-devs-ci-async-invoke-config-succ
maxAsyncEventAgeInSeconds: 360
maxAsyncRetryAttempts: 3
qualifier: test
tags:
- key: test
value: test_value
qualifier: test
21 changes: 21 additions & 0 deletions __tests__/e2e/nodejs/s_tags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
edition: 3.0.0
name: test-node-app
access: quanxi

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

resources:
fcDemo1: # 业务名称/模块名称
component: ${env('fc_component_version', path('../../../'))}
props: # 组件的属性值
region: ${vars.region}
functionName: fc3-event-${env('fc_component_function_name', 'nodejs18')}-tags
runtime: ${env('fc_component_runtime', 'nodejs18')}
code: ./test-auto-code
handler: index.handler
memorySize: 128
timeout: 30
tags:
- Key: test
Value: value
23 changes: 23 additions & 0 deletions __tests__/e2e/nodejs/s_tags2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
edition: 3.0.0
name: test-node-app
access: quanxi

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

resources:
fcDemo1: # 业务名称/模块名称
component: ${env('fc_component_version', path('../../../'))}
props: # 组件的属性值
region: ${vars.region}
functionName: fc3-event-${env('fc_component_function_name', 'nodejs18')}-tags
runtime: ${env('fc_component_runtime', 'nodejs18')}
code: ./test-auto-code
handler: index.handler
memorySize: 128
timeout: 30
tags:
- Key: test
Value: value
- Key: test1
Value: value1
18 changes: 18 additions & 0 deletions __tests__/e2e/nodejs/s_tags3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
edition: 3.0.0
name: test-node-app
access: quanxi

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

resources:
fcDemo1: # 业务名称/模块名称
component: ${env('fc_component_version', path('../../../'))}
props: # 组件的属性值
region: ${vars.region}
functionName: fc3-event-${env('fc_component_function_name', 'nodejs18')}-tags
runtime: ${env('fc_component_runtime', 'nodejs18')}
code: ./test-auto-code
handler: index.handler
memorySize: 128
timeout: 30
76 changes: 75 additions & 1 deletion src/resources/fc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
GetAsyncInvokeConfigRequest,
ListInstancesRequest,
ListAsyncInvokeConfigsRequest,
TagResourcesRequest,
UntagResourcesRequest,
} from '@alicloud/fc20230330';
import { RuntimeOptions } from '@alicloud/tea-util';

Expand Down Expand Up @@ -126,8 +128,9 @@ export default class FC extends FC_Client {
async deployFunction(config: IFunction, { slsAuto, type }): Promise<void> {
logger.debug(`Deploy function use config:\n${JSON.stringify(config, null, 2)}`);
let needUpdate = false;
let remoteConfig = null;
try {
await this.getFunction(config.functionName);
remoteConfig = await this.getFunction(config.functionName);
needUpdate = true;
} catch (err) {
if (err.code !== FC_API_ERROR_CODE.FunctionNotFound) {
Expand All @@ -145,6 +148,10 @@ export default class FC extends FC_Client {
const calculateRetryTime = (minute: number) =>
new Date().getTime() - startTime > minute * 60 * 1000;

if (config?.tags) {
config.tags = this.tagsToLowerCase(config?.tags);
}

while (true) {
try {
if (!needUpdate) {
Expand Down Expand Up @@ -176,6 +183,43 @@ export default class FC extends FC_Client {
});
}

/*
* 如果tags有更新,则需要更新tags
* 判断tags的长度是否相等,如果不相等,则需要更新tags
* 如果tags的长度相等,则比较tags的值是否相等,如果不相等,则需要更新tags
*/
const remoteTags = remoteConfig?.body?.tags;
const localTags = config.tags;
if (this.isUpdateTags(remoteTags, localTags)) {
logger.debug(`Updating tags for function ${config.functionName}`);
/*
* 如果远程tags不为空,则先删除远程tags(当前 sdk 只是设置 tags)
* 然后再添加本地tags
*/
if (remoteTags?.length) {
const remoteTagsKey = remoteTags.map((item) => item.key);
const untagResourcesRequest = new UntagResourcesRequest({
resourceId: [remoteConfig?.body?.functionArn],
resourceType: 'function',
tagKey: remoteTagsKey,
});
await this.fc20230330Client.untagResources(untagResourcesRequest);
}
/*
* 如果本地tags为空,则不设置tags
*/
if (localTags?.length) {
const tagResourcesRequest = new TagResourcesRequest({
body: {
resourceId: [remoteConfig?.body?.functionArn],
resourceType: 'FUNCTION',
tag: localTags,
},
});
await this.fc20230330Client.tagResources(tagResourcesRequest);
}
}

logger.debug(`Need update function ${config.functionName}`);
if (type === 'code') {
// eslint-disable-next-line no-param-reassign
Expand Down Expand Up @@ -812,4 +856,34 @@ export default class FC extends FC_Client {
});
});
}

tagsToLowerCase(tags) {
if (!tags) {
return [];
}
const newTags = [];
for (const tag of tags) {
const newTag = {};
for (const key in tag) {
if (Object.prototype.hasOwnProperty.call(tag, key)) {
const lowerCaseKey = key.toLowerCase();
newTag[lowerCaseKey] = tag[key];
}
}
newTags.push(newTag);
}
return newTags;
}

isUpdateTags(remoteTags, localTags) {
if (remoteTags?.length !== localTags?.length) {
return true;
}
const noNeedUpdate = remoteTags?.every((item) => {
return localTags?.some((localItem) => {
return localItem.key === item.key && localItem.value === item.value;
});
});
return !noNeedUpdate;
}
}

0 comments on commit bb67aec

Please sign in to comment.