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
| 1 | import * as Kubernetes from "@alchemy.run/kubernetes"; |
| 2 | import type { Connection } from "@alchemy.run/kubernetes/Connection"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 6 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | |
| 9 | /** |
| 10 | * A kind cluster for one Kubernetes test suite. Each suite gets its own |
| 11 | * cluster name, registry port, and kubeconfig under `.alchemy/` so suites |
| 12 | * run concurrently and never touch `~/.kube/config`. |
| 13 | */ |
| 14 | export const KindTestCluster = (suite: string, registryPort: number) => |
| 15 | Kubernetes.LocalCluster(`KindTestCluster`, { |
| 16 | name: `alchemy-test-${suite}`, |
| 17 | registryPort, |
| 18 | kubeconfig: `.alchemy/test-${suite}.kubeconfig`, |
| 19 | }); |
| 20 | |
| 21 | export type KindCluster = Kubernetes.LocalCluster["Attributes"]; |
| 22 | |
| 23 | /** |
| 24 | * Run kubectl against the test cluster (out-of-band of alchemy) and return |
| 25 | * stdout. A non-zero exit fails with stderr. |
| 26 | */ |
| 27 | export const kubectl = (cluster: KindCluster, args: string[], stdin?: string) => |
| 28 | Effect.gen(function* () { |
| 29 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 30 | const result = yield* ChildProcess.make( |
| 31 | "kubectl", |
| 32 | [ |
| 33 | ...(cluster.kubeconfig ? ["--kubeconfig", cluster.kubeconfig] : []), |
| 34 | "--context", |
| 35 | cluster.context, |
| 36 | ...args, |
| 37 | ], |
| 38 | { |
| 39 | stdin: stdin === undefined ? "ignore" : Stream.make(new TextEncoder().encode(stdin)), |
| 40 | stdout: "pipe", |
| 41 | stderr: "pipe", |
| 42 | }, |
| 43 | ).pipe( |
| 44 | spawner.spawn, |
| 45 | Effect.flatMap((child) => |
| 46 | Effect.all( |
| 47 | { |
| 48 | exitCode: child.exitCode, |
| 49 | stdout: child.stdout.pipe(Stream.decodeText, Stream.mkString), |
| 50 | stderr: child.stderr.pipe(Stream.decodeText, Stream.mkString), |
| 51 | }, |
| 52 | { concurrency: "unbounded" }, |
| 53 | ), |
| 54 | ), |
| 55 | Effect.scoped, |
| 56 | ); |
| 57 | if (result.exitCode !== 0) { |
| 58 | return yield* Effect.fail( |
| 59 | new Error(`kubectl ${args.join(" ")} exited ${String(result.exitCode)}: ${result.stderr}`), |
| 60 | ); |
| 61 | } |
| 62 | return result.stdout; |
| 63 | }); |
| 64 | |
| 65 | /** Names of the kind clusters on this machine (out-of-band of alchemy). */ |
| 66 | export const kindClusters = Effect.gen(function* () { |
| 67 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 68 | return yield* spawner.lines(ChildProcess.make("kind", ["get", "clusters"])); |
| 69 | }); |
| 70 | |
| 71 | /** Names of all Docker containers on this machine. */ |
| 72 | export const dockerContainers = Effect.gen(function* () { |
| 73 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 74 | return yield* spawner.lines( |
| 75 | ChildProcess.make("docker", ["ps", "--all", "--format", "{{.Names}}"]), |
| 76 | ); |
| 77 | }); |
| 78 | |
| 79 | /** |
| 80 | * Destroy the cluster stack and verify out-of-band that the kind cluster |
| 81 | * and its registry container are gone. |
| 82 | */ |
| 83 | export const destroyKindStack = ( |
| 84 | stack: { destroy: () => Effect.Effect<void, any, never> }, |
| 85 | cluster: KindCluster | undefined, |
| 86 | ) => |
| 87 | Effect.gen(function* () { |
| 88 | yield* stack.destroy(); |
| 89 | if (cluster === undefined) return; |
| 90 | expect(yield* kindClusters).not.toContain(cluster.name); |
| 91 | expect(yield* dockerContainers).not.toContain(cluster.registryContainer); |
| 92 | }); |
| 93 | |
| 94 | /** GET an object as JSON through kubectl, or `undefined` when it is absent. */ |
| 95 | export const kubectlGet = (cluster: KindCluster, args: string[]) => |
| 96 | kubectl(cluster, ["get", ...args, "--ignore-not-found", "-o", "json"]).pipe( |
| 97 | Effect.map((stdout) => (stdout.trim() === "" ? undefined : (JSON.parse(stdout) as any))), |
| 98 | ); |
| 99 | |
| 100 | /** Decode a base64 Secret value. */ |
| 101 | export const decodeBase64 = (value: string) => |
| 102 | Effect.sync(() => Buffer.from(value, "base64").toString("utf8")); |
| 103 | |
| 104 | export interface RbacRule { |
| 105 | apiGroups: string[]; |
| 106 | resources: string[]; |
| 107 | verbs: string[]; |
| 108 | } |
| 109 | |
| 110 | const rbacObjects = (name: string, rules: RbacRule[]) => |
| 111 | JSON.stringify({ |
| 112 | apiVersion: "v1", |
| 113 | kind: "List", |
| 114 | items: [ |
| 115 | { |
| 116 | apiVersion: "v1", |
| 117 | kind: "ServiceAccount", |
| 118 | metadata: { name, namespace: "default" }, |
| 119 | }, |
| 120 | { |
| 121 | apiVersion: "rbac.authorization.k8s.io/v1", |
| 122 | kind: "Role", |
| 123 | metadata: { name, namespace: "default" }, |
| 124 | rules, |
| 125 | }, |
| 126 | { |
| 127 | apiVersion: "rbac.authorization.k8s.io/v1", |
| 128 | kind: "RoleBinding", |
| 129 | metadata: { name, namespace: "default" }, |
| 130 | roleRef: { apiGroup: "rbac.authorization.k8s.io", kind: "Role", name }, |
| 131 | subjects: [{ kind: "ServiceAccount", name, namespace: "default" }], |
| 132 | }, |
| 133 | ], |
| 134 | }); |
| 135 | |
| 136 | /** |
| 137 | * Create (or update) a ServiceAccount bound to a Role with exactly `rules`, |
| 138 | * mint a bearer token for it, and return a token-auth {@link Connection} |
| 139 | * against the cluster's API server. Re-running with new rules updates the |
| 140 | * Role in place (RBAC changes apply to the existing token). |
| 141 | */ |
| 142 | export const restrictedConnection = (cluster: KindCluster, name: string, rules: RbacRule[]) => |
| 143 | Effect.gen(function* () { |
| 144 | yield* kubectl(cluster, ["apply", "-f", "-"], rbacObjects(name, rules)); |
| 145 | const token = yield* kubectl(cluster, [ |
| 146 | "create", |
| 147 | "token", |
| 148 | name, |
| 149 | "--namespace", |
| 150 | "default", |
| 151 | "--duration", |
| 152 | "1h", |
| 153 | ]); |
| 154 | const server = yield* kubectl(cluster, [ |
| 155 | "config", |
| 156 | "view", |
| 157 | "--raw", |
| 158 | "--minify", |
| 159 | "-o", |
| 160 | "jsonpath={.clusters[0].cluster.server}", |
| 161 | ]); |
| 162 | const ca = yield* kubectl(cluster, [ |
| 163 | "config", |
| 164 | "view", |
| 165 | "--raw", |
| 166 | "--minify", |
| 167 | "-o", |
| 168 | "jsonpath={.clusters[0].cluster.certificate-authority-data}", |
| 169 | ]); |
| 170 | return { |
| 171 | endpoint: server.trim(), |
| 172 | certificateAuthorityData: ca.trim(), |
| 173 | auth: { kind: "token", token: token.trim() }, |
| 174 | } satisfies Connection as Connection; |
| 175 | }); |
| 176 | |
| 177 | /** Update the Role created by {@link restrictedConnection}. */ |
| 178 | export const updateRole = (cluster: KindCluster, name: string, rules: RbacRule[]) => |
| 179 | kubectl(cluster, ["apply", "-f", "-"], rbacObjects(name, rules)); |
| 180 | |
| 181 | /** Remove the ServiceAccount, Role, and RoleBinding. */ |
| 182 | export const removeRbac = (cluster: KindCluster, name: string) => |
| 183 | kubectl(cluster, [ |
| 184 | "delete", |
| 185 | "serviceaccount,role,rolebinding", |
| 186 | name, |
| 187 | "--namespace", |
| 188 | "default", |
| 189 | "--ignore-not-found", |
| 190 | ]); |