@env-phopper/ source-k8s (英语)
从Kubernetes集群读取部署和入侵,然后返回为 plain records (namespace, name, 图像, 标签, 复制品, 主机). 没有 绘制它自己的地图:需要保存哪些工作量以及它们对你的目录的意义 被呼叫者传入.
该船还:
- 一个保持记录流畅的监视器(列表,然后从列表中查看) 资源version,在410上重新列名,并用备份重试;
- 健康检查员
@env-hopper/backend-core健康检查 运行.
安装
npm install @env-hopper/source-k8s @env-hopper/backend-core @kubernetes/client-node@^0.22.3
@env-hopper/backend-core 是同伴依赖,用于健康检查器
类型。 客户职能预计 @kubernetes/client-node 0.22 API (英语).
使用量
import { KubeConfig } from '@kubernetes/client-node'
import { createK8sSource, createK8sWatcher, k8sClientsFromKubeConfig, watchFromKubeConfig } from '@env-hopper/source-k8s'
const kc = new KubeConfig()
kc.loadFromDefault()
const { apps, networking } = k8sClientsFromKubeConfig(kc)
// One-off read
const source = createK8sSource({ cluster: 'production', apps, networking })
const workloads = await source.listWorkloads()
const ingresses = await source.listIngresses()
// Kept current
const watcher = createK8sWatcher({
cluster: 'production',
apps,
networking,
watch: watchFromKubeConfig(kc),
keepWorkload: (w) => w.namespace !== 'kube-system',
onChange: (change) => console.log(change.kind, change.type, change.record.name),
})
watcher.start()
短寿符
当 API 服务器取出一个过期的符号( 从您所显示的云中 ) 身份,比如, 包入KubeConfig, 所以每个请求都要求当前一个:
import { cachedToken, withBearerToken } from '@env-hopper/source-k8s'
const token = cachedToken(async () => ({ token: await mintToken(), expiresAt: new Date(Date.now() + 15 * 60_000) }), { refreshBeforeMs: 5 * 60_000 })
const authed = withBearerToken(kc, token)
const { apps, networking } = k8sClientsFromKubeConfig(authed)
const watch = watchFromKubeConfig(authed)
cachedToken 薄荷薄荷一次,再用这个符号直到 refreshBeforeMs 在此之前
expiresAt;同时调用者共享一分薄荷,失败的薄荷被重审
下一个电话.
健康检查
import { runCheck } from '@env-hopper/backend-core'
import { k8sReadinessChecker } from '@env-hopper/source-k8s'
const result = await runCheck(k8sReadinessChecker, {
cluster: 'production',
namespace: 'shop',
name: 'checkout',
apps, // from k8sClientsFromKubeConfig(kc)
})
replicas === 0数字 :unknown(缩放后,未断出)readyReplicas === replicas数字 :healthy0 < readyReplicas < replicas数字 :degradedreadyReplicas === 0数字 :down
许可证
麻省理工学院