@geek-fun/serverlessinsight 0.8.3 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.serverlessinsight/state-cdn-flow-app-cdn-flow-service.json.backup +3 -2
- package/.serverlessinsight/state-iam-test-app-iam-test-service.json.backup +21 -2
- package/.serverlessinsight/state-insight-poc-deploy-app-insight-poc-deploy.json.backup +44 -33
- package/.serverlessinsight/state-insight-poc-destroy-app-insight-poc-destroy.json.backup +3 -2
- package/.serverlessinsight/state-insight-volc-app-insight-volc.json.backup +63 -23
- package/dist/package.json +3 -2
- package/dist/src/common/aliyunClient/apigwOperations.js +36 -0
- package/dist/src/common/aliyunClient/slsOperations.js +34 -2
- package/dist/src/common/constants.js +5 -1
- package/dist/src/common/stateBackend/remoteStateBackend.js +4 -1
- package/dist/src/common/stateManager.js +52 -2
- package/dist/src/common/tencentClient/clsOperations.js +137 -0
- package/dist/src/common/tencentClient/index.js +21 -1
- package/dist/src/common/tencentClient/scfOperations.js +11 -0
- package/dist/src/common/volcengineClient/iamOperations.js +33 -0
- package/dist/src/common/volcengineClient/tlsOperations.js +84 -1
- package/dist/src/lang/en.js +23 -1
- package/dist/src/lang/zh-CN.js +23 -1
- package/dist/src/stack/aliyunStack/apigwPlanner.js +2 -0
- package/dist/src/stack/aliyunStack/apigwResource.js +118 -1
- package/dist/src/stack/aliyunStack/apigwTypes.js +19 -1
- package/dist/src/stack/aliyunStack/destroyer.js +25 -0
- package/dist/src/stack/aliyunStack/fc3Resource.js +91 -32
- package/dist/src/stack/aliyunStack/sharedLogProject.js +137 -0
- package/dist/src/stack/ownershipTag.js +19 -1
- package/dist/src/stack/scfStack/destroyer.js +13 -0
- package/dist/src/stack/scfStack/scfPlanner.js +9 -1
- package/dist/src/stack/scfStack/scfResource.js +205 -13
- package/dist/src/stack/scfStack/scfTypes.js +6 -0
- package/dist/src/stack/scfStack/sharedLogset.js +126 -0
- package/dist/src/stack/volcengineStack/apigwResource.js +64 -48
- package/dist/src/stack/volcengineStack/apigwTypes.js +9 -0
- package/dist/src/stack/volcengineStack/destroyer.js +12 -0
- package/dist/src/stack/volcengineStack/sharedLogProject.js +190 -0
- package/dist/src/stack/volcengineStack/vefaasPlanner.js +20 -13
- package/dist/src/stack/volcengineStack/vefaasResource.js +76 -76
- package/dist/src/types/domains/state.js +3 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isOwnedByStack = exports.parseOwnershipTagValue = exports.buildOwnershipTagValue = exports.OWNERSHIP_TAG_KEY = void 0;
|
|
3
|
+
exports.isOwnedByApp = exports.buildSharedOwnershipTagValue = exports.SHARED_SCOPE = exports.isOwnedByStack = exports.parseOwnershipTagValue = exports.buildOwnershipTagValue = exports.OWNERSHIP_TAG_KEY = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Ownership tag stamped on provider resources at create time so a deploy can
|
|
6
6
|
* idempotently adopt a resource that exists in the cloud but is missing from
|
|
@@ -38,3 +38,21 @@ const isOwnedByStack = (context, logicalId, tags) => {
|
|
|
38
38
|
return parsed.stack === `${context.app}-${context.service}` && parsed.logicalId === logicalId;
|
|
39
39
|
};
|
|
40
40
|
exports.isOwnedByStack = isOwnedByStack;
|
|
41
|
+
/**
|
|
42
|
+
* App-scoped ownership for resources shared across services of one app
|
|
43
|
+
* (issue #214 unified log container). Exact full-value match by design:
|
|
44
|
+
* per-service stacks have no claim on shared resources, so only the
|
|
45
|
+
* owning app may adopt them.
|
|
46
|
+
*/
|
|
47
|
+
exports.SHARED_SCOPE = 'shared';
|
|
48
|
+
const buildSharedOwnershipTagValue = (app, logicalId) => `${app}:${exports.SHARED_SCOPE}:${logicalId}`;
|
|
49
|
+
exports.buildSharedOwnershipTagValue = buildSharedOwnershipTagValue;
|
|
50
|
+
const isOwnedByApp = (app, logicalId, tags) => {
|
|
51
|
+
if (!tags)
|
|
52
|
+
return false;
|
|
53
|
+
const tag = tags.find((t) => t.Key === exports.OWNERSHIP_TAG_KEY);
|
|
54
|
+
if (!tag?.Value)
|
|
55
|
+
return false;
|
|
56
|
+
return tag.Value === (0, exports.buildSharedOwnershipTagValue)(app, logicalId);
|
|
57
|
+
};
|
|
58
|
+
exports.isOwnedByApp = isOwnedByApp;
|
|
@@ -11,6 +11,9 @@ const tdsqlcPlanner_1 = require("./tdsqlcPlanner");
|
|
|
11
11
|
const tdsqlcExecutor_1 = require("./tdsqlcExecutor");
|
|
12
12
|
const esServerlessPlanner_1 = require("./esServerlessPlanner");
|
|
13
13
|
const esServerlessExecutor_1 = require("./esServerlessExecutor");
|
|
14
|
+
const tencentClient_1 = require("../../common/tencentClient");
|
|
15
|
+
const stateManager_1 = require("../../common/stateManager");
|
|
16
|
+
const sharedLogset_1 = require("./sharedLogset");
|
|
14
17
|
const createSaveStateFn = (backend, app, service, stage) => async (state) => {
|
|
15
18
|
await backend.saveState(state, app, service, stage);
|
|
16
19
|
};
|
|
@@ -86,6 +89,16 @@ const destroyTencentStack = async (backend) => {
|
|
|
86
89
|
],
|
|
87
90
|
});
|
|
88
91
|
}
|
|
92
|
+
// Release the shared CLS logset once no topics reference it anymore. The
|
|
93
|
+
// per-function teardowns above deleted their own topics, so a logset with
|
|
94
|
+
// zero remaining topics is safe to delete.
|
|
95
|
+
const sharedLogset = (0, stateManager_1.getSharedResource)(state, context.stage, sharedLogset_1.SHARED_LOGSET_KEY);
|
|
96
|
+
if (sharedLogset) {
|
|
97
|
+
const releaseResult = await (0, sharedLogset_1.releaseSharedLogsetIfUnused)(context, (0, tencentClient_1.createTencentClient)(context), sharedLogset);
|
|
98
|
+
if (releaseResult === 'deleted') {
|
|
99
|
+
state = (0, stateManager_1.removeSharedResource)(state, context.stage, sharedLogset_1.SHARED_LOGSET_KEY);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
89
102
|
await backend.saveState(state, context.app, context.service, context.stage);
|
|
90
103
|
};
|
|
91
104
|
exports.destroyTencentStack = destroyTencentStack;
|
|
@@ -6,6 +6,7 @@ const scfTypes_1 = require("./scfTypes");
|
|
|
6
6
|
const stateManager_1 = require("../../common/stateManager");
|
|
7
7
|
const hashUtils_1 = require("../../common/hashUtils");
|
|
8
8
|
const ownershipTag_1 = require("../ownershipTag");
|
|
9
|
+
const sharedLogset_1 = require("./sharedLogset");
|
|
9
10
|
const planFunctionDeletion = (logicalId, definition) => ({
|
|
10
11
|
logicalId,
|
|
11
12
|
action: 'delete',
|
|
@@ -24,7 +25,14 @@ const generateFunctionPlan = async (context, state, functions) => {
|
|
|
24
25
|
const functionItems = await Promise.all(functions.map(async (fn) => {
|
|
25
26
|
const logicalId = `functions.${fn.key}`;
|
|
26
27
|
const currentState = (0, stateManager_1.getResource)(state, logicalId);
|
|
27
|
-
|
|
28
|
+
let config = (0, scfTypes_1.functionToScfConfig)(fn);
|
|
29
|
+
if (fn.log) {
|
|
30
|
+
config = {
|
|
31
|
+
...config,
|
|
32
|
+
ClsLogsetName: (0, sharedLogset_1.buildSharedLogsetName)(context.app, context.stage),
|
|
33
|
+
ClsTopicName: (0, sharedLogset_1.buildFunctionTopicName)(context),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
28
36
|
const codePath = fn.code.path;
|
|
29
37
|
const desiredCodeHash = await (0, hashUtils_1.computeZipContentHash)(codePath);
|
|
30
38
|
const desiredDefinition = (0, scfTypes_1.extractScfDefinition)(config, desiredCodeHash, fn.iam);
|
|
@@ -13,6 +13,7 @@ const logger_1 = require("../../common/logger");
|
|
|
13
13
|
const lang_1 = require("../../lang");
|
|
14
14
|
const ownershipTag_1 = require("../ownershipTag");
|
|
15
15
|
const alreadyExists_1 = require("../alreadyExists");
|
|
16
|
+
const sharedLogset_1 = require("./sharedLogset");
|
|
16
17
|
/**
|
|
17
18
|
* Build the Function URL trigger description for Tencent CreateTrigger
|
|
18
19
|
* (Type: 'http'). Format per official docs (创建函数 URL / Creating a Function
|
|
@@ -88,6 +89,16 @@ const isRecoverableCreateError = (error) => {
|
|
|
88
89
|
message.includes('econnreset') ||
|
|
89
90
|
message.includes('etimedout'));
|
|
90
91
|
};
|
|
92
|
+
// CLS destination drift is only meaningful when the desired config carries one —
|
|
93
|
+
// a log-enabled function whose stable destination names differ from state must
|
|
94
|
+
// re-push configuration so UpdateFunctionConfiguration rebinds the function.
|
|
95
|
+
const isLogConfigChanged = (desiredDefinition, existingDefinition) => {
|
|
96
|
+
const desiredLogConfig = desiredDefinition.logConfig;
|
|
97
|
+
if (!desiredLogConfig) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
return !(0, common_1.attributesEqual)(desiredLogConfig, existingDefinition.logConfig ?? {});
|
|
101
|
+
};
|
|
91
102
|
const buildScfInstanceFromProvider = (info, sid) => {
|
|
92
103
|
const envMap = info.Environment?.Variables?.reduce((acc, v) => ({ ...acc, [v.Key]: v.Value }), {}) ?? {};
|
|
93
104
|
const triggersArray = info.Triggers?.map((t) => ({
|
|
@@ -258,19 +269,73 @@ const buildScfInstanceFromProvider = (info, sid) => {
|
|
|
258
269
|
: {},
|
|
259
270
|
};
|
|
260
271
|
};
|
|
261
|
-
const createDependentResources = async (context, fn, existingInstances = []) => {
|
|
272
|
+
const createDependentResources = async (context, fn, existingInstances = [], state) => {
|
|
262
273
|
const client = (0, tencentClient_1.createTencentClient)(context);
|
|
274
|
+
const clsInstances = [];
|
|
275
|
+
let clsConfig;
|
|
276
|
+
let sharedInstance;
|
|
277
|
+
if (fn.log) {
|
|
278
|
+
const topicInstance = existingInstances.find((i) => i.type === types_1.ResourceTypeEnum.TENCENT_CLS_TOPIC);
|
|
279
|
+
const logsetInstance = existingInstances.find((i) => i.type === types_1.ResourceTypeEnum.TENCENT_CLS_LOGSET);
|
|
280
|
+
if (topicInstance && logsetInstance) {
|
|
281
|
+
clsConfig = {
|
|
282
|
+
logsetId: logsetInstance.logsetId ?? '',
|
|
283
|
+
logsetName: logsetInstance.id,
|
|
284
|
+
topicId: topicInstance.id,
|
|
285
|
+
topicName: topicInstance.topicName ?? topicInstance.id,
|
|
286
|
+
};
|
|
287
|
+
clsInstances.push(logsetInstance, topicInstance);
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
const legacyFn = existingInstances.find((i) => i.type === undefined);
|
|
291
|
+
if (legacyFn?.clsLogsetId && legacyFn?.clsTopicId) {
|
|
292
|
+
clsConfig = {
|
|
293
|
+
logsetId: legacyFn.clsLogsetId,
|
|
294
|
+
logsetName: (0, sharedLogset_1.buildSharedLogsetName)(context.app, context.stage),
|
|
295
|
+
topicId: legacyFn.clsTopicId,
|
|
296
|
+
topicName: (0, sharedLogset_1.buildFunctionTopicName)(context),
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
const shared = await (0, sharedLogset_1.ensureSharedLogset)(context, client, state);
|
|
301
|
+
sharedInstance = (0, sharedLogset_1.buildSharedLogsetResourceState)(context, shared);
|
|
302
|
+
const topic = await (0, sharedLogset_1.ensureFunctionTopic)(context, client, {
|
|
303
|
+
logsetId: shared.logsetId,
|
|
304
|
+
logsetName: shared.logsetName,
|
|
305
|
+
topicName: (0, sharedLogset_1.buildFunctionTopicName)(context),
|
|
306
|
+
logicalId: `functions.${fn.key}`,
|
|
307
|
+
});
|
|
308
|
+
clsInstances.push({
|
|
309
|
+
sid: (0, common_1.buildSid)('tencent', 'cls-logset', context.stage, shared.logsetName),
|
|
310
|
+
type: types_1.ResourceTypeEnum.TENCENT_CLS_LOGSET,
|
|
311
|
+
id: shared.logsetName,
|
|
312
|
+
logsetId: shared.logsetId,
|
|
313
|
+
}, {
|
|
314
|
+
sid: (0, common_1.buildSid)('tencent', 'cls-topic', context.stage, topic.topicName),
|
|
315
|
+
type: types_1.ResourceTypeEnum.TENCENT_CLS_TOPIC,
|
|
316
|
+
id: topic.topicId,
|
|
317
|
+
topicName: topic.topicName,
|
|
318
|
+
});
|
|
319
|
+
clsConfig = {
|
|
320
|
+
logsetId: shared.logsetId,
|
|
321
|
+
logsetName: shared.logsetName,
|
|
322
|
+
topicId: topic.topicId,
|
|
323
|
+
topicName: topic.topicName,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
263
328
|
const iamConfig = fn.iam?.role;
|
|
264
329
|
// No IAM role configured - skip
|
|
265
330
|
if (!iamConfig) {
|
|
266
|
-
return {};
|
|
331
|
+
return { clsConfig, clsInstances, sharedInstance };
|
|
267
332
|
}
|
|
268
333
|
const statements = iamConfig && typeof iamConfig !== 'string' ? iamConfig.statements : undefined;
|
|
269
334
|
const managedPolicies = iamConfig && typeof iamConfig !== 'string' ? iamConfig.managed_policies : undefined;
|
|
270
335
|
const customRoleName = iamConfig && typeof iamConfig !== 'string' ? iamConfig.name : undefined;
|
|
271
336
|
// External role (string) - skip creation, use ARN directly
|
|
272
337
|
if (typeof iamConfig === 'string') {
|
|
273
|
-
return { role: { roleName: '', arn: iamConfig } };
|
|
338
|
+
return { role: { roleName: '', arn: iamConfig }, clsConfig, clsInstances, sharedInstance };
|
|
274
339
|
}
|
|
275
340
|
const hasCamRole = existingInstances.some((i) => i.type === 'TENCENT_SCF_ROLE');
|
|
276
341
|
const serviceName = `${context.app}-${context.service}`;
|
|
@@ -278,14 +343,19 @@ const createDependentResources = async (context, fn, existingInstances = []) =>
|
|
|
278
343
|
const roleName = customRoleName ?? defaultRoleName;
|
|
279
344
|
if (hasCamRole) {
|
|
280
345
|
// Role already exists - reuse it
|
|
281
|
-
return { role: { roleName, arn: roleName } };
|
|
346
|
+
return { role: { roleName, arn: roleName }, clsConfig, clsInstances, sharedInstance };
|
|
282
347
|
}
|
|
283
348
|
// Create new CAM role
|
|
284
349
|
const trustedServices = ['scf.qcloud.com'];
|
|
285
350
|
logger_1.logger.info(lang_1.lang.__('CREATING_RAM_ROLE', { roleName }));
|
|
286
351
|
const camRole = await client.cam.createRole(roleName, trustedServices, `ServerlessInsight SCF execution role for ${serviceName}`, statements, managedPolicies);
|
|
287
352
|
await delay(constants_1.RAM_ROLE_PROPAGATION_DELAY_MS);
|
|
288
|
-
return {
|
|
353
|
+
return {
|
|
354
|
+
role: { roleName: camRole.roleName, arn: camRole.roleArn ?? roleName },
|
|
355
|
+
clsConfig,
|
|
356
|
+
clsInstances,
|
|
357
|
+
sharedInstance,
|
|
358
|
+
};
|
|
289
359
|
};
|
|
290
360
|
const deleteDependentResources = async (context, instances) => {
|
|
291
361
|
const client = (0, tencentClient_1.createTencentClient)(context);
|
|
@@ -298,6 +368,15 @@ const deleteDependentResources = async (context, instances) => {
|
|
|
298
368
|
await client.cam.deleteRole(instance.id);
|
|
299
369
|
break;
|
|
300
370
|
}
|
|
371
|
+
case types_1.ResourceTypeEnum.TENCENT_CLS_TOPIC: {
|
|
372
|
+
logger_1.logger.info(lang_1.lang.__('DELETING_CLS_TOPIC', { id: instance.id }));
|
|
373
|
+
await client.cls.deleteTopic(instance.id);
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
case types_1.ResourceTypeEnum.TENCENT_CLS_LOGSET: {
|
|
377
|
+
// The shared CLS logset is released by the destroyer once no topics remain.
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
301
380
|
default:
|
|
302
381
|
logger_1.logger.warn(lang_1.lang.__('UNKNOWN_RESOURCE_TYPE', { type: instance.type }));
|
|
303
382
|
}
|
|
@@ -311,8 +390,7 @@ const createResource = async (context, fn, state) => {
|
|
|
311
390
|
throw new Error(lang_1.lang.__('HTTP_TRIGGER_AUTH_TYPE_REQUIRED', { functionName: fn.name }));
|
|
312
391
|
}
|
|
313
392
|
const existingResourceState = (0, stateManager_1.getResource)(state, logicalId);
|
|
314
|
-
const
|
|
315
|
-
const dependentResources = await createDependentResources(context, fn, existingDependentInstances);
|
|
393
|
+
const dependentResources = await createDependentResources(context, fn, existingResourceState?.instances ?? [], state);
|
|
316
394
|
let config = (0, scfTypes_1.functionToScfConfig)(fn);
|
|
317
395
|
config.Tags = [{ Key: ownershipTag_1.OWNERSHIP_TAG_KEY, Value: (0, ownershipTag_1.buildOwnershipTagValue)(context, logicalId) }];
|
|
318
396
|
if (dependentResources.role) {
|
|
@@ -321,11 +399,20 @@ const createResource = async (context, fn, state) => {
|
|
|
321
399
|
Role: dependentResources.role.arn,
|
|
322
400
|
};
|
|
323
401
|
}
|
|
402
|
+
if (dependentResources.clsConfig) {
|
|
403
|
+
config = {
|
|
404
|
+
...config,
|
|
405
|
+
ClsLogsetId: dependentResources.clsConfig.logsetId,
|
|
406
|
+
ClsTopicId: dependentResources.clsConfig.topicId,
|
|
407
|
+
ClsLogsetName: dependentResources.clsConfig.logsetName,
|
|
408
|
+
ClsTopicName: dependentResources.clsConfig.topicName,
|
|
409
|
+
};
|
|
410
|
+
}
|
|
324
411
|
const codePath = fn.code.path;
|
|
325
412
|
const codeBase64 = (0, fileUtils_1.readFileAsBase64)(codePath);
|
|
326
413
|
const codeHash = await (0, hashUtils_1.computeZipContentHash)(codePath);
|
|
327
414
|
const definition = (0, scfTypes_1.extractScfDefinition)(config, codeHash, fn.iam);
|
|
328
|
-
const dependentInstances = [];
|
|
415
|
+
const dependentInstances = [...dependentResources.clsInstances];
|
|
329
416
|
if (dependentResources.role) {
|
|
330
417
|
const iamConfig = fn.iam?.role;
|
|
331
418
|
const isExternalRole = typeof iamConfig === 'string';
|
|
@@ -346,7 +433,10 @@ const createResource = async (context, fn, state) => {
|
|
|
346
433
|
lastUpdated: new Date().toISOString(),
|
|
347
434
|
status: 'tainted',
|
|
348
435
|
};
|
|
349
|
-
const
|
|
436
|
+
const stateWithShared = dependentResources.sharedInstance
|
|
437
|
+
? (0, stateManager_1.setSharedResource)(state, context.stage, sharedLogset_1.SHARED_LOGSET_KEY, dependentResources.sharedInstance)
|
|
438
|
+
: state;
|
|
439
|
+
const stateAfterDependents = (0, stateManager_1.setResource)(stateWithShared, logicalId, taintedResourceState);
|
|
350
440
|
const client = (0, tencentClient_1.createTencentClient)(context);
|
|
351
441
|
const isTainted = existingResourceState?.status === 'tainted';
|
|
352
442
|
const existingFunctionOnRetry = isTainted ? await client.scf.getFunction(fn.name) : null;
|
|
@@ -512,6 +602,14 @@ const updateResource = async (context, fn, state) => {
|
|
|
512
602
|
const existingState = (0, stateManager_1.getResource)(state, logicalId);
|
|
513
603
|
const existingInstances = (existingState?.instances ?? []);
|
|
514
604
|
const existingFnInstance = existingInstances.find((i) => i.type === undefined);
|
|
605
|
+
const existingClsTopicInstance = existingInstances.find((i) => i.type === types_1.ResourceTypeEnum.TENCENT_CLS_TOPIC);
|
|
606
|
+
const existingDefinition = (existingState?.definition ?? {});
|
|
607
|
+
const legacyLogsetId = existingFnInstance?.clsLogsetId;
|
|
608
|
+
const legacyTopicId = existingFnInstance?.clsTopicId;
|
|
609
|
+
// Log is bound via the unified flow (definition.logConfig) or legacy CLS ids
|
|
610
|
+
// stored directly on the function instance.
|
|
611
|
+
const wasLogEnabled = Boolean(existingDefinition.logConfig?.logset ||
|
|
612
|
+
(legacyLogsetId && legacyTopicId));
|
|
515
613
|
const hasCamRole = existingInstances.some((i) => i.type === types_1.ResourceTypeEnum.TENCENT_SCF_ROLE);
|
|
516
614
|
const client = (0, tencentClient_1.createTencentClient)(context);
|
|
517
615
|
const newIamRole = fn.iam?.role;
|
|
@@ -562,6 +660,69 @@ const updateResource = async (context, fn, state) => {
|
|
|
562
660
|
Role: role.arn,
|
|
563
661
|
};
|
|
564
662
|
}
|
|
663
|
+
let clsConfig;
|
|
664
|
+
let clsTopicInstance;
|
|
665
|
+
let clsSharedInstance;
|
|
666
|
+
const disableLog = !fn.log && wasLogEnabled;
|
|
667
|
+
let topicToDelete;
|
|
668
|
+
let topicNameToDelete;
|
|
669
|
+
let sharedLogsetReleased = false;
|
|
670
|
+
if (fn.log) {
|
|
671
|
+
if (legacyLogsetId && legacyTopicId) {
|
|
672
|
+
clsConfig = {
|
|
673
|
+
logsetId: legacyLogsetId,
|
|
674
|
+
logsetName: (0, sharedLogset_1.buildSharedLogsetName)(context.app, context.stage),
|
|
675
|
+
topicId: legacyTopicId,
|
|
676
|
+
topicName: (0, sharedLogset_1.buildFunctionTopicName)(context),
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
else {
|
|
680
|
+
const sharedBefore = (0, stateManager_1.getSharedResource)(state, context.stage, sharedLogset_1.SHARED_LOGSET_KEY);
|
|
681
|
+
const logset = await (0, sharedLogset_1.ensureSharedLogset)(context, client, state);
|
|
682
|
+
if (!sharedBefore) {
|
|
683
|
+
clsSharedInstance = (0, sharedLogset_1.buildSharedLogsetResourceState)(context, logset);
|
|
684
|
+
}
|
|
685
|
+
const topic = await (0, sharedLogset_1.ensureFunctionTopic)(context, client, {
|
|
686
|
+
logsetId: logset.logsetId,
|
|
687
|
+
logsetName: logset.logsetName,
|
|
688
|
+
topicName: (0, sharedLogset_1.buildFunctionTopicName)(context),
|
|
689
|
+
logicalId,
|
|
690
|
+
});
|
|
691
|
+
clsConfig = {
|
|
692
|
+
logsetId: logset.logsetId,
|
|
693
|
+
logsetName: logset.logsetName,
|
|
694
|
+
topicId: topic.topicId,
|
|
695
|
+
topicName: topic.topicName,
|
|
696
|
+
};
|
|
697
|
+
clsTopicInstance = {
|
|
698
|
+
sid: (0, common_1.buildSid)('tencent', 'cls-topic', context.stage, topic.topicName),
|
|
699
|
+
type: types_1.ResourceTypeEnum.TENCENT_CLS_TOPIC,
|
|
700
|
+
id: topic.topicId,
|
|
701
|
+
topicName: topic.topicName,
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
else if (disableLog) {
|
|
706
|
+
// Log was enabled and is now disabled: clear the CLS binding so
|
|
707
|
+
// UpdateFunctionConfiguration unbinds the function, and tear down the
|
|
708
|
+
// function's CLS topic (index first, then topic) below.
|
|
709
|
+
config = {
|
|
710
|
+
...config,
|
|
711
|
+
ClsLogsetId: '',
|
|
712
|
+
ClsTopicId: '',
|
|
713
|
+
};
|
|
714
|
+
topicToDelete = existingClsTopicInstance?.id ?? legacyTopicId;
|
|
715
|
+
topicNameToDelete = existingClsTopicInstance?.topicName ?? (0, sharedLogset_1.buildFunctionTopicName)(context);
|
|
716
|
+
}
|
|
717
|
+
if (clsConfig) {
|
|
718
|
+
config = {
|
|
719
|
+
...config,
|
|
720
|
+
ClsLogsetId: clsConfig.logsetId,
|
|
721
|
+
ClsTopicId: clsConfig.topicId,
|
|
722
|
+
ClsLogsetName: clsConfig.logsetName,
|
|
723
|
+
ClsTopicName: clsConfig.topicName,
|
|
724
|
+
};
|
|
725
|
+
}
|
|
565
726
|
const codePath = fn.code.path;
|
|
566
727
|
const codeBase64 = (0, fileUtils_1.readFileAsBase64)(codePath);
|
|
567
728
|
const codeHash = await (0, hashUtils_1.computeZipContentHash)(codePath);
|
|
@@ -570,7 +731,6 @@ const updateResource = async (context, fn, state) => {
|
|
|
570
731
|
// creation) and we don't want to re-send unchanged values. Handler/Runtime
|
|
571
732
|
// changes are a hard error: the platform cannot apply them on update.
|
|
572
733
|
const desiredDefinition = (0, scfTypes_1.extractScfDefinition)(config, codeHash, fn.iam);
|
|
573
|
-
const existingDefinition = (existingState?.definition ?? {});
|
|
574
734
|
const CONFIG_DIFF_KEYS = ['runtime', 'handler', 'memorySize', 'timeout', 'environment', 'role'];
|
|
575
735
|
const mutableKeys = CONFIG_DIFF_KEYS.filter((k) => k !== 'runtime' && k !== 'handler');
|
|
576
736
|
if (desiredDefinition.handler &&
|
|
@@ -583,10 +743,31 @@ const updateResource = async (context, fn, state) => {
|
|
|
583
743
|
desiredDefinition.runtime !== existingDefinition.runtime) {
|
|
584
744
|
throw new Error(`Runtime is immutable in Tencent SCF and cannot be changed on update (${String(existingDefinition.runtime)} -> ${String(desiredDefinition.runtime)}). Delete and recreate the function instead.`);
|
|
585
745
|
}
|
|
586
|
-
const configChanged = mutableKeys.some((k) => desiredDefinition[k] !== existingDefinition[k])
|
|
746
|
+
const configChanged = mutableKeys.some((k) => desiredDefinition[k] !== existingDefinition[k]) ||
|
|
747
|
+
isLogConfigChanged(desiredDefinition, existingDefinition) ||
|
|
748
|
+
// Disabling log always re-pushes config so the empty Cls ids reach the
|
|
749
|
+
// UpdateFunctionConfiguration unbind contract.
|
|
750
|
+
disableLog;
|
|
587
751
|
if (configChanged) {
|
|
588
752
|
await client.scf.updateFunctionConfiguration(config);
|
|
589
753
|
}
|
|
754
|
+
if (disableLog) {
|
|
755
|
+
// Unbind (above), then delete index before topic so the logset releases.
|
|
756
|
+
if (topicToDelete) {
|
|
757
|
+
logger_1.logger.info(lang_1.lang.__('DELETING_CLS_INDEX', { topicName: topicNameToDelete ?? topicToDelete }));
|
|
758
|
+
await client.cls.deleteIndex(topicToDelete);
|
|
759
|
+
logger_1.logger.info(lang_1.lang.__('DELETING_CLS_TOPIC', { id: topicToDelete }));
|
|
760
|
+
await client.cls.deleteTopic(topicToDelete);
|
|
761
|
+
}
|
|
762
|
+
const sharedBefore = (0, stateManager_1.getSharedResource)(state, context.stage, sharedLogset_1.SHARED_LOGSET_KEY);
|
|
763
|
+
if (sharedBefore) {
|
|
764
|
+
const release = await (0, sharedLogset_1.releaseSharedLogsetIfUnused)(context, client, sharedBefore);
|
|
765
|
+
if (release === 'deleted') {
|
|
766
|
+
sharedLogsetReleased = true;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
logger_1.logger.info(lang_1.lang.__('CLS_LOG_DISABLED', { functionName: fn.name }));
|
|
770
|
+
}
|
|
590
771
|
// Update code
|
|
591
772
|
await client.scf.updateFunctionCode(fn.name, codeBase64);
|
|
592
773
|
const existingHttpTrigger = existingFnInstance?.triggers?.find((t) => t.type === 'http');
|
|
@@ -753,7 +934,10 @@ const updateResource = async (context, fn, state) => {
|
|
|
753
934
|
const sid = (0, common_1.buildSid)('tencent', 'scf', context.stage, fn.name);
|
|
754
935
|
const existingDependentInstances = existingInstances
|
|
755
936
|
.filter((i) => i.type !== undefined &&
|
|
756
|
-
i.type !== 'TENCENT_SCF_CUSTOM_DOMAIN'
|
|
937
|
+
i.type !== 'TENCENT_SCF_CUSTOM_DOMAIN' &&
|
|
938
|
+
!((clsTopicInstance || disableLog) &&
|
|
939
|
+
(i.type === types_1.ResourceTypeEnum.TENCENT_CLS_TOPIC ||
|
|
940
|
+
i.type === types_1.ResourceTypeEnum.TENCENT_CLS_LOGSET)))
|
|
757
941
|
.map((i) => {
|
|
758
942
|
const { type, id, sid, roleArn, external } = i;
|
|
759
943
|
return {
|
|
@@ -783,6 +967,9 @@ const updateResource = async (context, fn, state) => {
|
|
|
783
967
|
protocol: fn.domain.protocol,
|
|
784
968
|
});
|
|
785
969
|
}
|
|
970
|
+
if (clsTopicInstance) {
|
|
971
|
+
newDependentInstances.push(clsTopicInstance);
|
|
972
|
+
}
|
|
786
973
|
const resourceState = {
|
|
787
974
|
mode: 'managed',
|
|
788
975
|
region: context.region,
|
|
@@ -794,7 +981,12 @@ const updateResource = async (context, fn, state) => {
|
|
|
794
981
|
],
|
|
795
982
|
lastUpdated: new Date().toISOString(),
|
|
796
983
|
};
|
|
797
|
-
|
|
984
|
+
const stateWithShared = clsSharedInstance
|
|
985
|
+
? (0, stateManager_1.setSharedResource)(state, context.stage, sharedLogset_1.SHARED_LOGSET_KEY, clsSharedInstance)
|
|
986
|
+
: sharedLogsetReleased
|
|
987
|
+
? (0, stateManager_1.removeSharedResource)(state, context.stage, sharedLogset_1.SHARED_LOGSET_KEY)
|
|
988
|
+
: state;
|
|
989
|
+
return (0, stateManager_1.setResource)(stateWithShared, logicalId, resourceState);
|
|
798
990
|
};
|
|
799
991
|
exports.updateResource = updateResource;
|
|
800
992
|
const deleteResource = async (context, functionName, logicalId, state) => {
|
|
@@ -49,6 +49,11 @@ const functionToScfConfig = (fn, role) => {
|
|
|
49
49
|
exports.functionToScfConfig = functionToScfConfig;
|
|
50
50
|
const extractScfDefinition = (config, codeHash, iam) => {
|
|
51
51
|
const envMap = config.Environment?.Variables?.reduce((acc, v) => ({ ...acc, [v.Key]: v.Value }), {}) ?? {};
|
|
52
|
+
// The CLS destination is diffed by stable names (not raw ids) so planner
|
|
53
|
+
// drift detection survives logset/topic id churn across re-deploys.
|
|
54
|
+
const logConfig = config.ClsLogsetName && config.ClsTopicName
|
|
55
|
+
? { logset: config.ClsLogsetName, topic: config.ClsTopicName }
|
|
56
|
+
: undefined;
|
|
52
57
|
const baseDefinition = {
|
|
53
58
|
functionName: config.FunctionName,
|
|
54
59
|
runtime: config.Runtime,
|
|
@@ -62,6 +67,7 @@ const extractScfDefinition = (config, codeHash, iam) => {
|
|
|
62
67
|
cfsConfig: config.CfsConfig ?? null,
|
|
63
68
|
useGpu: config.UseGpu ?? null,
|
|
64
69
|
imageConfig: config.ImageConfig ?? null,
|
|
70
|
+
...(logConfig ? { logConfig } : {}),
|
|
65
71
|
};
|
|
66
72
|
return iam ? { ...baseDefinition, iam } : baseDefinition;
|
|
67
73
|
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ensureFunctionTopic = exports.releaseSharedLogsetIfUnused = exports.buildSharedLogsetResourceState = exports.ensureSharedLogset = exports.buildFunctionTopicName = exports.buildSharedLogsetName = exports.SHARED_LOGSET_KEY = void 0;
|
|
4
|
+
const common_1 = require("../../common");
|
|
5
|
+
const types_1 = require("../../types");
|
|
6
|
+
const logger_1 = require("../../common/logger");
|
|
7
|
+
const lang_1 = require("../../lang");
|
|
8
|
+
const ownershipTag_1 = require("../ownershipTag");
|
|
9
|
+
/**
|
|
10
|
+
* Issue #214: one shared CLS logset per app×stage, tracked in the stage-level
|
|
11
|
+
* `shared` slot under the provider-neutral key 'logs.project'. Per-resource
|
|
12
|
+
* topics stay nested under their owning resource (function) so each owner tears
|
|
13
|
+
* down only its own topic; the shared logset is released by the destroyer once
|
|
14
|
+
* the provider reports zero remaining topics.
|
|
15
|
+
*/
|
|
16
|
+
exports.SHARED_LOGSET_KEY = 'logs.project';
|
|
17
|
+
const buildSharedLogsetName = (app, stage) => `${app}-${stage}-cls`;
|
|
18
|
+
exports.buildSharedLogsetName = buildSharedLogsetName;
|
|
19
|
+
const buildFunctionTopicName = (context) => `${context.service}-${context.stage}-fn-logs`;
|
|
20
|
+
exports.buildFunctionTopicName = buildFunctionTopicName;
|
|
21
|
+
const resolveSharedLogsetName = (shared) => {
|
|
22
|
+
const instanceId = shared.instances?.[0]?.id;
|
|
23
|
+
return instanceId ?? shared.definition?.logsetName;
|
|
24
|
+
};
|
|
25
|
+
const resolveSharedLogsetId = (shared) => shared.instances?.[0]?.logsetId ??
|
|
26
|
+
shared.instances?.[0]?.attributes
|
|
27
|
+
?.logsetId ??
|
|
28
|
+
'';
|
|
29
|
+
const ensureSharedLogset = async (context, client, state) => {
|
|
30
|
+
const shared = state ? (0, common_1.getSharedResource)(state, context.stage, exports.SHARED_LOGSET_KEY) : undefined;
|
|
31
|
+
if (shared) {
|
|
32
|
+
const logsetName = resolveSharedLogsetName(shared);
|
|
33
|
+
if (logsetName) {
|
|
34
|
+
return { logsetName, logsetId: resolveSharedLogsetId(shared) };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const logsetName = (0, exports.buildSharedLogsetName)(context.app, context.stage);
|
|
38
|
+
// Local shared state is absent (state reset, or a sibling service of this app
|
|
39
|
+
// already created the logset): probe the provider for a same-named logset and
|
|
40
|
+
// adopt it ONLY on an exact app-scope ownership-tag match. A foreign or
|
|
41
|
+
// untagged logset is refused rather than silently taken over or retagged.
|
|
42
|
+
const existing = await client.cls.getLogsetByName(logsetName);
|
|
43
|
+
if (existing?.LogsetId) {
|
|
44
|
+
if (!(0, ownershipTag_1.isOwnedByApp)(context.app, exports.SHARED_LOGSET_KEY, existing.Tags)) {
|
|
45
|
+
throw new Error(lang_1.lang.__('CLS_LOGSET_FOREIGN_OWNED', { logsetName }));
|
|
46
|
+
}
|
|
47
|
+
return { logsetName, logsetId: existing.LogsetId };
|
|
48
|
+
}
|
|
49
|
+
logger_1.logger.info(lang_1.lang.__('CREATING_CLS_LOGSET', { logsetName }));
|
|
50
|
+
const created = await client.cls.createLogset(logsetName, [
|
|
51
|
+
{ key: ownershipTag_1.OWNERSHIP_TAG_KEY, value: (0, ownershipTag_1.buildSharedOwnershipTagValue)(context.app, exports.SHARED_LOGSET_KEY) },
|
|
52
|
+
]);
|
|
53
|
+
return { logsetName, logsetId: created.logsetId };
|
|
54
|
+
};
|
|
55
|
+
exports.ensureSharedLogset = ensureSharedLogset;
|
|
56
|
+
const buildSharedLogsetResourceState = (context, result) => ({
|
|
57
|
+
mode: 'managed',
|
|
58
|
+
region: context.region,
|
|
59
|
+
definition: {
|
|
60
|
+
logsetName: result.logsetName,
|
|
61
|
+
region: context.region,
|
|
62
|
+
stage: context.stage,
|
|
63
|
+
},
|
|
64
|
+
instances: [
|
|
65
|
+
{
|
|
66
|
+
sid: (0, common_1.buildSid)('tencent', 'cls-logset', context.stage, result.logsetName),
|
|
67
|
+
type: types_1.ResourceTypeEnum.TENCENT_CLS_LOGSET,
|
|
68
|
+
id: result.logsetName,
|
|
69
|
+
logsetId: result.logsetId,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
lastUpdated: new Date().toISOString(),
|
|
73
|
+
});
|
|
74
|
+
exports.buildSharedLogsetResourceState = buildSharedLogsetResourceState;
|
|
75
|
+
const releaseSharedLogsetIfUnused = async (context, client, shared) => {
|
|
76
|
+
if (!shared) {
|
|
77
|
+
return 'absent';
|
|
78
|
+
}
|
|
79
|
+
const logsetName = resolveSharedLogsetName(shared);
|
|
80
|
+
if (!logsetName) {
|
|
81
|
+
return 'absent';
|
|
82
|
+
}
|
|
83
|
+
const logset = await client.cls.getLogsetByName(logsetName);
|
|
84
|
+
if (!logset?.LogsetId) {
|
|
85
|
+
return 'absent';
|
|
86
|
+
}
|
|
87
|
+
const topics = await client.cls.listTopicsByLogset(logset.LogsetId);
|
|
88
|
+
if (topics.length > 0) {
|
|
89
|
+
logger_1.logger.info(lang_1.lang.__('SHARED_CLS_LOGSET_IN_USE', {
|
|
90
|
+
logsetName,
|
|
91
|
+
count: String(topics.length),
|
|
92
|
+
}));
|
|
93
|
+
return 'retained';
|
|
94
|
+
}
|
|
95
|
+
await client.cls.deleteLogset(logset.LogsetId);
|
|
96
|
+
logger_1.logger.info(lang_1.lang.__('SHARED_CLS_LOGSET_DELETED', { logsetName }));
|
|
97
|
+
return 'deleted';
|
|
98
|
+
};
|
|
99
|
+
exports.releaseSharedLogsetIfUnused = releaseSharedLogsetIfUnused;
|
|
100
|
+
/**
|
|
101
|
+
* Adopt-or-create a topic inside the shared logset. Reuses a same-named topic
|
|
102
|
+
* only when its ownership tag belongs to this app's stack; a foreign or
|
|
103
|
+
* untagged topic is refused rather than silently taken over.
|
|
104
|
+
*/
|
|
105
|
+
const ensureFunctionTopic = async (context, client, params) => {
|
|
106
|
+
const { logsetId, topicName, logicalId } = params;
|
|
107
|
+
const existing = await client.cls.getTopicByName(logsetId, topicName);
|
|
108
|
+
if (existing) {
|
|
109
|
+
const tag = (existing.Tags ?? []).find((t) => t.Key === ownershipTag_1.OWNERSHIP_TAG_KEY);
|
|
110
|
+
const parsed = (0, ownershipTag_1.parseOwnershipTagValue)(tag?.Value);
|
|
111
|
+
if (parsed?.stack === `${context.app}-${context.service}`) {
|
|
112
|
+
return { topicName: existing.TopicName ?? topicName, topicId: existing.TopicId ?? '' };
|
|
113
|
+
}
|
|
114
|
+
throw new Error(lang_1.lang.__('CLS_TOPIC_FOREIGN_OWNED', { topicName }));
|
|
115
|
+
}
|
|
116
|
+
logger_1.logger.info(lang_1.lang.__('CREATING_CLS_TOPIC', { topicName }));
|
|
117
|
+
const topic = await client.cls.createTopic(logsetId, topicName, {
|
|
118
|
+
period: 30,
|
|
119
|
+
storageType: 'hot',
|
|
120
|
+
tags: [{ key: ownershipTag_1.OWNERSHIP_TAG_KEY, value: (0, ownershipTag_1.buildOwnershipTagValue)(context, logicalId) }],
|
|
121
|
+
});
|
|
122
|
+
logger_1.logger.info(lang_1.lang.__('CREATING_CLS_INDEX', { topicName }));
|
|
123
|
+
await client.cls.createFulltextIndex(topic.topicId);
|
|
124
|
+
return { topicName, topicId: topic.topicId };
|
|
125
|
+
};
|
|
126
|
+
exports.ensureFunctionTopic = ensureFunctionTopic;
|