UNPKG

9.47 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.NamespacedResourceClient = exports.ResourceClient = exports.CustomResourceClient = void 0;
13const client_1 = require("./client");
14const prom_client_1 = require("prom-client");
15const resource_listwatch_1 = require("./resource_listwatch");
16const debug = require("debug")("kubernetes:resource");
17const sleep = (ms) => new Promise(res => setTimeout(res, ms));
18class CustomResourceClient {
19 constructor(inner, kind, apiVersion) {
20 this.inner = inner;
21 this.kind = kind;
22 this.apiVersion = apiVersion;
23 }
24 list(listOptions) {
25 return this.inner.list(listOptions);
26 }
27 get(name) {
28 return this.inner.get(name);
29 }
30 apply(resource) {
31 return this.inner.apply(Object.assign(Object.assign({}, resource), { kind: this.kind, apiVersion: this.apiVersion }));
32 }
33 put(resource) {
34 return this.inner.put(Object.assign(Object.assign({}, resource), { kind: this.kind, apiVersion: this.apiVersion }));
35 }
36 post(resource) {
37 return this.inner.post(Object.assign(Object.assign({}, resource), { kind: this.kind, apiVersion: this.apiVersion }));
38 }
39 delete(resourceOrName, deleteOptions) {
40 return this.inner.delete(resourceOrName, deleteOptions);
41 }
42 deleteMany(opts) {
43 return this.inner.deleteMany(opts);
44 }
45 watch(handler, errorHandler, opts) {
46 return this.inner.watch(handler, errorHandler, opts);
47 }
48 listWatch(handler, errorHandler, opts) {
49 return this.inner.listWatch(handler, errorHandler, opts);
50 }
51 patchJSON(resourceOrName, patch) {
52 return this.inner.patchJSON(resourceOrName, patch);
53 }
54 patchStrategic(resourceOrName, patch) {
55 return this.inner.patchStrategic(resourceOrName, patch);
56 }
57 patchMerge(resourceOrName, patch) {
58 return this.inner.patchMerge(resourceOrName, patch);
59 }
60 namespace(ns) {
61 return new CustomResourceClient(this.inner.namespace(ns), this.kind, this.apiVersion);
62 }
63 allNamespaces() {
64 return new CustomResourceClient(this.inner.allNamespaces(), this.kind, this.apiVersion);
65 }
66}
67exports.CustomResourceClient = CustomResourceClient;
68const resourceMetricLabels = ["baseURL"];
69class ResourceClient {
70 constructor(client, apiBaseURL, resourceBaseURL, registry) {
71 this.client = client;
72 this.apiBaseURL = apiBaseURL;
73 this.resourceBaseURL = resourceBaseURL;
74 this.supportsCollectionDeletion = true;
75 this.apiBaseURL = apiBaseURL.replace(/\/$/, "");
76 this.resourceBaseURL = resourceBaseURL.replace(/^\//, "").replace(/\/$/, "");
77 this.baseURL = apiBaseURL + "/" + resourceBaseURL;
78 // Metrics need to be static, because there can be multiple ResourceClients, but
79 // metrics may exist only _once_.
80 if (!ResourceClient.watchResyncErrorCount) {
81 ResourceClient.watchResyncErrorCount = new prom_client_1.Counter({
82 name: "kubernetes_listwatch_resync_errors",
83 help: "Amount of resync errors while running listwatches",
84 registers: [registry],
85 labelNames: resourceMetricLabels,
86 });
87 }
88 if (!ResourceClient.watchOpenCount) {
89 ResourceClient.watchOpenCount = new prom_client_1.Gauge({
90 name: "kubernetes_listwatch_open",
91 help: "Amount of currently open listwatches",
92 registers: [registry],
93 labelNames: resourceMetricLabels,
94 });
95 }
96 }
97 urlForResource(r) {
98 return this.baseURL + "/" + r.metadata.name;
99 }
100 urlForResourceOrName(r) {
101 return (typeof r === "string") ? this.baseURL + "/" + r : this.urlForResource(r);
102 }
103 list(opts) {
104 return __awaiter(this, void 0, void 0, function* () {
105 const list = yield this.client.get(this.baseURL, opts);
106 return list.items || [];
107 });
108 }
109 get(name) {
110 return __awaiter(this, void 0, void 0, function* () {
111 return yield this.client.get(this.baseURL + "/" + name);
112 });
113 }
114 watch(handler, errorHandler, opts = {}) {
115 errorHandler = errorHandler || (() => { });
116 return this.client.watch(this.baseURL, handler, errorHandler, opts);
117 }
118 listWatch(handler, errorHandler, opts = {}) {
119 return new resource_listwatch_1.ListWatch(handler, errorHandler, this.client, this.baseURL, this.resourceBaseURL, opts, { watchOpenCount: ResourceClient.watchOpenCount, watchResyncErrorCount: ResourceClient.watchResyncErrorCount }).run();
120 }
121 apply(resource) {
122 return __awaiter(this, void 0, void 0, function* () {
123 const existing = yield this.client.get(this.urlForResource(resource));
124 if (existing) {
125 return yield this.put(resource);
126 }
127 else {
128 return yield this.post(resource);
129 }
130 });
131 }
132 put(resource) {
133 return __awaiter(this, void 0, void 0, function* () {
134 return yield this.client.put(this.urlForResource(resource), resource);
135 });
136 }
137 post(resource) {
138 return __awaiter(this, void 0, void 0, function* () {
139 return yield this.client.post(this.baseURL, resource);
140 });
141 }
142 patchStrategic(resourceOrName, patch) {
143 return __awaiter(this, void 0, void 0, function* () {
144 return yield this.client.patch(this.urlForResourceOrName(resourceOrName), patch, client_1.patchKindStrategicMergePatch);
145 });
146 }
147 patchMerge(resourceOrName, patch) {
148 return __awaiter(this, void 0, void 0, function* () {
149 return yield this.client.patch(this.urlForResourceOrName(resourceOrName), patch, client_1.patchKindMergePatch);
150 });
151 }
152 patchJSON(resourceOrName, patch) {
153 return __awaiter(this, void 0, void 0, function* () {
154 return yield this.client.patch(this.urlForResourceOrName(resourceOrName), patch, client_1.patchKindJSONPatch);
155 });
156 }
157 delete(resourceOrName, deleteOptions) {
158 return __awaiter(this, void 0, void 0, function* () {
159 let url;
160 if (typeof resourceOrName === "string") {
161 url = this.baseURL + "/" + resourceOrName;
162 }
163 else {
164 url = this.urlForResource(resourceOrName);
165 }
166 return yield this.client.delete(url, deleteOptions);
167 });
168 }
169 deleteMany(opts) {
170 return __awaiter(this, void 0, void 0, function* () {
171 if (this.supportsCollectionDeletion) {
172 return yield this.client.delete(this.baseURL, opts);
173 }
174 const resources = yield this.list(opts);
175 yield Promise.all(resources.map(r => this.delete(r)));
176 });
177 }
178}
179exports.ResourceClient = ResourceClient;
180class NamespacedResourceClient extends ResourceClient {
181 constructor(client, apiBaseURL, resourceBaseURL, registry, ns) {
182 super(client, apiBaseURL, resourceBaseURL, registry);
183 this.registry = registry;
184 apiBaseURL = apiBaseURL.replace(/\/$/, "");
185 resourceBaseURL = resourceBaseURL.replace(/^\//, "").replace(/\/$/, "");
186 this.ns = ns;
187 if (ns) {
188 this.baseURL = apiBaseURL + "/namespaces/" + ns + "/" + resourceBaseURL;
189 }
190 else {
191 this.baseURL = apiBaseURL + "/" + resourceBaseURL;
192 }
193 }
194 urlForResource(r) {
195 const namespace = r.metadata.namespace || this.ns;
196 if (namespace) {
197 return this.apiBaseURL + "/namespaces/" + namespace + "/" + this.resourceBaseURL + "/" + r.metadata.name;
198 }
199 return this.apiBaseURL + "/" + this.resourceBaseURL + "/" + r.metadata.name;
200 }
201 namespace(ns) {
202 const n = new NamespacedResourceClient(this.client, this.apiBaseURL, this.resourceBaseURL, this.registry, ns);
203 n.supportsCollectionDeletion = this.supportsCollectionDeletion;
204 return n;
205 }
206 allNamespaces() {
207 const n = new NamespacedResourceClient(this.client, this.apiBaseURL, this.resourceBaseURL, this.registry);
208 n.supportsCollectionDeletion = this.supportsCollectionDeletion;
209 return n;
210 }
211 post(resource) {
212 return __awaiter(this, void 0, void 0, function* () {
213 let url = this.baseURL;
214 if (resource.metadata.namespace) {
215 url = this.apiBaseURL + "/namespaces/" + resource.metadata.namespace + "/" + this.resourceBaseURL;
216 }
217 return yield this.client.post(url, resource);
218 });
219 }
220}
221exports.NamespacedResourceClient = NamespacedResourceClient;
222//# sourceMappingURL=resource.js.map
\No newline at end of file