@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 350 lines 12.3 KB
Raw
1 import * as Kubernetes from "@alchemy.run/kubernetes";
2 import { connectCluster, readObject } from "@alchemy.run/kubernetes/Client";
3 import {
4 kindClusterConfig,
5 kindConfigOf,
6 REGISTRY_CONTAINERD_PATCH,
7 } from "@alchemy.run/kubernetes/internal/kind";
8 import { imagePlatformOf } from "@alchemy.run/kubernetes/internal/workload";
9 import { describe, expect } from "@alchemy.run/test";
10 import * as Test from "@alchemy.run/test/Alchemy";
11 import * as Core from "@alchemy.run/test/Core";
12 import * as Provider from "alchemy/Provider";
13 import * as Effect from "effect/Effect";
14 import * as HttpClient from "effect/http/HttpClient";
15 import * as HttpClientRequest from "effect/http/HttpClientRequest";
16 import * as ChildProcess from "effect/process/ChildProcess";
17 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
18 import * as Schedule from "effect/Schedule";
19 import { kubectl } from "./fixtures/kind.ts";
20 import LocalDockerfileJob, { BUILD_SECRET } from "./fixtures/local-dockerfile-job.ts";
21 import LocalEffectJob from "./fixtures/local-job.ts";
22 import LocalEffectServer from "./fixtures/local-server.ts";
23 import { TestLocalCluster } from "./fixtures/local.ts";
24
25 const testOptions = { providers: Kubernetes.providers() };
26 const { test, beforeAll, afterAll } = Test.make(testOptions);
27
28 const tags = ["provider:kubernetes", "provider:kubernetes:localcluster"];
29
30 const attrs = (name: string): Kubernetes.LocalCluster["Attributes"] => ({
31 name,
32 context: `kind-${name}`,
33 kubeconfig: undefined,
34 registry: { server: "localhost:5001" },
35 registryContainer: `${name}-registry`,
36 architecture: "arm64",
37 connection: {
38 auth: { kind: "kubeconfig", context: `kind-${name}` },
39 registry: { server: "localhost:5001" },
40 architecture: "arm64",
41 },
42 });
43
44 test.provider(
45 "diff replaces the cluster when its name or node image changes",
46 () =>
47 Effect.gen(function* () {
48 const provider = yield* Provider.findProvider(Kubernetes.LocalCluster);
49 const base = {
50 id: "Cluster",
51 fqn: "Cluster",
52 instanceId: "instance",
53 oldBindings: [],
54 newBindings: [],
55 output: attrs("dev"),
56 };
57 const renamed = yield* provider.diff!({
58 ...base,
59 olds: { name: "dev" },
60 news: { name: "other" },
61 });
62 expect(renamed?.action).toBe("replace");
63
64 const reimaged = yield* provider.diff!({
65 ...base,
66 olds: { name: "dev" },
67 news: { name: "dev", nodeImage: "kindest/node:v1.33.1" },
68 });
69 expect(reimaged?.action).toBe("replace");
70
71 const ports = [{ containerPort: 30080, hostPort: 8080 }];
72 const reconfigured = yield* provider.diff!({
73 ...base,
74 olds: { name: "dev" },
75 news: {
76 name: "dev",
77 nodes: [{ role: "control-plane", extraPortMappings: ports }],
78 },
79 });
80 expect(reconfigured?.action).toBe("replace");
81
82 const sameConfig = yield* provider.diff!({
83 ...base,
84 olds: {
85 name: "dev",
86 nodes: [{ role: "control-plane", extraPortMappings: ports }],
87 },
88 news: {
89 name: "dev",
90 nodes: [{ extraPortMappings: ports, role: "control-plane" }],
91 },
92 });
93 expect(sameConfig).toBeUndefined();
94
95 const moved = yield* provider.diff!({
96 ...base,
97 olds: { name: "dev" },
98 news: { name: "dev", registryPort: 5002 },
99 });
100 expect(moved).toBeUndefined();
101 }),
102 { tags: ["unit", ...tags, "local"] },
103 );
104
105 test.provider(
106 "user kind config is kept and the registry patch is appended",
107 () =>
108 Effect.sync(() => {
109 expect(kindClusterConfig(kindConfigOf({ name: "dev" }))).toEqual({
110 kind: "Cluster",
111 apiVersion: "kind.x-k8s.io/v1alpha4",
112 containerdConfigPatches: [REGISTRY_CONTAINERD_PATCH],
113 });
114 const config = kindClusterConfig(
115 kindConfigOf({
116 name: "dev",
117 registryPort: 5002,
118 nodeImage: "kindest/node:v1.33.1",
119 nodes: [
120 {
121 role: "control-plane",
122 extraPortMappings: [{ containerPort: 80, hostPort: 8080 }],
123 },
124 { role: "worker" },
125 ],
126 networking: { podSubnet: "10.244.0.0/16" },
127 containerdConfigPatches: ["# user patch"],
128 }),
129 );
130 // Alchemy's own props never reach kind's config.
131 expect(config.name).toBeUndefined();
132 expect(config.registryPort).toBeUndefined();
133 expect(config.nodeImage).toBeUndefined();
134 expect(config.networking).toEqual({ podSubnet: "10.244.0.0/16" });
135 expect(config.kind).toBe("Cluster");
136 expect(config.nodes).toHaveLength(2);
137 expect(config.containerdConfigPatches).toEqual(["# user patch", REGISTRY_CONTAINERD_PATCH]);
138 }),
139 { tags: ["unit", ...tags, "local"] },
140 );
141
142 test.provider(
143 "workloads build for the connection's node architecture by default",
144 () =>
145 Effect.sync(() => {
146 const connection = attrs("dev").connection;
147 expect(imagePlatformOf(undefined, connection)).toBe("linux/arm64");
148 expect(imagePlatformOf("amd64", connection)).toBe("linux/amd64");
149 expect(imagePlatformOf(undefined, undefined)).toBe("linux/amd64");
150 }),
151 { tags: ["unit", ...tags, "local"] },
152 );
153
154 const stack = Core.scratchStack(testOptions, "LocalClusterE2E");
155
156 // Creates a real kind cluster (~30s) and builds images with Docker; needs
157 // Docker and the kind CLI.
158 describe("Kubernetes LocalCluster E2E", { tags: [...tags, "live"] }, () => {
159 let cluster: Kubernetes.LocalCluster["Attributes"];
160 let jobName: string;
161 let dockerfileJobName: string;
162
163 beforeAll(
164 Effect.gen(function* () {
165 yield* stack.destroy();
166 const deployed = yield* stack.deploy(
167 Effect.gen(function* () {
168 const cluster = yield* TestLocalCluster;
169 const job = yield* LocalEffectJob;
170 const dockerfileJob = yield* LocalDockerfileJob;
171 yield* LocalEffectServer;
172 return { cluster, jobName: job.jobName, dockerfileJobName: dockerfileJob.jobName };
173 }),
174 );
175 cluster = deployed.cluster;
176 jobName = deployed.jobName;
177 dockerfileJobName = deployed.dockerfileJobName;
178 }),
179 { timeout: 300_000 },
180 );
181
182 afterAll.skipIf(!!process.env.NO_DESTROY)(
183 Effect.gen(function* () {
184 yield* stack.destroy();
185 // Out-of-band: the kind cluster and its registry container are gone.
186 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
187 const clusters = yield* spawner.lines(ChildProcess.make("kind", ["get", "clusters"]));
188 expect(clusters).not.toContain(cluster.name);
189 const containers = yield* spawner.lines(
190 ChildProcess.make("docker", ["ps", "--all", "--format", "{{.Names}}"]),
191 );
192 expect(containers).not.toContain(cluster.registryContainer);
193 }),
194 { timeout: 180_000 },
195 );
196
197 test.provider("the cluster exposes a connection with its registry and architecture", () =>
198 Effect.sync(() => {
199 expect(cluster.context).toBe("kind-alchemy-test-local");
200 expect(cluster.registry.server).toBe("localhost:5061");
201 expect(cluster.connection.registry?.server).toBe("localhost:5061");
202 expect(["amd64", "arm64"]).toContain(cluster.architecture);
203 }),
204 );
205
206 test.provider(
207 "an Effect Job is built into the local registry and runs to completion",
208 () =>
209 Effect.gen(function* () {
210 const transport = yield* connectCluster(cluster.connection);
211 const job = yield* readObject({
212 transport,
213 object: {
214 apiVersion: "batch/v1",
215 kind: "Job",
216 name: jobName,
217 namespace: "default",
218 },
219 }).pipe(
220 Effect.repeat({
221 schedule: Schedule.spaced("2 seconds"),
222 until: (job) =>
223 ((job as { status?: { succeeded?: number } }).status?.succeeded ?? 0) >= 1,
224 times: 45,
225 }),
226 );
227 const spec = job as {
228 status?: { succeeded?: number };
229 spec?: {
230 template?: { spec?: { containers?: { image?: string }[] } };
231 };
232 };
233 expect(spec.status?.succeeded).toBe(1);
234 expect(spec.spec?.template?.spec?.containers?.[0]?.image).toMatch(/^localhost:5061\//);
235 }),
236 { timeout: 120_000 },
237 );
238
239 test.provider(
240 "a Dockerfile Job is built with its build args and BuildKit secrets",
241 () =>
242 Effect.gen(function* () {
243 const transport = yield* connectCluster(cluster.connection);
244 yield* readObject({
245 transport,
246 object: {
247 apiVersion: "batch/v1",
248 kind: "Job",
249 name: dockerfileJobName,
250 namespace: "default",
251 },
252 }).pipe(
253 Effect.repeat({
254 schedule: Schedule.spaced("2 seconds"),
255 until: (job) =>
256 ((job as { status?: { succeeded?: number } }).status?.succeeded ?? 0) >= 1,
257 times: 45,
258 }),
259 );
260 // The image prints the file its build wrote: the arg, and the
261 // secret's length rather than its value.
262 const logs = yield* kubectl(cluster, ["logs", `job/${dockerfileJobName}`]);
263 expect(logs.trim()).toBe(`hello ${BUILD_SECRET.length}`);
264 }),
265 { timeout: 120_000 },
266 );
267
268 // The build targets the cluster's platform, so the push must too: an
269 // unscoped `docker push` from a containerd image store publishes an OCI
270 // index carrying a Buildx attestation manifest, which strict consumers
271 // (e.g. Lambda) reject.
272 test.provider(
273 "the Effect Job image is pushed as a single manifest for the cluster platform",
274 () =>
275 Effect.gen(function* () {
276 const client = yield* HttpClient.HttpClient;
277 const transport = yield* connectCluster(cluster.connection);
278 const job = (yield* readObject({
279 transport,
280 object: { apiVersion: "batch/v1", kind: "Job", name: jobName, namespace: "default" },
281 })) as { spec?: { template?: { spec?: { containers?: { image?: string }[] } } } };
282 const image = job.spec?.template?.spec?.containers?.[0]?.image ?? "";
283 const match = /^localhost:5061\/(.+):([^:]+)$/.exec(image);
284 expect(match).not.toBeNull();
285 const [, repository, tag] = match!;
286
287 const get = (reference: string) =>
288 client
289 .execute(
290 HttpClientRequest.get(
291 `http://localhost:5061/v2/${repository}/manifests/${reference}`,
292 ).pipe(
293 HttpClientRequest.setHeader(
294 "accept",
295 [
296 "application/vnd.oci.image.index.v1+json",
297 "application/vnd.oci.image.manifest.v1+json",
298 "application/vnd.docker.distribution.manifest.list.v2+json",
299 "application/vnd.docker.distribution.manifest.v2+json",
300 ].join(", "),
301 ),
302 ),
303 )
304 .pipe(Effect.flatMap((response) => response.json));
305
306 const manifest = (yield* get(tag!)) as {
307 mediaType?: string;
308 config?: { digest: string };
309 };
310 expect(manifest.mediaType).not.toMatch(/index|manifest\.list/);
311 const config = (yield* client
312 .get(`http://localhost:5061/v2/${repository}/blobs/${manifest.config!.digest}`)
313 .pipe(Effect.flatMap((response) => response.json))) as {
314 os: string;
315 architecture: string;
316 };
317 expect(`${config.os}/${config.architecture}`).toBe(`linux/${cluster.architecture}`);
318 }),
319 { timeout: 60_000 },
320 );
321
322 test.provider(
323 "an Effect server Deployment becomes available",
324 () =>
325 Effect.gen(function* () {
326 const transport = yield* connectCluster(cluster.connection);
327 const deployment = yield* readObject({
328 transport,
329 object: {
330 apiVersion: "apps/v1",
331 kind: "Deployment",
332 name: "local-effect-server",
333 namespace: "default",
334 },
335 }).pipe(
336 Effect.repeat({
337 schedule: Schedule.spaced("2 seconds"),
338 until: (d) =>
339 ((d as { status?: { availableReplicas?: number } }).status?.availableReplicas ?? 0) >=
340 1,
341 times: 45,
342 }),
343 );
344 expect(
345 (deployment as { status?: { availableReplicas?: number } }).status?.availableReplicas,
346 ).toBe(1);
347 }),
348 { timeout: 120_000 },
349 );
350 });