@leitwerk-dev/worker-runners 0.1.2 → 0.1.4

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.
Files changed (57) hide show
  1. package/dist/docker-engine-client.d.ts +86 -0
  2. package/dist/docker-engine-client.d.ts.map +1 -0
  3. package/dist/docker-engine-client.js +10 -0
  4. package/dist/docker-engine-client.js.map +1 -0
  5. package/dist/docker-engine-http-client.d.ts +10 -11
  6. package/dist/docker-engine-http-client.d.ts.map +1 -0
  7. package/dist/docker-engine-http-client.js +201 -14
  8. package/dist/docker-engine-http-client.js.map +1 -1
  9. package/dist/docker-worker-runner.d.ts +7 -9
  10. package/dist/docker-worker-runner.d.ts.map +1 -0
  11. package/dist/docker-worker-runner.js +235 -9
  12. package/dist/docker-worker-runner.js.map +1 -1
  13. package/dist/fake-docker-engine-client.d.ts +40 -0
  14. package/dist/fake-docker-engine-client.d.ts.map +1 -0
  15. package/dist/fake-docker-engine-client.js +131 -0
  16. package/dist/fake-docker-engine-client.js.map +1 -0
  17. package/dist/index.d.ts +12 -96
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +9 -284
  20. package/dist/index.js.map +1 -1
  21. package/dist/kubernetes-api-client.d.ts +71 -0
  22. package/dist/kubernetes-api-client.d.ts.map +1 -0
  23. package/dist/kubernetes-api-client.js +142 -0
  24. package/dist/kubernetes-api-client.js.map +1 -0
  25. package/dist/kubernetes-http-client.d.ts +5 -8
  26. package/dist/kubernetes-http-client.d.ts.map +1 -0
  27. package/dist/kubernetes-http-client.js +287 -298
  28. package/dist/kubernetes-http-client.js.map +1 -1
  29. package/dist/kubernetes-manifests.d.ts +232 -0
  30. package/dist/kubernetes-manifests.d.ts.map +1 -0
  31. package/dist/kubernetes-manifests.js +370 -0
  32. package/dist/kubernetes-manifests.js.map +1 -0
  33. package/dist/kubernetes-worker-runner.d.ts +6 -7
  34. package/dist/kubernetes-worker-runner.d.ts.map +1 -0
  35. package/dist/kubernetes-worker-runner.js +161 -9
  36. package/dist/kubernetes-worker-runner.js.map +1 -1
  37. package/dist/local-worker-runner.d.ts +7 -9
  38. package/dist/local-worker-runner.d.ts.map +1 -0
  39. package/dist/local-worker-runner.js +108 -11
  40. package/dist/local-worker-runner.js.map +1 -1
  41. package/dist/process-volume-retention.d.ts +21 -0
  42. package/dist/process-volume-retention.d.ts.map +1 -0
  43. package/dist/process-volume-retention.js +46 -0
  44. package/dist/process-volume-retention.js.map +1 -0
  45. package/dist/runner-utils.d.ts +16 -0
  46. package/dist/runner-utils.d.ts.map +1 -0
  47. package/dist/runner-utils.js +36 -0
  48. package/dist/runner-utils.js.map +1 -0
  49. package/dist/types.d.ts +16 -16
  50. package/dist/types.d.ts.map +1 -0
  51. package/dist/types.js +11 -0
  52. package/dist/types.js.map +1 -1
  53. package/dist/worker-labels.d.ts +50 -0
  54. package/dist/worker-labels.d.ts.map +1 -0
  55. package/dist/worker-labels.js +83 -0
  56. package/dist/worker-labels.js.map +1 -0
  57. package/package.json +3 -3
