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, 04:45 UTC
| 1 | import * as container from "@distilled.cloud/gcp/container_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import { tagRecord } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { GcpEnvironment } from "../Environment.ts"; |
| 12 | import { |
| 13 | createInternalLabels, |
| 14 | diffLabels, |
| 15 | hasAlchemyLabels, |
| 16 | stripInternalLabels, |
| 17 | toLabels, |
| 18 | } from "../Labels.ts"; |
| 19 | import { waitForOperation as waitForGcpOperation } from "../Operation.ts"; |
| 20 | import { matchesDesired } from "../Proto.ts"; |
| 21 | import type { Providers } from "../Providers.ts"; |
| 22 | |
| 23 | // A zone, not a region: `GCP.Region` has no zone and a regional default would triple node count. |
| 24 | const DEFAULT_LOCATION = "us-central1-a"; |
| 25 | const DEFAULT_MACHINE_TYPE = "e2-medium"; |
| 26 | const DEFAULT_DISK_TYPE = "pd-standard"; |
| 27 | const DEFAULT_DISK_SIZE_GB = 20; |
| 28 | const DEFAULT_NODE_COUNT = 1; |
| 29 | const MAX_NAME_LENGTH = 40; |
| 30 | |
| 31 | export type NodePoolAutoscaling = { |
| 32 | /** Enable the cluster autoscaler for this pool. */ |
| 33 | enabled?: boolean; |
| 34 | /** Minimum nodes per zone. Mutually exclusive with `totalMinNodeCount`. */ |
| 35 | minNodeCount?: number; |
| 36 | /** Maximum nodes per zone. Mutually exclusive with `totalMaxNodeCount`. */ |
| 37 | maxNodeCount?: number; |
| 38 | /** Minimum nodes across all zones. */ |
| 39 | totalMinNodeCount?: number; |
| 40 | /** Maximum nodes across all zones. */ |
| 41 | totalMaxNodeCount?: number; |
| 42 | /** Scale-up location policy (`BALANCED`, `ANY`). */ |
| 43 | locationPolicy?: container.NodePoolAutoscalingLocationPolicyEnum | (string & {}); |
| 44 | /** Allow NAP to delete this pool. */ |
| 45 | autoprovisioned?: boolean; |
| 46 | }; |
| 47 | |
| 48 | export type NodePoolManagement = { |
| 49 | /** Automatically repair unhealthy nodes. */ |
| 50 | autoRepair?: boolean; |
| 51 | /** Automatically upgrade nodes. */ |
| 52 | autoUpgrade?: boolean; |
| 53 | }; |
| 54 | |
| 55 | export type NodePoolUpgradeSettings = { |
| 56 | /** Extra nodes created during an upgrade. */ |
| 57 | maxSurge?: number; |
| 58 | /** Nodes allowed to be unavailable during an upgrade. */ |
| 59 | maxUnavailable?: number; |
| 60 | /** Upgrade strategy (`SURGE`, `BLUE_GREEN`). */ |
| 61 | strategy?: container.UpgradeSettingsStrategyEnum | (string & {}); |
| 62 | }; |
| 63 | |
| 64 | export type NodePoolTaint = { |
| 65 | /** Taint key. */ |
| 66 | key?: string; |
| 67 | /** Taint value. */ |
| 68 | value?: string; |
| 69 | /** Taint effect (`NO_SCHEDULE`, `PREFER_NO_SCHEDULE`, `NO_EXECUTE`). */ |
| 70 | effect?: container.NodeTaintEffectEnum | (string & {}); |
| 71 | }; |
| 72 | |
| 73 | export type NodePoolProps = { |
| 74 | /** |
| 75 | * Parent cluster id or full resource name |
| 76 | * (`projects/{project}/locations/{location}/clusters/{cluster}`). |
| 77 | * Immutable — changing it replaces the node pool. |
| 78 | */ |
| 79 | cluster: string; |
| 80 | /** |
| 81 | * Zone or region of the parent cluster (`us-central1-a`, `us-central1`, |
| 82 | * …). Ignored when `cluster` is a full resource name. Immutable — |
| 83 | * changing it replaces the node pool. |
| 84 | * @default "us-central1-a" |
| 85 | */ |
| 86 | location?: string; |
| 87 | /** |
| 88 | * Node pool id (the `{nodePool}` segment of |
| 89 | * `.../clusters/{cluster}/nodePools/{nodePool}`). If omitted, a unique |
| 90 | * RFC1035 name is generated. Must be 1-40 characters and match |
| 91 | * `[a-z]([a-z0-9-]*[a-z0-9])?`. Immutable — changing it replaces the |
| 92 | * pool. |
| 93 | */ |
| 94 | nodePoolId?: string; |
| 95 | /** |
| 96 | * Desired node count (per zone for zonal/multi-zonal pools). Create |
| 97 | * uses `initialNodeCount`; later changes call `setSize`. Ignored while |
| 98 | * autoscaling is enabled. |
| 99 | * @default 1 |
| 100 | */ |
| 101 | nodeCount?: number; |
| 102 | /** |
| 103 | * User labels stored as GKE `config.resourceLabels` on the pool's GCE |
| 104 | * VMs. Alchemy ownership labels are merged in automatically. |
| 105 | */ |
| 106 | labels?: Record<string, string>; |
| 107 | /** |
| 108 | * Machine type (`e2-medium`, `e2-small`, …). |
| 109 | * @default "e2-medium" |
| 110 | */ |
| 111 | machineType?: string; |
| 112 | /** |
| 113 | * Boot disk size in GB. Smallest allowed is 10. |
| 114 | * @default 20 |
| 115 | */ |
| 116 | diskSizeGb?: number; |
| 117 | /** |
| 118 | * Boot disk type (`pd-standard`, `pd-balanced`, `pd-ssd`). |
| 119 | * @default "pd-standard" |
| 120 | */ |
| 121 | diskType?: string; |
| 122 | /** |
| 123 | * Node image type (`COS_CONTAINERD`, `UBUNTU_CONTAINERD`, …). |
| 124 | */ |
| 125 | imageType?: string; |
| 126 | /** |
| 127 | * Kubernetes version for nodes (or alias such as `"-"` for the |
| 128 | * control-plane version). |
| 129 | */ |
| 130 | version?: string; |
| 131 | /** |
| 132 | * Use Spot VMs. Immutable — changing it replaces the pool. |
| 133 | * @default false |
| 134 | */ |
| 135 | spot?: boolean; |
| 136 | /** |
| 137 | * Use preemptible VMs. Immutable — changing it replaces the pool. |
| 138 | * @default false |
| 139 | */ |
| 140 | preemptible?: boolean; |
| 141 | /** |
| 142 | * Zones in which this pool's nodes should run. |
| 143 | */ |
| 144 | nodeLocations?: string[]; |
| 145 | /** |
| 146 | * Kubernetes labels applied to each node. |
| 147 | */ |
| 148 | nodeLabels?: Record<string, string>; |
| 149 | /** |
| 150 | * Compute Engine instance metadata applied to each node. Changing it |
| 151 | * replaces the pool. |
| 152 | */ |
| 153 | metadata?: Record<string, string>; |
| 154 | /** |
| 155 | * Workload Identity metadata exposure mode for each node. |
| 156 | * @default `{ mode: "GKE_METADATA" }` when the cluster has Workload Identity |
| 157 | */ |
| 158 | workloadMetadataConfig?: container.WorkloadMetadataConfig; |
| 159 | /** |
| 160 | * Shielded VM settings applied to each node. Changing them replaces the |
| 161 | * pool. |
| 162 | */ |
| 163 | shieldedInstanceConfig?: container.ShieldedInstanceConfig; |
| 164 | /** |
| 165 | * Advanced Compute Engine VM features, such as nested virtualization. |
| 166 | * Changing them replaces the pool (GKE cannot update them in place). |
| 167 | */ |
| 168 | advancedMachineFeatures?: container.AdvancedMachineFeatures; |
| 169 | /** |
| 170 | * Network tags applied to each node. |
| 171 | */ |
| 172 | tags?: string[]; |
| 173 | /** |
| 174 | * Kubernetes taints applied to each node. |
| 175 | */ |
| 176 | taints?: NodePoolTaint[]; |
| 177 | /** |
| 178 | * OAuth scopes for the node service account. Immutable — changing them |
| 179 | * replaces the pool. |
| 180 | */ |
| 181 | oauthScopes?: string[]; |
| 182 | /** |
| 183 | * Service account email for node VMs. Immutable — changing it replaces |
| 184 | * the pool. |
| 185 | */ |
| 186 | serviceAccount?: string; |
| 187 | /** |
| 188 | * Local SSD count. Immutable — changing it replaces the pool. |
| 189 | */ |
| 190 | localSsdCount?: number; |
| 191 | /** |
| 192 | * Customer-managed KMS key for boot disks. Immutable — changing it |
| 193 | * replaces the pool. |
| 194 | */ |
| 195 | bootDiskKmsKey?: string; |
| 196 | /** |
| 197 | * Max pods per node. Immutable — changing it replaces the pool. |
| 198 | */ |
| 199 | maxPodsPerNode?: number; |
| 200 | /** |
| 201 | * Cluster autoscaler settings for this pool. |
| 202 | */ |
| 203 | autoscaling?: NodePoolAutoscaling; |
| 204 | /** |
| 205 | * Auto-repair / auto-upgrade settings. |
| 206 | */ |
| 207 | management?: NodePoolManagement; |
| 208 | /** |
| 209 | * Upgrade surge settings. |
| 210 | */ |
| 211 | upgradeSettings?: NodePoolUpgradeSettings; |
| 212 | }; |
| 213 | |
| 214 | export type NodePool = Resource< |
| 215 | "GCP.Container.NodePool", |
| 216 | NodePoolProps, |
| 217 | { |
| 218 | /** Full resource name `projects/{project}/locations/{location}/clusters/{cluster}/nodePools/{nodePool}`. */ |
| 219 | name: string; |
| 220 | /** Node pool id (last path segment). */ |
| 221 | nodePoolId: string; |
| 222 | /** Parent cluster id. */ |
| 223 | clusterId: string; |
| 224 | /** Parent cluster resource name. */ |
| 225 | clusterName: string; |
| 226 | /** Project id. */ |
| 227 | project: string; |
| 228 | /** Zone or region id. */ |
| 229 | location: string; |
| 230 | /** Server-reported status (`RUNNING`, `PROVISIONING`, …). */ |
| 231 | status: string | undefined; |
| 232 | /** Kubernetes version currently on the nodes. */ |
| 233 | version: string | undefined; |
| 234 | /** Desired / initial node count. */ |
| 235 | nodeCount: number; |
| 236 | /** Machine type. */ |
| 237 | machineType: string | undefined; |
| 238 | /** Boot disk size in GB. */ |
| 239 | diskSizeGb: number | undefined; |
| 240 | /** Boot disk type. */ |
| 241 | diskType: string | undefined; |
| 242 | /** Node image type. */ |
| 243 | imageType: string | undefined; |
| 244 | /** Whether nodes are Spot VMs. */ |
| 245 | spot: boolean; |
| 246 | /** Whether nodes are preemptible VMs. */ |
| 247 | preemptible: boolean; |
| 248 | /** User GCP labels (Alchemy ownership labels stripped). */ |
| 249 | labels: Record<string, string>; |
| 250 | /** Kubernetes node labels. */ |
| 251 | nodeLabels: Record<string, string>; |
| 252 | /** Compute Engine instance metadata (including GKE-injected keys). */ |
| 253 | metadata: Record<string, string>; |
| 254 | /** Workload Identity metadata exposure mode. */ |
| 255 | workloadMetadataConfig: container.WorkloadMetadataConfig | undefined; |
| 256 | /** Shielded VM settings. */ |
| 257 | shieldedInstanceConfig: container.ShieldedInstanceConfig | undefined; |
| 258 | /** Advanced Compute Engine VM features. */ |
| 259 | advancedMachineFeatures: container.AdvancedMachineFeatures | undefined; |
| 260 | /** Network tags. */ |
| 261 | tags: string[]; |
| 262 | /** Node locations. */ |
| 263 | nodeLocations: string[]; |
| 264 | /** Autoscaler settings currently applied. */ |
| 265 | autoscaling: NodePoolAutoscaling | undefined; |
| 266 | /** Management settings currently applied. */ |
| 267 | management: NodePoolManagement | undefined; |
| 268 | /** Managed instance group URLs. */ |
| 269 | instanceGroupUrls: string[]; |
| 270 | /** Server-defined URL. */ |
| 271 | selfLink: string | undefined; |
| 272 | }, |
| 273 | never, |
| 274 | Providers |
| 275 | >; |
| 276 | |
| 277 | /** |
| 278 | * A Google Kubernetes Engine node pool. |
| 279 | * |
| 280 | * Node pools belong to a {@link Cluster}. Changing `cluster`, `location`, |
| 281 | * `nodePoolId`, `spot`, `preemptible`, `serviceAccount`, `oauthScopes`, |
| 282 | * `localSsdCount`, `bootDiskKmsKey`, `maxPodsPerNode`, `metadata`, |
| 283 | * `shieldedInstanceConfig`, or `advancedMachineFeatures` replaces the pool. |
| 284 | * Size, labels, machine type, disk, image, version, workload metadata, |
| 285 | * management, autoscaling, and upgrade settings update in place. |
| 286 | * |
| 287 | * Provisioning typically takes several minutes. |
| 288 | * |
| 289 | * ### Creating a Node Pool |
| 290 | * **Example:** Generated name on an existing cluster |
| 291 | * ```typescript |
| 292 | * const cluster = yield* GCP.Container.Cluster("App", {}); |
| 293 | * const pool = yield* GCP.Container.NodePool("Workers", { |
| 294 | * cluster: cluster.clusterId, |
| 295 | * location: cluster.location, |
| 296 | * }); |
| 297 | * ``` |
| 298 | * |
| 299 | * **Example:** Explicit id, Spot nodes, and labels |
| 300 | * ```typescript |
| 301 | * const pool = yield* GCP.Container.NodePool("Workers", { |
| 302 | * cluster: cluster.name, |
| 303 | * nodePoolId: "app-workers", |
| 304 | * nodeCount: 1, |
| 305 | * machineType: "e2-small", |
| 306 | * diskSizeGb: 20, |
| 307 | * spot: true, |
| 308 | * labels: { env: "prod" }, |
| 309 | * }); |
| 310 | * ``` |
| 311 | * |
| 312 | * ### Autoscaling |
| 313 | * **Example:** Per-zone autoscaling |
| 314 | * ```typescript |
| 315 | * const pool = yield* GCP.Container.NodePool("Workers", { |
| 316 | * cluster: cluster.clusterId, |
| 317 | * location: cluster.location, |
| 318 | * autoscaling: { enabled: true, minNodeCount: 1, maxNodeCount: 3 }, |
| 319 | * }); |
| 320 | * ``` |
| 321 | * |
| 322 | * ### Reading a Node Pool |
| 323 | * **Example:** Get the bound node pool |
| 324 | * ```typescript |
| 325 | * const getNodePool = yield* GCP.Container.GetNodePool(pool); |
| 326 | * const live = yield* getNodePool(); |
| 327 | * ``` |
| 328 | * |
| 329 | * @resource |
| 330 | * @category Container |
| 331 | */ |
| 332 | export const NodePool = Resource<NodePool>("GCP.Container.NodePool"); |
| 333 | |
| 334 | export class NodePoolNotResolved extends Data.TaggedError("GCP.Container.NodePoolNotResolved")<{ |
| 335 | resourceName: string; |
| 336 | }> {} |
| 337 | |
| 338 | export class NodePoolClusterMissing extends Data.TaggedError( |
| 339 | "GCP.Container.NodePoolClusterMissing", |
| 340 | )<{ |
| 341 | message: string; |
| 342 | }> {} |
| 343 | |
| 344 | export class NodePoolNotReady extends Data.TaggedError("GCP.Container.NodePoolNotReady")<{ |
| 345 | resourceName: string; |
| 346 | status: string; |
| 347 | }> {} |
| 348 | |
| 349 | export class NodePoolOperationFailed extends Data.TaggedError( |
| 350 | "GCP.Container.NodePoolOperationFailed", |
| 351 | )<{ |
| 352 | operation: string; |
| 353 | message: string; |
| 354 | }> {} |
| 355 | |
| 356 | export class NodePoolStillExists extends Data.TaggedError("GCP.Container.NodePoolStillExists")<{ |
| 357 | resourceName: string; |
| 358 | }> {} |
| 359 | |
| 360 | const lastSegment = (value: string) => { |
| 361 | const trimmed = value.replace(/\/+$/, ""); |
| 362 | const parts = trimmed.split("/"); |
| 363 | return parts[parts.length - 1] || trimmed; |
| 364 | }; |
| 365 | |
| 366 | const normalizeLocation = (location: string | undefined) => |
| 367 | lastSegment(location ?? DEFAULT_LOCATION).toLowerCase(); |
| 368 | |
| 369 | const rfc1035 = (name: string): string => { |
| 370 | let next = name |
| 371 | .toLowerCase() |
| 372 | .replace(/[^a-z0-9-]/g, "-") |
| 373 | .replace(/-+/g, "-") |
| 374 | .replace(/^-+|-+$/g, ""); |
| 375 | if (!/^[a-z]/.test(next)) next = `n${next}`; |
| 376 | next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, ""); |
| 377 | if (next.length === 0) return "nodepool"; |
| 378 | if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, MAX_NAME_LENGTH - 1)}0`; |
| 379 | return next.slice(0, MAX_NAME_LENGTH); |
| 380 | }; |
| 381 | |
| 382 | const clusterNameOf = (project: string, location: string, clusterId: string) => |
| 383 | `projects/${project}/locations/${location}/clusters/${clusterId}`; |
| 384 | |
| 385 | const resourceName = (project: string, location: string, clusterId: string, nodePoolId: string) => |
| 386 | `${clusterNameOf(project, location, clusterId)}/nodePools/${nodePoolId}`; |
| 387 | |
| 388 | const parseName = (name: string) => { |
| 389 | const idx = name.indexOf("projects/"); |
| 390 | const path = idx >= 0 ? name.slice(idx) : name; |
| 391 | const parts = path.split("/").filter((part) => part.length > 0); |
| 392 | const poolsAt = parts.lastIndexOf("nodePools"); |
| 393 | const clustersAt = parts.lastIndexOf("clusters"); |
| 394 | // `selfLink` spells the location as `zones/{zone}` (zonal clusters) or |
| 395 | // `regions/{region}`; resource names use `locations/{location}`. |
| 396 | const locationsAt = Math.max( |
| 397 | parts.lastIndexOf("locations"), |
| 398 | parts.lastIndexOf("zones"), |
| 399 | parts.lastIndexOf("regions"), |
| 400 | ); |
| 401 | const projectsAt = parts.lastIndexOf("projects"); |
| 402 | return { |
| 403 | project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "", |
| 404 | location: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : "", |
| 405 | clusterId: clustersAt >= 0 && parts[clustersAt + 1] ? parts[clustersAt + 1]! : "", |
| 406 | nodePoolId: poolsAt >= 0 && parts[poolsAt + 1] ? parts[poolsAt + 1]! : lastSegment(name), |
| 407 | }; |
| 408 | }; |
| 409 | |
| 410 | const parseClusterRef = ( |
| 411 | cluster: string, |
| 412 | fallbackProject: string, |
| 413 | fallbackLocation: string | undefined, |
| 414 | ) => { |
| 415 | const trimmed = cluster.trim(); |
| 416 | if (trimmed.includes("/clusters/") || trimmed.includes("projects/")) { |
| 417 | const parsed = parseName(trimmed.includes("/nodePools/") ? trimmed : `${trimmed}/nodePools/_`); |
| 418 | return { |
| 419 | project: parsed.project || fallbackProject, |
| 420 | location: normalizeLocation(parsed.location || fallbackLocation), |
| 421 | clusterId: parsed.clusterId, |
| 422 | }; |
| 423 | } |
| 424 | return { |
| 425 | project: fallbackProject, |
| 426 | location: normalizeLocation(fallbackLocation), |
| 427 | clusterId: lastSegment(trimmed), |
| 428 | }; |
| 429 | }; |
| 430 | |
| 431 | const userLabels = ( |
| 432 | labels: Record<string, string | undefined> | null | undefined, |
| 433 | ): Record<string, string> => stripInternalLabels(tagRecord(labels)); |
| 434 | |
| 435 | const stringMap = ( |
| 436 | labels: Record<string, string | undefined> | null | undefined, |
| 437 | ): Record<string, string> => tagRecord(labels); |
| 438 | |
| 439 | const sameStrings = (left: readonly string[], right: readonly string[]) => { |
| 440 | if (left.length !== right.length) return false; |
| 441 | const a = [...left].map((item) => item.toLowerCase()).sort(); |
| 442 | const b = [...right].map((item) => item.toLowerCase()).sort(); |
| 443 | return a.every((value, index) => value === b[index]); |
| 444 | }; |
| 445 | |
| 446 | const sameStringMap = ( |
| 447 | left: Record<string, string> | undefined, |
| 448 | right: Record<string, string> | undefined, |
| 449 | ) => { |
| 450 | const a = stringMap(left); |
| 451 | const b = stringMap(right); |
| 452 | const keys = Object.keys(a); |
| 453 | return keys.length === Object.keys(b).length && keys.every((key) => a[key] === b[key]); |
| 454 | }; |
| 455 | |
| 456 | const taintKey = (taint: NodePoolTaint) => |
| 457 | `${taint.key ?? ""}\0${taint.value ?? ""}\0${(taint.effect ?? "").toUpperCase()}`; |
| 458 | |
| 459 | const sameTaints = ( |
| 460 | left: readonly NodePoolTaint[] | undefined, |
| 461 | right: readonly NodePoolTaint[] | undefined, |
| 462 | ) => { |
| 463 | const a = [...(left ?? [])].map(taintKey).sort(); |
| 464 | const b = [...(right ?? [])].map(taintKey).sort(); |
| 465 | return a.length === b.length && a.every((value, index) => value === b[index]); |
| 466 | }; |
| 467 | |
| 468 | const autoscalingKey = (value: NodePoolAutoscaling | undefined) => |
| 469 | JSON.stringify({ |
| 470 | enabled: value?.enabled === true, |
| 471 | minNodeCount: value?.minNodeCount ?? 0, |
| 472 | maxNodeCount: value?.maxNodeCount ?? 0, |
| 473 | totalMinNodeCount: value?.totalMinNodeCount ?? 0, |
| 474 | totalMaxNodeCount: value?.totalMaxNodeCount ?? 0, |
| 475 | locationPolicy: (value?.locationPolicy ?? "").toUpperCase(), |
| 476 | autoprovisioned: value?.autoprovisioned === true, |
| 477 | }); |
| 478 | |
| 479 | const managementKey = (value: NodePoolManagement | undefined) => |
| 480 | JSON.stringify({ |
| 481 | autoRepair: value?.autoRepair === true, |
| 482 | autoUpgrade: value?.autoUpgrade === true, |
| 483 | }); |
| 484 | |
| 485 | const upgradeKey = (value: NodePoolUpgradeSettings | undefined) => |
| 486 | JSON.stringify({ |
| 487 | maxSurge: value?.maxSurge ?? 0, |
| 488 | maxUnavailable: value?.maxUnavailable ?? 0, |
| 489 | strategy: (value?.strategy ?? "").toUpperCase(), |
| 490 | }); |
| 491 | |
| 492 | const toId = (id: string, nodePoolId: string | undefined, existing?: string) => |
| 493 | Effect.gen(function* () { |
| 494 | return ( |
| 495 | nodePoolId ?? |
| 496 | existing ?? |
| 497 | rfc1035( |
| 498 | yield* createPhysicalName({ |
| 499 | id, |
| 500 | maxLength: MAX_NAME_LENGTH, |
| 501 | lowercase: true, |
| 502 | }), |
| 503 | ) |
| 504 | ); |
| 505 | }); |
| 506 | |
| 507 | const toAttrs = ( |
| 508 | pool: container.NodePool, |
| 509 | project: string, |
| 510 | location: string, |
| 511 | clusterId: string, |
| 512 | ) => { |
| 513 | const selfLink = pool.selfLink ?? ""; |
| 514 | const parsed = parseName( |
| 515 | selfLink.length > 0 ? selfLink : resourceName(project, location, clusterId, pool.name ?? ""), |
| 516 | ); |
| 517 | const nodePoolId = pool.name || parsed.nodePoolId; |
| 518 | const resolvedProject = parsed.project || project; |
| 519 | const resolvedLocation = normalizeLocation(parsed.location || location); |
| 520 | const resolvedCluster = parsed.clusterId || clusterId; |
| 521 | const config = pool.config; |
| 522 | return { |
| 523 | name: resourceName(resolvedProject, resolvedLocation, resolvedCluster, nodePoolId), |
| 524 | nodePoolId, |
| 525 | clusterId: resolvedCluster, |
| 526 | clusterName: clusterNameOf(resolvedProject, resolvedLocation, resolvedCluster), |
| 527 | project: resolvedProject, |
| 528 | location: resolvedLocation, |
| 529 | status: pool.status, |
| 530 | version: pool.version, |
| 531 | // The API omits a zero count. |
| 532 | nodeCount: pool.initialNodeCount ?? 0, |
| 533 | machineType: config?.machineType, |
| 534 | diskSizeGb: config?.diskSizeGb, |
| 535 | diskType: config?.diskType, |
| 536 | imageType: config?.imageType, |
| 537 | spot: config?.spot === true, |
| 538 | preemptible: config?.preemptible === true, |
| 539 | labels: userLabels(config?.resourceLabels), |
| 540 | nodeLabels: stringMap(config?.labels), |
| 541 | metadata: stringMap(config?.metadata), |
| 542 | workloadMetadataConfig: config?.workloadMetadataConfig, |
| 543 | shieldedInstanceConfig: config?.shieldedInstanceConfig, |
| 544 | advancedMachineFeatures: config?.advancedMachineFeatures, |
| 545 | tags: [...(config?.tags ?? [])], |
| 546 | nodeLocations: [...(pool.locations ?? [])], |
| 547 | autoscaling: pool.autoscaling, |
| 548 | management: pool.management |
| 549 | ? { |
| 550 | autoRepair: pool.management.autoRepair, |
| 551 | autoUpgrade: pool.management.autoUpgrade, |
| 552 | } |
| 553 | : undefined, |
| 554 | instanceGroupUrls: [...(pool.instanceGroupUrls ?? [])], |
| 555 | selfLink: pool.selfLink, |
| 556 | }; |
| 557 | }; |
| 558 | |
| 559 | const getByName = (name: string) => |
| 560 | container |
| 561 | .getProjectsLocationsClustersNodePools({ name }) |
| 562 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 563 | |
| 564 | const operationResourceName = ( |
| 565 | project: string, |
| 566 | location: string, |
| 567 | operation: container.Operation, |
| 568 | ) => { |
| 569 | const raw = operation.name ?? ""; |
| 570 | if (raw.includes("/operations/")) { |
| 571 | const idx = raw.indexOf("projects/"); |
| 572 | return idx >= 0 ? raw.slice(idx) : raw; |
| 573 | } |
| 574 | const fromLink = operation.selfLink ?? ""; |
| 575 | if (fromLink.includes("/operations/")) { |
| 576 | const idx = fromLink.indexOf("projects/"); |
| 577 | return idx >= 0 ? fromLink.slice(idx) : fromLink; |
| 578 | } |
| 579 | const loc = lastSegment(operation.location ?? operation.zone ?? location); |
| 580 | return `projects/${project}/locations/${loc}/operations/${lastSegment(raw)}`; |
| 581 | }; |
| 582 | |
| 583 | /** |
| 584 | * Wait for a GKE node-pool operation (create/delete takes several |
| 585 | * minutes). ALREADY_EXISTS (code 6) counts as success (create race); with |
| 586 | * `notFoundOk`, so does NOT_FOUND (code 5, delete race) and an operation |
| 587 | * that is already gone. |
| 588 | */ |
| 589 | const waitForOperation = ( |
| 590 | project: string, |
| 591 | location: string, |
| 592 | operation: container.Operation, |
| 593 | options?: { notFoundOk?: boolean }, |
| 594 | ) => |
| 595 | waitForGcpOperation( |
| 596 | { |
| 597 | ...operation, |
| 598 | name: operationResourceName(project, location, operation), |
| 599 | }, |
| 600 | (name) => container.getProjectsLocationsOperations({ name }), |
| 601 | { budget: "20 minutes", interval: "10 seconds" }, |
| 602 | ).pipe( |
| 603 | Effect.catchIf( |
| 604 | (error) => |
| 605 | (error._tag === "GCP.OperationFailed" && |
| 606 | (error.code === 6 || (options?.notFoundOk === true && error.code === 5))) || |
| 607 | (options?.notFoundOk === true && error._tag === "NotFound"), |
| 608 | () => Effect.succeed(operation), |
| 609 | ), |
| 610 | ); |
| 611 | |
| 612 | const waitUntilExists = (name: string) => |
| 613 | getByName(name).pipe( |
| 614 | Effect.filterOrFail( |
| 615 | (pool): pool is container.NodePool => pool !== undefined, |
| 616 | () => new NodePoolNotResolved({ resourceName: name }), |
| 617 | ), |
| 618 | Effect.retry({ |
| 619 | while: (error) => error._tag === "GCP.Container.NodePoolNotResolved", |
| 620 | times: 8, |
| 621 | schedule: Schedule.spaced("1 second"), |
| 622 | }), |
| 623 | ); |
| 624 | |
| 625 | const waitUntilReady = (name: string) => |
| 626 | getByName(name).pipe( |
| 627 | Effect.filterOrFail( |
| 628 | (pool): pool is container.NodePool => pool !== undefined, |
| 629 | () => new NodePoolNotResolved({ resourceName: name }), |
| 630 | ), |
| 631 | Effect.filterOrFail( |
| 632 | (pool) => { |
| 633 | const status = pool.status ?? "STATUS_UNSPECIFIED"; |
| 634 | return status !== "ERROR" && status !== "STOPPING"; |
| 635 | }, |
| 636 | (pool) => |
| 637 | new NodePoolOperationFailed({ |
| 638 | operation: name, |
| 639 | message: pool.statusMessage ?? `node pool is in ${pool.status ?? "STATUS_UNSPECIFIED"}`, |
| 640 | }), |
| 641 | ), |
| 642 | Effect.filterOrFail( |
| 643 | (pool) => { |
| 644 | const status = pool.status ?? "STATUS_UNSPECIFIED"; |
| 645 | return status === "RUNNING" || status === "RUNNING_WITH_ERROR"; |
| 646 | }, |
| 647 | (pool) => |
| 648 | new NodePoolNotReady({ |
| 649 | resourceName: name, |
| 650 | status: pool.status ?? "STATUS_UNSPECIFIED", |
| 651 | }), |
| 652 | ), |
| 653 | Effect.retry({ |
| 654 | while: (error) => |
| 655 | error._tag === "GCP.Container.NodePoolNotReady" || |
| 656 | error._tag === "GCP.Container.NodePoolNotResolved", |
| 657 | // Node pool create/delete takes several minutes. |
| 658 | times: 60, |
| 659 | schedule: Schedule.spaced("10 seconds"), |
| 660 | }), |
| 661 | ); |
| 662 | |
| 663 | const waitUntilGone = (name: string) => |
| 664 | getByName(name).pipe( |
| 665 | Effect.flatMap((pool) => |
| 666 | pool === undefined |
| 667 | ? Effect.void |
| 668 | : Effect.fail(new NodePoolStillExists({ resourceName: name })), |
| 669 | ), |
| 670 | Effect.retry({ |
| 671 | while: (error) => error._tag === "GCP.Container.NodePoolStillExists", |
| 672 | // Node pool create/delete takes several minutes. |
| 673 | times: 60, |
| 674 | schedule: Schedule.spaced("10 seconds"), |
| 675 | }), |
| 676 | ); |
| 677 | |
| 678 | const retryConflict = <A, E extends { readonly _tag: string }, R>(effect: Effect.Effect<A, E, R>) => |
| 679 | effect.pipe( |
| 680 | Effect.retry({ |
| 681 | while: (error) => error._tag === "Conflict", |
| 682 | times: 8, |
| 683 | schedule: Schedule.spaced("5 seconds"), |
| 684 | }), |
| 685 | ); |
| 686 | |
| 687 | const toCreatePool = ( |
| 688 | news: NodePoolProps, |
| 689 | nodePoolId: string, |
| 690 | desiredLabels: Record<string, string>, |
| 691 | workloadIdentity: boolean, |
| 692 | ): container.NodePool => ({ |
| 693 | name: nodePoolId, |
| 694 | initialNodeCount: news.nodeCount ?? DEFAULT_NODE_COUNT, |
| 695 | config: { |
| 696 | machineType: news.machineType ?? DEFAULT_MACHINE_TYPE, |
| 697 | diskSizeGb: news.diskSizeGb ?? DEFAULT_DISK_SIZE_GB, |
| 698 | diskType: news.diskType ?? DEFAULT_DISK_TYPE, |
| 699 | imageType: news.imageType, |
| 700 | spot: news.spot === true, |
| 701 | preemptible: news.preemptible === true, |
| 702 | resourceLabels: desiredLabels, |
| 703 | labels: news.nodeLabels, |
| 704 | metadata: news.metadata, |
| 705 | tags: news.tags, |
| 706 | taints: news.taints, |
| 707 | oauthScopes: news.oauthScopes, |
| 708 | serviceAccount: news.serviceAccount, |
| 709 | localSsdCount: news.localSsdCount, |
| 710 | bootDiskKmsKey: news.bootDiskKmsKey, |
| 711 | // GKE rejects GKE_METADATA on clusters without Workload Identity. |
| 712 | workloadMetadataConfig: |
| 713 | news.workloadMetadataConfig ?? (workloadIdentity ? { mode: "GKE_METADATA" } : undefined), |
| 714 | shieldedInstanceConfig: news.shieldedInstanceConfig, |
| 715 | advancedMachineFeatures: news.advancedMachineFeatures, |
| 716 | }, |
| 717 | autoscaling: news.autoscaling, |
| 718 | management: news.management, |
| 719 | version: news.version, |
| 720 | locations: news.nodeLocations, |
| 721 | maxPodsConstraint: |
| 722 | news.maxPodsPerNode !== undefined ? { maxPodsPerNode: String(news.maxPodsPerNode) } : undefined, |
| 723 | upgradeSettings: news.upgradeSettings, |
| 724 | }); |
| 725 | |
| 726 | const poolLabels = (pool: container.NodePool) => tagRecord(pool.config?.resourceLabels); |
| 727 | |
| 728 | const systemLabels = (labels: Record<string, string>) => |
| 729 | Object.fromEntries(Object.entries(labels).filter(([key]) => key.startsWith("goog-"))); |
| 730 | |
| 731 | const shortName = (value: string | undefined, fallback: string) => lastSegment(value ?? fallback); |
| 732 | |
| 733 | const desiredManagement = ( |
| 734 | news: NodePoolManagement, |
| 735 | observed: NodePoolManagement | undefined, |
| 736 | ): NodePoolManagement => ({ |
| 737 | autoRepair: news.autoRepair ?? observed?.autoRepair, |
| 738 | autoUpgrade: news.autoUpgrade ?? observed?.autoUpgrade, |
| 739 | }); |
| 740 | |
| 741 | export const NodePoolProvider = () => |
| 742 | Provider.succeed(NodePool, { |
| 743 | stables: ["name", "nodePoolId", "clusterId", "clusterName", "project", "location", "selfLink"], |
| 744 | |
| 745 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 746 | if (!isResolved(news)) return undefined; |
| 747 | if (news.cluster === undefined || news.cluster.length === 0) { |
| 748 | return undefined; |
| 749 | } |
| 750 | |
| 751 | const previousId = olds?.nodePoolId ?? output?.nodePoolId; |
| 752 | const nextId = news.nodePoolId ?? previousId; |
| 753 | const previousCluster = lastSegment(olds?.cluster ?? output?.clusterId ?? ""); |
| 754 | const nextCluster = lastSegment( |
| 755 | parseClusterRef(news.cluster, "", news.location ?? output?.location).clusterId, |
| 756 | ); |
| 757 | const previousLocation = normalizeLocation(olds?.location ?? output?.location); |
| 758 | const nextLocation = normalizeLocation( |
| 759 | parseClusterRef(news.cluster, "", news.location ?? output?.location).location, |
| 760 | ); |
| 761 | const previousSpot = olds?.spot === true || output?.spot === true; |
| 762 | const nextSpot = news.spot === true; |
| 763 | const previousPreemptible = olds?.preemptible === true || output?.preemptible === true; |
| 764 | const nextPreemptible = news.preemptible === true; |
| 765 | const previousSa = olds?.serviceAccount ?? ""; |
| 766 | const nextSa = news.serviceAccount ?? previousSa; |
| 767 | const previousKms = olds?.bootDiskKmsKey ?? ""; |
| 768 | const nextKms = news.bootDiskKmsKey ?? previousKms; |
| 769 | const previousSsds = olds?.localSsdCount ?? 0; |
| 770 | const nextSsds = news.localSsdCount ?? previousSsds; |
| 771 | const previousPods = olds?.maxPodsPerNode; |
| 772 | const nextPods = news.maxPodsPerNode ?? previousPods; |
| 773 | const previousScopes = olds?.oauthScopes ?? []; |
| 774 | const nextScopes = news.oauthScopes ?? previousScopes; |
| 775 | // Observed node config is the baseline: GKE injects metadata keys and |
| 776 | // Shielded VM defaults the user never declared, so a redeploy that |
| 777 | // spells them out must not replace the pool. |
| 778 | const previousMetadata = output?.metadata ?? olds?.metadata ?? {}; |
| 779 | const previousShielded = output?.shieldedInstanceConfig ?? olds?.shieldedInstanceConfig; |
| 780 | const previousAdvanced = output?.advancedMachineFeatures ?? olds?.advancedMachineFeatures; |
| 781 | |
| 782 | const replace = |
| 783 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 784 | (previousCluster.length > 0 && nextCluster.length > 0 && previousCluster !== nextCluster) || |
| 785 | previousLocation !== nextLocation || |
| 786 | previousSpot !== nextSpot || |
| 787 | previousPreemptible !== nextPreemptible || |
| 788 | previousSa !== nextSa || |
| 789 | previousKms !== nextKms || |
| 790 | previousSsds !== nextSsds || |
| 791 | (previousPods !== undefined && nextPods !== undefined && previousPods !== nextPods) || |
| 792 | (news.oauthScopes !== undefined && !sameStrings(previousScopes, nextScopes)) || |
| 793 | (news.metadata !== undefined && !matchesDesired(previousMetadata, news.metadata)) || |
| 794 | (news.shieldedInstanceConfig !== undefined && |
| 795 | !matchesDesired(previousShielded, news.shieldedInstanceConfig)) || |
| 796 | (news.advancedMachineFeatures !== undefined && |
| 797 | !matchesDesired(previousAdvanced, news.advancedMachineFeatures)); |
| 798 | |
| 799 | if (!replace) return undefined; |
| 800 | return { |
| 801 | action: "replace" as const, |
| 802 | deleteFirst: |
| 803 | previousLocation === nextLocation && |
| 804 | previousCluster === nextCluster && |
| 805 | previousId !== undefined && |
| 806 | nextId === previousId, |
| 807 | }; |
| 808 | }), |
| 809 | |
| 810 | read: Effect.fn(function* ({ id, olds, output }) { |
| 811 | const env = yield* GcpEnvironment.current; |
| 812 | const nodePoolId = yield* toId(id, olds?.nodePoolId, output?.nodePoolId); |
| 813 | const clusterRef = |
| 814 | (typeof olds?.cluster === "string" ? olds.cluster : undefined) ?? |
| 815 | output?.clusterName ?? |
| 816 | output?.clusterId ?? |
| 817 | ""; |
| 818 | // A pool whose cluster never resolved (e.g. the cluster create failed) |
| 819 | // was never created and cannot be looked up. |
| 820 | if (output?.name === undefined && clusterRef.trim().length === 0) { |
| 821 | return undefined; |
| 822 | } |
| 823 | const ref = parseClusterRef(clusterRef, env.project, olds?.location ?? output?.location); |
| 824 | const name = |
| 825 | output?.name ?? resourceName(ref.project, ref.location, ref.clusterId, nodePoolId); |
| 826 | const existing = yield* getByName(name); |
| 827 | if (existing === undefined) return undefined; |
| 828 | const attrs = toAttrs(existing, ref.project, ref.location, ref.clusterId); |
| 829 | return (yield* hasAlchemyLabels(id, poolLabels(existing))) ? attrs : Unowned(attrs); |
| 830 | }), |
| 831 | |
| 832 | list: () => |
| 833 | Effect.gen(function* () { |
| 834 | const env = yield* GcpEnvironment.current; |
| 835 | const page = yield* container |
| 836 | .listProjectsLocationsClusters({ |
| 837 | parent: `projects/${env.project}/locations/-`, |
| 838 | }) |
| 839 | .pipe( |
| 840 | Effect.catchTag("NotFound", () => |
| 841 | Effect.succeed({ |
| 842 | clusters: [], |
| 843 | } satisfies container.ListClustersResponse), |
| 844 | ), |
| 845 | ); |
| 846 | |
| 847 | const found: ReturnType<typeof toAttrs>[] = []; |
| 848 | for (const cluster of page.clusters ?? []) { |
| 849 | const parsed = parseName( |
| 850 | cluster.selfLink ?? |
| 851 | resourceName( |
| 852 | env.project, |
| 853 | cluster.location ?? DEFAULT_LOCATION, |
| 854 | cluster.name ?? "", |
| 855 | "_", |
| 856 | ), |
| 857 | ); |
| 858 | const project = parsed.project || env.project; |
| 859 | const location = normalizeLocation(cluster.location ?? parsed.location); |
| 860 | const clusterId = cluster.name || parsed.clusterId; |
| 861 | const nested = cluster.nodePools; |
| 862 | const pools = |
| 863 | nested !== undefined |
| 864 | ? nested |
| 865 | : yield* container |
| 866 | .listProjectsLocationsClustersNodePools({ |
| 867 | parent: clusterNameOf(project, location, clusterId), |
| 868 | }) |
| 869 | .pipe( |
| 870 | Effect.map((response) => response.nodePools ?? []), |
| 871 | Effect.catchTag("NotFound", () => Effect.succeed([] as container.NodePool[])), |
| 872 | ); |
| 873 | for (const pool of pools) { |
| 874 | if ( |
| 875 | Object.keys(pool.config?.resourceLabels ?? {}).some((key) => |
| 876 | key.startsWith("alchemy-"), |
| 877 | ) |
| 878 | ) { |
| 879 | found.push(toAttrs(pool, project, location, clusterId)); |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | return found; |
| 884 | }), |
| 885 | |
| 886 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 887 | const env = yield* GcpEnvironment.current; |
| 888 | if (news.cluster === undefined || news.cluster.length === 0) { |
| 889 | return yield* new NodePoolClusterMissing({ |
| 890 | message: "GCP.Container.NodePool requires `cluster` (cluster id or full resource name).", |
| 891 | }); |
| 892 | } |
| 893 | const ref = parseClusterRef(news.cluster, env.project, news.location ?? output?.location); |
| 894 | const nodePoolId = yield* toId(id, news.nodePoolId, output?.nodePoolId); |
| 895 | const name = resourceName(ref.project, ref.location, ref.clusterId, nodePoolId); |
| 896 | const parent = clusterNameOf(ref.project, ref.location, ref.clusterId); |
| 897 | const desiredLabels = { |
| 898 | ...toLabels(news.labels), |
| 899 | ...(yield* createInternalLabels(id)), |
| 900 | }; |
| 901 | const nodeCount = news.nodeCount ?? DEFAULT_NODE_COUNT; |
| 902 | const machineType = news.machineType ?? DEFAULT_MACHINE_TYPE; |
| 903 | const diskSizeGb = news.diskSizeGb ?? DEFAULT_DISK_SIZE_GB; |
| 904 | const diskType = news.diskType ?? DEFAULT_DISK_TYPE; |
| 905 | |
| 906 | let current = yield* getByName(output?.name ?? name); |
| 907 | |
| 908 | if (current === undefined) { |
| 909 | const cluster = yield* container |
| 910 | .getProjectsLocationsClusters({ name: parent }) |
| 911 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 912 | const workloadIdentity = (cluster?.workloadIdentityConfig?.workloadPool ?? "").length > 0; |
| 913 | const created = yield* retryConflict( |
| 914 | container |
| 915 | .createProjectsLocationsClustersNodePools({ |
| 916 | parent, |
| 917 | body: { |
| 918 | nodePool: toCreatePool(news, nodePoolId, desiredLabels, workloadIdentity), |
| 919 | }, |
| 920 | }) |
| 921 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)), |
| 922 | ); |
| 923 | if (created !== undefined) { |
| 924 | yield* waitForOperation(ref.project, ref.location, created); |
| 925 | } |
| 926 | current = yield* waitUntilExists(name); |
| 927 | } |
| 928 | |
| 929 | if (current === undefined) { |
| 930 | return yield* new NodePoolNotResolved({ resourceName: name }); |
| 931 | } |
| 932 | |
| 933 | let live = current; |
| 934 | const status = live.status ?? "STATUS_UNSPECIFIED"; |
| 935 | if (status !== "RUNNING" && status !== "RUNNING_WITH_ERROR") { |
| 936 | live = yield* waitUntilReady(name); |
| 937 | } |
| 938 | |
| 939 | const apply = (operation: container.Operation) => |
| 940 | Effect.gen(function* () { |
| 941 | yield* waitForOperation(ref.project, ref.location, operation); |
| 942 | return yield* waitUntilReady(name); |
| 943 | }); |
| 944 | |
| 945 | const observedLabels = poolLabels(live); |
| 946 | const nextLabels = { ...systemLabels(observedLabels), ...desiredLabels }; |
| 947 | const { upsert, removed } = diffLabels(observedLabels, nextLabels); |
| 948 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 949 | const observedMachine = shortName(live.config?.machineType, DEFAULT_MACHINE_TYPE); |
| 950 | const machineChanged = observedMachine.toLowerCase() !== machineType.toLowerCase(); |
| 951 | const observedDiskSize = live.config?.diskSizeGb ?? DEFAULT_DISK_SIZE_GB; |
| 952 | const diskSizeChanged = observedDiskSize !== diskSizeGb; |
| 953 | const observedDiskType = shortName(live.config?.diskType, DEFAULT_DISK_TYPE).toLowerCase(); |
| 954 | const diskTypeChanged = observedDiskType !== diskType.toLowerCase(); |
| 955 | const imageChanged = |
| 956 | news.imageType !== undefined && (live.config?.imageType ?? "") !== news.imageType; |
| 957 | const versionChanged = news.version !== undefined && (live.version ?? "") !== news.version; |
| 958 | const nodeLabelsChanged = |
| 959 | news.nodeLabels !== undefined && |
| 960 | !sameStringMap(stringMap(live.config?.labels), news.nodeLabels); |
| 961 | const tagsChanged = |
| 962 | news.tags !== undefined && !sameStrings(live.config?.tags ?? [], news.tags); |
| 963 | const taintsChanged = |
| 964 | news.taints !== undefined && !sameTaints(live.config?.taints, news.taints); |
| 965 | const workloadMetadataChanged = |
| 966 | news.workloadMetadataConfig !== undefined && |
| 967 | !matchesDesired(live.config?.workloadMetadataConfig, news.workloadMetadataConfig); |
| 968 | const upgradeChanged = |
| 969 | news.upgradeSettings !== undefined && |
| 970 | upgradeKey(live.upgradeSettings) !== upgradeKey(news.upgradeSettings); |
| 971 | const locationsChanged = |
| 972 | news.nodeLocations !== undefined && !sameStrings(live.locations ?? [], news.nodeLocations); |
| 973 | |
| 974 | const configChanged = |
| 975 | labelsChanged || |
| 976 | machineChanged || |
| 977 | diskSizeChanged || |
| 978 | diskTypeChanged || |
| 979 | imageChanged || |
| 980 | versionChanged || |
| 981 | nodeLabelsChanged || |
| 982 | tagsChanged || |
| 983 | taintsChanged || |
| 984 | workloadMetadataChanged || |
| 985 | upgradeChanged; |
| 986 | |
| 987 | if (configChanged) { |
| 988 | const imageType = news.imageType ?? live.config?.imageType; |
| 989 | const nodeVersion = news.version ?? live.version; |
| 990 | const body: container.UpdateNodePoolRequest = {}; |
| 991 | if (imageType !== undefined) body.imageType = imageType; |
| 992 | if (nodeVersion !== undefined) body.nodeVersion = nodeVersion; |
| 993 | if (labelsChanged) { |
| 994 | body.resourceLabels = { labels: nextLabels }; |
| 995 | } |
| 996 | if (machineChanged) body.machineType = machineType; |
| 997 | if (diskSizeChanged) body.diskSizeGb = String(diskSizeGb); |
| 998 | if (diskTypeChanged) body.diskType = diskType; |
| 999 | if (nodeLabelsChanged) { |
| 1000 | body.labels = { labels: news.nodeLabels }; |
| 1001 | } |
| 1002 | if (tagsChanged) body.tags = { tags: news.tags }; |
| 1003 | if (taintsChanged) body.taints = { taints: news.taints }; |
| 1004 | if (workloadMetadataChanged) { |
| 1005 | body.workloadMetadataConfig = news.workloadMetadataConfig; |
| 1006 | } |
| 1007 | if (upgradeChanged) body.upgradeSettings = news.upgradeSettings; |
| 1008 | const updated = yield* retryConflict( |
| 1009 | container.updateProjectsLocationsClustersNodePools({ |
| 1010 | name, |
| 1011 | body, |
| 1012 | }), |
| 1013 | ); |
| 1014 | live = yield* apply(updated); |
| 1015 | } |
| 1016 | |
| 1017 | if (locationsChanged) { |
| 1018 | const imageType = news.imageType ?? live.config?.imageType; |
| 1019 | const nodeVersion = news.version ?? live.version; |
| 1020 | const relocated = yield* retryConflict( |
| 1021 | container.updateProjectsLocationsClustersNodePools({ |
| 1022 | name, |
| 1023 | body: { |
| 1024 | locations: news.nodeLocations, |
| 1025 | ...(imageType !== undefined ? { imageType } : {}), |
| 1026 | ...(nodeVersion !== undefined ? { nodeVersion } : {}), |
| 1027 | }, |
| 1028 | }), |
| 1029 | ); |
| 1030 | live = yield* apply(relocated); |
| 1031 | } |
| 1032 | |
| 1033 | if ( |
| 1034 | news.autoscaling !== undefined && |
| 1035 | autoscalingKey(live.autoscaling) !== autoscalingKey(news.autoscaling) |
| 1036 | ) { |
| 1037 | const scaled = yield* retryConflict( |
| 1038 | container.setAutoscalingProjectsLocationsClustersNodePools({ |
| 1039 | name, |
| 1040 | body: { autoscaling: news.autoscaling }, |
| 1041 | }), |
| 1042 | ); |
| 1043 | live = yield* apply(scaled); |
| 1044 | } |
| 1045 | |
| 1046 | if (news.management !== undefined) { |
| 1047 | const nextManagement = desiredManagement(news.management, live.management); |
| 1048 | if (managementKey(live.management) !== managementKey(nextManagement)) { |
| 1049 | const managed = yield* retryConflict( |
| 1050 | container.setManagementProjectsLocationsClustersNodePools({ |
| 1051 | name, |
| 1052 | body: { management: nextManagement }, |
| 1053 | }), |
| 1054 | ); |
| 1055 | live = yield* apply(managed); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | const autoscalingOn = |
| 1060 | news.autoscaling?.enabled === true || live.autoscaling?.enabled === true; |
| 1061 | if (!autoscalingOn && (live.initialNodeCount ?? 0) !== nodeCount) { |
| 1062 | const resized = yield* retryConflict( |
| 1063 | container.setSizeProjectsLocationsClustersNodePools({ |
| 1064 | name, |
| 1065 | body: { nodeCount }, |
| 1066 | }), |
| 1067 | ); |
| 1068 | live = yield* apply(resized); |
| 1069 | } |
| 1070 | |
| 1071 | return toAttrs(live, ref.project, ref.location, ref.clusterId); |
| 1072 | }), |
| 1073 | |
| 1074 | delete: Effect.fn(function* ({ output }) { |
| 1075 | const env = yield* GcpEnvironment.current; |
| 1076 | const location = normalizeLocation(output.location); |
| 1077 | const operation = yield* container |
| 1078 | .deleteProjectsLocationsClustersNodePools({ name: output.name }) |
| 1079 | .pipe( |
| 1080 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 1081 | Effect.retry({ |
| 1082 | while: (error) => error._tag === "Conflict", |
| 1083 | times: 8, |
| 1084 | schedule: Schedule.spaced("5 seconds"), |
| 1085 | }), |
| 1086 | ); |
| 1087 | if (operation !== undefined) { |
| 1088 | yield* waitForOperation(env.project, location, operation, { |
| 1089 | notFoundOk: true, |
| 1090 | }); |
| 1091 | } |
| 1092 | yield* waitUntilGone(output.name); |
| 1093 | }), |
| 1094 | }); |