Files
certd/packages/libs/lib-k8s/src/lib/k8s.client.ts
T

120 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-07-15 00:30:33 +08:00
import kubernetesClient from 'kubernetes-client';
2024-07-20 17:35:07 +08:00
//@ts-ignore
2024-07-15 00:30:33 +08:00
import dns from 'dns';
2024-08-06 10:22:28 +08:00
import { ILogger } from '@certd/pipeline';
2022-11-07 23:31:20 +08:00
2024-07-19 15:14:03 +08:00
//@ts-ignore
2022-11-07 23:31:20 +08:00
const { KubeConfig, Client, Request } = kubernetesClient;
export class K8sClient {
kubeConfigStr: string;
lookup!: any;
client!: any;
2024-08-06 10:22:28 +08:00
logger: ILogger;
constructor(kubeConfigStr: string, logger: ILogger) {
2022-11-07 23:31:20 +08:00
this.kubeConfigStr = kubeConfigStr;
2024-08-06 10:22:28 +08:00
this.logger = logger;
2022-11-07 23:31:20 +08:00
this.init();
}
init() {
const kubeconfig = new KubeConfig();
kubeconfig.loadFromString(this.kubeConfigStr);
const reqOpts = { kubeconfig, request: {} } as any;
if (this.lookup) {
reqOpts.request.lookup = this.lookup;
}
const backend = new Request(reqOpts);
2024-07-15 00:30:33 +08:00
this.client = new Client({ backend, version: '1.13' });
2022-11-07 23:31:20 +08:00
}
/**
*
* @param localRecords { [domain]:{ip:'xxx.xx.xxx'} }
*/
setLookup(localRecords: { [key: string]: { ip: string } }) {
this.lookup = (hostnameReq: any, options: any, callback: any) => {
2024-08-06 10:22:28 +08:00
this.logger.info('custom lookup', hostnameReq, localRecords);
2022-11-07 23:31:20 +08:00
if (localRecords[hostnameReq]) {
2024-08-06 10:22:28 +08:00
this.logger.info('local record', hostnameReq, localRecords[hostnameReq]);
2022-11-07 23:31:20 +08:00
callback(null, localRecords[hostnameReq].ip, 4);
} else {
dns.lookup(hostnameReq, options, callback);
}
};
this.init();
}
/**
* 查询 secret列表
* @param opts = {namespace:default}
* @returns secretsList
*/
async getSecret(opts: { namespace: string }) {
2024-07-15 00:30:33 +08:00
const namespace = opts.namespace || 'default';
2022-11-07 23:31:20 +08:00
return await this.client.api.v1.namespaces(namespace).secrets.get();
}
/**
* 创建Secret
* @param opts {namespace:default, body:yamlStr}
* @returns {Promise<*>}
*/
async createSecret(opts: any) {
2024-07-15 00:30:33 +08:00
const namespace = opts.namespace || 'default';
2022-11-07 23:31:20 +08:00
const created = await this.client.api.v1.namespaces(namespace).secrets.post({
body: opts.body,
});
2024-08-06 10:22:28 +08:00
this.logger.info('new secrets:', created);
2022-11-07 23:31:20 +08:00
return created;
}
async updateSecret(opts: any) {
2024-07-15 00:30:33 +08:00
const namespace = opts.namespace || 'default';
2022-11-07 23:31:20 +08:00
const secretName = opts.secretName;
if (secretName == null) {
2024-07-15 00:30:33 +08:00
throw new Error('secretName 不能为空');
2022-11-07 23:31:20 +08:00
}
return await this.client.api.v1.namespaces(namespace).secrets(secretName).put({
body: opts.body,
});
}
async patchSecret(opts: any) {
2024-07-15 00:30:33 +08:00
const namespace = opts.namespace || 'default';
2022-11-07 23:31:20 +08:00
const secretName = opts.secretName;
if (secretName == null) {
2024-07-15 00:30:33 +08:00
throw new Error('secretName 不能为空');
2022-11-07 23:31:20 +08:00
}
return await this.client.api.v1.namespaces(namespace).secrets(secretName).patch({
body: opts.body,
});
}
async getIngressList(opts: any) {
2024-07-15 00:30:33 +08:00
const namespace = opts.namespace || 'default';
2022-11-07 23:31:20 +08:00
return await this.client.apis.extensions.v1beta1.namespaces(namespace).ingresses.get();
}
async getIngress(opts: any) {
2024-07-15 00:30:33 +08:00
const namespace = opts.namespace || 'default';
2022-11-07 23:31:20 +08:00
const ingressName = opts.ingressName;
if (!ingressName) {
2024-07-15 00:30:33 +08:00
throw new Error('ingressName 不能为空');
2022-11-07 23:31:20 +08:00
}
return await this.client.apis.extensions.v1beta1.namespaces(namespace).ingresses(ingressName).get();
}
async patchIngress(opts: any) {
2024-07-15 00:30:33 +08:00
const namespace = opts.namespace || 'default';
2022-11-07 23:31:20 +08:00
const ingressName = opts.ingressName;
if (!ingressName) {
2024-07-15 00:30:33 +08:00
throw new Error('ingressName 不能为空');
2022-11-07 23:31:20 +08:00
}
return await this.client.apis.extensions.v1beta1.namespaces(namespace).ingresses(ingressName).patch({
body: opts.body,
});
}
}