perf: 支持阿里云 ack ingress

This commit is contained in:
xiaojunnuo
2021-12-04 16:57:12 +08:00
parent 6a88dd476e
commit b7c12e6d91
35 changed files with 379 additions and 88380 deletions

View File

@@ -0,0 +1,14 @@
{
"extends": "standard",
"env": {
"mocha": true
},
"overrides": [
{
"files": ["*.test.js", "*.spec.js"],
"rules": {
"no-unused-expressions": "off"
}
}
]
}

View File

@@ -0,0 +1,7 @@
.vscode/
node_modules/
npm-debug.log
yarn-error.log
yarn.lock
package-lock.json
/.idea/

View File

@@ -0,0 +1,23 @@
{
"name": "@certd/plugin-common",
"version": "0.2.0",
"description": "",
"main": "src/index.js",
"type": "module",
"dependencies": {
"kubernetes-client": "^9.0.0"
},
"devDependencies": {
"@certd/certd": "^0.2.0",
"chai": "^4.2.0",
"eslint": "^7.15.0",
"eslint-config-standard": "^16.0.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"mocha": "^8.2.1"
},
"author": "Greper",
"license": "MIT",
"gitHead": "5fbd7742665c0a949333d805153e9b6af91c0a71"
}

View File

@@ -0,0 +1 @@
export { K8sClient } from './lib/k8s.client.js'

View File

@@ -0,0 +1,114 @@
import kubernetesClient from 'kubernetes-client'
import { util } from '@certd/api'
import Request from 'kubernetes-client/backends/request/index.js'
import dns from 'dns'
const { KubeConfig, Client } = kubernetesClient
const logger = util.logger
export class K8sClient {
constructor (kubeConfigStr) {
this.kubeConfigStr = kubeConfigStr
this.init()
}
init () {
const kubeconfig = new KubeConfig()
kubeconfig.loadFromString(this.kubeConfigStr)
const reqOpts = { kubeconfig, request: {} }
if (this.lookup) {
reqOpts.request.lookup = this.lookup
}
const backend = new Request(reqOpts)
this.client = new Client({ backend, version: '1.13' })
}
/**
*
* @param localRecords { [domain]:{ip:'xxx.xx.xxx'} }
*/
setLookup (localRecords) {
this.lookup = (hostnameReq, options, callback) => {
logger.info('custom lookup', hostnameReq, localRecords)
if (localRecords[hostnameReq]) {
logger.info('local record', hostnameReq, localRecords[hostnameReq])
callback(null, localRecords[hostnameReq].ip, 4)
} else {
dns.lookup(hostnameReq, options, callback)
}
}
this.init()
}
/**
* 查询 secret列表
* @param opts = {namespace:default}
* @returns secretsList
*/
async getSecret (opts = {}) {
const namespace = opts.namespace || 'default'
const secrets = await this.client.api.v1.namespaces(namespace).secrets.get()
return secrets
}
/**
* 创建Secret
* @param opts {namespace:default, body:yamlStr}
* @returns {Promise<*>}
*/
async createSecret (opts) {
const namespace = opts.namespace || 'default'
const created = await this.client.api.v1.namespaces(namespace).secrets.post({
body: opts.body
})
logger.info('new secrets:', created)
return created
}
async updateSecret (opts) {
const namespace = opts.namespace || 'default'
const secretName = opts.secretName
if (secretName == null) {
throw new Error('secretName 不能为空')
}
return await this.client.api.v1.namespaces(namespace).secrets(secretName).put({
body: opts.body
})
}
async patchSecret (opts) {
const namespace = opts.namespace || 'default'
const secretName = opts.secretName
if (secretName == null) {
throw new Error('secretName 不能为空')
}
return await this.client.api.v1.namespaces(namespace).secrets(secretName).patch({
body: opts.body
})
}
async getIngressList (opts) {
const namespace = opts.namespace || 'default'
return await this.client.apis.extensions.v1beta1.namespaces(namespace).ingresses.get()
}
async getIngress (opts) {
const namespace = opts.namespace || 'default'
const ingressName = opts.ingressName
if (!ingressName) {
throw new Error('ingressName 不能为空')
}
return await this.client.apis.extensions.v1beta1.namespaces(namespace).ingresses(ingressName).get()
}
async patchIngress (opts) {
const namespace = opts.namespace || 'default'
const ingressName = opts.ingressName
if (!ingressName) {
throw new Error('ingressName 不能为空')
}
return await this.client.apis.extensions.v1beta1.namespaces(namespace).ingresses(ingressName).patch({
body: opts.body
})
}
}