@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 496 lines 15.3 KB
Raw
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 });