@leitwerk-dev/worker-runners 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/docker-engine-http-client.d.ts +11 -10
- package/dist/docker-engine-http-client.js +14 -201
- package/dist/docker-engine-http-client.js.map +1 -1
- package/dist/docker-worker-runner.d.ts +9 -7
- package/dist/docker-worker-runner.js +9 -235
- package/dist/docker-worker-runner.js.map +1 -1
- package/dist/index.d.ts +96 -12
- package/dist/index.js +284 -9
- package/dist/index.js.map +1 -1
- package/dist/kubernetes-http-client.d.ts +8 -5
- package/dist/kubernetes-http-client.js +298 -287
- package/dist/kubernetes-http-client.js.map +1 -1
- package/dist/kubernetes-worker-runner.d.ts +7 -6
- package/dist/kubernetes-worker-runner.js +9 -161
- package/dist/kubernetes-worker-runner.js.map +1 -1
- package/dist/local-worker-runner.d.ts +9 -7
- package/dist/local-worker-runner.js +11 -108
- package/dist/local-worker-runner.js.map +1 -1
- package/dist/types.d.ts +16 -16
- package/dist/types.js +0 -11
- package/dist/types.js.map +1 -1
- package/package.json +3 -3
- package/dist/docker-engine-client.d.ts +0 -86
- package/dist/docker-engine-client.d.ts.map +0 -1
- package/dist/docker-engine-client.js +0 -10
- package/dist/docker-engine-client.js.map +0 -1
- package/dist/docker-engine-http-client.d.ts.map +0 -1
- package/dist/docker-worker-runner.d.ts.map +0 -1
- package/dist/fake-docker-engine-client.d.ts +0 -40
- package/dist/fake-docker-engine-client.d.ts.map +0 -1
- package/dist/fake-docker-engine-client.js +0 -131
- package/dist/fake-docker-engine-client.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/kubernetes-api-client.d.ts +0 -71
- package/dist/kubernetes-api-client.d.ts.map +0 -1
- package/dist/kubernetes-api-client.js +0 -142
- package/dist/kubernetes-api-client.js.map +0 -1
- package/dist/kubernetes-http-client.d.ts.map +0 -1
- package/dist/kubernetes-manifests.d.ts +0 -232
- package/dist/kubernetes-manifests.d.ts.map +0 -1
- package/dist/kubernetes-manifests.js +0 -370
- package/dist/kubernetes-manifests.js.map +0 -1
- package/dist/kubernetes-worker-runner.d.ts.map +0 -1
- package/dist/local-worker-runner.d.ts.map +0 -1
- package/dist/process-volume-retention.d.ts +0 -21
- package/dist/process-volume-retention.d.ts.map +0 -1
- package/dist/process-volume-retention.js +0 -46
- package/dist/process-volume-retention.js.map +0 -1
- package/dist/runner-utils.d.ts +0 -16
- package/dist/runner-utils.d.ts.map +0 -1
- package/dist/runner-utils.js +0 -36
- package/dist/runner-utils.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/worker-labels.d.ts +0 -50
- package/dist/worker-labels.d.ts.map +0 -1
- package/dist/worker-labels.js +0 -83
- package/dist/worker-labels.js.map +0 -1
|
@@ -1,296 +1,307 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import {
|
|
2
|
+
mapKubernetesPodExit
|
|
3
|
+
} from "./chunk-PM7IYWFL.js";
|
|
4
|
+
import "./chunk-6WACBFXN.js";
|
|
5
|
+
|
|
6
|
+
// src/kubernetes-http-client.ts
|
|
7
|
+
import { readFileSync } from "fs";
|
|
8
|
+
import http from "http";
|
|
9
|
+
import https from "https";
|
|
10
|
+
import { URL } from "url";
|
|
11
|
+
var IN_CLUSTER_TOKEN_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/token";
|
|
12
|
+
var IN_CLUSTER_CA_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt";
|
|
13
|
+
var DEFAULT_IN_CLUSTER_API = "https://kubernetes.default.svc";
|
|
14
|
+
function createInClusterKubernetesApiClient(options = {}) {
|
|
15
|
+
const token = options.bearerToken ?? readFileSync(IN_CLUSTER_TOKEN_PATH, "utf8").trim();
|
|
16
|
+
const ca = readFileSync(options.caFile ?? IN_CLUSTER_CA_PATH);
|
|
17
|
+
return createKubernetesHttpApiClient({
|
|
18
|
+
apiServerUrl: options.apiServerUrl ?? DEFAULT_IN_CLUSTER_API,
|
|
19
|
+
bearerToken: token,
|
|
20
|
+
ca,
|
|
21
|
+
pollIntervalMs: options.pollIntervalMs
|
|
22
|
+
});
|
|
18
23
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (serialized !== undefined)
|
|
50
|
-
req.write(serialized);
|
|
51
|
-
req.end();
|
|
52
|
-
});
|
|
53
|
-
const ok = input.ok ?? [200, 201, 202];
|
|
54
|
-
if (!ok.includes(response.statusCode)) {
|
|
55
|
-
throw new Error(`Kubernetes API ${input.method} ${input.path} failed with HTTP ${response.statusCode}: ${response.text}`);
|
|
56
|
-
}
|
|
57
|
-
const text = response.text.trim();
|
|
58
|
-
return {
|
|
59
|
-
status: response.statusCode,
|
|
60
|
-
text: response.text,
|
|
61
|
-
body: text.length > 0 ? JSON.parse(text) : null,
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
async function exists(path) {
|
|
65
|
-
const result = await request({
|
|
66
|
-
method: "GET",
|
|
67
|
-
path,
|
|
68
|
-
ok: [200, 404],
|
|
69
|
-
});
|
|
70
|
-
return result.status === 404 ? null : result.body;
|
|
71
|
-
}
|
|
72
|
-
/** Idempotent create-or-patch for K8s resources. */
|
|
73
|
-
async function upsertResource(input) {
|
|
74
|
-
const existing = await exists(input.existsPath);
|
|
75
|
-
if (!existing) {
|
|
76
|
-
await request({ method: "POST", path: input.createPath, body: input.createBody });
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
await request({
|
|
80
|
-
method: "PATCH",
|
|
81
|
-
path: input.patchPath,
|
|
82
|
-
contentType: "application/merge-patch+json",
|
|
83
|
-
body: input.patchBody,
|
|
24
|
+
function createKubernetesHttpApiClient(options) {
|
|
25
|
+
const baseUrl = new URL(options.apiServerUrl);
|
|
26
|
+
const pollIntervalMs = options.pollIntervalMs ?? 2e3;
|
|
27
|
+
async function request(input) {
|
|
28
|
+
const url = new URL(input.path, baseUrl);
|
|
29
|
+
const serialized = input.body === void 0 ? void 0 : JSON.stringify(input.body);
|
|
30
|
+
const headers = {
|
|
31
|
+
Accept: "application/json",
|
|
32
|
+
...serialized !== void 0 ? { "Content-Type": input.contentType ?? "application/json" } : {},
|
|
33
|
+
...options.bearerToken ? { Authorization: `Bearer ${options.bearerToken}` } : {}
|
|
34
|
+
};
|
|
35
|
+
const transport = url.protocol === "http:" ? http : https;
|
|
36
|
+
const response = await new Promise((resolve, reject) => {
|
|
37
|
+
const req = transport.request(
|
|
38
|
+
url,
|
|
39
|
+
{
|
|
40
|
+
method: input.method,
|
|
41
|
+
headers,
|
|
42
|
+
...url.protocol === "https:" && options.ca ? { ca: options.ca } : {}
|
|
43
|
+
},
|
|
44
|
+
(res) => {
|
|
45
|
+
const chunks = [];
|
|
46
|
+
res.on(
|
|
47
|
+
"data",
|
|
48
|
+
(chunk) => chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))
|
|
49
|
+
);
|
|
50
|
+
res.on("end", () => {
|
|
51
|
+
resolve({
|
|
52
|
+
statusCode: res.statusCode ?? 0,
|
|
53
|
+
text: Buffer.concat(chunks).toString("utf8")
|
|
84
54
|
});
|
|
55
|
+
});
|
|
85
56
|
}
|
|
57
|
+
);
|
|
58
|
+
req.on("error", reject);
|
|
59
|
+
if (serialized !== void 0) req.write(serialized);
|
|
60
|
+
req.end();
|
|
61
|
+
});
|
|
62
|
+
const ok = input.ok ?? [200, 201, 202];
|
|
63
|
+
if (!ok.includes(response.statusCode)) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Kubernetes API ${input.method} ${input.path} failed with HTTP ${response.statusCode}: ${response.text}`
|
|
66
|
+
);
|
|
86
67
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const statusWithTermination = item.status?.containerStatuses?.find((s) => s.state?.terminated || s.lastState?.terminated);
|
|
114
|
-
const status = statusWithTermination?.state?.terminated ?? statusWithTermination?.lastState?.terminated;
|
|
115
|
-
return mapKubernetesPodExit({
|
|
116
|
-
phase: item.status?.phase,
|
|
117
|
-
reason: status?.reason ?? item.status?.reason,
|
|
118
|
-
exitCode: status?.exitCode,
|
|
119
|
-
signal: status?.signal ? `SIG${status.signal}` : null,
|
|
120
|
-
oomKilled: status?.reason === "OOMKilled",
|
|
121
|
-
});
|
|
68
|
+
const text = response.text.trim();
|
|
69
|
+
return {
|
|
70
|
+
status: response.statusCode,
|
|
71
|
+
text: response.text,
|
|
72
|
+
body: text.length > 0 ? JSON.parse(text) : null
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
async function exists(path) {
|
|
76
|
+
const result = await request({
|
|
77
|
+
method: "GET",
|
|
78
|
+
path,
|
|
79
|
+
ok: [200, 404]
|
|
80
|
+
});
|
|
81
|
+
return result.status === 404 ? null : result.body;
|
|
82
|
+
}
|
|
83
|
+
async function upsertResource(input) {
|
|
84
|
+
const existing = await exists(input.existsPath);
|
|
85
|
+
if (!existing) {
|
|
86
|
+
await request({ method: "POST", path: input.createPath, body: input.createBody });
|
|
87
|
+
} else {
|
|
88
|
+
await request({
|
|
89
|
+
method: "PATCH",
|
|
90
|
+
path: input.patchPath,
|
|
91
|
+
contentType: "application/merge-patch+json",
|
|
92
|
+
body: input.patchBody
|
|
93
|
+
});
|
|
122
94
|
}
|
|
95
|
+
}
|
|
96
|
+
function labelSelector(labels) {
|
|
97
|
+
return Object.entries(labels).map(([key, value]) => `${key}=${value}`).join(",");
|
|
98
|
+
}
|
|
99
|
+
function namespaceSummary(item) {
|
|
100
|
+
const name = item.metadata?.name;
|
|
101
|
+
if (!name) return null;
|
|
102
|
+
return { name, labels: { ...item.metadata?.labels ?? {} } };
|
|
103
|
+
}
|
|
104
|
+
function podSummary(item) {
|
|
105
|
+
const name = item.metadata?.name;
|
|
106
|
+
const namespace = item.metadata?.namespace;
|
|
107
|
+
if (!name || !namespace) return null;
|
|
123
108
|
return {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
createPath: "/api/v1/namespaces",
|
|
129
|
-
createBody: manifest,
|
|
130
|
-
patchPath: `/api/v1/namespaces/${encodeURIComponent(name)}`,
|
|
131
|
-
patchBody: { metadata: { labels: manifest.metadata.labels } },
|
|
132
|
-
});
|
|
133
|
-
},
|
|
134
|
-
async deleteNamespace(name) {
|
|
135
|
-
await request({
|
|
136
|
-
method: "DELETE",
|
|
137
|
-
path: `/api/v1/namespaces/${encodeURIComponent(name)}`,
|
|
138
|
-
ok: [200, 202, 404],
|
|
139
|
-
});
|
|
140
|
-
},
|
|
141
|
-
async listNamespaces(labels) {
|
|
142
|
-
const selector = labelSelector(labels);
|
|
143
|
-
const result = await request({
|
|
144
|
-
method: "GET",
|
|
145
|
-
path: `/api/v1/namespaces?labelSelector=${encodeURIComponent(selector)}`,
|
|
146
|
-
});
|
|
147
|
-
return (result.body?.items ?? []).map(namespaceSummary).filter((item) => item !== null);
|
|
148
|
-
},
|
|
149
|
-
async ensureServiceAccount(name, namespace, labels) {
|
|
150
|
-
const enc = (s) => encodeURIComponent(s);
|
|
151
|
-
await upsertResource({
|
|
152
|
-
existsPath: `/api/v1/namespaces/${enc(namespace)}/serviceaccounts/${enc(name)}`,
|
|
153
|
-
createPath: `/api/v1/namespaces/${enc(namespace)}/serviceaccounts`,
|
|
154
|
-
createBody: {
|
|
155
|
-
apiVersion: "v1",
|
|
156
|
-
kind: "ServiceAccount",
|
|
157
|
-
metadata: { name, namespace, labels },
|
|
158
|
-
automountServiceAccountToken: false,
|
|
159
|
-
},
|
|
160
|
-
patchPath: `/api/v1/namespaces/${enc(namespace)}/serviceaccounts/${enc(name)}`,
|
|
161
|
-
patchBody: { metadata: { labels } },
|
|
162
|
-
});
|
|
163
|
-
},
|
|
164
|
-
async ensureConfigMap(manifest) {
|
|
165
|
-
const name = encodeURIComponent(manifest.metadata.name);
|
|
166
|
-
const namespace = encodeURIComponent(manifest.metadata.namespace);
|
|
167
|
-
await upsertResource({
|
|
168
|
-
existsPath: `/api/v1/namespaces/${namespace}/configmaps/${name}`,
|
|
169
|
-
createPath: `/api/v1/namespaces/${namespace}/configmaps`,
|
|
170
|
-
createBody: manifest,
|
|
171
|
-
patchPath: `/api/v1/namespaces/${namespace}/configmaps/${name}`,
|
|
172
|
-
patchBody: { metadata: { labels: manifest.metadata.labels }, data: manifest.data },
|
|
173
|
-
});
|
|
174
|
-
},
|
|
175
|
-
async getDockerConfigJsonSecret(name, namespace) {
|
|
176
|
-
const result = await request({
|
|
177
|
-
method: "GET",
|
|
178
|
-
path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/secrets/${encodeURIComponent(name)}`,
|
|
179
|
-
ok: [200, 404],
|
|
180
|
-
});
|
|
181
|
-
if (result.status === 404) {
|
|
182
|
-
throw new Error(`Kubernetes image-pull Secret ${namespace}/${name} was not found`);
|
|
183
|
-
}
|
|
184
|
-
if (result.body?.type !== "kubernetes.io/dockerconfigjson") {
|
|
185
|
-
throw new Error(`Kubernetes image-pull Secret ${namespace}/${name} must have type kubernetes.io/dockerconfigjson`);
|
|
186
|
-
}
|
|
187
|
-
const dockerConfigJson = result.body.data?.[".dockerconfigjson"];
|
|
188
|
-
if (!dockerConfigJson) {
|
|
189
|
-
throw new Error(`Kubernetes image-pull Secret ${namespace}/${name} is missing .dockerconfigjson`);
|
|
190
|
-
}
|
|
191
|
-
return dockerConfigJson;
|
|
192
|
-
},
|
|
193
|
-
async ensureDockerConfigJsonSecret(manifest) {
|
|
194
|
-
const namespace = encodeURIComponent(manifest.metadata.namespace);
|
|
195
|
-
const name = encodeURIComponent(manifest.metadata.name);
|
|
196
|
-
const created = await request({
|
|
197
|
-
method: "POST",
|
|
198
|
-
path: `/api/v1/namespaces/${namespace}/secrets`,
|
|
199
|
-
body: manifest,
|
|
200
|
-
ok: [201, 409],
|
|
201
|
-
});
|
|
202
|
-
if (created.status === 409) {
|
|
203
|
-
await request({
|
|
204
|
-
method: "PATCH",
|
|
205
|
-
path: `/api/v1/namespaces/${namespace}/secrets/${name}`,
|
|
206
|
-
contentType: "application/merge-patch+json",
|
|
207
|
-
body: {
|
|
208
|
-
metadata: { labels: manifest.metadata.labels },
|
|
209
|
-
type: manifest.type,
|
|
210
|
-
data: manifest.data,
|
|
211
|
-
},
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
},
|
|
215
|
-
async ensurePersistentVolumeClaim(manifest) {
|
|
216
|
-
const name = encodeURIComponent(manifest.metadata.name);
|
|
217
|
-
const namespace = encodeURIComponent(manifest.metadata.namespace);
|
|
218
|
-
const existing = await exists(`/api/v1/namespaces/${namespace}/persistentvolumeclaims/${name}`);
|
|
219
|
-
if (!existing) {
|
|
220
|
-
await request({
|
|
221
|
-
method: "POST",
|
|
222
|
-
path: `/api/v1/namespaces/${namespace}/persistentvolumeclaims`,
|
|
223
|
-
body: manifest,
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
},
|
|
227
|
-
async deletePersistentVolumeClaim(name, namespace) {
|
|
228
|
-
await request({
|
|
229
|
-
method: "DELETE",
|
|
230
|
-
path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/persistentvolumeclaims/${encodeURIComponent(name)}`,
|
|
231
|
-
ok: [200, 202, 404],
|
|
232
|
-
});
|
|
233
|
-
},
|
|
234
|
-
async createPod(manifest) {
|
|
235
|
-
await request({
|
|
236
|
-
method: "POST",
|
|
237
|
-
path: `/api/v1/namespaces/${encodeURIComponent(manifest.metadata.namespace)}/pods`,
|
|
238
|
-
body: manifest,
|
|
239
|
-
});
|
|
240
|
-
},
|
|
241
|
-
async deletePod(name, namespace, options) {
|
|
242
|
-
await request({
|
|
243
|
-
method: "DELETE",
|
|
244
|
-
path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/pods/${encodeURIComponent(name)}`,
|
|
245
|
-
body: { gracePeriodSeconds: options.gracePeriodSeconds },
|
|
246
|
-
ok: [200, 202, 404],
|
|
247
|
-
});
|
|
248
|
-
},
|
|
249
|
-
async getPod(name, namespace) {
|
|
250
|
-
const result = await request({
|
|
251
|
-
method: "GET",
|
|
252
|
-
path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/pods/${encodeURIComponent(name)}`,
|
|
253
|
-
ok: [200, 404],
|
|
254
|
-
});
|
|
255
|
-
return result.status === 404 ? null : podSummary(result.body ?? {});
|
|
256
|
-
},
|
|
257
|
-
async listPods(namespace, labels) {
|
|
258
|
-
const selector = labelSelector(labels);
|
|
259
|
-
const result = await request({
|
|
260
|
-
method: "GET",
|
|
261
|
-
path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/pods?labelSelector=${encodeURIComponent(selector)}`,
|
|
262
|
-
});
|
|
263
|
-
return (result.body?.items ?? []).map(podSummary).filter((item) => item !== null);
|
|
264
|
-
},
|
|
265
|
-
onPodExit(name, namespace, listener) {
|
|
266
|
-
let stopped = false;
|
|
267
|
-
const timer = setInterval(async () => {
|
|
268
|
-
if (stopped)
|
|
269
|
-
return;
|
|
270
|
-
try {
|
|
271
|
-
const result = await request({
|
|
272
|
-
method: "GET",
|
|
273
|
-
path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/pods/${encodeURIComponent(name)}`,
|
|
274
|
-
ok: [200, 404],
|
|
275
|
-
});
|
|
276
|
-
if (result.status === 404 ||
|
|
277
|
-
["Failed", "Succeeded"].includes(result.body?.status?.phase ?? "")) {
|
|
278
|
-
stopped = true;
|
|
279
|
-
clearInterval(timer);
|
|
280
|
-
listener(podExitInfo(result.status === 404 ? null : result.body));
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
catch {
|
|
284
|
-
// Transient API failures are handled by the next poll; the supervisor's
|
|
285
|
-
// heartbeat/startup watchdogs remain the authoritative failure boundary.
|
|
286
|
-
}
|
|
287
|
-
}, pollIntervalMs);
|
|
288
|
-
timer.unref?.();
|
|
289
|
-
return () => {
|
|
290
|
-
stopped = true;
|
|
291
|
-
clearInterval(timer);
|
|
292
|
-
};
|
|
293
|
-
},
|
|
109
|
+
name,
|
|
110
|
+
namespace,
|
|
111
|
+
labels: { ...item.metadata?.labels ?? {} },
|
|
112
|
+
phase: item.status?.phase
|
|
294
113
|
};
|
|
114
|
+
}
|
|
115
|
+
function podExitInfo(item) {
|
|
116
|
+
if (!item) return { exitCode: 0, signal: null, reason: "Deleted" };
|
|
117
|
+
const statusWithTermination = item.status?.containerStatuses?.find(
|
|
118
|
+
(s) => s.state?.terminated || s.lastState?.terminated
|
|
119
|
+
);
|
|
120
|
+
const status = statusWithTermination?.state?.terminated ?? statusWithTermination?.lastState?.terminated;
|
|
121
|
+
return mapKubernetesPodExit({
|
|
122
|
+
phase: item.status?.phase,
|
|
123
|
+
reason: status?.reason ?? item.status?.reason,
|
|
124
|
+
exitCode: status?.exitCode,
|
|
125
|
+
signal: status?.signal ? `SIG${status.signal}` : null,
|
|
126
|
+
oomKilled: status?.reason === "OOMKilled"
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
async ensureNamespace(manifest) {
|
|
131
|
+
const name = manifest.metadata.name;
|
|
132
|
+
await upsertResource({
|
|
133
|
+
existsPath: `/api/v1/namespaces/${encodeURIComponent(name)}`,
|
|
134
|
+
createPath: "/api/v1/namespaces",
|
|
135
|
+
createBody: manifest,
|
|
136
|
+
patchPath: `/api/v1/namespaces/${encodeURIComponent(name)}`,
|
|
137
|
+
patchBody: { metadata: { labels: manifest.metadata.labels } }
|
|
138
|
+
});
|
|
139
|
+
},
|
|
140
|
+
async deleteNamespace(name) {
|
|
141
|
+
await request({
|
|
142
|
+
method: "DELETE",
|
|
143
|
+
path: `/api/v1/namespaces/${encodeURIComponent(name)}`,
|
|
144
|
+
ok: [200, 202, 404]
|
|
145
|
+
});
|
|
146
|
+
},
|
|
147
|
+
async listNamespaces(labels) {
|
|
148
|
+
const selector = labelSelector(labels);
|
|
149
|
+
const result = await request({
|
|
150
|
+
method: "GET",
|
|
151
|
+
path: `/api/v1/namespaces?labelSelector=${encodeURIComponent(selector)}`
|
|
152
|
+
});
|
|
153
|
+
return (result.body?.items ?? []).map(namespaceSummary).filter((item) => item !== null);
|
|
154
|
+
},
|
|
155
|
+
async ensureServiceAccount(name, namespace, labels) {
|
|
156
|
+
const enc = (s) => encodeURIComponent(s);
|
|
157
|
+
await upsertResource({
|
|
158
|
+
existsPath: `/api/v1/namespaces/${enc(namespace)}/serviceaccounts/${enc(name)}`,
|
|
159
|
+
createPath: `/api/v1/namespaces/${enc(namespace)}/serviceaccounts`,
|
|
160
|
+
createBody: {
|
|
161
|
+
apiVersion: "v1",
|
|
162
|
+
kind: "ServiceAccount",
|
|
163
|
+
metadata: { name, namespace, labels },
|
|
164
|
+
automountServiceAccountToken: false
|
|
165
|
+
},
|
|
166
|
+
patchPath: `/api/v1/namespaces/${enc(namespace)}/serviceaccounts/${enc(name)}`,
|
|
167
|
+
patchBody: { metadata: { labels } }
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
async ensureConfigMap(manifest) {
|
|
171
|
+
const name = encodeURIComponent(manifest.metadata.name);
|
|
172
|
+
const namespace = encodeURIComponent(manifest.metadata.namespace);
|
|
173
|
+
await upsertResource({
|
|
174
|
+
existsPath: `/api/v1/namespaces/${namespace}/configmaps/${name}`,
|
|
175
|
+
createPath: `/api/v1/namespaces/${namespace}/configmaps`,
|
|
176
|
+
createBody: manifest,
|
|
177
|
+
patchPath: `/api/v1/namespaces/${namespace}/configmaps/${name}`,
|
|
178
|
+
patchBody: { metadata: { labels: manifest.metadata.labels }, data: manifest.data }
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
async getDockerConfigJsonSecret(name, namespace) {
|
|
182
|
+
const result = await request({
|
|
183
|
+
method: "GET",
|
|
184
|
+
path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/secrets/${encodeURIComponent(name)}`,
|
|
185
|
+
ok: [200, 404]
|
|
186
|
+
});
|
|
187
|
+
if (result.status === 404) {
|
|
188
|
+
throw new Error(`Kubernetes image-pull Secret ${namespace}/${name} was not found`);
|
|
189
|
+
}
|
|
190
|
+
if (result.body?.type !== "kubernetes.io/dockerconfigjson") {
|
|
191
|
+
throw new Error(
|
|
192
|
+
`Kubernetes image-pull Secret ${namespace}/${name} must have type kubernetes.io/dockerconfigjson`
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
const dockerConfigJson = result.body.data?.[".dockerconfigjson"];
|
|
196
|
+
if (!dockerConfigJson) {
|
|
197
|
+
throw new Error(
|
|
198
|
+
`Kubernetes image-pull Secret ${namespace}/${name} is missing .dockerconfigjson`
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
return dockerConfigJson;
|
|
202
|
+
},
|
|
203
|
+
async ensureDockerConfigJsonSecret(manifest) {
|
|
204
|
+
const namespace = encodeURIComponent(manifest.metadata.namespace);
|
|
205
|
+
const name = encodeURIComponent(manifest.metadata.name);
|
|
206
|
+
const created = await request({
|
|
207
|
+
method: "POST",
|
|
208
|
+
path: `/api/v1/namespaces/${namespace}/secrets`,
|
|
209
|
+
body: manifest,
|
|
210
|
+
ok: [201, 409]
|
|
211
|
+
});
|
|
212
|
+
if (created.status === 409) {
|
|
213
|
+
await request({
|
|
214
|
+
method: "PATCH",
|
|
215
|
+
path: `/api/v1/namespaces/${namespace}/secrets/${name}`,
|
|
216
|
+
contentType: "application/merge-patch+json",
|
|
217
|
+
body: {
|
|
218
|
+
metadata: { labels: manifest.metadata.labels },
|
|
219
|
+
type: manifest.type,
|
|
220
|
+
data: manifest.data
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
async ensurePersistentVolumeClaim(manifest) {
|
|
226
|
+
const name = encodeURIComponent(manifest.metadata.name);
|
|
227
|
+
const namespace = encodeURIComponent(manifest.metadata.namespace);
|
|
228
|
+
const existing = await exists(
|
|
229
|
+
`/api/v1/namespaces/${namespace}/persistentvolumeclaims/${name}`
|
|
230
|
+
);
|
|
231
|
+
if (!existing) {
|
|
232
|
+
await request({
|
|
233
|
+
method: "POST",
|
|
234
|
+
path: `/api/v1/namespaces/${namespace}/persistentvolumeclaims`,
|
|
235
|
+
body: manifest
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
async deletePersistentVolumeClaim(name, namespace) {
|
|
240
|
+
await request({
|
|
241
|
+
method: "DELETE",
|
|
242
|
+
path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/persistentvolumeclaims/${encodeURIComponent(name)}`,
|
|
243
|
+
ok: [200, 202, 404]
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
async createPod(manifest) {
|
|
247
|
+
await request({
|
|
248
|
+
method: "POST",
|
|
249
|
+
path: `/api/v1/namespaces/${encodeURIComponent(manifest.metadata.namespace)}/pods`,
|
|
250
|
+
body: manifest
|
|
251
|
+
});
|
|
252
|
+
},
|
|
253
|
+
async deletePod(name, namespace, options2) {
|
|
254
|
+
await request({
|
|
255
|
+
method: "DELETE",
|
|
256
|
+
path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/pods/${encodeURIComponent(name)}`,
|
|
257
|
+
body: { gracePeriodSeconds: options2.gracePeriodSeconds },
|
|
258
|
+
ok: [200, 202, 404]
|
|
259
|
+
});
|
|
260
|
+
},
|
|
261
|
+
async getPod(name, namespace) {
|
|
262
|
+
const result = await request({
|
|
263
|
+
method: "GET",
|
|
264
|
+
path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/pods/${encodeURIComponent(name)}`,
|
|
265
|
+
ok: [200, 404]
|
|
266
|
+
});
|
|
267
|
+
return result.status === 404 ? null : podSummary(result.body ?? {});
|
|
268
|
+
},
|
|
269
|
+
async listPods(namespace, labels) {
|
|
270
|
+
const selector = labelSelector(labels);
|
|
271
|
+
const result = await request({
|
|
272
|
+
method: "GET",
|
|
273
|
+
path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/pods?labelSelector=${encodeURIComponent(selector)}`
|
|
274
|
+
});
|
|
275
|
+
return (result.body?.items ?? []).map(podSummary).filter((item) => item !== null);
|
|
276
|
+
},
|
|
277
|
+
onPodExit(name, namespace, listener) {
|
|
278
|
+
let stopped = false;
|
|
279
|
+
const timer = setInterval(async () => {
|
|
280
|
+
if (stopped) return;
|
|
281
|
+
try {
|
|
282
|
+
const result = await request({
|
|
283
|
+
method: "GET",
|
|
284
|
+
path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/pods/${encodeURIComponent(name)}`,
|
|
285
|
+
ok: [200, 404]
|
|
286
|
+
});
|
|
287
|
+
if (result.status === 404 || ["Failed", "Succeeded"].includes(result.body?.status?.phase ?? "")) {
|
|
288
|
+
stopped = true;
|
|
289
|
+
clearInterval(timer);
|
|
290
|
+
listener(podExitInfo(result.status === 404 ? null : result.body));
|
|
291
|
+
}
|
|
292
|
+
} catch {
|
|
293
|
+
}
|
|
294
|
+
}, pollIntervalMs);
|
|
295
|
+
timer.unref?.();
|
|
296
|
+
return () => {
|
|
297
|
+
stopped = true;
|
|
298
|
+
clearInterval(timer);
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
};
|
|
295
302
|
}
|
|
303
|
+
export {
|
|
304
|
+
createInClusterKubernetesApiClient,
|
|
305
|
+
createKubernetesHttpApiClient
|
|
306
|
+
};
|
|
296
307
|
//# sourceMappingURL=kubernetes-http-client.js.map
|