mirror of
https://github.com/certd/certd.git
synced 2026-07-16 18:57:32 +08:00
perf: 优化vke keubconfig获取方式,改成先查询,如果没有再创建临时config
This commit is contained in:
@@ -95,6 +95,10 @@ export abstract class BaseNotification implements INotification {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async doSend(body: NotificationBody) {
|
async doSend(body: NotificationBody) {
|
||||||
|
if (body.content) {
|
||||||
|
const content = body.content?.replace(/\n/g, " \n");
|
||||||
|
body.content = content;
|
||||||
|
}
|
||||||
return await this.send(body);
|
return await this.send(body);
|
||||||
}
|
}
|
||||||
abstract send(body: NotificationBody): Promise<void>;
|
abstract send(body: NotificationBody): Promise<void>;
|
||||||
|
|||||||
@@ -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;
|
testRequest = true;
|
||||||
|
|
||||||
async onTestRequest() {
|
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";
|
return "ok";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,18 +64,18 @@ export class VolcengineAccess extends BaseAccess {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const result = res.Result || {};
|
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 {
|
return {
|
||||||
valid: true,
|
valid: true,
|
||||||
accountId: result.AccountId,
|
accountId: result.AccountId,
|
||||||
arn: result.Trn,
|
arn: result.Trn,
|
||||||
userId: result.IdentityId,
|
userId: parseInt(result.IdentityId),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getUserId() {
|
||||||
|
const { userId } = await this.getCallerIdentity();
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
new VolcengineAccess();
|
new VolcengineAccess();
|
||||||
|
|||||||
+70
-52
@@ -208,10 +208,10 @@ export class VolcengineDeployToVKE extends AbstractPlusTaskPlugin {
|
|||||||
this.logger.info("使用已有证书中心ID:" + certId);
|
this.logger.info("使用已有证书中心ID:" + certId);
|
||||||
}
|
}
|
||||||
|
|
||||||
const kubeconfigId = await this.createKubeconfig(vkeService);
|
const userId = await access.getUserId();
|
||||||
|
this.logger.info(`当前用户ID:${userId}`);
|
||||||
try {
|
const kubeconfig = await this.getOrCreateKubeconfig({ vkeService, userId });
|
||||||
const kubeconfig = await this.getKubeconfig(vkeService, kubeconfigId);
|
try{
|
||||||
const k8sClient = new this.K8sClient({
|
const k8sClient = new this.K8sClient({
|
||||||
kubeConfigStr: kubeconfig,
|
kubeConfigStr: kubeconfig,
|
||||||
logger: this.logger,
|
logger: this.logger,
|
||||||
@@ -224,9 +224,7 @@ export class VolcengineDeployToVKE extends AbstractPlusTaskPlugin {
|
|||||||
throw new Error(this.formatK8sError(e.response.body));
|
throw new Error(this.formatK8sError(e.response.body));
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
} finally {
|
}
|
||||||
await this.deleteKubeconfig(vkeService, kubeconfigId);
|
|
||||||
}
|
|
||||||
|
|
||||||
await utils.sleep(5000);
|
await utils.sleep(5000);
|
||||||
this.logger.info("VKE证书替换完成");
|
this.logger.info("VKE证书替换完成");
|
||||||
@@ -250,7 +248,19 @@ export class VolcengineDeployToVKE extends AbstractPlusTaskPlugin {
|
|||||||
return await client.getVkeService({ region: this.regionId });
|
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 clusterId = this.getClusterId();
|
||||||
const res = await vkeService.request({
|
const res = await vkeService.request({
|
||||||
action: "CreateKubeconfig",
|
action: "CreateKubeconfig",
|
||||||
@@ -266,25 +276,38 @@ export class VolcengineDeployToVKE extends AbstractPlusTaskPlugin {
|
|||||||
throw new Error(`生成VKE Kubeconfig失败:${JSON.stringify(res)}`);
|
throw new Error(`生成VKE Kubeconfig失败:${JSON.stringify(res)}`);
|
||||||
}
|
}
|
||||||
this.logger.info(`已生成临时Kubeconfig:${kubeconfigId}`);
|
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 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({
|
const res = await vkeService.request({
|
||||||
action: "ListKubeconfigs",
|
action: "ListKubeconfigs",
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: {
|
body: {
|
||||||
Filter: {
|
Filter: query,
|
||||||
ClusterIds: [clusterId],
|
|
||||||
Ids: [kubeconfigId],
|
|
||||||
Types: [this.kubeconfigType],
|
|
||||||
},
|
|
||||||
PageNumber: 1,
|
PageNumber: 1,
|
||||||
PageSize: 10,
|
PageSize: 10,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const items = res.Result?.Items || res.Items || [];
|
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 item = items.find((it: any) => it.Id === kubeconfigId) || items[0];
|
||||||
const kubeconfig = item?.Kubeconfig;
|
const kubeconfig = item?.Kubeconfig;
|
||||||
if (!kubeconfig) {
|
if (!kubeconfig) {
|
||||||
@@ -293,25 +316,25 @@ export class VolcengineDeployToVKE extends AbstractPlusTaskPlugin {
|
|||||||
return this.decodeKubeconfig(kubeconfig);
|
return this.decodeKubeconfig(kubeconfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async deleteKubeconfig(vkeService: any, kubeconfigId?: string) {
|
// private async deleteKubeconfig(vkeService: any, kubeconfigId?: string) {
|
||||||
if (!kubeconfigId) {
|
// if (!kubeconfigId) {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
const clusterId = this.getClusterId();
|
// const clusterId = this.getClusterId();
|
||||||
try {
|
// try {
|
||||||
await vkeService.request({
|
// await vkeService.request({
|
||||||
action: "DeleteKubeconfigs",
|
// action: "DeleteKubeconfigs",
|
||||||
method: "POST",
|
// method: "POST",
|
||||||
body: {
|
// body: {
|
||||||
ClusterId: clusterId,
|
// ClusterId: clusterId,
|
||||||
Ids: [kubeconfigId],
|
// Ids: [kubeconfigId],
|
||||||
},
|
// },
|
||||||
});
|
// });
|
||||||
this.logger.info(`已删除临时Kubeconfig:${kubeconfigId}`);
|
// this.logger.info(`已删除临时Kubeconfig:${kubeconfigId}`);
|
||||||
} catch (e) {
|
// } catch (e) {
|
||||||
this.logger.warn(`删除临时Kubeconfig失败:${e.message || e}`);
|
// this.logger.warn(`删除临时Kubeconfig失败:${e.message || e}`);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
private getClusterId() {
|
private getClusterId() {
|
||||||
if (!this.clusterId) {
|
if (!this.clusterId) {
|
||||||
@@ -476,24 +499,19 @@ export class VolcengineDeployToVKE extends AbstractPlusTaskPlugin {
|
|||||||
}
|
}
|
||||||
const access = await this.getAccess<VolcengineAccess>(this.accessId);
|
const access = await this.getAccess<VolcengineAccess>(this.accessId);
|
||||||
const vkeService = await this.getVkeService(access);
|
const vkeService = await this.getVkeService(access);
|
||||||
const kubeconfigId = await this.createKubeconfig(vkeService);
|
const userId = await access.getUserId();
|
||||||
|
const kubeconfig = await this.getOrCreateKubeconfig({ vkeService, userId });
|
||||||
try {
|
const k8sClient = new this.K8sClient({
|
||||||
const kubeconfig = await this.getKubeconfig(vkeService, kubeconfigId);
|
kubeConfigStr: kubeconfig,
|
||||||
const k8sClient = new this.K8sClient({
|
logger: this.logger,
|
||||||
kubeConfigStr: kubeconfig,
|
skipTLSVerify: this.skipTLSVerify,
|
||||||
logger: this.logger,
|
});
|
||||||
skipTLSVerify: this.skipTLSVerify,
|
const res = await k8sClient.getSecrets({ namespace: this.namespace || "default" });
|
||||||
});
|
const list = res.body?.items || res.items || [];
|
||||||
const res = await k8sClient.getSecrets({ namespace: this.namespace || "default" });
|
return list.map((item: any) => ({
|
||||||
const list = res.body?.items || res.items || [];
|
label: item.metadata.name,
|
||||||
return list.map((item: any) => ({
|
value: item.metadata.name,
|
||||||
label: item.metadata.name,
|
}));
|
||||||
value: item.metadata.name,
|
|
||||||
}));
|
|
||||||
} finally {
|
|
||||||
await this.deleteKubeconfig(vkeService, kubeconfigId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user