@@ -1,307 +1,296 @@
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
- });
1
+ import { readFileSync } from "node:fs";
2
+ import http from "node:http";
3
+ import https from "node:https";
4
+ import { URL } from "node:url";
5
+ import { mapKubernetesPodExit, } from "./kubernetes-manifests.js";
6
+ const IN_CLUSTER_TOKEN_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/token";
7
+ const IN_CLUSTER_CA_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt";
8
+ const DEFAULT_IN_CLUSTER_API = "https://kubernetes.default.svc";
9
+ export function createInClusterKubernetesApiClient(options = {}) {
10
+ const token = options.bearerToken ?? readFileSync(IN_CLUSTER_TOKEN_PATH, "utf8").trim();
11
+ const ca = readFileSync(options.caFile ?? IN_CLUSTER_CA_PATH);
12
+ return createKubernetesHttpApiClient({
13
+ apiServerUrl: options.apiServerUrl ?? DEFAULT_IN_CLUSTER_API,
14
+ bearerToken: token,
15
+ ca,
16
+ pollIntervalMs: options.pollIntervalMs,
17
+ });
23
18
  }
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")
19
+ export function createKubernetesHttpApiClient(options) {
20
+ const baseUrl = new URL(options.apiServerUrl);
21
+ const pollIntervalMs = options.pollIntervalMs ?? 2_000;
22
+ async function request(input) {
23
+ const url = new URL(input.path, baseUrl);
24
+ const serialized = input.body === undefined ? undefined : JSON.stringify(input.body);
25
+ const headers = {
26
+ Accept: "application/json",
27
+ ...(serialized !== undefined
28
+ ? { "Content-Type": input.contentType ?? "application/json" }
29
+ : {}),
30
+ ...(options.bearerToken ? { Authorization: `Bearer ${options.bearerToken}` } : {}),
31
+ };
32
+ const transport = url.protocol === "http:" ? http : https;
33
+ const response = await new Promise((resolve, reject) => {
34
+ const req = transport.request(url, {
35
+ method: input.method,
36
+ headers,
37
+ ...(url.protocol === "https:" && options.ca ? { ca: options.ca } : {}),
38
+ }, (res) => {
39
+ const chunks = [];
40
+ res.on("data", (chunk) => chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
41
+ res.on("end", () => {
42
+ resolve({
43
+ statusCode: res.statusCode ?? 0,
44
+ text: Buffer.concat(chunks).toString("utf8"),
45
+ });
46
+ });
54
47
  });
55
- });
48
+ req.on("error", reject);
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
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
- );
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
+ };
67
63
  }
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
- });
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;
108
- return {
109
- name,
110
- namespace,
111
- labels: { ...item.metadata?.labels ?? {} },
112
- phase: item.status?.phase
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({
64
+ async function exists(path) {
65
+ const result = await request({
283
66
  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 {
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 });
293
77
  }
294
- }, pollIntervalMs);
295
- timer.unref?.();
296
- return () => {
297
- stopped = true;
298
- clearInterval(timer);
299
- };
78
+ else {
79
+ await request({
80
+ method: "PATCH",
81
+ path: input.patchPath,
82
+ contentType: "application/merge-patch+json",
83
+ body: input.patchBody,
84
+ });
85
+ }
86
+ }
87
+ function labelSelector(labels) {
88
+ return Object.entries(labels)
89
+ .map(([key, value]) => `${key}=${value}`)
90
+ .join(",");
300
91
  }
301
- };
92
+ function namespaceSummary(item) {
93
+ const name = item.metadata?.name;
94
+ if (!name)
95
+ return null;
96
+ return { name, labels: { ...(item.metadata?.labels ?? {}) } };
97
+ }
98
+ function podSummary(item) {
99
+ const name = item.metadata?.name;
100
+ const namespace = item.metadata?.namespace;
101
+ if (!name || !namespace)
102
+ return null;
103
+ return {
104
+ name,
105
+ namespace,
106
+ labels: { ...(item.metadata?.labels ?? {}) },
107
+ phase: item.status?.phase,
108
+ };
109
+ }
110
+ function podExitInfo(item) {
111
+ if (!item)
112
+ return { exitCode: 0, signal: null, reason: "Deleted" };
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
+ });
122
+ }
123
+ return {
124
+ async ensureNamespace(manifest) {
125
+ const name = manifest.metadata.name;
126
+ await upsertResource({
127
+ existsPath: `/api/v1/namespaces/${encodeURIComponent(name)}`,
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
+ },
294
+ };
302
295
  }
303
- export {
304
- createInClusterKubernetesApiClient,
305
- createKubernetesHttpApiClient
306
- };
307
296
  //# sourceMappingURL=kubernetes-http-client.js.map