mirror of
https://github.com/certd/certd.git
synced 2026-08-10 09:15:49 +08:00
Merge branch 'v2-dev' of https://github.com/certd/certd into v2-dev
This commit is contained in:
@@ -10,6 +10,7 @@ export type K8sClientOpts = {
|
||||
//暂时没用
|
||||
lookup?: any;
|
||||
skipTLSVerify?: boolean;
|
||||
debug?: boolean;
|
||||
};
|
||||
export class K8sClient {
|
||||
kubeconfig!: KubeConfig;
|
||||
@@ -18,11 +19,13 @@ export class K8sClient {
|
||||
client!: CoreV1Api;
|
||||
logger: ILogger;
|
||||
skipTLSVerify?: boolean;
|
||||
debug?: boolean;
|
||||
constructor(opts: K8sClientOpts) {
|
||||
this.kubeConfigStr = opts.kubeConfigStr;
|
||||
this.logger = opts.logger;
|
||||
this.setLookup(opts.lookup);
|
||||
this.skipTLSVerify = opts.skipTLSVerify;
|
||||
this.debug = opts.debug;
|
||||
this.init();
|
||||
}
|
||||
|
||||
@@ -86,6 +89,9 @@ export class K8sClient {
|
||||
yml.metadata = {};
|
||||
}
|
||||
yml.metadata.resourceVersion = existing.body.metadata.resourceVersion;
|
||||
if (this.debug) {
|
||||
this.logger.info("patch yaml body:", JSON.stringify(yml));
|
||||
}
|
||||
const res = await client.patch(yml);
|
||||
return res?.body;
|
||||
}
|
||||
@@ -126,6 +132,9 @@ export class K8sClient {
|
||||
async createSecret(opts: { namespace: string; body: V1Secret }) {
|
||||
const namespace = opts.namespace || "default";
|
||||
this.logger.info("create secret:", opts.body.metadata);
|
||||
if (this.debug) {
|
||||
this.logger.info("create secret body:", JSON.stringify(opts.body));
|
||||
}
|
||||
const created = await this.client.createNamespacedSecret(namespace, opts.body);
|
||||
this.logger.info("new secrets:", opts.body.metadata);
|
||||
return created.body;
|
||||
@@ -162,6 +171,9 @@ export class K8sClient {
|
||||
},
|
||||
opts.body
|
||||
);
|
||||
if (this.debug) {
|
||||
this.logger.info("create secret:", JSON.stringify(body));
|
||||
}
|
||||
const res = await this.createSecret({ namespace, body });
|
||||
this.logger.info(`secret ${secretName} 已创建`);
|
||||
return res;
|
||||
@@ -173,6 +185,9 @@ export class K8sClient {
|
||||
}
|
||||
|
||||
const newSecret = merge(oldSecret.body, opts.body);
|
||||
if (this.debug) {
|
||||
this.logger.info("patch secret:", JSON.stringify(newSecret));
|
||||
}
|
||||
const res = await this.client.replaceNamespacedSecret(secretName, namespace, newSecret);
|
||||
this.logger.info(`secret ${secretName} 已更新`);
|
||||
return res.body;
|
||||
@@ -207,6 +222,9 @@ export class K8sClient {
|
||||
const client = this.kubeconfig.makeApiClient(NetworkingV1Api);
|
||||
const oldIngress = await client.readNamespacedIngress(ingressName, namespace);
|
||||
const newIngress = merge(oldIngress.body, opts.body);
|
||||
if (this.debug) {
|
||||
this.logger.info("patch ingress:", JSON.stringify(newIngress));
|
||||
}
|
||||
const res = await client.replaceNamespacedIngress(ingressName, namespace, newIngress);
|
||||
|
||||
this.logger.info("ingress patched", opts.body);
|
||||
@@ -222,6 +240,7 @@ export class K8sClient {
|
||||
},
|
||||
};
|
||||
for (const ingress of ingressNames) {
|
||||
this.logger.info(`ingress 开始重启:${ingress}`);
|
||||
await this.patchIngress({ namespace, ingressName: ingress, body });
|
||||
this.logger.info(`ingress已重启:${ingress}`);
|
||||
}
|
||||
|
||||
@@ -144,6 +144,17 @@ export class DeployCertToAliyunAckPlugin extends AbstractTaskPlugin {
|
||||
})
|
||||
createOnNotFound: boolean;
|
||||
|
||||
@TaskInput({
|
||||
title: "调试模式",
|
||||
value: false,
|
||||
component: {
|
||||
name: "a-switch",
|
||||
vModel: "checked",
|
||||
},
|
||||
helper: "是否开启调试模式,开启后将打印更多日志",
|
||||
})
|
||||
debug: boolean;
|
||||
|
||||
K8sClient: any;
|
||||
async onInstance() {
|
||||
const sdk = await import("@certd/lib-k8s");
|
||||
@@ -157,10 +168,14 @@ export class DeployCertToAliyunAckPlugin extends AbstractTaskPlugin {
|
||||
const kubeConfigStr = await this.getKubeConfig(client, clusterId, isPrivateIpAddress);
|
||||
|
||||
this.logger.info("kubeconfig已成功获取");
|
||||
if (this.debug) {
|
||||
this.logger.info("kubeconfig:", kubeConfigStr);
|
||||
}
|
||||
const k8sClient = new this.K8sClient({
|
||||
kubeConfigStr,
|
||||
logger: this.logger,
|
||||
skipTLSVerify: this.skipTLSVerify,
|
||||
debug: this.debug,
|
||||
});
|
||||
await this.patchCertSecret({ cert, k8sClient });
|
||||
|
||||
@@ -173,7 +188,7 @@ export class DeployCertToAliyunAckPlugin extends AbstractTaskPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
async restartIngress(options: { k8sClient: any }) {
|
||||
async restartIngress(options: { k8sClient: any; }) {
|
||||
const { k8sClient } = options;
|
||||
const { namespace } = this;
|
||||
|
||||
@@ -184,6 +199,7 @@ export class DeployCertToAliyunAckPlugin extends AbstractTaskPlugin {
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const ingressList = await k8sClient.getIngressList({ namespace });
|
||||
this.logger.info("ingressList:", ingressList);
|
||||
if (!ingressList || !ingressList.items) {
|
||||
@@ -210,7 +226,7 @@ export class DeployCertToAliyunAckPlugin extends AbstractTaskPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
async patchCertSecret(options: { cert: CertInfo; k8sClient: any }) {
|
||||
async patchCertSecret(options: { cert: CertInfo; k8sClient: any; }) {
|
||||
const { cert, k8sClient } = options;
|
||||
const crt = cert.crt;
|
||||
const key = cert.key;
|
||||
@@ -266,6 +282,9 @@ export class DeployCertToAliyunAckPlugin extends AbstractTaskPlugin {
|
||||
|
||||
try {
|
||||
const res = await client.request(httpMethod, uriPath, queries, body, headers, requestOption);
|
||||
if (this.debug) {
|
||||
this.logger.info("res:", res);
|
||||
}
|
||||
return res.config;
|
||||
} catch (e) {
|
||||
console.error("请求出错:", e);
|
||||
|
||||
+17
-1
@@ -53,6 +53,22 @@ export class JDCloudDeployToCDN extends AbstractTaskPlugin {
|
||||
)
|
||||
domainName!: string | string[];
|
||||
|
||||
@TaskInput({
|
||||
title: "跳转类型",
|
||||
helper: "http与https之间的跳转方式,default:不强制跳转",
|
||||
value: "default",
|
||||
component: {
|
||||
name: "a-select",
|
||||
options: [
|
||||
{ label: "默认", value: "default" },
|
||||
{ label: "强制跳转http", value: "http" },
|
||||
{ label: "强制跳转https", value: "https" },
|
||||
],
|
||||
},
|
||||
required: false,
|
||||
})
|
||||
jumpType;
|
||||
|
||||
async onInstance() {}
|
||||
|
||||
async execute(): Promise<void> {
|
||||
@@ -101,7 +117,7 @@ export class JDCloudDeployToCDN extends AbstractTaskPlugin {
|
||||
httpType: "https",
|
||||
// certificate: certInfo.crt,
|
||||
// rsaKey: certInfo.key,
|
||||
jumpType: "default",
|
||||
jumpType: this.jumpType || "default", // 旧版数据未配置跳转类型时,走默认跳转
|
||||
certFrom: "ssl",
|
||||
sslCertId: certId, // 不用certId 方式,会报证书已存在错误,目前还没找到怎么查询重复证书
|
||||
syncToSsl: false,
|
||||
|
||||
+7
-5
@@ -103,6 +103,9 @@ export class VolcengineDeployToDCDN extends AbstractTaskPlugin {
|
||||
this.certDomains = new CertReader(this.cert).getAllDomains();
|
||||
|
||||
let domainList = this.domainList;
|
||||
if (typeof domainList === "string") {
|
||||
domainList = [domainList];
|
||||
}
|
||||
if (!this.autoMatch) {
|
||||
//手动根据域名部署
|
||||
if (!this.domainList || this.domainList.length === 0) {
|
||||
@@ -128,21 +131,20 @@ export class VolcengineDeployToDCDN extends AbstractTaskPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
//域名十个十个的分割
|
||||
for (let i = 0; i < domainList.length; i += 10) {
|
||||
const batch = domainList.slice(i, i + 10);
|
||||
this.logger.info(`开始部署证书到域名:${batch}`);
|
||||
for (let i = 0; i < domainList.length; i++) {
|
||||
this.logger.info(`开始部署证书到域名:${domainList[i]}`);
|
||||
const res = await service.request({
|
||||
action: "CreateCertBind",
|
||||
method: "POST",
|
||||
body: {
|
||||
DomainNames: batch,
|
||||
DomainNames: [domainList[i]],
|
||||
CertSource: "volc",
|
||||
CertId: certId,
|
||||
},
|
||||
version: "2021-04-01",
|
||||
});
|
||||
this.logger.info(`部署证书到域名成功:`, JSON.stringify(res));
|
||||
await this.ctx.utils.sleep(2000);
|
||||
}
|
||||
|
||||
this.logger.info("部署完成");
|
||||
|
||||
Reference in New Issue
Block a user