@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, 06:00 UTC

main
ts 644 lines 23 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import * as Kubernetes from "@alchemy.run/kubernetes";
3 import type { Connection } from "@alchemy.run/kubernetes/Connection";
4 import { describe, expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as Core from "@alchemy.run/test/Core";
7 import * as NodeSocketServer from "@effect/platform-node/NodeSocketServer";
8 import * as Drift from "alchemy/Drift";
9 import * as Provider from "alchemy/Provider";
10 import * as Cause from "effect/Cause";
11 import * as Clock from "effect/Clock";
12 import * as Effect from "effect/Effect";
13 import * as Exit from "effect/Exit";
14 import * as Fiber from "effect/Fiber";
15 import * as FileSystem from "effect/FileSystem";
16 import * as Layer from "effect/Layer";
17 import * as Path from "effect/Path";
18 import * as Redacted from "effect/Redacted";
19 import * as Ref from "effect/Ref";
20 import * as Schedule from "effect/Schedule";
21 import {
22 decodeBase64,
23 destroyKindStack,
24 KindTestCluster,
25 kubectl,
26 kubectlGet,
27 removeRbac,
28 restrictedConnection,
29 updateRole,
30 type KindCluster,
31 } from "./fixtures/kind.ts";
32
33 const testOptions = {
34 providers: Layer.mergeAll(AWS.providers(), Kubernetes.providers()),
35 };
36 const { test, beforeAll, afterAll } = Test.make(testOptions);
37
38 const tags = ["provider:kubernetes", "provider:kubernetes:manifest"];
39
40 // Ungated probe: `Manifest` applies in-cluster objects that have no
41 // cloud-side enumeration attributing them to alchemy, so `list()` is
42 // intentionally empty. The probe proves the provider is registered and its
43 // record type-checks.
44 test.provider(
45 "list returns an empty array (in-cluster objects)",
46 () =>
47 Effect.gen(function* () {
48 const provider = yield* Provider.findProvider(Kubernetes.Manifest);
49 const all = yield* provider.list();
50 expect(Array.isArray(all)).toBe(true);
51 expect(all).toEqual([]);
52 }),
53 {
54 tags: ["provider:aws", ...tags, "live"],
55 },
56 );
57
58 const configMap = (name: string, data: Record<string, unknown>) => ({
59 apiVersion: "v1",
60 kind: "ConfigMap",
61 metadata: { name, namespace: "default" },
62 data,
63 });
64
65 /**
66 * A local TCP listener that accepts connections and never answers — not
67 * even the TLS handshake. It stands in for an API server whose load
68 * balancer or network has gone dark; it never returns an HTTP response.
69 */
70 const blackhole = Effect.gen(function* () {
71 const server = yield* NodeSocketServer.make({ host: "127.0.0.1", port: 0 });
72 const arrivals = yield* Ref.make<number[]>([]);
73 const open = yield* Ref.make(0);
74 yield* server
75 .run((socket) =>
76 Effect.gen(function* () {
77 const now = yield* Clock.currentTimeMillis;
78 yield* Ref.update(arrivals, (all) => [...all, now]);
79 yield* Ref.update(open, (n) => n + 1);
80 const reader = yield* socket.reader;
81 yield* reader.pull.pipe(Effect.forever);
82 }).pipe(Effect.scoped, Effect.ignore, Effect.ensuring(Ref.update(open, (n) => n - 1))),
83 )
84 .pipe(Effect.forkScoped);
85 const port = server.address._tag === "InetAddressV4" ? server.address.port : 0;
86 return { endpoint: `https://127.0.0.1:${port}`, arrivals, open };
87 });
88
89 test.provider(
90 "a stalled API server fails each attempt at the deadline and interruption releases the socket",
91 (stack) =>
92 Effect.gen(function* () {
93 yield* stack.destroy();
94 const { endpoint, arrivals, open } = yield* blackhole;
95 const cluster: Connection = {
96 endpoint,
97 insecureSkipTlsVerify: true,
98 auth: { kind: "token", token: "unused" },
99 };
100 const deploying = yield* stack
101 .deploy(
102 Kubernetes.Manifest("Stalled", {
103 cluster,
104 manifest: configMap("stalled", { key: "value" }),
105 }),
106 )
107 .pipe(Effect.forkChild);
108
109 // The first attempt hangs; the 10s attempt deadline aborts it and the
110 // retry opens a second connection.
111 const seen = yield* Ref.get(arrivals).pipe(
112 Effect.repeat({
113 schedule: Schedule.spaced("1 second"),
114 until: (all) => all.length >= 2,
115 times: 40,
116 }),
117 );
118 expect(seen.length).toBeGreaterThanOrEqual(2);
119 const gap = seen[1]! - seen[0]!;
120 // 10s attempt deadline + 5s retry spacing.
121 expect(gap).toBeGreaterThan(8_000);
122 expect(gap).toBeLessThan(22_000);
123
124 // Interrupting the stalled deploy returns promptly instead of waiting
125 // out the attempt.
126 expect(yield* Ref.get(open)).toBeGreaterThanOrEqual(1);
127 const before = yield* Clock.currentTimeMillis;
128 yield* Fiber.interrupt(deploying);
129 const after = yield* Clock.currentTimeMillis;
130 expect(after - before).toBeLessThan(3_000);
131
132 // ...and destroys the socket instead of leaving it to the server.
133 const remaining = yield* Ref.get(open).pipe(
134 Effect.repeat({
135 schedule: Schedule.spaced("200 millis"),
136 until: (n) => n === 0,
137 times: 25,
138 }),
139 );
140 expect(remaining).toBe(0);
141
142 yield* stack.destroy();
143 }).pipe(Effect.scoped),
144 { tags: [...tags, "local"], timeout: 90_000 },
145 );
146
147 test.provider(
148 "a stalled credential plugin fails at the header deadline",
149 (stack) =>
150 Effect.gen(function* () {
151 yield* stack.destroy();
152 const fs = yield* FileSystem.FileSystem;
153 const path = yield* Path.Path;
154 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-k8s-exec-" });
155 const marker = path.join(dir, "minted");
156 // A real exec credential plugin: the first call (at connect) mints an
157 // already-expired token, so the request re-mints and the plugin hangs.
158 const credential = JSON.stringify({
159 apiVersion: "client.authentication.k8s.io/v1",
160 kind: "ExecCredential",
161 status: { token: "expired", expirationTimestamp: "2000-01-01T00:00:00Z" },
162 });
163 const cluster: Connection = {
164 endpoint: "https://127.0.0.1:9",
165 insecureSkipTlsVerify: true,
166 auth: {
167 kind: "exec",
168 command: "sh",
169 args: [
170 "-c",
171 `if [ -e "$1" ]; then exec sleep 600; fi; : > "$1"; printf '%s' '${credential}'`,
172 "sh",
173 marker,
174 ],
175 },
176 };
177 const before = yield* Clock.currentTimeMillis;
178 const exit = yield* Effect.exit(
179 stack.deploy(
180 Kubernetes.Manifest("StalledMint", {
181 cluster,
182 manifest: configMap("stalled-mint", { key: "value" }),
183 }),
184 ),
185 );
186 const elapsed = (yield* Clock.currentTimeMillis) - before;
187 expect(Exit.isFailure(exit)).toBe(true);
188 expect(elapsed).toBeGreaterThan(8_000);
189 expect(elapsed).toBeLessThan(25_000);
190 expect(yield* fs.exists(marker)).toBe(true);
191
192 yield* stack.destroy();
193 }).pipe(Effect.scoped),
194 { tags: [...tags, "local"], timeout: 90_000 },
195 );
196
197 const kindStack = Core.scratchStack(testOptions, "KubernetesManifestKind");
198
199 // Creates a real kind cluster (~30s); needs Docker, kind, and kubectl.
200 describe(
201 "Kubernetes Manifest on kind",
202 { tags: [...tags, "provider:kubernetes:localcluster", "live"] },
203 () => {
204 let cluster: KindCluster;
205
206 beforeAll(
207 Effect.gen(function* () {
208 yield* kindStack.destroy();
209 cluster = yield* kindStack.deploy(KindTestCluster("manifest", 5062));
210 }),
211 { timeout: 300_000 },
212 );
213
214 afterAll.skipIf(!!process.env.NO_DESTROY)(
215 Effect.suspend(() => destroyKindStack(kindStack, cluster)),
216 { timeout: 180_000 },
217 );
218
219 test.provider(
220 "Redacted Secret values are applied unwrapped and compared without false drift",
221 (stack) =>
222 Effect.gen(function* () {
223 yield* stack.destroy();
224 const leaf = "manifest-leaf-sentinel";
225 const encoded = "manifest-data-sentinel";
226 const nested = "manifest-nested-sentinel";
227 const program = Effect.gen(function* () {
228 yield* Kubernetes.Manifest("LeafSecret", {
229 cluster: cluster.connection,
230 manifest: {
231 apiVersion: "v1",
232 kind: "Secret",
233 metadata: { name: "leaf-secret", namespace: "default" },
234 stringData: { token: Redacted.make(leaf) },
235 },
236 });
237 yield* Kubernetes.Manifest("DataSecret", {
238 cluster: cluster.connection,
239 manifest: {
240 apiVersion: "v1",
241 kind: "Secret",
242 metadata: { name: "data-secret", namespace: "default" },
243 data: { token: Redacted.make(Buffer.from(encoded).toString("base64")) },
244 },
245 });
246 yield* Kubernetes.Manifest("ContainerSecret", {
247 cluster: cluster.connection,
248 manifest: {
249 apiVersion: "v1",
250 kind: "Secret",
251 metadata: { name: "container-secret", namespace: "default" },
252 stringData: Redacted.make({ token: leaf, nested }),
253 },
254 });
255 });
256 yield* stack.deploy(program);
257
258 const readToken = (name: string, key = "token") =>
259 kubectlGet(cluster, ["secret", name, "--namespace", "default"]).pipe(
260 Effect.flatMap((secret) => decodeBase64(secret.data[key])),
261 );
262 expect(yield* readToken("leaf-secret")).toBe(leaf);
263 expect(yield* readToken("data-secret")).toBe(encoded);
264 expect(yield* readToken("container-secret")).toBe(leaf);
265 expect(yield* readToken("container-secret", "nested")).toBe(nested);
266
267 // Write-only stringData compares against the stored base64 data.
268 const steady = yield* Drift.detect({ name: stack.name, stage: stack.stage });
269 expect(steady.resources.LeafSecret?.action).toBe("unchanged");
270 expect(steady.resources.DataSecret?.action).toBe("unchanged");
271 expect(steady.resources.ContainerSecret?.action).toBe("unchanged");
272
273 const replaced = yield* Effect.sync(() => Buffer.from("edited").toString("base64"));
274 yield* kubectl(cluster, [
275 "patch",
276 "secret",
277 "leaf-secret",
278 "--namespace",
279 "default",
280 "--type",
281 "merge",
282 "-p",
283 JSON.stringify({ data: { token: replaced } }),
284 ]);
285 const edited = yield* Drift.detect({ name: stack.name, stage: stack.stage });
286 expect(edited.resources.LeafSecret?.action).toBe("drifted");
287 expect(edited.resources.DataSecret?.action).toBe("unchanged");
288 expect(edited.resources.ContainerSecret?.action).toBe("unchanged");
289
290 yield* stack.destroy();
291 for (const name of ["leaf-secret", "data-secret", "container-secret"]) {
292 expect(yield* kubectlGet(cluster, ["secret", name, "--namespace", "default"])).toBe(
293 undefined,
294 );
295 }
296 }),
297 { timeout: 180_000 },
298 );
299
300 test.provider(
301 "an API rejection does not surface Redacted values",
302 (stack) =>
303 Effect.gen(function* () {
304 yield* stack.destroy();
305 // The apiserver echoes an invalid `spec.type` back in its 422 body,
306 // Go-quoted — the quotes exercise the JSON-escaped form too.
307 const leaf = 'leaf-redact-sentinel-"q"';
308 const nested = 'nested-redact-sentinel-"q"';
309 const rejected = (id: string, manifest: Kubernetes.KubernetesManifest) =>
310 Effect.gen(function* () {
311 const exit = yield* Effect.exit(
312 stack.deploy(Kubernetes.Manifest(id, { cluster: cluster.connection, manifest })),
313 );
314 expect(Exit.isFailure(exit)).toBe(true);
315 return Exit.isFailure(exit) ? Cause.pretty(exit.cause) : "";
316 });
317
318 const leafError = yield* rejected("LeafRejected", {
319 apiVersion: "v1",
320 kind: "Service",
321 metadata: { name: "leaf-rejected", namespace: "default" },
322 spec: { type: Redacted.make(leaf), ports: [{ port: 80 }] },
323 });
324 expect(leafError).toContain("spec.type");
325 expect(leafError).toContain("leaf-rejected");
326 expect(leafError).toContain("<redacted>");
327 expect(leafError).not.toContain("redact-sentinel");
328 expect(leafError).not.toContain(Buffer.from(leaf).toString("base64"));
329
330 const containerError = yield* rejected("ContainerRejected", {
331 apiVersion: "v1",
332 kind: "Service",
333 metadata: { name: "container-rejected", namespace: "default" },
334 spec: Redacted.make({ type: nested, ports: [{ port: 80 }] }),
335 });
336 expect(containerError).toContain("spec.type");
337 expect(containerError).toContain("container-rejected");
338 expect(containerError).toContain("<redacted>");
339 expect(containerError).not.toContain("redact-sentinel");
340 expect(containerError).not.toContain(Buffer.from(nested).toString("base64"));
341
342 yield* stack.destroy();
343 }),
344 { timeout: 120_000 },
345 );
346
347 test.provider(
348 "destroy tolerates an object deleted out of band",
349 (stack) =>
350 Effect.gen(function* () {
351 yield* stack.destroy();
352 yield* stack.deploy(
353 Kubernetes.Manifest("Vanished", {
354 cluster: cluster.connection,
355 manifest: configMap("vanished", { key: "value" }),
356 }),
357 );
358 yield* kubectl(cluster, ["delete", "configmap", "vanished", "--namespace", "default"]);
359 yield* stack.destroy();
360 expect(
361 yield* kubectlGet(cluster, ["configmap", "vanished", "--namespace", "default"]),
362 ).toBe(undefined);
363 }),
364 { timeout: 120_000 },
365 );
366
367 test.provider(
368 "destroy surfaces a 403 and leaves the object in place",
369 (stack) =>
370 Effect.gen(function* () {
371 yield* stack.destroy();
372 const account = "manifest-no-delete";
373 const rules = (verbs: string[]) => [
374 { apiGroups: [""], resources: ["configmaps"], verbs },
375 ];
376 const restricted = yield* restrictedConnection(
377 cluster,
378 account,
379 rules(["get", "list", "create", "patch", "update"]),
380 );
381 yield* stack.deploy(
382 Kubernetes.Manifest("Kept", {
383 cluster: restricted,
384 manifest: configMap("kept", { key: "value" }),
385 }),
386 );
387
388 const exit = yield* Effect.exit(stack.destroy());
389 expect(Exit.isFailure(exit)).toBe(true);
390 if (Exit.isFailure(exit)) {
391 expect(Cause.pretty(exit.cause)).toContain("403");
392 }
393 expect(
394 yield* kubectlGet(cluster, ["configmap", "kept", "--namespace", "default"]),
395 ).toBeDefined();
396
397 // Granting delete lets the same destroy converge.
398 yield* updateRole(
399 cluster,
400 account,
401 rules(["get", "list", "create", "patch", "update", "delete"]),
402 );
403 yield* stack
404 .destroy()
405 .pipe(Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 5 }));
406 expect(yield* kubectlGet(cluster, ["configmap", "kept", "--namespace", "default"])).toBe(
407 undefined,
408 );
409 yield* removeRbac(cluster, account);
410 }),
411 { timeout: 120_000 },
412 );
413
414 test.provider(
415 "drift reports declared-field edits and ignores fields alchemy does not own",
416 (stack) =>
417 Effect.gen(function* () {
418 yield* stack.destroy();
419 const image = "registry.k8s.io/pause:3.10";
420 const deployment = (
421 name: string,
422 options: {
423 replicas?: number;
424 annotations?: Record<string, string>;
425 templateAnnotations?: Record<string, string>;
426 spec?: Record<string, unknown>;
427 } = {},
428 ): Kubernetes.KubernetesManifest => ({
429 apiVersion: "apps/v1",
430 kind: "Deployment",
431 metadata: {
432 name,
433 namespace: "default",
434 ...(options.annotations ? { annotations: options.annotations } : {}),
435 },
436 spec: {
437 ...(options.replicas !== undefined ? { replicas: options.replicas } : {}),
438 selector: { matchLabels: { app: name } },
439 template: {
440 metadata: {
441 labels: { app: name },
442 ...(options.templateAnnotations
443 ? { annotations: options.templateAnnotations }
444 : {}),
445 },
446 spec: options.spec ?? { containers: [{ name: "app", image }] },
447 },
448 },
449 });
450 const manifests: Record<string, Kubernetes.KubernetesManifest> = {
451 // Undeclared replicas (an HPA scales them) and canonicalized
452 // quantities ("0.1" → "100m", "1.5Gi" → "1536Mi").
453 Scaled: deployment("drift-scaled", {
454 spec: {
455 containers: [
456 {
457 name: "app",
458 image,
459 resources: { requests: { cpu: "0.1", memory: "1.5Gi" } },
460 },
461 ],
462 },
463 }),
464 Image: deployment("drift-image"),
465 Replicas: deployment("drift-replicas", { replicas: 1 }),
466 RootEdit: deployment("drift-root-edit", { annotations: { app: "web" } }),
467 RootDrop: deployment("drift-root-drop", { annotations: { app: "web" } }),
468 TemplateEdit: deployment("drift-template-edit", {
469 templateAnnotations: { "prometheus.io/scrape": "true" },
470 }),
471 TemplateDrop: deployment("drift-template-drop", {
472 templateAnnotations: { "prometheus.io/scrape": "true" },
473 }),
474 // The controller stamps deployment.kubernetes.io/revision.
475 Controller: deployment("drift-controller", { annotations: { app: "web" } }),
476 InitOrder: deployment("drift-init-order", {
477 spec: {
478 initContainers: [
479 { name: "a", image },
480 { name: "b", image },
481 ],
482 containers: [{ name: "app", image }],
483 },
484 }),
485 DataEdit: configMap("drift-data-edit", { token: "declared" }),
486 Steady: configMap("drift-steady", { token: "declared" }),
487 };
488 const program = Effect.forEach(
489 Object.entries(manifests),
490 ([id, manifest]) => Kubernetes.Manifest(id, { cluster: cluster.connection, manifest }),
491 { discard: true },
492 );
493 yield* stack.deploy(program);
494
495 const detect = Drift.detect({ name: stack.name, stage: stack.stage });
496 const baseline = yield* detect;
497 for (const id of Object.keys(manifests)) {
498 expect({ id, action: baseline.resources[id]?.action }).toEqual({
499 id,
500 action: "unchanged",
501 });
502 }
503
504 const ns = ["--namespace", "default"];
505 yield* kubectl(cluster, [
506 "scale",
507 "deployment",
508 "drift-scaled",
509 "--replicas",
510 "3",
511 ...ns,
512 ]);
513 yield* kubectl(cluster, [
514 "set",
515 "image",
516 "deployment/drift-image",
517 "app=registry.k8s.io/pause:3.9",
518 ...ns,
519 ]);
520 yield* kubectl(cluster, [
521 "scale",
522 "deployment",
523 "drift-replicas",
524 "--replicas",
525 "3",
526 ...ns,
527 ]);
528 yield* kubectl(cluster, [
529 "annotate",
530 "deployment",
531 "drift-root-edit",
532 "app=api",
533 "--overwrite",
534 ...ns,
535 ]);
536 yield* kubectl(cluster, ["annotate", "deployment", "drift-root-drop", "app-", ...ns]);
537 yield* kubectl(cluster, [
538 "patch",
539 "deployment",
540 "drift-template-edit",
541 "--type",
542 "merge",
543 "-p",
544 JSON.stringify({
545 spec: {
546 template: { metadata: { annotations: { "prometheus.io/scrape": "false" } } },
547 },
548 }),
549 ...ns,
550 ]);
551 yield* kubectl(cluster, [
552 "patch",
553 "deployment",
554 "drift-template-drop",
555 "--type",
556 "json",
557 "-p",
558 JSON.stringify([
559 {
560 op: "remove",
561 path: "/spec/template/metadata/annotations/prometheus.io~1scrape",
562 },
563 ]),
564 ...ns,
565 ]);
566 yield* kubectl(cluster, [
567 "annotate",
568 "deployment",
569 "drift-controller",
570 "example.com/owner=someone-else",
571 ...ns,
572 ]);
573 yield* kubectl(cluster, [
574 "patch",
575 "deployment",
576 "drift-init-order",
577 "--type",
578 "json",
579 "-p",
580 JSON.stringify([
581 {
582 op: "replace",
583 path: "/spec/template/spec/initContainers",
584 value: [
585 { name: "b", image },
586 { name: "a", image },
587 ],
588 },
589 ]),
590 ...ns,
591 ]);
592 yield* kubectl(cluster, [
593 "patch",
594 "configmap",
595 "drift-data-edit",
596 "--type",
597 "merge",
598 "-p",
599 JSON.stringify({ data: { token: "edited" } }),
600 ...ns,
601 ]);
602 const controller = yield* kubectlGet(cluster, ["deployment", "drift-controller", ...ns]);
603 expect(controller.metadata.annotations["deployment.kubernetes.io/revision"]).toBe("1");
604 const steadyVersion = (yield* kubectlGet(cluster, ["configmap", "drift-steady", ...ns]))
605 .metadata.resourceVersion;
606
607 const after = yield* detect;
608 const expected: Record<string, "drifted" | "unchanged"> = {
609 Scaled: "unchanged",
610 Image: "drifted",
611 Replicas: "drifted",
612 RootEdit: "drifted",
613 RootDrop: "drifted",
614 TemplateEdit: "drifted",
615 TemplateDrop: "drifted",
616 Controller: "unchanged",
617 InitOrder: "unchanged",
618 DataEdit: "drifted",
619 Steady: "unchanged",
620 };
621 for (const [id, action] of Object.entries(expected)) {
622 expect({ id, action: after.resources[id]?.action }).toEqual({ id, action });
623 }
624 // Drift reads are GETs: the untouched object was not written.
625 expect(
626 (yield* kubectlGet(cluster, ["configmap", "drift-steady", ...ns])).metadata
627 .resourceVersion,
628 ).toBe(steadyVersion);
629
630 yield* stack.destroy();
631 for (const manifest of Object.values(manifests)) {
632 expect(
633 yield* kubectlGet(cluster, [
634 manifest.kind.toLowerCase(),
635 manifest.metadata!.name!,
636 ...ns,
637 ]),
638 ).toBe(undefined);
639 }
640 }),
641 { timeout: 180_000 },
642 );
643 },
644 );