The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { findClusterAdapter } from "@alchemy.run/kubernetes/ClusterAdapter"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as container from "@distilled.cloud/gcp/container_v1"; |
| 7 | import * as Output from "alchemy/Output"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Result from "effect/Result"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { CAPACITY_REGION, CAPACITY_ZONE, CAPACITY_ZONE_2, withGkeClusterSlot } from "../zones.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: GCP.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | // GKE cluster create and delete each take 5-10 minutes. |
| 19 | const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST; |
| 20 | |
| 21 | const waitUntilGone = (name: string) => |
| 22 | container.getProjectsLocationsClusters({ name }).pipe( |
| 23 | Effect.as("found" as const), |
| 24 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 25 | Effect.repeat({ |
| 26 | schedule: Schedule.spaced("3 seconds"), |
| 27 | until: (status) => status === "gone", |
| 28 | times: 10, |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | test.provider( |
| 33 | "lists clusters and treats a missing cluster as NotFound", |
| 34 | (stack) => |
| 35 | Effect.gen(function* () { |
| 36 | yield* stack.destroy(); |
| 37 | |
| 38 | const { project } = yield* GcpEnvironment.current; |
| 39 | const page = yield* container.listProjectsLocationsClusters({ |
| 40 | parent: `projects/${project}/locations/-`, |
| 41 | }); |
| 42 | expect((page.clusters ?? []).map((cluster) => cluster.name)).not.toContain( |
| 43 | "alchemy-missing-cluster", |
| 44 | ); |
| 45 | |
| 46 | const missing = yield* container |
| 47 | .getProjectsLocationsClusters({ |
| 48 | name: `projects/${project}/locations/${CAPACITY_ZONE}/clusters/alchemy-missing-cluster`, |
| 49 | }) |
| 50 | .pipe( |
| 51 | Effect.as("found" as const), |
| 52 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 53 | ); |
| 54 | expect(missing).toEqual("gone"); |
| 55 | |
| 56 | yield* stack.destroy(); |
| 57 | }).pipe(logLevel), |
| 58 | { tags: ["provider:gcp", "provider:gcp:container", "live"], timeout: 90_000 }, |
| 59 | ); |
| 60 | |
| 61 | test.provider( |
| 62 | "registers the gcp-gke kubernetes adapter", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | yield* stack.destroy(); |
| 66 | |
| 67 | const adapter = yield* findClusterAdapter("gcp-gke"); |
| 68 | expect(adapter.kind).toEqual("Kubernetes.ClusterAdapter"); |
| 69 | expect(adapter.identity).toBeDefined(); |
| 70 | expect(adapter.registry).toBeDefined(); |
| 71 | expect(adapter.bootstrap).toBeDefined(); |
| 72 | expect(adapter.loadBalancerDefaults).toBeDefined(); |
| 73 | |
| 74 | yield* stack.destroy(); |
| 75 | }).pipe(logLevel), |
| 76 | { tags: ["provider:gcp", "provider:gcp:container", "live"], timeout: 90_000 }, |
| 77 | ); |
| 78 | |
| 79 | test.provider.skipIf(!runLifecycle)( |
| 80 | "create, update, and delete a cluster", |
| 81 | (stack) => |
| 82 | withGkeClusterSlot( |
| 83 | Effect.gen(function* () { |
| 84 | const { project } = yield* GcpEnvironment.current; |
| 85 | yield* stack.destroy(); |
| 86 | |
| 87 | const created = yield* stack.deploy( |
| 88 | Effect.gen(function* () { |
| 89 | return yield* GCP.Container.Cluster("App", { |
| 90 | location: CAPACITY_ZONE, |
| 91 | machineType: "e2-medium", |
| 92 | initialNodeCount: 1, |
| 93 | diskSizeGb: 20, |
| 94 | spot: true, |
| 95 | description: "alchemy-test-cluster", |
| 96 | labels: { env: "test" }, |
| 97 | ipAllocationPolicy: { useIpAliases: true }, |
| 98 | enableShieldedNodes: true, |
| 99 | addonsConfig: { horizontalPodAutoscaling: { disabled: false } }, |
| 100 | costManagementConfig: { enabled: true }, |
| 101 | loggingConfig: { |
| 102 | componentConfig: { enableComponents: ["SYSTEM_COMPONENTS"] }, |
| 103 | }, |
| 104 | monitoringConfig: { |
| 105 | componentConfig: { enableComponents: ["SYSTEM_COMPONENTS"] }, |
| 106 | managedPrometheusConfig: { enabled: true }, |
| 107 | }, |
| 108 | removeDefaultNodePool: true, |
| 109 | }); |
| 110 | }), |
| 111 | ); |
| 112 | |
| 113 | expect(created.name).toContain("/clusters/"); |
| 114 | expect(created.clusterId).toEqual(expect.any(String)); |
| 115 | expect(created.location).toEqual(CAPACITY_ZONE); |
| 116 | expect(created.description).toEqual("alchemy-test-cluster"); |
| 117 | expect(created.labels).toMatchObject({ env: "test" }); |
| 118 | expect(created.status).toEqual("RUNNING"); |
| 119 | expect(created.autopilot).toEqual(false); |
| 120 | expect(created.endpoint).toEqual(expect.stringMatching(/^https:\/\//)); |
| 121 | expect(created.certificateAuthorityData).toEqual(expect.any(String)); |
| 122 | expect(created.connection.auth.kind).toEqual("gcp-gke"); |
| 123 | if (created.connection.auth.kind === "gcp-gke") { |
| 124 | expect(created.connection.auth.clusterId).toEqual(created.clusterId); |
| 125 | } |
| 126 | expect(created.kubernetesObjects).toEqual([]); |
| 127 | expect(created.workloadPool).toEqual(`${project}.svc.id.goog`); |
| 128 | expect(created.ipAllocationPolicy?.useIpAliases).toEqual(true); |
| 129 | expect(created.enableShieldedNodes).toEqual(true); |
| 130 | // GKE omits proto3 defaults, so an enabled addon reports no `disabled`. |
| 131 | expect(created.addonsConfig?.horizontalPodAutoscaling?.disabled ?? false).toEqual(false); |
| 132 | expect(created.costManagementConfig?.enabled).toEqual(true); |
| 133 | expect(created.loggingConfig?.componentConfig?.enableComponents).toContain( |
| 134 | "SYSTEM_COMPONENTS", |
| 135 | ); |
| 136 | expect(created.monitoringConfig?.componentConfig?.enableComponents).toContain( |
| 137 | "SYSTEM_COMPONENTS", |
| 138 | ); |
| 139 | expect(created.monitoringConfig?.managedPrometheusConfig?.enabled).toEqual(true); |
| 140 | |
| 141 | const fetched = yield* container.getProjectsLocationsClusters({ |
| 142 | name: created.name, |
| 143 | }); |
| 144 | expect(fetched.name).toEqual(created.clusterId); |
| 145 | expect(fetched.resourceLabels?.env).toEqual("test"); |
| 146 | expect(fetched.description).toEqual("alchemy-test-cluster"); |
| 147 | expect(fetched.status).toEqual("RUNNING"); |
| 148 | expect(fetched.shieldedNodes?.enabled).toEqual(true); |
| 149 | expect(fetched.costManagementConfig?.enabled).toEqual(true); |
| 150 | |
| 151 | const defaultPool = yield* container |
| 152 | .getProjectsLocationsClustersNodePools({ |
| 153 | name: `${created.name}/nodePools/default-pool`, |
| 154 | }) |
| 155 | .pipe( |
| 156 | Effect.as("found" as const), |
| 157 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 158 | ); |
| 159 | expect(defaultPool).toEqual("gone"); |
| 160 | |
| 161 | const prodProps: GCP.Container.ClusterProps = { |
| 162 | clusterId: created.clusterId, |
| 163 | location: CAPACITY_ZONE, |
| 164 | machineType: "e2-medium", |
| 165 | initialNodeCount: 1, |
| 166 | diskSizeGb: 20, |
| 167 | spot: true, |
| 168 | description: "alchemy-test-cluster", |
| 169 | // `loggingConfig` is dropped here — it conflicts with "none". |
| 170 | loggingService: "none", |
| 171 | monitoringConfig: { |
| 172 | componentConfig: { enableComponents: ["SYSTEM_COMPONENTS"] }, |
| 173 | managedPrometheusConfig: { enabled: true }, |
| 174 | }, |
| 175 | labels: { env: "prod", role: "k8s" }, |
| 176 | ipAllocationPolicy: { useIpAliases: true }, |
| 177 | enableShieldedNodes: true, |
| 178 | addonsConfig: { horizontalPodAutoscaling: { disabled: false } }, |
| 179 | costManagementConfig: { enabled: true }, |
| 180 | removeDefaultNodePool: true, |
| 181 | }; |
| 182 | const prod = (props: Partial<GCP.Container.ClusterProps> = {}) => |
| 183 | GCP.Container.Cluster("App", { ...prodProps, ...props }); |
| 184 | |
| 185 | const updated = yield* stack.deploy(prod()); |
| 186 | |
| 187 | expect(updated.name).toEqual(created.name); |
| 188 | expect(updated.clusterUid).toEqual(created.clusterUid); |
| 189 | expect(updated.labels).toMatchObject({ env: "prod", role: "k8s" }); |
| 190 | expect(updated.loggingService).toEqual("none"); |
| 191 | |
| 192 | const refetched = yield* container.getProjectsLocationsClusters({ |
| 193 | name: created.name, |
| 194 | }); |
| 195 | expect(refetched.resourceLabels?.env).toEqual("prod"); |
| 196 | expect(refetched.resourceLabels?.role).toEqual("k8s"); |
| 197 | expect(refetched.loggingService).toEqual("none"); |
| 198 | // An unspecified monitoring service keeps its observed value. |
| 199 | expect(refetched.monitoringService).toEqual(created.monitoringService); |
| 200 | |
| 201 | // The default pool is gone, so its node shape describes no node. |
| 202 | const reshaped = yield* stack.plan( |
| 203 | prod({ machineType: "e2-standard-4", initialNodeCount: 3 }), |
| 204 | ); |
| 205 | expect(reshaped.resources.App.action).not.toBe("replace"); |
| 206 | // GKE only creates default-pool with the cluster, so restoring it replaces. |
| 207 | expect( |
| 208 | (yield* stack.plan(prod({ removeDefaultNodePool: false }))).resources.App.action, |
| 209 | ).toBe("replace"); |
| 210 | // Secondary ranges are fixed at creation. |
| 211 | expect( |
| 212 | (yield* stack.plan( |
| 213 | prod({ |
| 214 | ipAllocationPolicy: { useIpAliases: true, clusterSecondaryRangeName: "pods-b" }, |
| 215 | }), |
| 216 | )).resources.App, |
| 217 | ).toMatchObject({ action: "replace", deleteFirst: true }); |
| 218 | |
| 219 | // Deletion protection blocks a replacement and a destroy until it is turned off. |
| 220 | yield* stack.deploy(prod({ deletionProtection: true })); |
| 221 | yield* Effect.gen(function* () { |
| 222 | const blockedReplace = yield* stack |
| 223 | .plan(prod({ deletionProtection: true, location: CAPACITY_ZONE_2 })) |
| 224 | .pipe(Effect.flip); |
| 225 | expect(blockedReplace).toMatchObject({ _tag: "GCP.Container.ClusterDeletionProtected" }); |
| 226 | expect(Result.isFailure(yield* stack.destroy().pipe(Effect.result))).toBe(true); |
| 227 | expect( |
| 228 | (yield* container.getProjectsLocationsClusters({ name: created.name })).status, |
| 229 | ).toEqual("RUNNING"); |
| 230 | }).pipe( |
| 231 | // Never leave the cluster locked, even if an assertion fails. |
| 232 | Effect.ensuring(Effect.ignore(stack.deploy(prod({ deletionProtection: false })))), |
| 233 | ); |
| 234 | |
| 235 | yield* stack.destroy(); |
| 236 | |
| 237 | const gone = yield* waitUntilGone(created.name); |
| 238 | expect(gone).toEqual("gone"); |
| 239 | }), |
| 240 | ).pipe(logLevel), |
| 241 | // Create ~6 min, logging update ~5 min, delete ~5 min. |
| 242 | { |
| 243 | tags: ["provider:gcp", "provider:gcp:container", "live"], |
| 244 | timeout: 1_800_000, |
| 245 | retry: 0, |
| 246 | }, |
| 247 | ); |
| 248 | |
| 249 | test.provider.skipIf(!runLifecycle)( |
| 250 | "runs autopilot nodes as a dedicated service account", |
| 251 | (stack) => |
| 252 | withGkeClusterSlot( |
| 253 | Effect.gen(function* () { |
| 254 | const { project } = yield* GcpEnvironment.current; |
| 255 | yield* stack.destroy(); |
| 256 | |
| 257 | const autopilot = (serviceAccount?: string) => |
| 258 | Effect.gen(function* () { |
| 259 | const nodes = yield* GCP.IAM.ServiceAccount("GkeNodes", {}); |
| 260 | const role = yield* GCP.IAM.Member("GkeNodesRole", { |
| 261 | kind: "project", |
| 262 | name: project, |
| 263 | role: "roles/container.defaultNodeServiceAccount", |
| 264 | member: Output.interpolate`serviceAccount:${nodes.email}`, |
| 265 | }); |
| 266 | const cluster = yield* GCP.Container.Cluster("Autopilot", { |
| 267 | location: CAPACITY_REGION, |
| 268 | autopilot: true, |
| 269 | // Grant the node role before GKE provisions any node. |
| 270 | serviceAccount: |
| 271 | serviceAccount ?? |
| 272 | Output.all(nodes.email, role.role).pipe(Output.map(([email]) => email)), |
| 273 | }); |
| 274 | return { nodes, cluster }; |
| 275 | }); |
| 276 | |
| 277 | const { nodes, cluster } = yield* stack.deploy(autopilot()); |
| 278 | |
| 279 | expect(cluster.autopilot).toEqual(true); |
| 280 | const fetched = yield* container.getProjectsLocationsClusters({ |
| 281 | name: cluster.name, |
| 282 | }); |
| 283 | expect(fetched.autopilot?.enabled).toEqual(true); |
| 284 | expect(fetched.autoscaling?.autoprovisioningNodePoolDefaults?.serviceAccount).toEqual( |
| 285 | nodes.email, |
| 286 | ); |
| 287 | |
| 288 | // Redeploying the same identity leaves the cluster in place. |
| 289 | expect((yield* stack.plan(autopilot(nodes.email))).resources.Autopilot.action).not.toBe( |
| 290 | "replace", |
| 291 | ); |
| 292 | // Autopilot fixes its nodes' identity at creation. |
| 293 | expect( |
| 294 | (yield* stack.plan(autopilot(`alchemy-other-nodes@${project}.iam.gserviceaccount.com`))) |
| 295 | .resources.Autopilot.action, |
| 296 | ).toBe("replace"); |
| 297 | |
| 298 | yield* stack.destroy(); |
| 299 | |
| 300 | const gone = yield* waitUntilGone(cluster.name); |
| 301 | expect(gone).toEqual("gone"); |
| 302 | }), |
| 303 | ).pipe(logLevel), |
| 304 | // Autopilot create ~10 min, delete ~5 min. |
| 305 | { |
| 306 | tags: ["provider:gcp", "provider:gcp:container", "live"], |
| 307 | timeout: 1_800_000, |
| 308 | retry: 0, |
| 309 | }, |
| 310 | ); |