@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.
Files changed (39) hide show
  1. package/.serverlessinsight/state-cdn-flow-app-cdn-flow-service.json.backup +3 -2
  2. package/.serverlessinsight/state-iam-test-app-iam-test-service.json.backup +21 -2
  3. package/.serverlessinsight/state-insight-poc-deploy-app-insight-poc-deploy.json.backup +44 -33
  4. package/.serverlessinsight/state-insight-poc-destroy-app-insight-poc-destroy.json.backup +3 -2
  5. package/.serverlessinsight/state-insight-volc-app-insight-volc.json.backup +63 -23
  6. package/dist/package.json +3 -2
  7. package/dist/src/common/aliyunClient/apigwOperations.js +36 -0
  8. package/dist/src/common/aliyunClient/slsOperations.js +34 -2
  9. package/dist/src/common/constants.js +5 -1
  10. package/dist/src/common/stateBackend/remoteStateBackend.js +4 -1
  11. package/dist/src/common/stateManager.js +52 -2
  12. package/dist/src/common/tencentClient/clsOperations.js +137 -0
  13. package/dist/src/common/tencentClient/index.js +21 -1
  14. package/dist/src/common/tencentClient/scfOperations.js +11 -0
  15. package/dist/src/common/volcengineClient/iamOperations.js +33 -0
  16. package/dist/src/common/volcengineClient/tlsOperations.js +84 -1
  17. package/dist/src/lang/en.js +23 -1
  18. package/dist/src/lang/zh-CN.js +23 -1
  19. package/dist/src/stack/aliyunStack/apigwPlanner.js +2 -0
  20. package/dist/src/stack/aliyunStack/apigwResource.js +118 -1
  21. package/dist/src/stack/aliyunStack/apigwTypes.js +19 -1
  22. package/dist/src/stack/aliyunStack/destroyer.js +25 -0
  23. package/dist/src/stack/aliyunStack/fc3Resource.js +91 -32
  24. package/dist/src/stack/aliyunStack/sharedLogProject.js +137 -0
  25. package/dist/src/stack/ownershipTag.js +19 -1
  26. package/dist/src/stack/scfStack/destroyer.js +13 -0
  27. package/dist/src/stack/scfStack/scfPlanner.js +9 -1
  28. package/dist/src/stack/scfStack/scfResource.js +205 -13
  29. package/dist/src/stack/scfStack/scfTypes.js +6 -0
  30. package/dist/src/stack/scfStack/sharedLogset.js +126 -0
  31. package/dist/src/stack/volcengineStack/apigwResource.js +64 -48
  32. package/dist/src/stack/volcengineStack/apigwTypes.js +9 -0
  33. package/dist/src/stack/volcengineStack/destroyer.js +12 -0
  34. package/dist/src/stack/volcengineStack/sharedLogProject.js +190 -0
  35. package/dist/src/stack/volcengineStack/vefaasPlanner.js +20 -13
  36. package/dist/src/stack/volcengineStack/vefaasResource.js +76 -76
  37. package/dist/src/types/domains/state.js +3 -0
  38. package/dist/tsconfig.tsbuildinfo +1 -1
  39. package/package.json +3 -2
@@ -10,6 +10,7 @@ const logger_1 = require("../../common/logger");
10
10
  const lang_1 = require("../../lang");
11
11
  const ownershipTag_1 = require("../ownershipTag");
12
12
  const alreadyExists_1 = require("../alreadyExists");
