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 gkeonprem from "@distilled.cloud/gcp/gkeonprem_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { hasAlchemyLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | ResourceNotResolved, |
| 14 | collectPages, |
| 15 | createInternalLabels, |
| 16 | desiredAnnotations, |
| 17 | differs, |
| 18 | encodeOwnership, |
| 19 | expandParent, |
| 20 | fieldMask, |
| 21 | isOwned, |
| 22 | listAtNested, |
| 23 | listChildrenOf, |
| 24 | listAtLocation, |
| 25 | normalizeLocation, |
| 26 | parseName, |
| 27 | parseOwnership, |
| 28 | replaceOnIdentity, |
| 29 | rfc1035, |
| 30 | sameText, |
| 31 | textState, |
| 32 | toPhysicalId, |
| 33 | userLabels, |
| 34 | waitForOperation, |
| 35 | waitUntilExists, |
| 36 | waitUntilGone, |
| 37 | } from "./internal.ts"; |
| 38 | import type { BareMetalNodePoolConfig, BareMetalNodePoolUpgradePolicy } from "./types.ts"; |
| 39 | |
| 40 | const COLLECTION = "bareMetalNodePools"; |
| 41 | const PARENT_COLLECTION = "bareMetalClusters"; |
| 42 | |
| 43 | export type BareMetalClustersBareMetalNodePoolProps = { |
| 44 | /** |
| 45 | * Parent bare metal user cluster. Full name |
| 46 | * `projects/{project}/locations/{location}/bareMetalClusters/{cluster}` |
| 47 | * or the cluster id (combined with `location`). Immutable — changing |
| 48 | * it replaces the node pool. |
| 49 | */ |
| 50 | bareMetalCluster: string; |
| 51 | /** |
| 52 | * Region used when `bareMetalCluster` is a bare id. |
| 53 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 54 | */ |
| 55 | location?: string; |
| 56 | /** |
| 57 | * Node pool id (the `{bareMetalNodePool}` segment). If omitted, a unique |
| 58 | * RFC1035 name is generated. Immutable — changing it replaces the pool. |
| 59 | */ |
| 60 | bareMetalNodePoolId?: string; |
| 61 | /** |
| 62 | * Node pool configuration, including machine addresses. |
| 63 | */ |
| 64 | nodePoolConfig: BareMetalNodePoolConfig; |
| 65 | /** |
| 66 | * Display name. Node pools have no GCP labels field, so Alchemy stamps |
| 67 | * ownership into annotations and a `[alchemy …]` displayName prefix |
| 68 | * and strips both from attributes. |
| 69 | */ |
| 70 | displayName?: string; |
| 71 | /** |
| 72 | * Worker node pool upgrade policy. |
| 73 | */ |
| 74 | upgradePolicy?: BareMetalNodePoolUpgradePolicy; |
| 75 | /** |
| 76 | * Kubernetes-style annotations. Alchemy ownership keys are merged in. |
| 77 | */ |
| 78 | annotations?: Record<string, string>; |
| 79 | /** |
| 80 | * User labels stored as annotations (keys sanitized like GCP labels). |
| 81 | * Alchemy ownership labels are merged in automatically. |
| 82 | */ |
| 83 | labels?: Record<string, string>; |
| 84 | }; |
| 85 | |
| 86 | export type BareMetalClustersBareMetalNodePool = Resource< |
| 87 | "GCP.GKEOnPrem.BareMetalClustersBareMetalNodePool", |
| 88 | BareMetalClustersBareMetalNodePoolProps, |
| 89 | { |
| 90 | /** Full resource name. */ |
| 91 | name: string; |
| 92 | /** Node pool id (last path segment). */ |
| 93 | bareMetalNodePoolId: string; |
| 94 | /** Parent cluster resource name. */ |
| 95 | bareMetalCluster: string; |
| 96 | /** Project id. */ |
| 97 | project: string; |
| 98 | /** Location id. */ |
| 99 | location: string; |
| 100 | /** Node pool configuration. */ |
| 101 | nodePoolConfig: gkeonprem.BareMetalNodePoolConfig | undefined; |
| 102 | /** User display name with the Alchemy ownership prefix stripped. */ |
| 103 | displayName: string | undefined; |
| 104 | /** Upgrade policy. */ |
| 105 | upgradePolicy: gkeonprem.BareMetalNodePoolUpgradePolicy | undefined; |
| 106 | /** User annotations (Alchemy ownership keys stripped). */ |
| 107 | annotations: Record<string, string>; |
| 108 | /** User labels (Alchemy ownership keys stripped). */ |
| 109 | labels: Record<string, string>; |
| 110 | /** Server-reported state. */ |
| 111 | state: string | undefined; |
| 112 | /** Server-generated resource uid. */ |
| 113 | uid: string | undefined; |
| 114 | /** Whether a change is in flight. */ |
| 115 | reconciling: boolean | undefined; |
| 116 | /** Controller error message. */ |
| 117 | errorMessage: string | undefined; |
| 118 | /** RFC3339 creation timestamp. */ |
| 119 | createTime: string | undefined; |
| 120 | /** RFC3339 last-update timestamp. */ |
| 121 | updateTime: string | undefined; |
| 122 | /** RFC3339 deletion timestamp. */ |
| 123 | deleteTime: string | undefined; |
| 124 | /** Server etag. */ |
| 125 | etag: string | undefined; |
| 126 | }, |
| 127 | never, |
| 128 | Providers |
| 129 | >; |
| 130 | |
| 131 | /** |
| 132 | * A worker node pool on an Anthos on bare metal user cluster. |
| 133 | * |
| 134 | * Node pools have no GCP labels field, so Alchemy stamps ownership into |
| 135 | * annotations and a `[alchemy …]` displayName prefix for `list` / nuke. |
| 136 | * Pool id, location, and parent cluster are identity — changing them |
| 137 | * replaces the pool. Config, display name, and upgrade policy update |
| 138 | * in place. |
| 139 | * |
| 140 | * ### Creating a Bare Metal Node Pool |
| 141 | * **Example:** Worker pool with two machines |
| 142 | * ```typescript |
| 143 | * const pool = yield* GCP.GKEOnPrem.BareMetalClustersBareMetalNodePool( |
| 144 | * "Workers", |
| 145 | * { |
| 146 | * bareMetalCluster: cluster.name, |
| 147 | * nodePoolConfig: { |
| 148 | * nodeConfigs: [ |
| 149 | * { nodeIp: "10.200.0.11" }, |
| 150 | * { nodeIp: "10.200.0.12" }, |
| 151 | * ], |
| 152 | * }, |
| 153 | * displayName: "app workers", |
| 154 | * }, |
| 155 | * ); |
| 156 | * ``` |
| 157 | * |
| 158 | * ### Updating a Bare Metal Node Pool |
| 159 | * Re-declare the same logical id with changed props; the engine keeps the |
| 160 | * physical resource and updates it in place. |
| 161 | * |
| 162 | * **Example:** Add a machine |
| 163 | * ```typescript |
| 164 | * const pool = yield* GCP.GKEOnPrem.BareMetalClustersBareMetalNodePool( |
| 165 | * "Workers", |
| 166 | * { |
| 167 | * bareMetalCluster: cluster.name, |
| 168 | * nodePoolConfig: { |
| 169 | * nodeConfigs: [ |
| 170 | * { nodeIp: "10.200.0.11" }, |
| 171 | * { nodeIp: "10.200.0.12" }, |
| 172 | * { nodeIp: "10.200.0.13" }, |
| 173 | * ], |
| 174 | * }, |
| 175 | * displayName: "app workers v2", |
| 176 | * }, |
| 177 | * ); |
| 178 | * ``` |
| 179 | * |
| 180 | * @resource |
| 181 | * @category GKEOnPrem |
| 182 | */ |
| 183 | export const BareMetalClustersBareMetalNodePool = Resource<BareMetalClustersBareMetalNodePool>( |
| 184 | "GCP.GKEOnPrem.BareMetalClustersBareMetalNodePool", |
| 185 | ); |
| 186 | |
| 187 | const resourceName = (cluster: string, bareMetalNodePoolId: string) => |
| 188 | `${cluster}/${COLLECTION}/${bareMetalNodePoolId}`; |
| 189 | |
| 190 | const toAttrs = (item: gkeonprem.BareMetalNodePool, project: string, region: string) => { |
| 191 | const name = item.name ?? ""; |
| 192 | const parsed = parseName(name, COLLECTION, region); |
| 193 | const ownership = parseOwnership(item.displayName); |
| 194 | const annotations = userLabels(item.annotations); |
| 195 | return { |
| 196 | name, |
| 197 | bareMetalNodePoolId: parsed.id, |
| 198 | bareMetalCluster: parsed.parent, |
| 199 | project: parsed.project || project, |
| 200 | location: parsed.location, |
| 201 | nodePoolConfig: item.nodePoolConfig, |
| 202 | displayName: ownership.text, |
| 203 | upgradePolicy: item.upgradePolicy, |
| 204 | annotations, |
| 205 | labels: annotations, |
| 206 | state: textState(item.state), |
| 207 | uid: item.uid, |
| 208 | reconciling: item.reconciling, |
| 209 | errorMessage: item.status?.errorMessage, |
| 210 | createTime: item.createTime, |
| 211 | updateTime: item.updateTime, |
| 212 | deleteTime: item.deleteTime, |
| 213 | etag: item.etag, |
| 214 | }; |
| 215 | }; |
| 216 | |
| 217 | const getByName = (name: string) => |
| 218 | gkeonprem |
| 219 | .getProjectsLocationsBareMetalClustersBareMetalNodePools({ |
| 220 | name, |
| 221 | view: "FULL", |
| 222 | }) |
| 223 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 224 | |
| 225 | const listChildren = (parent: string) => |
| 226 | collectPages( |
| 227 | gkeonprem.listProjectsLocationsBareMetalClustersBareMetalNodePools.pages({ |
| 228 | parent, |
| 229 | pageSize: 1000, |
| 230 | view: "FULL", |
| 231 | }), |
| 232 | (page): readonly gkeonprem.BareMetalNodePool[] | undefined => page.bareMetalNodePools, |
| 233 | ); |
| 234 | |
| 235 | const listOwned = (project: string, region: string) => |
| 236 | listAtNested(project, region, `${PARENT_COLLECTION}/-`, listChildren).pipe( |
| 237 | Effect.flatMap((items) => |
| 238 | items.length > 0 |
| 239 | ? Effect.succeed(items) |
| 240 | : listChildrenOf( |
| 241 | listAtLocation(project, region, (parent) => |
| 242 | collectPages( |
| 243 | gkeonprem.listProjectsLocationsBareMetalClusters.pages({ |
| 244 | parent, |
| 245 | pageSize: 1000, |
| 246 | view: "BASIC", |
| 247 | }), |
| 248 | (page): readonly gkeonprem.BareMetalCluster[] | undefined => page.bareMetalClusters, |
| 249 | ), |
| 250 | ), |
| 251 | (cluster: gkeonprem.BareMetalCluster) => cluster.name, |
| 252 | listChildren, |
| 253 | ), |
| 254 | ), |
| 255 | Effect.map((items) => |
| 256 | items.filter((item: gkeonprem.BareMetalNodePool) => |
| 257 | isOwned(item.annotations, item.displayName), |
| 258 | ), |
| 259 | ), |
| 260 | ); |
| 261 | |
| 262 | export const BareMetalClustersBareMetalNodePoolProvider = () => |
| 263 | Provider.succeed(BareMetalClustersBareMetalNodePool, { |
| 264 | stables: [ |
| 265 | "name", |
| 266 | "bareMetalNodePoolId", |
| 267 | "bareMetalCluster", |
| 268 | "project", |
| 269 | "location", |
| 270 | "uid", |
| 271 | "createTime", |
| 272 | ], |
| 273 | |
| 274 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 275 | if (!isResolved(news)) return undefined; |
| 276 | const env = yield* GcpEnvironment.current; |
| 277 | return replaceOnIdentity({ |
| 278 | previousId: olds?.bareMetalNodePoolId ?? output?.bareMetalNodePoolId, |
| 279 | nextId: news.bareMetalNodePoolId |
| 280 | ? rfc1035(news.bareMetalNodePoolId, "baremetalnodepool") |
| 281 | : (olds?.bareMetalNodePoolId ?? output?.bareMetalNodePoolId), |
| 282 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 283 | nextLocation: normalizeLocation( |
| 284 | news.location ?? olds?.location ?? output?.location, |
| 285 | env.region, |
| 286 | ), |
| 287 | previousParent: olds?.bareMetalCluster ?? output?.bareMetalCluster, |
| 288 | nextParent: news.bareMetalCluster, |
| 289 | }); |
| 290 | }), |
| 291 | |
| 292 | read: Effect.fn(function* ({ id, olds, output }) { |
| 293 | const env = yield* GcpEnvironment.current; |
| 294 | const bareMetalNodePoolId = yield* toPhysicalId( |
| 295 | id, |
| 296 | olds?.bareMetalNodePoolId, |
| 297 | output?.bareMetalNodePoolId, |
| 298 | "baremetalnodepool", |
| 299 | ); |
| 300 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 301 | const cluster = expandParent( |
| 302 | olds?.bareMetalCluster ?? output?.bareMetalCluster ?? "", |
| 303 | env.project, |
| 304 | location, |
| 305 | PARENT_COLLECTION, |
| 306 | ); |
| 307 | const name = output?.name ?? resourceName(cluster, bareMetalNodePoolId); |
| 308 | const existing = yield* getByName(name); |
| 309 | if (existing === undefined) return undefined; |
| 310 | const attrs = toAttrs(existing, env.project, env.region); |
| 311 | const fromName = parseOwnership(existing.displayName).labels; |
| 312 | const owned = |
| 313 | (yield* hasAlchemyLabels(id, tagRecord(existing.annotations))) || |
| 314 | (yield* hasAlchemyLabels(id, fromName)); |
| 315 | return owned ? attrs : Unowned(attrs); |
| 316 | }), |
| 317 | |
| 318 | list: () => |
| 319 | Effect.gen(function* () { |
| 320 | const env = yield* GcpEnvironment.current; |
| 321 | const items = yield* listOwned(env.project, env.region); |
| 322 | return items.map((item: gkeonprem.BareMetalNodePool) => |
| 323 | toAttrs(item, env.project, env.region), |
| 324 | ); |
| 325 | }), |
| 326 | |
| 327 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 328 | const env = yield* GcpEnvironment.current; |
| 329 | const bareMetalNodePoolId = yield* toPhysicalId( |
| 330 | id, |
| 331 | news.bareMetalNodePoolId, |
| 332 | output?.bareMetalNodePoolId, |
| 333 | "baremetalnodepool", |
| 334 | ); |
| 335 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 336 | const cluster = expandParent(news.bareMetalCluster, env.project, location, PARENT_COLLECTION); |
| 337 | const name = resourceName(cluster, bareMetalNodePoolId); |
| 338 | const ownership = yield* createInternalLabels(id); |
| 339 | const annotations = desiredAnnotations(ownership, news.labels, news.annotations); |
| 340 | const displayName = encodeOwnership(ownership, news.displayName); |
| 341 | const body: gkeonprem.BareMetalNodePool = { |
| 342 | nodePoolConfig: news.nodePoolConfig, |
| 343 | displayName, |
| 344 | upgradePolicy: news.upgradePolicy, |
| 345 | annotations, |
| 346 | }; |
| 347 | |
| 348 | let current = yield* getByName(output?.name ?? name); |
| 349 | |
| 350 | if (current === undefined) { |
| 351 | const created = yield* gkeonprem |
| 352 | .createProjectsLocationsBareMetalClustersBareMetalNodePools({ |
| 353 | parent: cluster, |
| 354 | bareMetalNodePoolId, |
| 355 | body, |
| 356 | }) |
| 357 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 358 | if (created !== undefined) { |
| 359 | yield* waitForOperation(created); |
| 360 | } |
| 361 | current = yield* waitUntilExists(getByName(name), name); |
| 362 | } |
| 363 | |
| 364 | if (current === undefined) { |
| 365 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 366 | } |
| 367 | |
| 368 | const mask = fieldMask([ |
| 369 | differs(current.annotations, annotations) && "annotations", |
| 370 | !sameText(parseOwnership(current.displayName).text, news.displayName) && "displayName", |
| 371 | differs(current.nodePoolConfig, news.nodePoolConfig) && "nodePoolConfig", |
| 372 | differs(current.upgradePolicy, news.upgradePolicy) && "upgradePolicy", |
| 373 | ]); |
| 374 | |
| 375 | if (mask.length > 0) { |
| 376 | const operation = |
| 377 | yield* gkeonprem.patchProjectsLocationsBareMetalClustersBareMetalNodePools({ |
| 378 | name: current.name ?? name, |
| 379 | updateMask: mask, |
| 380 | body: { |
| 381 | ...body, |
| 382 | etag: current.etag, |
| 383 | }, |
| 384 | }); |
| 385 | yield* waitForOperation(operation); |
| 386 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 387 | } |
| 388 | |
| 389 | if (current === undefined) { |
| 390 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 391 | } |
| 392 | return toAttrs(current, env.project, env.region); |
| 393 | }), |
| 394 | |
| 395 | delete: Effect.fn(function* ({ output }) { |
| 396 | const operation = yield* gkeonprem |
| 397 | .deleteProjectsLocationsBareMetalClustersBareMetalNodePools({ |
| 398 | name: output.name, |
| 399 | allowMissing: true, |
| 400 | etag: output.etag, |
| 401 | }) |
| 402 | .pipe( |
| 403 | Effect.retry({ |
| 404 | while: (error) => error._tag === "Conflict", |
| 405 | times: 8, |
| 406 | schedule: Schedule.spaced("2 seconds"), |
| 407 | }), |
| 408 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 409 | ); |
| 410 | if (operation !== undefined) { |
| 411 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 412 | } |
| 413 | yield* waitUntilGone(getByName(output.name), output.name); |
| 414 | }), |
| 415 | }); |