@alchemy.run/kubernetes 0.0.0 → 2.0.0-beta.85

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 (94) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +41 -4
  3. package/lib/BuiltinAdapters.d.ts +43 -0
  4. package/lib/BuiltinAdapters.d.ts.map +1 -0
  5. package/lib/BuiltinAdapters.js +129 -0
  6. package/lib/BuiltinAdapters.js.map +1 -0
  7. package/lib/Client.d.ts +97 -0
  8. package/lib/Client.d.ts.map +1 -0
  9. package/lib/Client.js +312 -0
  10. package/lib/Client.js.map +1 -0
  11. package/lib/ClusterAdapter.d.ts +324 -0
  12. package/lib/ClusterAdapter.d.ts.map +1 -0
  13. package/lib/ClusterAdapter.js +65 -0
  14. package/lib/ClusterAdapter.js.map +1 -0
  15. package/lib/Connection.d.ts +210 -0
  16. package/lib/Connection.d.ts.map +1 -0
  17. package/lib/Connection.js +58 -0
  18. package/lib/Connection.js.map +1 -0
  19. package/lib/Deployment.d.ts +401 -0
  20. package/lib/Deployment.d.ts.map +1 -0
  21. package/lib/Deployment.js +550 -0
  22. package/lib/Deployment.js.map +1 -0
  23. package/lib/HelmChart.d.ts +147 -0
  24. package/lib/HelmChart.d.ts.map +1 -0
  25. package/lib/HelmChart.js +248 -0
  26. package/lib/HelmChart.js.map +1 -0
  27. package/lib/Job.d.ts +333 -0
  28. package/lib/Job.d.ts.map +1 -0
  29. package/lib/Job.js +443 -0
  30. package/lib/Job.js.map +1 -0
  31. package/lib/LocalCluster.d.ts +243 -0
  32. package/lib/LocalCluster.d.ts.map +1 -0
  33. package/lib/LocalCluster.js +359 -0
  34. package/lib/LocalCluster.js.map +1 -0
  35. package/lib/Manifest.d.ts +157 -0
  36. package/lib/Manifest.d.ts.map +1 -0
  37. package/lib/Manifest.js +205 -0
  38. package/lib/Manifest.js.map +1 -0
  39. package/lib/Objects.d.ts +60 -0
  40. package/lib/Objects.d.ts.map +1 -0
  41. package/lib/Objects.js +132 -0
  42. package/lib/Objects.js.map +1 -0
  43. package/lib/Providers.d.ts +24 -0
  44. package/lib/Providers.d.ts.map +1 -0
  45. package/lib/Providers.js +36 -0
  46. package/lib/Providers.js.map +1 -0
  47. package/lib/index.d.ts +10 -0
  48. package/lib/index.d.ts.map +1 -0
  49. package/lib/index.js +10 -0
  50. package/lib/index.js.map +1 -0
  51. package/lib/internal/declared.d.ts +19 -0
  52. package/lib/internal/declared.d.ts.map +1 -0
  53. package/lib/internal/declared.js +435 -0
  54. package/lib/internal/declared.js.map +1 -0
  55. package/lib/internal/helm.d.ts +65 -0
  56. package/lib/internal/helm.d.ts.map +1 -0
  57. package/lib/internal/helm.js +173 -0
  58. package/lib/internal/helm.js.map +1 -0
  59. package/lib/internal/kind.d.ts +17 -0
  60. package/lib/internal/kind.d.ts.map +1 -0
  61. package/lib/internal/kind.js +31 -0
  62. package/lib/internal/kind.js.map +1 -0
  63. package/lib/internal/kubeconfig.d.ts +79 -0
  64. package/lib/internal/kubeconfig.d.ts.map +1 -0
  65. package/lib/internal/kubeconfig.js +222 -0
  66. package/lib/internal/kubeconfig.js.map +1 -0
  67. package/lib/internal/registry.d.ts +44 -0
  68. package/lib/internal/registry.d.ts.map +1 -0
  69. package/lib/internal/registry.js +72 -0
  70. package/lib/internal/registry.js.map +1 -0
  71. package/lib/internal/workload.d.ts +127 -0
  72. package/lib/internal/workload.d.ts.map +1 -0
  73. package/lib/internal/workload.js +411 -0
  74. package/lib/internal/workload.js.map +1 -0
  75. package/package.json +55 -3
  76. package/src/BuiltinAdapters.ts +202 -0
  77. package/src/Client.ts +496 -0
  78. package/src/ClusterAdapter.ts +373 -0
  79. package/src/Connection.ts +234 -0
  80. package/src/Deployment.ts +872 -0
  81. package/src/HelmChart.ts +388 -0
  82. package/src/Job.ts +709 -0
  83. package/src/LocalCluster.ts +631 -0
  84. package/src/Manifest.ts +301 -0
  85. package/src/Objects.ts +213 -0
  86. package/src/Providers.ts +52 -0
  87. package/src/globals.d.ts +2 -0
  88. package/src/index.ts +9 -0
  89. package/src/internal/declared.ts +471 -0
  90. package/src/internal/helm.ts +245 -0
  91. package/src/internal/kind.ts +43 -0
  92. package/src/internal/kubeconfig.ts +364 -0
  93. package/src/internal/registry.ts +108 -0
  94. package/src/internal/workload.ts +504 -0
