From 604fa5be634d099d797bfee5c2b0f26ce0ac8461 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Wed, 15 Jul 2026 23:01:32 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96vke=20keubconfig?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F=EF=BC=8C=E6=94=B9=E6=88=90?= =?UTF-8?q?=E5=85=88=E6=9F=A5=E8=AF=A2=EF=BC=8C=E5=A6=82=E6=9E=9C=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=86=8D=E5=88=9B=E5=BB=BA=E4=B8=B4=E6=97=B6config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/pipeline/src/notification/api.ts | 4 + packages/ui/certd-server/.eslintrc | 25 ---- .../src/plugins/plugin-volcengine/access.ts | 18 ++- .../plugins/plugin-deploy-to-vke.ts | 122 ++++++++++-------- 4 files changed, 85 insertions(+), 84 deletions(-) delete mode 100644 packages/ui/certd-server/.eslintrc diff --git a/packages/core/pipeline/src/notification/api.ts b/packages/core/pipeline/src/notification/api.ts index aa9481178..c79d309a4 100644 --- a/packages/core/pipeline/src/notification/api.ts +++ b/packages/core/pipeline/src/notification/api.ts @@ -95,6 +95,10 @@ export abstract class BaseNotification implements INotification { } async doSend(body: NotificationBody) { + if (body.content) { + const content = body.content?.replace(/\n/g, " \n"); + body.content = content; + } return await this.send(body); } abstract send(body: NotificationBody): Promise; diff --git a/packages/ui/certd-server/.eslintrc b/packages/ui/certd-server/.eslintrc deleted file mode 100644 index 8b68bd3a1..000000000 --- a/packages/ui/certd-server/.eslintrc +++ /dev/null @@ -1,25 +0,0 @@ -{ - "parser": "@typescript-eslint/parser", - "plugins": [ - "@typescript-eslint" - ], - "ignorePatterns": ["dist"], - "extends": [ - "plugin:@typescript-eslint/recommended", - "plugin:prettier/recommended", - "prettier" - ], - "env": { - "mocha": true - }, - "rules": { - "@typescript-eslint/no-var-requires": "off", - "@typescript-eslint/ban-ts-comment": "off", - "@typescript-eslint/ban-ts-ignore": "off", - "@typescript-eslint/no-explicit-any": "off", - "@typescript-eslint/no-empty-function": "off", - "@typescript-eslint/no-unused-vars": "off", - "@typescript-eslint/no-this-alias": "off", - "@typescript-eslint/no-unsafe-assignment": "off" - } -} diff --git a/packages/ui/certd-server/src/plugins/plugin-volcengine/access.ts b/packages/ui/certd-server/src/plugins/plugin-volcengine/access.ts index 8c1a3ae96..818b1582b 100644 --- a/packages/ui/certd-server/src/plugins/plugin-volcengine/access.ts +++ b/packages/ui/certd-server/src/plugins/plugin-volcengine/access.ts @@ -43,7 +43,11 @@ export class VolcengineAccess extends BaseAccess { testRequest = true; async onTestRequest() { - await this.getCallerIdentity(); + const result = await this.getCallerIdentity(); + this.ctx.logger.info("✅ 密钥有效!"); + this.ctx.logger.info(` 账户ID: ${result.accountId}`); + this.ctx.logger.info(` ARN: ${result.arn}`); + this.ctx.logger.info(` 用户ID: ${result.userId}`); return "ok"; } @@ -60,18 +64,18 @@ export class VolcengineAccess extends BaseAccess { }); const result = res.Result || {}; - this.ctx.logger.info("✅ 密钥有效!"); - this.ctx.logger.info(` 账户ID: ${result.AccountId}`); - this.ctx.logger.info(` ARN: ${result.Trn}`); - this.ctx.logger.info(` 用户ID: ${result.IdentityId}`); - return { valid: true, accountId: result.AccountId, arn: result.Trn, - userId: result.IdentityId, + userId: parseInt(result.IdentityId), }; } + + async getUserId() { + const { userId } = await this.getCallerIdentity(); + return userId; + } } new VolcengineAccess(); diff --git a/packages/ui/certd-server/src/plugins/plugin-volcengine/plugins/plugin-deploy-to-vke.ts b/packages/ui/certd-server/src/plugins/plugin-volcengine/plugins/plugin-deploy-to-vke.ts index 7cf98c127..fe58bf23e 100644 --- a/packages/ui/certd-server/src/plugins/plugin-volcengine/plugins/plugin-deploy-to-vke.ts +++ b/packages/ui/certd-server/src/plugins/plugin-volcengine/plugins/plugin-deploy-to-vke.ts @@ -208,10 +208,10 @@ export class VolcengineDeployToVKE extends AbstractPlusTaskPlugin { this.logger.info("使用已有证书中心ID:" + certId); } - const kubeconfigId = await this.createKubeconfig(vkeService); - - try { - const kubeconfig = await this.getKubeconfig(vkeService, kubeconfigId); + const userId = await access.getUserId(); + this.logger.info(`当前用户ID:${userId}`); + const kubeconfig = await this.getOrCreateKubeconfig({ vkeService, userId }); + try{ const k8sClient = new this.K8sClient({ kubeConfigStr: kubeconfig, logger: this.logger, @@ -224,9 +224,7 @@ export class VolcengineDeployToVKE extends AbstractPlusTaskPlugin { throw new Error(this.formatK8sError(e.response.body)); } throw e; - } finally { - await this.deleteKubeconfig(vkeService, kubeconfigId); - } + } await utils.sleep(5000); this.logger.info("VKE证书替换完成"); @@ -250,7 +248,19 @@ export class VolcengineDeployToVKE extends AbstractPlusTaskPlugin { return await client.getVkeService({ region: this.regionId }); } - private async createKubeconfig(vkeService: any) { + async getOrCreateKubeconfig(opts: {vkeService: any, userId: number }) : Promise { + // 先查询 + let kubeconfig = await this.getKubeconfig(opts); + // 如果不存在,再创建 + if (!kubeconfig) { + return await this.createKubeconfig(opts); + } + return kubeconfig; + } + + + private async createKubeconfig(opts: {vkeService: any}) { + const {vkeService} = opts; const clusterId = this.getClusterId(); const res = await vkeService.request({ action: "CreateKubeconfig", @@ -266,25 +276,38 @@ export class VolcengineDeployToVKE extends AbstractPlusTaskPlugin { throw new Error(`生成VKE Kubeconfig失败:${JSON.stringify(res)}`); } this.logger.info(`已生成临时Kubeconfig:${kubeconfigId}`); - return kubeconfigId; + + const config = await this.getKubeconfig({ vkeService, kubeconfigId }); + return config; } - private async getKubeconfig(vkeService: any, kubeconfigId: string) { + private async getKubeconfig(opts: {vkeService: any, kubeconfigId?: string, userId?:number }) { + const {vkeService, kubeconfigId, userId} = opts; const clusterId = this.getClusterId(); + const query: any = { + ClusterIds: [clusterId], + Types: [this.kubeconfigType], + } + if (kubeconfigId) { + query.Ids = [kubeconfigId]; + } + if (userId) { + query.UserIds = [userId]; + } const res = await vkeService.request({ action: "ListKubeconfigs", method: "POST", body: { - Filter: { - ClusterIds: [clusterId], - Ids: [kubeconfigId], - Types: [this.kubeconfigType], - }, + Filter: query, PageNumber: 1, PageSize: 10, }, }); const items = res.Result?.Items || res.Items || []; + if (items.length === 0) { + return null; + } + const item = items.find((it: any) => it.Id === kubeconfigId) || items[0]; const kubeconfig = item?.Kubeconfig; if (!kubeconfig) { @@ -293,25 +316,25 @@ export class VolcengineDeployToVKE extends AbstractPlusTaskPlugin { return this.decodeKubeconfig(kubeconfig); } - private async deleteKubeconfig(vkeService: any, kubeconfigId?: string) { - if (!kubeconfigId) { - return; - } - const clusterId = this.getClusterId(); - try { - await vkeService.request({ - action: "DeleteKubeconfigs", - method: "POST", - body: { - ClusterId: clusterId, - Ids: [kubeconfigId], - }, - }); - this.logger.info(`已删除临时Kubeconfig:${kubeconfigId}`); - } catch (e) { - this.logger.warn(`删除临时Kubeconfig失败:${e.message || e}`); - } - } + // private async deleteKubeconfig(vkeService: any, kubeconfigId?: string) { + // if (!kubeconfigId) { + // return; + // } + // const clusterId = this.getClusterId(); + // try { + // await vkeService.request({ + // action: "DeleteKubeconfigs", + // method: "POST", + // body: { + // ClusterId: clusterId, + // Ids: [kubeconfigId], + // }, + // }); + // this.logger.info(`已删除临时Kubeconfig:${kubeconfigId}`); + // } catch (e) { + // this.logger.warn(`删除临时Kubeconfig失败:${e.message || e}`); + // } + // } private getClusterId() { if (!this.clusterId) { @@ -476,24 +499,19 @@ export class VolcengineDeployToVKE extends AbstractPlusTaskPlugin { } const access = await this.getAccess(this.accessId); const vkeService = await this.getVkeService(access); - const kubeconfigId = await this.createKubeconfig(vkeService); - - try { - const kubeconfig = await this.getKubeconfig(vkeService, kubeconfigId); - const k8sClient = new this.K8sClient({ - kubeConfigStr: kubeconfig, - logger: this.logger, - skipTLSVerify: this.skipTLSVerify, - }); - const res = await k8sClient.getSecrets({ namespace: this.namespace || "default" }); - const list = res.body?.items || res.items || []; - return list.map((item: any) => ({ - label: item.metadata.name, - value: item.metadata.name, - })); - } finally { - await this.deleteKubeconfig(vkeService, kubeconfigId); - } + const userId = await access.getUserId(); + const kubeconfig = await this.getOrCreateKubeconfig({ vkeService, userId }); + const k8sClient = new this.K8sClient({ + kubeConfigStr: kubeconfig, + logger: this.logger, + skipTLSVerify: this.skipTLSVerify, + }); + const res = await k8sClient.getSecrets({ namespace: this.namespace || "default" }); + const list = res.body?.items || res.items || []; + return list.map((item: any) => ({ + label: item.metadata.name, + value: item.metadata.name, + })); } }