perf: 优化vke keubconfig获取方式,改成先查询,如果没有再创建临时config

This commit is contained in:
xiaojunnuo
2026-07-15 23:01:32 +08:00
parent 7ed1be994f
commit 604fa5be63
4 changed files with 85 additions and 84 deletions
@@ -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<void>;
-25
View File
@@ -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"
}
}
@@ -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();
@@ -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<string> {
// 先查询
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<VolcengineAccess>(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,
}));
}
}