@mittwald/kubernetes
Advanced tools
Comparing version 2.0.0-beta.9 to 2.0.0-beta.10
import { IKubernetesRESTClient } from "./client"; | ||
import { AppsAPI, BatchAPI, CoreAPI, ExtensionsAPI, RBACAPI } from "./apis"; | ||
import { Registry } from "prom-client"; | ||
export interface IKubernetesAPI { | ||
@@ -13,3 +14,4 @@ extend<C>(name: string, customResourceAPI: C): this & C; | ||
private restClient; | ||
constructor(restClient: IKubernetesRESTClient); | ||
private registry; | ||
constructor(restClient: IKubernetesRESTClient, registry: Registry); | ||
private nc<R, K, V, O>(apiBaseURL, resourceBaseURL); | ||
@@ -16,0 +18,0 @@ private c<R, K, V, O>(apiBaseURL, resourceBaseURL); |
27
api.js
@@ -7,10 +7,11 @@ "use strict"; | ||
class KubernetesAPI { | ||
constructor(restClient) { | ||
constructor(restClient, registry) { | ||
this.restClient = restClient; | ||
this.registry = registry; | ||
} | ||
nc(apiBaseURL, resourceBaseURL) { | ||
return new resource_1.NamespacedResourceClient(this.restClient, apiBaseURL, resourceBaseURL); | ||
return new resource_1.NamespacedResourceClient(this.restClient, apiBaseURL, resourceBaseURL, this.registry); | ||
} | ||
c(apiBaseURL, resourceBaseURL) { | ||
return new resource_1.ResourceClient(this.restClient, apiBaseURL, resourceBaseURL); | ||
return new resource_1.ResourceClient(this.restClient, apiBaseURL, resourceBaseURL, this.registry); | ||
} | ||
@@ -32,3 +33,3 @@ extend(name, customResourceAPI) { | ||
services: () => { | ||
const client = new resource_1.NamespacedResourceClient(this.restClient, "/api/v1", "/services"); | ||
const client = new resource_1.NamespacedResourceClient(this.restClient, "/api/v1", "/services", this.registry); | ||
client.supportsCollectionDeletion = false; | ||
@@ -51,4 +52,4 @@ return client; | ||
v1beta1: () => ({ | ||
deployments: () => new resourceAppsV1beta1.DeploymentResourceClient(this.restClient), | ||
statefulSets: () => new resourceAppsV1beta1.StatefulSetResourceClient(this.restClient), | ||
deployments: () => new resourceAppsV1beta1.DeploymentResourceClient(this.restClient, this.registry), | ||
statefulSets: () => new resourceAppsV1beta1.StatefulSetResourceClient(this.restClient, this.registry), | ||
}), | ||
@@ -71,3 +72,3 @@ }; | ||
daemonSets: () => this.nc("/apis/extensions/v1beta1", "/daemonsets"), | ||
deployments: () => new resourceExtensionsV1beta1.DeploymentResourceClient(this.restClient), | ||
deployments: () => new resourceExtensionsV1beta1.DeploymentResourceClient(this.restClient, this.registry), | ||
ingresses: () => this.nc("/apis/extensions/v1beta1", "/ingresses"), | ||
@@ -82,10 +83,10 @@ networkPolicies: () => this.nc("/apis/extensions/v1beta1", "/networkpolicies"), | ||
v1: () => ({ | ||
clusterRoles: () => new resource_1.ResourceClient(this.restClient, "/apis/rbac.authorization.k8s.io/v1", "/clusterroles"), | ||
clusterRoleBindings: () => new resource_1.ResourceClient(this.restClient, "/apis/rbac.authorization.k8s.io/v1", "/clusterrolebindings"), | ||
roles: () => new resource_1.NamespacedResourceClient(this.restClient, "/apis/rbac.authorization.k8s.io/v1", "/roles"), | ||
roleBindings: () => new resource_1.NamespacedResourceClient(this.restClient, "/apis/rbac.authorization.k8s.io/v1", "/rolebindings"), | ||
clusterRoles: () => new resource_1.ResourceClient(this.restClient, "/apis/rbac.authorization.k8s.io/v1", "/clusterroles", this.registry), | ||
clusterRoleBindings: () => new resource_1.ResourceClient(this.restClient, "/apis/rbac.authorization.k8s.io/v1", "/clusterrolebindings", this.registry), | ||
roles: () => new resource_1.NamespacedResourceClient(this.restClient, "/apis/rbac.authorization.k8s.io/v1", "/roles", this.registry), | ||
roleBindings: () => new resource_1.NamespacedResourceClient(this.restClient, "/apis/rbac.authorization.k8s.io/v1", "/rolebindings", this.registry), | ||
}), | ||
v1beta1: () => ({ | ||
roles: () => new resource_1.NamespacedResourceClient(this.restClient, "/apis/rbac.authorization.k8s.io/v1beta1", "/roles"), | ||
roleBindings: () => new resource_1.NamespacedResourceClient(this.restClient, "/apis/rbac.authorization.k8s.io/v1beta1", "/rolebindings"), | ||
roles: () => new resource_1.NamespacedResourceClient(this.restClient, "/apis/rbac.authorization.k8s.io/v1beta1", "/roles", this.registry), | ||
roleBindings: () => new resource_1.NamespacedResourceClient(this.restClient, "/apis/rbac.authorization.k8s.io/v1beta1", "/rolebindings", this.registry), | ||
}), | ||
@@ -92,0 +93,0 @@ }; |
@@ -6,5 +6,6 @@ import { NamespacedResourceClient } from "../../../resource"; | ||
import { DeleteOptions } from "../../../types/meta/v1"; | ||
import { Registry } from "prom-client"; | ||
export declare class DeploymentResourceClient extends NamespacedResourceClient<Deployment, "Deployment", "apps/v1beta1"> { | ||
protected client: IKubernetesRESTClient; | ||
constructor(client: IKubernetesRESTClient); | ||
constructor(client: IKubernetesRESTClient, registry: Registry); | ||
namespace(ns: string): DeploymentResourceClient; | ||
@@ -11,0 +12,0 @@ allNamespaces(): DeploymentResourceClient; |
@@ -13,4 +13,4 @@ "use strict"; | ||
class DeploymentResourceClient extends resource_1.NamespacedResourceClient { | ||
constructor(client) { | ||
super(client, "/apis/apps/v1beta1", "/deployments"); | ||
constructor(client, registry) { | ||
super(client, "/apis/apps/v1beta1", "/deployments", registry); | ||
this.client = client; | ||
@@ -17,0 +17,0 @@ } |
@@ -6,5 +6,6 @@ import { NamespacedResourceClient } from "../../../resource"; | ||
import { DeleteOptions } from "../../../types/meta/v1"; | ||
import { Registry } from "prom-client"; | ||
export declare class StatefulSetResourceClient extends NamespacedResourceClient<StatefulSet, "StatefulSet", "apps/v1beta1"> { | ||
protected client: IKubernetesRESTClient; | ||
constructor(client: IKubernetesRESTClient); | ||
constructor(client: IKubernetesRESTClient, registry: Registry); | ||
namespace(ns: string): StatefulSetResourceClient; | ||
@@ -11,0 +12,0 @@ allNamespaces(): StatefulSetResourceClient; |
@@ -13,4 +13,4 @@ "use strict"; | ||
class StatefulSetResourceClient extends resource_1.NamespacedResourceClient { | ||
constructor(client) { | ||
super(client, "/apis/apps/v1beta1", "/statefulsets"); | ||
constructor(client, registry) { | ||
super(client, "/apis/apps/v1beta1", "/statefulsets", registry); | ||
this.client = client; | ||
@@ -17,0 +17,0 @@ } |
@@ -6,5 +6,6 @@ import { NamespacedResourceClient } from "../../../resource"; | ||
import { DeleteOptions } from "../../../types/meta/v1"; | ||
import { Registry } from "prom-client"; | ||
export declare class DeploymentResourceClient extends NamespacedResourceClient<Deployment, "Deployment", "extensions/v1beta1"> { | ||
protected client: IKubernetesRESTClient; | ||
constructor(client: IKubernetesRESTClient); | ||
constructor(client: IKubernetesRESTClient, registry: Registry); | ||
namespace(ns: string): DeploymentResourceClient; | ||
@@ -11,0 +12,0 @@ allNamespaces(): DeploymentResourceClient; |
@@ -13,4 +13,4 @@ "use strict"; | ||
class DeploymentResourceClient extends resource_1.NamespacedResourceClient { | ||
constructor(client) { | ||
super(client, "/apis/extensions/v1beta1", "/deployments"); | ||
constructor(client, registry) { | ||
super(client, "/apis/extensions/v1beta1", "/deployments", registry); | ||
this.client = client; | ||
@@ -17,0 +17,0 @@ } |
@@ -24,2 +24,3 @@ { | ||
"lodash": "^4.17.4", | ||
"prom-client": "^11.0.0", | ||
"request": "^2.83.0", | ||
@@ -57,3 +58,3 @@ "yamljs": "^0.3.0" | ||
}, | ||
"version": "2.0.0-beta.9" | ||
"version": "2.0.0-beta.10" | ||
} |
@@ -6,2 +6,3 @@ import { IKubernetesRESTClient, WatchOptions, WatchResult } from "./client"; | ||
import { WatchHandle } from "./watch"; | ||
import { Registry } from "prom-client"; | ||
export interface IResourceClient<R extends MetadataObject, K, V, O extends R = R> { | ||
@@ -43,5 +44,7 @@ list(labelSelector?: LabelSelector): Promise<Array<APIObject<K, V> & O>>; | ||
protected resourceBaseURL: string; | ||
private static watchResyncErrorCount; | ||
private static watchOpenCount; | ||
protected baseURL: string; | ||
supportsCollectionDeletion: boolean; | ||
constructor(client: IKubernetesRESTClient, apiBaseURL: string, resourceBaseURL: string); | ||
constructor(client: IKubernetesRESTClient, apiBaseURL: string, resourceBaseURL: string, registry: Registry); | ||
protected urlForResource(r: R): string; | ||
@@ -59,4 +62,5 @@ list(labelSelector?: LabelSelector): Promise<Array<APIObject<K, V> & O>>; | ||
export declare class NamespacedResourceClient<R extends MetadataObject, K, V, O extends R = R> extends ResourceClient<R, K, V, O> implements INamespacedResourceClient<R, K, V, O> { | ||
private registry; | ||
private ns?; | ||
constructor(client: IKubernetesRESTClient, apiBaseURL: string, resourceBaseURL: string, ns?: string); | ||
constructor(client: IKubernetesRESTClient, apiBaseURL: string, resourceBaseURL: string, registry: Registry, ns?: string); | ||
protected urlForResource(r: R): string; | ||
@@ -63,0 +67,0 @@ namespace(ns: string): INamespacedResourceClient<R, K, V, O>; |
@@ -11,2 +11,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const prom_client_1 = require("prom-client"); | ||
class CustomResourceClient { | ||
@@ -54,3 +55,3 @@ constructor(inner, kind, apiVersion) { | ||
class ResourceClient { | ||
constructor(client, apiBaseURL, resourceBaseURL) { | ||
constructor(client, apiBaseURL, resourceBaseURL, registry) { | ||
this.client = client; | ||
@@ -63,2 +64,20 @@ this.apiBaseURL = apiBaseURL; | ||
this.baseURL = apiBaseURL + "/" + resourceBaseURL; | ||
// Metrics need to be static, because there can be multiple ResourceClients, but | ||
// metrics may exist only _once_. | ||
if (!ResourceClient.watchResyncErrorCount) { | ||
ResourceClient.watchResyncErrorCount = new prom_client_1.Counter({ | ||
name: "kubernetes_listwatch_resync_errors", | ||
help: "Amount of resync errors while running listwatches", | ||
registers: [registry], | ||
labelNames: ["baseURL"], | ||
}); | ||
} | ||
if (!ResourceClient.watchOpenCount) { | ||
ResourceClient.watchOpenCount = new prom_client_1.Gauge({ | ||
name: "kubernetes_listwatch_open", | ||
help: "Amount of currently open listwatches", | ||
registers: [registry], | ||
labelNames: ["baseURL"], | ||
}); | ||
} | ||
} | ||
@@ -86,3 +105,4 @@ urlForResource(r) { | ||
let running = true; | ||
const initialized = this.client.get(this.baseURL, opts.labelSelector) | ||
ResourceClient.watchOpenCount.inc({ baseURL: this.baseURL }); | ||
const resync = () => this.client.get(this.baseURL, opts.labelSelector) | ||
.then((list) => { | ||
@@ -95,8 +115,23 @@ resourceVersion = parseInt(list.metadata.resourceVersion, 10); | ||
}); | ||
const initialized = resync(); | ||
initialized.then(() => __awaiter(this, void 0, void 0, function* () { | ||
errorHandler = errorHandler || (() => { }); | ||
let errorCount = 0; | ||
while (running) { | ||
const result = yield this.client.watch(this.baseURL, handler, errorHandler, Object.assign({}, opts, { resourceVersion })); | ||
resourceVersion = Math.max(resourceVersion, result.resourceVersion); | ||
try { | ||
const result = yield this.client.watch(this.baseURL, handler, errorHandler, Object.assign({}, opts, { resourceVersion })); | ||
resourceVersion = Math.max(resourceVersion, result.resourceVersion); | ||
errorCount--; | ||
} | ||
catch (err) { | ||
errorCount++; | ||
ResourceClient.watchResyncErrorCount.inc({ baseURL: this.baseURL }); | ||
if (errorCount > 10) { | ||
ResourceClient.watchOpenCount.dec({ baseURL: this.baseURL }); | ||
throw new Error("more than 10 consecutive errors when watching " + this.baseURL); | ||
} | ||
yield resync(); | ||
} | ||
} | ||
ResourceClient.watchOpenCount.dec({ baseURL: this.baseURL }); | ||
})); | ||
@@ -155,6 +190,7 @@ return { | ||
class NamespacedResourceClient extends ResourceClient { | ||
constructor(client, apiBaseURL, resourceBaseURL, ns) { | ||
constructor(client, apiBaseURL, resourceBaseURL, registry, ns) { | ||
super(client, apiBaseURL, resourceBaseURL, registry); | ||
this.registry = registry; | ||
apiBaseURL = apiBaseURL.replace(/\/$/, ""); | ||
resourceBaseURL = resourceBaseURL.replace(/^\//, "").replace(/\/$/, ""); | ||
super(client, apiBaseURL, resourceBaseURL); | ||
this.ns = ns; | ||
@@ -176,3 +212,3 @@ if (ns) { | ||
namespace(ns) { | ||
const n = new NamespacedResourceClient(this.client, this.apiBaseURL, this.resourceBaseURL, ns); | ||
const n = new NamespacedResourceClient(this.client, this.apiBaseURL, this.resourceBaseURL, this.registry, ns); | ||
n.supportsCollectionDeletion = this.supportsCollectionDeletion; | ||
@@ -182,3 +218,3 @@ return n; | ||
allNamespaces() { | ||
const n = new NamespacedResourceClient(this.client, this.apiBaseURL, this.resourceBaseURL); | ||
const n = new NamespacedResourceClient(this.client, this.apiBaseURL, this.resourceBaseURL, this.registry); | ||
n.supportsCollectionDeletion = this.supportsCollectionDeletion; | ||
@@ -185,0 +221,0 @@ return n; |
@@ -14,5 +14,6 @@ "use strict"; | ||
const api_1 = require("./api"); | ||
const prom_client_1 = require("prom-client"); | ||
const config = new config_1.FileBasedConfig("/home/mhelmich/.kube/config"); | ||
const client = new client_1.KubernetesRESTClient(config); | ||
const api = new api_1.KubernetesAPI(client); | ||
const api = new api_1.KubernetesAPI(client, prom_client_1.register); | ||
(() => __awaiter(this, void 0, void 0, function* () { | ||
@@ -19,0 +20,0 @@ // const pods = await api.pods().namespace("kube-system").list(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
131301
218
2381
8
+ Addedprom-client@^11.0.0
+ Addedbintrees@1.0.2(transitive)
+ Addedprom-client@11.5.3(transitive)
+ Addedtdigest@0.1.2(transitive)