package/src/Client.ts ADDED
@@ -0,0 +1,496 @@
1
+ import * as https from "node:https";
2
+ import { isPlainObject, unwrapRedacted } from "alchemy/Util/data";
3
+ import { collectRedactedSecrets, redactJson } from "alchemy/Util/Redaction";
4
+ /**
5
+ * Internal Kubernetes API client: transport-agnostic server-side apply and
6
+ * kind discovery for arbitrary (CRD) manifests. Powers
7
+ * `Kubernetes.Manifest`, `Kubernetes.Deployment`, `Kubernetes.Job`,
8
+ * `Kubernetes.HelmChart`, and the `AWS.EKS.Cluster` kubernetes-object
9
+ * binding channel. Not exported from the Kubernetes index.
10
+ *
11
+ * Authentication is delegated to the connection's {@link ClusterAdapter}:
12
+ * every request mints headers through the resolved
13
+ * {@link ClusterTransport}, so short-lived tokens (EKS SigV4 presigns,
14
+ * exec-plugin credentials) stay fresh.
15
+ */
16
+ import * as Data from "effect/Data";
17
+ import * as Effect from "effect/Effect";
18
+ import * as Schedule from "effect/Schedule";
19
+ import {
20
+ ClusterNotFoundError,
21
+ findClusterAdapter,
22
+ type ClusterTransport,
23
+ } from "./ClusterAdapter.ts";
24
+ import type { Connection } from "./Connection.ts";
25
+ import { driftMask, hashDriftSelection } from "./internal/declared.ts";
26
+ import {
27
+ buildKubernetesObjectPathWithSpec,
28
+ chunkByApplyRank,
29
+ DEFAULT_APPLY_RANK,
30
+ kubernetesObjectKey,
31
+ lookupKubernetesKindSpec,
32
+ sortRefsForDelete,
33
+ toKubernetesObjectRef,
34
+ type KubernetesObjectDefinition,
35
+ type KubernetesObjectKindSpec,
36
+ type KubernetesObjectRef,
37
+ } from "./Objects.ts";
38
+
39
+ export class KubernetesClientOperationFailed extends Data.TaggedError(
40
+ "KubernetesClientOperationFailed",
41
+ )<{
42
+ readonly message: string;
43
+ readonly cause?: unknown;
44
+ }> {}
45
+
46
+ export class KubernetesApiError extends Data.TaggedError("KubernetesApiError")<{
47
+ method: string;
48
+ path: string;
49
+ statusCode: number;
50
+ body: string;
51
+ }> {
52
+ override get message(): string {
53
+ return `${this.method} ${this.path} responded ${this.statusCode}: ${
54
+ this.body.length > 0 ? this.body.slice(0, 1000) : "(empty body)"
55
+ }`;
56
+ }
57
+ }
58
+
59
+ /** An object ref that cannot be mapped to a REST path (e.g. a namespaced kind without a namespace). */
60
+ export class KubernetesObjectPathError extends Data.TaggedError("KubernetesObjectPathError")<{
61
+ message: string;
62
+ cause?: unknown;
63
+ }> {}
64
+
65
+ const fieldManager = "alchemy";
66
+
67
+ /** Deadline for auth-header minting and for each HTTP attempt. */
68
+ const requestTimeout = "10 seconds";
69
+
70
+ /**
71
+ * Resolve the {@link ClusterTransport} for a connection through its
72
+ * registered adapter.
73
+ */
74
+ export const connectCluster = (connection: Connection) =>
75
+ findClusterAdapter(connection.auth.kind).pipe(
76
+ Effect.flatMap((adapter) => adapter.connect(connection)),
77
+ );
78
+
79
+ /** 404 from the apiserver, including a removed CRD's discovery 404. */
80
+ export class KubernetesNotFound extends Data.TaggedError("KubernetesNotFound")<{
81
+ method: string;
82
+ path: string;
83
+ body: string;
84
+ }> {
85
+ override get message(): string {
86
+ return `${this.method} ${this.path} responded 404: ${
87
+ this.body.length > 0 ? this.body.slice(0, 1000) : "(empty body)"
88
+ }`;
89
+ }
90
+ }
91
+
92
+ /**
93
+ * The effect's result when the cluster exists. `ClusterNotFoundError`
94
+ * becomes `undefined`; auth and HTTP failures stay failures.
95
+ */
96
+ export const ifClusterExists = <A, E, R>(effect: Effect.Effect<A, ClusterNotFoundError | E, R>) =>
97
+ effect.pipe(Effect.catchTag("Kubernetes.ClusterNotFoundError", () => Effect.undefined));
98
+
99
+ const requestJson = Effect.fn(function* ({
100
+ transport,
101
+ method,
102
+ path,
103
+ body,
104
+ redactions = [],
105
+ }: {
106
+ transport: ClusterTransport;
107
+ method: string;
108
+ path: string;
109
+ body?: Record<string, unknown>;
110
+ /** Secrets already plaintext in `body` (Helm renders them). */
111
+ redactions?: readonly string[];
112
+ }) {
113
+ // Outside the attempt retry: a hung token mint must not be retried as
114
+ // if it were a refused connection.
115
+ const headers = yield* transport.headers.pipe(Effect.timeout(requestTimeout));
116
+ const url = new URL(path, transport.endpoint);
117
+ // JSON.stringify(Redacted) emits the display placeholder. Unwrap only at
118
+ // the wire so plans and state keep the wrapper.
119
+ const prepared = yield* Effect.sync(() => {
120
+ const payload = body === undefined ? undefined : JSON.stringify(unwrapRedacted(body));
121
+ return {
122
+ payload,
123
+ contentLength: payload === undefined ? undefined : Buffer.byteLength(payload),
124
+ ca:
125
+ transport.certificateAuthorityData === undefined
126
+ ? undefined
127
+ : Buffer.from(transport.certificateAuthorityData, "base64").toString("utf8"),
128
+ secrets: [...(body === undefined ? [] : collectRedactedSecrets(body)), ...redactions],
129
+ };
130
+ });
131
+
132
+ const response = yield* Effect.callback<
133
+ { readonly statusCode: number; readonly body: string },
134
+ Error
135
+ >((resume, signal) => {
136
+ const request = https.request(
137
+ {
138
+ signal,
139
+ protocol: url.protocol,
140
+ hostname: url.hostname,
141
+ port: url.port || 443,
142
+ path: `${url.pathname}${url.search}`,
143
+ method,
144
+ headers: {
145
+ ...headers,
146
+ Accept: "application/json",
147
+ ...(prepared.payload === undefined
148
+ ? {}
149
+ : {
150
+ "Content-Type": "application/apply-patch+yaml",
151
+ "Content-Length": prepared.contentLength,
152
+ }),
153
+ },
154
+ ...(prepared.ca === undefined ? {} : { ca: prepared.ca }),
155
+ ...(transport.clientCert
156
+ ? {
157
+ cert: transport.clientCert.certificate,
158
+ key: transport.clientCert.key,
159
+ }
160
+ : {}),
161
+ ...(transport.insecureSkipTlsVerify ? { rejectUnauthorized: false } : {}),
162
+ },
163
+ (incoming) => {
164
+ const chunks: Uint8Array[] = [];
165
+ incoming.on("data", (chunk: Uint8Array) => {
166
+ chunks.push(chunk);
167
+ });
168
+ incoming.on("error", (error) => {
169
+ resume(Effect.fail(error));
170
+ });
171
+ incoming.on("aborted", () => {
172
+ resume(
173
+ Effect.fail(
174
+ new KubernetesClientOperationFailed({
175
+ message: `Kubernetes ${method} ${path} response aborted`,
176
+ }),
177
+ ),
178
+ );
179
+ });
180
+ incoming.on("end", () => {
181
+ if (incoming.complete === false) {
182
+ resume(
183
+ Effect.fail(
184
+ new KubernetesClientOperationFailed({
185
+ message: `Kubernetes ${method} ${path} response ended before completion`,
186
+ }),
187
+ ),
188
+ );
189
+ return;
190
+ }
191
+ const statusCode = incoming.statusCode ?? 500;
192
+ resume(
193
+ Effect.sync(() => ({
194
+ statusCode,
195
+ body: Buffer.concat(chunks).toString("utf8"),
196
+ })),
197
+ );
198
+ });
199
+ },
200
+ );
201
+ request.on("error", (error) => {
202
+ resume(
203
+ Effect.fail(
204
+ error instanceof Error
205
+ ? error
206
+ : new KubernetesClientOperationFailed({ message: String(error) }),
207
+ ),
208
+ );
209
+ });
210
+ if (prepared.payload !== undefined) request.write(prepared.payload);
211
+ request.end();
212
+ // Bun's TLS client ignores AbortSignal. Destroying the socket releases it.
213
+ // Effect.callback already drops a second resume, and this finalizer runs
214
+ // on the same interrupt that aborts `signal`.
215
+ return Effect.sync(() => {
216
+ request.destroy();
217
+ request.socket?.destroy();
218
+ });
219
+ }).pipe(
220
+ Effect.timeout(requestTimeout),
221
+ Effect.mapError(
222
+ (error) =>
223
+ new KubernetesClientOperationFailed({
224
+ message: `Failed Kubernetes ${method} ${path}: ${error instanceof Error ? error.message : String(error)}`,
225
+ }),
226
+ ),
227
+ // Transport-level failures (ECONNREFUSED/ECONNRESET/ETIMEDOUT/DNS,
228
+ // the per-attempt deadline) are transient — a fresh managed endpoint's
229
+ // load balancer can refuse connections for a short window after the
230
+ // cluster reports ready. Every request here is idempotent (GET / SSA
231
+ // PATCH / DELETE), so retry them. HTTP status errors are classified
232
+ // below and are not retried here.
233
+ Effect.retry({
234
+ schedule: Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(8)]),
235
+ }),
236
+ );
237
+
238
+ if (response.statusCode < 200 || response.statusCode >= 300) {
239
+ // Error bodies can echo the request. Scrub only failures —
240
+ // a successful dry-run is the object we compare for drift.
241
+ const responseBody = yield* Effect.sync(() => redactJson(response.body, prepared.secrets));
242
+ if (response.statusCode === 404) {
243
+ return yield* new KubernetesNotFound({ method, path, body: responseBody });
244
+ }
245
+ return yield* new KubernetesApiError({
246
+ method,
247
+ path,
248
+ statusCode: response.statusCode,
249
+ body: responseBody,
250
+ });
251
+ }
252
+
253
+ if (!response.body.trim()) return undefined;
254
+ return yield* Effect.sync(() => {
255
+ try {
256
+ return JSON.parse(response.body) as unknown;
257
+ } catch {
258
+ return response.body;
259
+ }
260
+ });
261
+ });
262
+
263
+ // ─────────────────────────────────────────────────────── kind discovery ──
264
+
265
+ interface ApiResourceList {
266
+ resources?: {
267
+ name?: string;
268
+ kind?: string;
269
+ namespaced?: boolean;
270
+ }[];
271
+ }
272
+
273
+ // One resolution per (endpoint, apiVersion, kind) per process. Plain cached
274
+ // values (no finalizers), so module scope is safe.
275
+ const discoveredKinds = new Map<string, KubernetesObjectKindSpec>();
276
+
277
+ /**
278
+ * Resolve the REST mapping (plural + scope) for an arbitrary kind: static
279
+ * table fast path, then the Kubernetes discovery API (`/apis/{g}/{v}` or
280
+ * `/api/v1`). This is what lets `Kubernetes.Manifest` apply any CRD.
281
+ */
282
+ export const resolveKindSpec = Effect.fn(function* ({
283
+ transport,
284
+ input,
285
+ }: {
286
+ transport: ClusterTransport;
287
+ input: Pick<KubernetesObjectRef, "apiVersion" | "kind">;
288
+ }) {
289
+ const staticSpec = lookupKubernetesKindSpec(input);
290
+ if (staticSpec) return staticSpec;
291
+
292
+ const cacheKey = `${transport.endpoint}|${input.apiVersion}|${input.kind}`;
293
+ const cached = discoveredKinds.get(cacheKey);
294
+ if (cached) return cached;
295
+
296
+ const discoveryPath = input.apiVersion.includes("/")
297
+ ? `/apis/${input.apiVersion}`
298
+ : `/api/${input.apiVersion}`;
299
+
300
+ const listed = (yield* requestJson({
301
+ transport,
302
+ method: "GET",
303
+ path: discoveryPath,
304
+ })) as ApiResourceList;
305
+
306
+ const resource = listed.resources?.find(
307
+ (candidate) =>
308
+ candidate.kind === input.kind &&
309
+ typeof candidate.name === "string" &&
310
+ !candidate.name.includes("/"),
311
+ );
312
+
313
+ if (!resource?.name) {
314
+ return yield* new KubernetesNotFound({
315
+ method: "GET",
316
+ path: discoveryPath,
317
+ body: `Kind '${input.kind}' not found in API group '${input.apiVersion}'`,
318
+ });
319
+ }
320
+
321
+ const spec: KubernetesObjectKindSpec = {
322
+ plural: resource.name,
323
+ scope: resource.namespaced ? "Namespaced" : "Cluster",
324
+ applyRank: DEFAULT_APPLY_RANK,
325
+ };
326
+ discoveredKinds.set(cacheKey, spec);
327
+ return spec;
328
+ });
329
+
330
+ const buildPath = Effect.fn(function* ({
331
+ transport,
332
+ object,
333
+ }: {
334
+ transport: ClusterTransport;
335
+ object: KubernetesObjectRef;
336
+ }) {
337
+ const spec = yield* resolveKindSpec({ transport, input: object });
338
+ return yield* Effect.try({
339
+ try: () => buildKubernetesObjectPathWithSpec(object, spec),
340
+ catch: (error) =>
341
+ new KubernetesObjectPathError({
342
+ message: error instanceof Error ? error.message : String(error),
343
+ cause: error,
344
+ }),
345
+ });
346
+ });
347
+
348
+ // ─────────────────────────────────────────────────────────── object ops ──
349
+
350
+ export const readObject = Effect.fn(function* ({
351
+ transport,
352
+ object,
353
+ }: {
354
+ transport: ClusterTransport;
355
+ object: KubernetesObjectRef;
356
+ }) {
357
+ return yield* requestJson({
358
+ transport,
359
+ method: "GET",
360
+ path: yield* buildPath({ transport, object }),
361
+ }).pipe(
362
+ // Drift reads a whole Helm release at once. A 429 is the apiserver
363
+ // asking for a pause, not a failed object.
364
+ Effect.retry({
365
+ while: (error) => error instanceof KubernetesApiError && error.statusCode === 429,
366
+ schedule: Schedule.max([Schedule.spaced("1 second"), Schedule.recurs(4)]),
367
+ }),
368
+ );
369
+ });
370
+
371
+ export const applyObject = Effect.fn(function* ({
372
+ transport,
373
+ object,
374
+ redactions,
375
+ }: {
376
+ transport: ClusterTransport;
377
+ object: KubernetesObjectDefinition;
378
+ /** Plaintext secrets already rendered into `object` (Helm values). */
379
+ redactions?: readonly string[];
380
+ }) {
381
+ const basePath = yield* buildPath({
382
+ transport,
383
+ object: toKubernetesObjectRef(object),
384
+ });
385
+ const path = `${basePath}?fieldManager=${fieldManager}&force=true`;
386
+
387
+ // A freshly provisioned cluster's API server briefly 5xxes while warming
388
+ // up, and the creator's bootstrap access can propagate asynchronously
389
+ // (401/403 in the first minute).
390
+ return yield* requestJson({
391
+ transport,
392
+ method: "PATCH",
393
+ path,
394
+ body: object,
395
+ redactions,
396
+ }).pipe(
397
+ Effect.retry({
398
+ while: (e): boolean =>
399
+ e instanceof KubernetesApiError &&
400
+ (e.statusCode >= 500 ||
401
+ e.statusCode === 429 ||
402
+ e.statusCode === 401 ||
403
+ e.statusCode === 403),
404
+ schedule: Schedule.max([Schedule.spaced("6 seconds"), Schedule.recurs(10)]),
405
+ }),
406
+ );
407
+ });
408
+
409
+ export const deleteObject = Effect.fn(function* ({
410
+ transport,
411
+ object,
412
+ }: {
413
+ transport: ClusterTransport;
414
+ object: KubernetesObjectRef;
415
+ }) {
416
+ yield* buildPath({ transport, object }).pipe(
417
+ Effect.flatMap((path) =>
418
+ requestJson({
419
+ transport,
420
+ method: "DELETE",
421
+ // batch/v1 Jobs orphan their pods on DELETE unless a propagation
422
+ // policy is set; Background matches kubectl's default.
423
+ path: `${path}?propagationPolicy=Background`,
424
+ }),
425
+ ),
426
+ Effect.catchTag("KubernetesNotFound", () => Effect.void),
427
+ );
428
+ });
429
+
430
+ export const reconcileObjects = Effect.fn(function* ({
431
+ transport,
432
+ previousObjects,
433
+ desiredObjects,
434
+ redactions,
435
+ }: {
436
+ transport: ClusterTransport;
437
+ previousObjects: ReadonlyArray<KubernetesObjectRef>;
438
+ desiredObjects: ReadonlyArray<KubernetesObjectDefinition>;
439
+ redactions?: readonly string[];
440
+ }) {
441
+ const desiredRefs = desiredObjects.map(toKubernetesObjectRef);
442
+ const desiredKeys = new Set(desiredRefs.map(kubernetesObjectKey));
443
+
444
+ const removedObjects = previousObjects.filter(
445
+ (object) => !desiredKeys.has(kubernetesObjectKey(object)),
446
+ );
447
+
448
+ for (const object of sortRefsForDelete(removedObjects)) {
449
+ yield* deleteObject({
450
+ transport,
451
+ object,
452
+ });
453
+ }
454
+
455
+ const baselines = new Map<string, { baselineHash: string; driftMask: unknown }>();
456
+ for (const chunk of chunkByApplyRank(desiredObjects)) {
457
+ yield* Effect.forEach(
458
+ chunk,
459
+ (object) =>
460
+ Effect.gen(function* () {
461
+ const applied = yield* applyObject({
462
+ transport,
463
+ object,
464
+ redactions,
465
+ });
466
+ const ref = toKubernetesObjectRef(object);
467
+ const witnessed = isPlainObject(applied) ? applied : undefined;
468
+ baselines.set(kubernetesObjectKey(ref), {
469
+ driftMask: driftMask(object, witnessed),
470
+ baselineHash: yield* hashDriftSelection(object, witnessed ?? object),
471
+ });
472
+ }),
473
+ { concurrency: "unbounded" },
474
+ );
475
+ }
476
+
477
+ return desiredRefs.map((ref) => ({
478
+ ...ref,
479
+ ...baselines.get(kubernetesObjectKey(ref)),
480
+ }));
481
+ });
482
+
483
+ export const deleteObjects = Effect.fn(function* ({
484
+ transport,
485
+ objects,
486
+ }: {
487
+ transport: ClusterTransport;
488
+ objects: ReadonlyArray<KubernetesObjectRef>;
489
+ }) {
490
+ for (const object of sortRefsForDelete(objects)) {
491
+ yield* deleteObject({
492
+ transport,
493
+ object,
494
+ });
495
+ }
496
+ });