@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, 04:45 UTC

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