13
+ const sharedLogProject_1 = require("./sharedLogProject");
13
14
  const buildGatewayInstance = (info, stage) => ({
14
15
  type: 'VOLCENGINE_APIGW_GATEWAY',
15
16
  sid: (0, common_1.buildSid)('volcengine', 'apigw', stage, info.gatewayId ?? ''),
@@ -31,64 +32,45 @@ const buildGatewayInstance = (info, stage) => ({
31
32
  logSpec: info.logSpec ?? null,
32
33
  resourceSpec: info.resourceSpec ?? null,
33
34
  });
34
- const ensureApigwLogResources = async (context, serviceName, existingInstances) => {
35
+ const ensureApigwLogResources = async (context, existingInstances, state, logicalId) => {
35
36
  const client = (0, volcengineClient_1.createVolcengineClient)(context);
36
37
  const tlsProjectInstance = existingInstances.find((i) => i.type === 'VOLCENGINE_TLS_PROJECT');
37
38
  const tlsTopicInstance = existingInstances.find((i) => i.type === 'VOLCENGINE_TLS_TOPIC');
38
39
  if (tlsProjectInstance && tlsTopicInstance) {
40
+ // Legacy own project+topic → reuse exactly as before.
39
41
  return {
40
42
  projectId: tlsProjectInstance.projectId ?? '',
41
43
  topicId: tlsTopicInstance.topicId ?? '',
42
44
  instances: existingInstances.filter((i) => i.type.startsWith('VOLCENGINE_TLS_')),
43
45
  };
44
46
  }
45
- const projectName = `${serviceName}-${context.stage}-apigw-tls`;
46
- const topicName = `${serviceName}-${context.stage}-apigw-logs`;
47
- logger_1.logger.info(lang_1.lang.__('CREATING_TLS_PROJECT', { projectName }));
48
- const project = await client.tls.createProject({
49
- projectName,
50
- description: `API Gateway access logs for ${serviceName}`,
51
- region: context.region,
52
- });
53
- logger_1.logger.info(lang_1.lang.__('CREATING_TLS_TOPIC', { topicName }));
54
- const topic = await client.tls.createTopic({
55
- projectName,
47
+ // Shared app-scoped TLS project (#214) with a per-event topic nested under it.
48
+ const shared = await (0, sharedLogProject_1.ensureSharedLogProject)(context, client, state);
49
+ const sharedInstance = (0, sharedLogProject_1.buildSharedProjectResourceState)(context, shared);
50
+ const topicName = `${context.service}-${context.stage}-apigw-logs`;
51
+ const topic = await (0, sharedLogProject_1.ensureOwnedTopic)(context, client, {
52
+ projectName: shared.projectName,
56
53
  topicName,
57
- description: `API Gateway access logs for ${serviceName}`,
58
- ttl: 30,
54
+ logicalId,
59
55
  });
60
- logger_1.logger.info(lang_1.lang.__('CREATING_TLS_INDEX', { topicName }));
61
- await client.tls.createIndex({
62
- projectName,
63
- topicName,
64
- fullTextIndex: {
65
- delimiter: ' ,.?;!\n\t',
66
- caseSensitive: false,
67
- },
68
- });
69
- logger_1.logger.info(lang_1.lang.__('WAITING_FOR_TLS_RESOURCES', { projectName, topicName }));
70
- await client.tls.waitForProject(projectName);
71
- await client.tls.waitForTopic(projectName, topicName);
72
56
  return {
73
- projectId: project.projectId ?? '',
74
- topicId: topic.topicId ?? '',
57
+ projectId: shared.projectId,
58
+ topicId: topic.topicId,
75
59
  instances: [
76
60
  {
77
- type: 'VOLCENGINE_TLS_PROJECT',
78
- sid: (0, common_1.buildSid)('volcengine', 'tls', context.stage, projectName),
79
- id: projectName,
80
- projectId: project.projectId ?? '',
81
- ...project,
61
+ type: 'VOLCENGINE_TLS_TOPIC',
62
+ sid: (0, common_1.buildSid)('volcengine', 'tls', context.stage, `${shared.projectName}/${topic.topicName}`),
63
+ id: `${shared.projectName}/${topic.topicName}`,
64
+ projectId: shared.projectId,
65
+ topicId: topic.topicId,
82
66
  },
83
67
  {
84
- type: 'VOLCENGINE_TLS_TOPIC',
85
- sid: (0, common_1.buildSid)('volcengine', 'tls', context.stage, `${projectName}/${topicName}`),
86
- id: `${projectName}/${topicName}`,
87
- projectId: project.projectId ?? '',
88
- topicId: topic.topicId ?? '',
89
- ...topic,
68
+ type: 'VOLCENGINE_TLS_INDEX',
69
+ sid: (0, common_1.buildSid)('volcengine', 'tls', context.stage, `${shared.projectName}/${topic.topicName}/index`),
70
+ id: `${shared.projectName}/${topic.topicName}/index`,
90
71
  },
91
72
  ],
73
+ sharedInstance,
92
74
  };
93
75
  };
94
76
  const buildServiceInstance = (serviceId, serviceName, gatewayId, stage, info) => ({
@@ -202,7 +184,12 @@ const createApigwResource = async (context, event, serviceName, state) => {
202
184
  gatewayInfo = await client.apigw.waitForGatewayRunning(gatewayId);
203
185
  const instances = [buildGatewayInstance(gatewayInfo, context.stage)];
204
186
  if (event.log) {
205
- const logResources = await ensureApigwLogResources(context, serviceName, []);
187
+ const existingEventState = (0, stateManager_1.getResource)(state, logicalId);
188
+ const existingInstances = existingEventState?.instances ?? [];
189
+ const logResources = await ensureApigwLogResources(context, existingInstances, state, logicalId);
190
+ if (logResources.sharedInstance) {
191
+ state = (0, stateManager_1.setSharedResource)(state, context.stage, 'logs.project', logResources.sharedInstance);
192
+ }
206
193
  await client.apigw.updateGatewayLog(gatewayId, {
207
194
  enable: true,
208
195
  projectId: logResources.projectId,
@@ -321,11 +308,13 @@ const updateApigwResource = async (context, event, serviceName, state) => {
321
308
  const neededRouteIds = new Set();
322
309
  const instances = existingState.instances.filter((i) => i.type === 'VOLCENGINE_APIGW_GATEWAY' ||
323
310
  i.type === 'VOLCENGINE_APIGW_SERVICE' ||
324
- i.type === 'VOLCENGINE_APIGW_UPSTREAM' ||
325
- i.type.startsWith('VOLCENGINE_TLS_'));
311
+ i.type === 'VOLCENGINE_APIGW_UPSTREAM');
326
312
  try {
327
313
  if (event.log) {
328
- const logResources = await ensureApigwLogResources(context, serviceName, existingState.instances);
314
+ const logResources = await ensureApigwLogResources(context, existingState.instances, state, `events.${event.key}`);
315
+ if (logResources.sharedInstance) {
316
+ state = (0, stateManager_1.setSharedResource)(state, context.stage, 'logs.project', logResources.sharedInstance);
317
+ }
329
318
  await client.apigw.updateGatewayLog(serviceInstance.gatewayId, {
330
319
  enable: true,
331
320
  projectId: logResources.projectId,
@@ -341,6 +330,14 @@ const updateApigwResource = async (context, event, serviceName, state) => {
341
330
  projectId: '',
342
331
  topicId: '',
343
332
  });
333
+ await (0, sharedLogProject_1.deleteTlsLogResources)(context, client, existingLogResources);
334
+ // Release the shared project when this was its last topic; keep the
335
+ // stage slot when other topics still reference it.
336
+ const shared = (0, stateManager_1.getSharedResource)(state, context.stage, sharedLogProject_1.SHARED_LOG_PROJECT_KEY);
337
+ const releaseResult = await (0, sharedLogProject_1.releaseSharedLogProjectIfUnused)(context, client, shared);
338
+ if (releaseResult === 'deleted') {
339
+ state = (0, stateManager_1.removeSharedResource)(state, context.stage, sharedLogProject_1.SHARED_LOG_PROJECT_KEY);
340
+ }
344
341
  }
345
342
  }
346
343
  const upstreamByFunction = new Map();
@@ -430,15 +427,16 @@ const deleteApigwResource = async (context, logicalId, state) => {
430
427
  }
431
428
  for (const instance of [...tlsInstances].reverse()) {
432
429
  try {
433
- if (instance.type === 'VOLCENGINE_TLS_TOPIC') {
430
+ if (instance.type === 'VOLCENGINE_TLS_INDEX') {
431
+ const [projectName, topicName] = instance.id.split('/');
432
+ logger_1.logger.info(lang_1.lang.__('DELETING_TLS_INDEX', { id: instance.id }));
433
+ await client.tls.deleteIndex(projectName, topicName);
434
+ }
435
+ else if (instance.type === 'VOLCENGINE_TLS_TOPIC') {
434
436
  const [projectName, topicName] = instance.id.split('/');
435
437
  logger_1.logger.info(lang_1.lang.__('DELETING_TLS_TOPIC', { id: instance.id }));
436
438
  await client.tls.deleteTopic(projectName, topicName);
437
439
  }
438
- else if (instance.type === 'VOLCENGINE_TLS_PROJECT') {
439
- logger_1.logger.info(lang_1.lang.__('DELETING_TLS_PROJECT', { id: instance.id }));
440
- await client.tls.deleteProject(instance.id);
441
- }
442
440
  }
443
441
  catch (error) {
444
442
  logger_1.logger.warn(lang_1.lang.__('FAILED_TO_DELETE_RESOURCE', {
@@ -448,6 +446,24 @@ const deleteApigwResource = async (context, logicalId, state) => {
448
446
  }));
449
447
  }
450
448
  }
449
+ // Legacy per-resource own-projects (pre-#214 scheme) are deleted after
450
+ // their children; the stage-shared project is destroyer-owned and never
451
+ // deleted at resource level.
452
+ const sharedProjectName = (0, sharedLogProject_1.buildSharedProjectName)(context.app, context.stage);
453
+ const legacyProject = tlsInstances.find((i) => i.type === 'VOLCENGINE_TLS_PROJECT' && i.id !== sharedProjectName);
454
+ if (legacyProject) {
455
+ try {
456
+ logger_1.logger.info(lang_1.lang.__('DELETING_TLS_PROJECT', { id: legacyProject.id }));
457
+ await client.tls.deleteProject(legacyProject.id);
458
+ }
459
+ catch (error) {
460
+ logger_1.logger.warn(lang_1.lang.__('FAILED_TO_DELETE_RESOURCE', {
461
+ type: String(legacyProject.type),
462
+ id: legacyProject.id,
463
+ error: String(error),
464
+ }));
465
+ }
466
+ }
451
467
  }
452
468
  if (serviceInstance) {
453
469
  const routes = existingState.instances.filter((i) => i.type === 'VOLCENGINE_APIGW_ROUTE');
@@ -90,12 +90,21 @@ const triggerToApigwRouteConfig = (event, trigger, serviceId, upstreamId) => {
90
90
  };
91
91
  exports.triggerToApigwRouteConfig = triggerToApigwRouteConfig;
92
92
  const extractEventDomainDefinition = (event) => {
93
+ const context = (0, common_1.getContext)();
93
94
  return {
94
95
  gatewayName: event.name,
95
96
  ...(event.network && {
96
97
  network: { vpcId: event.network.vpc_id, subnetIds: event.network.subnet_ids },
97
98
  }),
98
99
  logEnabled: event.log === true || event.log === 'true',
100
+ ...(event.log && context
101
+ ? {
102
+ logConfig: {
103
+ project: `${context.app}-${context.stage}-tls`,
104
+ topic: `${context.service}-${context.stage}-apigw-logs`,
105
+ },
106
+ }
107
+ : {}),
99
108
  triggers: event.triggers.map((t) => ({
100
109
  method: String(t.method),
101
110
  path: String(t.path),
@@ -2,11 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.destroyVolcengineStack = void 0;
4
4
  const common_1 = require("../../common");
5
+ const volcengineClient_1 = require("../../common/volcengineClient");
5
6
  const stateManager_1 = require("../../common/stateManager");
6
7
  const lang_1 = require("../../lang");
7
8
  const tosResource_1 = require("./tosResource");
8
9
  const vefaasResource_1 = require("./vefaasResource");
9
10
  const apigwResource_1 = require("./apigwResource");
11
+ const sharedLogProject_1 = require("./sharedLogProject");
10
12
  const destroyVolcengineStack = async (backend) => {
11
13
  const context = (0, common_1.getContext)();
12
14
  const providerName = common_1.ProviderEnum.VOLCENGINE;
@@ -67,6 +69,16 @@ const destroyVolcengineStack = async (backend) => {
67
69
  }
68
70
  }
69
71
  }
72
+ // Release the shared TLS project once no topics reference it anymore. The
73
+ // per-resource teardowns above deleted their own topics, so a project with
74
+ // zero remaining topics is safe to delete.
75
+ const sharedProject = (0, stateManager_1.getSharedResource)(state, context.stage, 'logs.project');
76
+ if (sharedProject) {
77
+ const releaseResult = await (0, sharedLogProject_1.releaseSharedLogProjectIfUnused)(context, (0, volcengineClient_1.createVolcengineClient)(context), sharedProject);
78
+ if (releaseResult === 'deleted') {
79
+ state = (0, stateManager_1.removeSharedResource)(state, context.stage, 'logs.project');
80
+ }
81
+ }
70
82
  await backend.saveState(state, context.app, context.service, context.stage);
71
83
  };
72
84
  exports.destroyVolcengineStack = destroyVolcengineStack;
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deleteTlsLogResources = exports.ensureOwnedTopic = exports.releaseSharedLogProjectIfUnused = exports.buildSharedProjectResourceState = exports.ensureSharedLogProject = exports.buildSharedProjectName = exports.SHARED_LOG_PROJECT_KEY = void 0;
4
+ const common_1 = require("../../common");
5
+ const logger_1 = require("../../common/logger");
6
+ const lang_1 = require("../../lang");
7
+ const ownershipTag_1 = require("../ownershipTag");
8
+ /**
9
+ * Issue #214: one shared TLS project per app×stage, tracked in the stage-level
10
+ * `shared` slot under the provider-neutral key 'logs.project'. Per-resource
11
+ * topics stay nested under their owning resource (function/event) so each
12
+ * owner tears down only its own topic; the shared project is released by the
13
+ * destroyer once the provider reports zero remaining topics.
14
+ */
15
+ exports.SHARED_LOG_PROJECT_KEY = 'logs.project';
16
+ const buildSharedProjectName = (app, stage) => `${app}-${stage}-tls`;
17
+ exports.buildSharedProjectName = buildSharedProjectName;
18
+ const resolveSharedProjectName = (shared) => {
19
+ const instanceId = shared.instances?.[0]?.id;
20
+ return instanceId ?? shared.definition?.projectName;
21
+ };
22
+ const ensureSharedLogProject = async (context, client, state) => {
23
+ const shared = state
24
+ ? (0, common_1.getSharedResource)(state, context.stage, exports.SHARED_LOG_PROJECT_KEY)
25
+ : undefined;
26
+ if (shared) {
27
+ const projectName = resolveSharedProjectName(shared);
28
+ if (projectName) {
29
+ const existing = await client.tls.getProject(projectName);
30
+ if (existing?.projectId) {
31
+ const tags = await client.tls.getProjectTags(projectName);
32
+ if ((0, ownershipTag_1.isOwnedByApp)(context.app, exports.SHARED_LOG_PROJECT_KEY, tags)) {
33
+ return { projectName, projectId: existing.projectId };
34
+ }
35
+ throw new Error(lang_1.lang.__('TLS_SHARED_PROJECT_NOT_OWNED', { projectName }));
36
+ }
37
+ }
38
+ }
39
+ const projectName = (0, exports.buildSharedProjectName)(context.app, context.stage);
40
+ logger_1.logger.info(lang_1.lang.__('CREATING_TLS_PROJECT', { projectName }));
41
+ const project = await client.tls.createProject({
42
+ projectName,
43
+ description: `Shared TLS logs for ${context.app}`,
44
+ region: context.region,
45
+ });
46
+ await client.tls.waitForProject(projectName);
47
+ if (project.projectId) {
48
+ if (project.created === true) {
49
+ await client.tls.addTags({
50
+ resourceType: 'project',
51
+ resourcesList: [project.projectId],
52
+ tags: [
53
+ {
54
+ key: ownershipTag_1.OWNERSHIP_TAG_KEY,
55
+ value: (0, ownershipTag_1.buildSharedOwnershipTagValue)(context.app, exports.SHARED_LOG_PROJECT_KEY),
56
+ },
57
+ ],
58
+ });
59
+ }
60
+ else {
61
+ // A same-named project already existed (create race). Re-read and verify
62
+ // ownership before adopting — an untagged or foreign project must not be
63
+ // silently taken over.
64
+ const tags = await client.tls.getProjectTags(projectName);
65
+ if (!(0, ownershipTag_1.isOwnedByApp)(context.app, exports.SHARED_LOG_PROJECT_KEY, tags)) {
66
+ throw new Error(lang_1.lang.__('TLS_SHARED_PROJECT_NOT_OWNED', { projectName }));
67
+ }
68
+ }
69
+ }
70
+ return { projectName, projectId: project.projectId ?? '' };
71
+ };
72
+ exports.ensureSharedLogProject = ensureSharedLogProject;
73
+ const buildSharedProjectResourceState = (context, result) => ({
74
+ mode: 'managed',
75
+ region: context.region,
76
+ definition: {
77
+ projectName: result.projectName,
78
+ region: context.region,
79
+ stage: context.stage,
80
+ },
81
+ instances: [
82
+ {
83
+ sid: (0, common_1.buildSid)('volcengine', 'tls', context.stage, result.projectName),
84
+ type: 'VOLCENGINE_TLS_PROJECT',
85
+ id: result.projectName,
86
+ projectId: result.projectId,
87
+ },
88
+ ],
89
+ lastUpdated: new Date().toISOString(),
90
+ });
91
+ exports.buildSharedProjectResourceState = buildSharedProjectResourceState;
92
+ const releaseSharedLogProjectIfUnused = async (context, client, shared) => {
93
+ if (!shared) {
94
+ return 'absent';
95
+ }
96
+ const projectName = resolveSharedProjectName(shared);
97
+ if (!projectName) {
98
+ return 'absent';
99
+ }
100
+ const project = await client.tls.getProject(projectName);
101
+ if (!project?.projectId) {
102
+ return 'absent';
103
+ }
104
+ // Never delete a project that is not provably this app's — the ownership tag
105
+ // proves the shared container belongs to us before teardown.
106
+ const tags = await client.tls.getProjectTags(projectName);
107
+ if (!(0, ownershipTag_1.isOwnedByApp)(context.app, exports.SHARED_LOG_PROJECT_KEY, tags)) {
108
+ logger_1.logger.warn(lang_1.lang.__('TLS_SHARED_PROJECT_NOT_OWNED', { projectName }));
109
+ return 'retained';
110
+ }
111
+ const topics = await client.tls.listTopics(projectName);
112
+ if (topics.length > 0) {
113
+ logger_1.logger.info(lang_1.lang.__('SHARED_TLS_PROJECT_IN_USE', {
114
+ projectName,
115
+ count: String(topics.length),
116
+ }));
117
+ return 'retained';
118
+ }
119
+ await client.tls.deleteProject(projectName);
120
+ logger_1.logger.info(lang_1.lang.__('SHARED_TLS_PROJECT_DELETED', { projectName }));
121
+ return 'deleted';
122
+ };
123
+ exports.releaseSharedLogProjectIfUnused = releaseSharedLogProjectIfUnused;
124
+ /**
125
+ * Adopt-or-create a topic inside a (shared) project. Reuses a same-named topic
126
+ * only when its ownership tag belongs to this app's stack; a foreign or
127
+ * untagged topic is refused rather than silently taken over.
128
+ */
129
+ const ensureOwnedTopic = async (context, client, params) => {
130
+ const { projectName, topicName, logicalId } = params;
131
+ const existing = await client.tls.getTopic(projectName, topicName);
132
+ if (existing) {
133
+ // Service-scoped topics are shared across a service's functions/events, so
134
+ // ownership is verified at the owning-stack level, not per logical id.
135
+ const tag = (existing.tags ?? []).find((t) => t.Key === ownershipTag_1.OWNERSHIP_TAG_KEY);
136
+ const parsed = (0, ownershipTag_1.parseOwnershipTagValue)(tag?.Value);
137
+ if (parsed?.stack === `${context.app}-${context.service}`) {
138
+ return { topicName: existing.topicName ?? topicName, topicId: existing.topicId ?? '' };
139
+ }
140
+ throw new Error(lang_1.lang.__('TLS_TOPIC_FOREIGN_OWNED', { topicName }));
141
+ }
142
+ logger_1.logger.info(lang_1.lang.__('CREATING_TLS_TOPIC', { topicName }));
143
+ const topic = await client.tls.createTopic({ projectName, topicName, ttl: 30 });
144
+ if (topic.topicId) {
145
+ await client.tls.addTags({
146
+ resourceType: 'topic',
147
+ resourcesList: [topic.topicId],
148
+ tags: [{ key: ownershipTag_1.OWNERSHIP_TAG_KEY, value: (0, ownershipTag_1.buildOwnershipTagValue)(context, logicalId) }],
149
+ });
150
+ }
151
+ logger_1.logger.info(lang_1.lang.__('CREATING_TLS_INDEX', { topicName }));
152
+ await client.tls.createIndex({
153
+ projectName,
154
+ topicName,
155
+ fullTextIndex: {
156
+ delimiter: ' ,.?;!\n\t',
157
+ caseSensitive: false,
158
+ },
159
+ });
160
+ logger_1.logger.info(lang_1.lang.__('WAITING_FOR_TLS_RESOURCES', { projectName, topicName }));
161
+ await client.tls.waitForProject(projectName);
162
+ await client.tls.waitForTopic(projectName, topicName);
163
+ return { topicName, topicId: topic.topicId ?? '' };
164
+ };
165
+ exports.ensureOwnedTopic = ensureOwnedTopic;
166
+ /**
167
+ * Delete per-resource TLS children (index then topic) and any legacy
168
+ * per-resource own-project. The stage-shared project is never deleted here.
169
+ */
170
+ const deleteTlsLogResources = async (context, client, instances) => {
171
+ const children = instances.filter((i) => i.type === 'VOLCENGINE_TLS_TOPIC' || i.type === 'VOLCENGINE_TLS_INDEX');
172
+ for (const instance of [...children].reverse()) {
173
+ const [projectName, topicName] = instance.id.split('/');
174
+ if (instance.type === 'VOLCENGINE_TLS_INDEX') {
175
+ logger_1.logger.info(lang_1.lang.__('DELETING_TLS_INDEX', { id: instance.id }));
176
+ await client.tls.deleteIndex(projectName, topicName);
177
+ }
178
+ else {
179
+ logger_1.logger.info(lang_1.lang.__('DELETING_TLS_TOPIC', { id: instance.id }));
180
+ await client.tls.deleteTopic(projectName, topicName);
181
+ }
182
+ }
183
+ const sharedProjectName = (0, exports.buildSharedProjectName)(context.app, context.stage);
184
+ const legacyProject = instances.find((i) => i.type === 'VOLCENGINE_TLS_PROJECT' && i.id !== sharedProjectName);
185
+ if (legacyProject) {
186
+ logger_1.logger.info(lang_1.lang.__('DELETING_TLS_PROJECT', { id: legacyProject.id }));
187
+ await client.tls.deleteProject(legacyProject.id);
188
+ }
189
+ };
190
+ exports.deleteTlsLogResources = deleteTlsLogResources;
@@ -45,23 +45,30 @@ const generateFunctionPlan = async (context, state, functions) => {
45
45
  // desiredDefinition.logConfig === undefined regardless of `fn.log` in the
46
46
  // YAML — meaning `log: true` could never be detected as drift and the
47
47
  // planner would report `noop` forever, so `UpdateFunction`'s `TlsConfig`
48
- // never gets sent. Derive the same project/topic names
49
- // createDependentResources would use (existing TLS instances in state
50
- // take priority; otherwise the deterministic `{service}-{stage}-tls` /
51
- // `{service}-{stage}-logs` names) so the diff can see the field change.
48
+ // never gets sent. Derive the same project/topic names the executor
49
+ // would use (shared stage slot → tracked topic instance → deterministic
50
+ // shared names `${app}-${stage}-tls` / `${service}-${stage}-fn-logs`) so
51
+ // the diff can see the field change without phantom drift.
52
52
  if (fn.log) {
53
- if (currentState?.instances?.length) {
54
- const tlsTopicInstance = currentState.instances.find((i) => i.type === 'VOLCENGINE_TLS_TOPIC');
55
- if (tlsTopicInstance?.id) {
56
- const [projectName, topicName] = tlsTopicInstance.id.split('/');
57
- config.logConfig = { project: projectName, topic: topicName };
53
+ const tlsTopicInstance = currentState?.instances?.find((i) => i.type === 'VOLCENGINE_TLS_TOPIC');
54
+ const shared = (0, stateManager_1.getSharedResource)(state, context.stage, 'logs.project');
55
+ if (tlsTopicInstance?.id) {
56
+ const [projectName, topicName] = tlsTopicInstance.id.split('/');
57
+ config.logConfig = { project: projectName, topic: topicName };
58
+ }
59
+ else if (shared) {
60
+ const projectName = shared.instances?.[0]?.id;
61
+ if (projectName) {
62
+ config.logConfig = {
63
+ project: projectName,
64
+ topic: `${context.service}-${context.stage}-fn-logs`,
65
+ };
58
66
  }
59
67
  }
60
- if (!config.logConfig) {
61
- const serviceName = `${context.app}-${context.service}`;
68
+ else {
62
69
  config.logConfig = {
63
- project: `${serviceName}-${context.stage}-tls`,
64
- topic: `${serviceName}-${context.stage}-logs`,
70
+ project: `${context.app}-${context.stage}-tls`,
71
+ topic: `${context.service}-${context.stage}-fn-logs`,
65
72
  };
66
73
  }
67
74
  }