@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, 06:00 UTC

main
ts 599 lines 22.6 KB
Raw
1 import * as eks from "@distilled.cloud/aws/eks";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import type { Input } from "alchemy/Input";
5 import { createPhysicalName } from "alchemy/PhysicalName";
6 import * as Provider from "alchemy/Provider";
7 import { Resource } from "alchemy/Resource";
8 import { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags";
9 import * as Data from "effect/Data";
10 import * as Effect from "effect/Effect";
11 import * as Schedule from "effect/Schedule";
12 import * as Stream from "effect/Stream";
13 import type { Providers } from "../Providers.ts";
14
15 export class NodegroupOperationFailed extends Data.TaggedError("NodegroupOperationFailed")<{
16 readonly message: string;
17 readonly cause?: unknown;
18 }> {}
19
20 export class NodegroupUnexpectedResponse extends Data.TaggedError("NodegroupUnexpectedResponse")<{
21 readonly message: string;
22 readonly cause?: unknown;
23 }> {}
24
25 export interface NodegroupProps {
26 /**
27 * Name of the EKS cluster that owns this managed node group.
28 */
29 clusterName: Input<string>;
30 /**
31 * Name of the managed node group. If omitted, a unique name is generated.
32 */
33 nodegroupName?: string;
34 /**
35 * ARN of the IAM role that provides permissions for the node group's worker
36 * nodes (the `NodeInstanceRole`). Changing this replaces the node group.
37 */
38 nodeRole: Input<string>;
39 /**
40 * Subnet IDs to launch worker nodes into. Changing this replaces the node
41 * group.
42 */
43 subnets: Input<string>[];
44 /**
45 * EC2 instance types for the node group. Changing this replaces the node
46 * group.
47 * @default ["t3.medium"]
48 */
49 instanceTypes?: string[];
50 /**
51 * Scaling configuration (min/max/desired node counts). Mutable — updated in
52 * place via `updateNodegroupConfig`.
53 */
54 scalingConfig?: eks.NodegroupScalingConfig;
55 /**
56 * AMI type for the node group's nodes (e.g. `AL2023_x86_64_STANDARD`,
57 * `BOTTLEROCKET_x86_64`). Changing this replaces the node group.
58 */
59 amiType?: eks.AMITypes;
60 /**
61 * Capacity type for the node group. Changing this replaces the node group.
62 * @default "ON_DEMAND"
63 */
64 capacityType?: eks.CapacityTypes;
65 /**
66 * Root device disk size in GiB. Changing this replaces the node group.
67 */
68 diskSize?: number;
69 /**
70 * Kubernetes labels applied to the nodes. Mutable — reconciled in place.
71 */
72 labels?: Record<string, string>;
73 /**
74 * Kubernetes taints applied to the nodes. Mutable — reconciled in place.
75 */
76 taints?: eks.Taint[];
77 /**
78 * Node group update configuration (`maxUnavailable` /
79 * `maxUnavailablePercentage`). Mutable — updated in place.
80 */
81 updateConfig?: eks.NodegroupUpdateConfig;
82 /**
83 * Remote SSH access configuration. Changing this replaces the node group.
84 */
85 remoteAccess?: eks.RemoteAccessConfig;
86 /**
87 * Kubernetes version for the node group. Mutable — upgraded in place via
88 * `updateNodegroupVersion`.
89 */
90 version?: string;
91 /**
92 * AMI release version for the node group. Mutable — upgraded in place.
93 */
94 releaseVersion?: string;
95 /**
96 * User-defined tags to apply to the node group.
97 */
98 tags?: Record<string, string>;
99 }
100
101 export interface Nodegroup extends Resource<
102 "AWS.EKS.Nodegroup",
103 NodegroupProps,
104 {
105 /** The name of the node group. */
106 nodegroupName: string;
107 /** The ARN of the node group. */
108 nodegroupArn: string;
109 /** The name of the EKS cluster the node group belongs to. */
110 clusterName: string;
111 /** The node group status (e.g. `CREATING`, `ACTIVE`, `UPDATING`). */
112 status: eks.NodegroupStatus;
113 /** The capacity type (`ON_DEMAND`, `SPOT`, or `CAPACITY_BLOCK`). */
114 capacityType: eks.CapacityTypes | undefined;
115 /** The scaling configuration (min/max/desired size). */
116 scalingConfig: eks.NodegroupScalingConfig | undefined;
117 /** The EC2 instance types the node group launches. */
118 instanceTypes: string[];
119 /** The IDs of the subnets nodes are launched into. */
120 subnets: string[];
121 /** The AMI type of the nodes (e.g. `AL2023_x86_64_STANDARD`). */
122 amiType: eks.AMITypes | undefined;
123 /** The ARN of the IAM role attached to the nodes. */
124 nodeRole: string;
125 /** The Kubernetes labels applied to nodes in the group. */
126 labels: Record<string, string>;
127 /** The Kubernetes taints applied to nodes in the group. */
128 taints: eks.Taint[];
129 /** The root device disk size in GiB, when not using a launch template. */
130 diskSize: number | undefined;
131 /** The Kubernetes version of the node group. */
132 version: string | undefined;
133 /** The AMI release version of the node group. */
134 releaseVersion: string | undefined;
135 /** The rolling-update configuration (max unavailable). */
136 updateConfig: eks.NodegroupUpdateConfig | undefined;
137 /** The tags applied to the node group. */
138 tags: Record<string, string>;
139 },
140 never,
141 Providers
142 > {}
143
144 /**
145 * An Amazon EKS managed node group — an EC2 Auto Scaling group of worker nodes
146 * managed by EKS for a cluster.
147 *
148 * Managed node groups provide the compute capacity a non-Auto-Mode EKS cluster
149 * needs to run pods. Creation is asynchronous (`CREATING` → `ACTIVE`, ~2–5 min)
150 * and the provider waits for the group to become `ACTIVE` before returning.
151 * Scaling, labels, taints, update config, and version are mutable in place;
152 * subnets, instance types, AMI type, disk size, node role, capacity type, and
153 * remote access are immutable and force a replacement.
154 * ### Creating Node Groups
155 * **Example:** Managed Node Group
156 * ```typescript
157 * const nodes = yield* Nodegroup("AppNodes", {
158 * clusterName: cluster.clusterName,
159 * nodeRole: nodeRole.roleArn,
160 * subnets: network.privateSubnetIds,
161 * instanceTypes: ["t3.medium"],
162 * scalingConfig: { minSize: 1, maxSize: 3, desiredSize: 2 },
163 * });
164 * ```
165 *
166 * **Example:** Spot Node Group with Labels and Taints
167 * ```typescript
168 * const spot = yield* Nodegroup("SpotNodes", {
169 * clusterName: cluster.clusterName,
170 * nodeRole: nodeRole.roleArn,
171 * subnets: network.privateSubnetIds,
172 * capacityType: "SPOT",
173 * instanceTypes: ["t3.large", "t3a.large"],
174 * scalingConfig: { minSize: 0, maxSize: 5, desiredSize: 1 },
175 * labels: { workload: "batch" },
176 * taints: [{ key: "spot", value: "true", effect: "NO_SCHEDULE" }],
177 * });
178 * ```
179 *
180 * @resource
181 */
182 export const Nodegroup = Resource<Nodegroup>("AWS.EKS.Nodegroup");
183
184 class NodegroupNotReady extends Data.TaggedError("EKS.NodegroupNotReady")<{
185 status: eks.NodegroupStatus | undefined;
186 }> {}
187
188 class NodegroupStillExists extends Data.TaggedError("EKS.NodegroupStillExists")<{}> {}
189
190 class NodegroupUpdateNotComplete extends Data.TaggedError("EKS.NodegroupUpdateNotComplete")<{
191 status: eks.UpdateStatus | undefined;
192 }> {}
193
194 const normalizeTags = (tags: Record<string, string | undefined> | undefined) =>
195 Object.fromEntries(
196 Object.entries(tags ?? {}).filter((entry): entry is [string, string] => entry[1] !== undefined),
197 );
198
199 const normalizeLabels = (labels: Record<string, string | undefined> | undefined) =>
200 Object.fromEntries(
201 Object.entries(labels ?? {}).filter(
202 (entry): entry is [string, string] => entry[1] !== undefined,
203 ),
204 );
205
206 // Wait budget: ~10 min at 5s spacing — node groups reach ACTIVE in 2–5 min.
207 const waitSchedule = Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(120)]);
208
209 const taintKey = (taint: eks.Taint) => `${taint.key ?? ""}::${taint.effect ?? ""}`;
210
211 /**
212 * Compute the `UpdateLabelsPayload` delta between observed and desired labels.
213 * Returns `undefined` when there is nothing to change.
214 */
215 const diffLabels = (
216 observed: Record<string, string>,
217 desired: Record<string, string>,
218 ): eks.UpdateLabelsPayload | undefined => {
219 const addOrUpdateLabels: Record<string, string> = {};
220 for (const [key, value] of Object.entries(desired)) {
221 if (observed[key] !== value) {
222 addOrUpdateLabels[key] = value;
223 }
224 }
225 const removeLabels = Object.keys(observed).filter((key) => !(key in desired));
226 if (Object.keys(addOrUpdateLabels).length === 0 && removeLabels.length === 0) {
227 return undefined;
228 }
229 return {
230 addOrUpdateLabels: Object.keys(addOrUpdateLabels).length > 0 ? addOrUpdateLabels : undefined,
231 removeLabels: removeLabels.length > 0 ? removeLabels : undefined,
232 };
233 };
234
235 /**
236 * Compute the `UpdateTaintsPayload` delta between observed and desired taints,
237 * keyed by (key, effect). Returns `undefined` when there is nothing to change.
238 */
239 const diffTaints = (
240 observed: eks.Taint[],
241 desired: eks.Taint[],
242 ): eks.UpdateTaintsPayload | undefined => {
243 const observedByKey = new Map(observed.map((t) => [taintKey(t), t]));
244 const desiredByKey = new Map(desired.map((t) => [taintKey(t), t]));
245
246 const addOrUpdateTaints = desired.filter((t) => {
247 const existing = observedByKey.get(taintKey(t));
248 return !existing || existing.value !== t.value;
249 });
250 const removeTaints = observed.filter((t) => !desiredByKey.has(taintKey(t)));
251
252 if (addOrUpdateTaints.length === 0 && removeTaints.length === 0) {
253 return undefined;
254 }
255 return {
256 addOrUpdateTaints: addOrUpdateTaints.length > 0 ? addOrUpdateTaints : undefined,
257 removeTaints: removeTaints.length > 0 ? removeTaints : undefined,
258 };
259 };
260
261 const scalingConfigChanged = (
262 observed: eks.NodegroupScalingConfig | undefined,
263 desired: eks.NodegroupScalingConfig | undefined,
264 ) => {
265 if (!desired) return false;
266 return (
267 observed?.minSize !== desired.minSize ||
268 observed?.maxSize !== desired.maxSize ||
269 observed?.desiredSize !== desired.desiredSize
270 );
271 };
272
273 const updateConfigChanged = (
274 observed: eks.NodegroupUpdateConfig | undefined,
275 desired: eks.NodegroupUpdateConfig | undefined,
276 ) => {
277 if (!desired) return false;
278 return (
279 observed?.maxUnavailable !== desired.maxUnavailable ||
280 observed?.maxUnavailablePercentage !== desired.maxUnavailablePercentage ||
281 observed?.updateStrategy !== desired.updateStrategy
282 );
283 };
284
285 const mapNodegroup = (
286 nodegroup: eks.Nodegroup,
287 tags: Record<string, string>,
288 ): Nodegroup["Attributes"] => ({
289 nodegroupName: nodegroup.nodegroupName!,
290 nodegroupArn: nodegroup.nodegroupArn!,
291 clusterName: nodegroup.clusterName!,
292 status: nodegroup.status ?? "CREATING",
293 capacityType: nodegroup.capacityType,
294 scalingConfig: nodegroup.scalingConfig,
295 instanceTypes: nodegroup.instanceTypes ?? [],
296 subnets: nodegroup.subnets ?? [],
297 amiType: nodegroup.amiType,
298 nodeRole: nodegroup.nodeRole!,
299 labels: normalizeLabels(nodegroup.labels),
300 taints: nodegroup.taints ?? [],
301 diskSize: nodegroup.diskSize,
302 version: nodegroup.version,
303 releaseVersion: nodegroup.releaseVersion,
304 updateConfig: nodegroup.updateConfig,
305 tags,
306 });
307
308 export const NodegroupProvider = () =>
309 Provider.effect(
310 Nodegroup,
311 Effect.gen(function* () {
312 const toNodegroupName = (id: string, props: { nodegroupName?: string } = {}) =>
313 props.nodegroupName
314 ? Effect.succeed(props.nodegroupName)
315 : createPhysicalName({ id, maxLength: 63 });
316
317 const toClientRequestToken = (id: string, action: string) =>
318 createPhysicalName({
319 id: `${id}-${action}`,
320 maxLength: 64,
321 delimiter: "-",
322 });
323
324 const readNodegroup = Effect.fn(function* ({
325 clusterName,
326 nodegroupName,
327 }: {
328 clusterName: string;
329 nodegroupName: string;
330 }) {
331 const described = yield* eks
332 .describeNodegroup({ clusterName, nodegroupName })
333 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
334 const nodegroup = described?.nodegroup;
335 if (
336 !nodegroup?.nodegroupArn ||
337 !nodegroup.nodegroupName ||
338 !nodegroup.clusterName ||
339 !nodegroup.nodeRole
340 ) {
341 return undefined;
342 }
343 return mapNodegroup(nodegroup, normalizeTags(nodegroup.tags));
344 });
345
346 const waitForNodegroupActive = (clusterName: string, nodegroupName: string) =>
347 readNodegroup({ clusterName, nodegroupName }).pipe(
348 Effect.flatMap(
349 Effect.fnUntraced(function* (state) {
350 if (!state) {
351 return yield* new NodegroupNotReady({ status: undefined });
352 }
353 if (state.status === "ACTIVE") {
354 return state;
355 }
356 if (state.status === "CREATE_FAILED" || state.status === "DELETE_FAILED") {
357 return yield* new NodegroupOperationFailed({
358 message: `EKS node group '${nodegroupName}' entered ${state.status}`,
359 });
360 }
361 return yield* new NodegroupNotReady({ status: state.status });
362 }),
363 ),
364 Effect.retry({
365 while: (error) => error instanceof NodegroupNotReady,
366 schedule: waitSchedule,
367 }),
368 );
369
370 const waitForNodegroupDeleted = (clusterName: string, nodegroupName: string) =>
371 readNodegroup({ clusterName, nodegroupName }).pipe(
372 Effect.flatMap((state) =>
373 state ? Effect.fail(new NodegroupStillExists()) : Effect.undefined,
374 ),
375 Effect.retry({
376 while: (error) => error instanceof NodegroupStillExists,
377 schedule: waitSchedule,
378 }),
379 );
380
381 const waitForUpdate = (clusterName: string, nodegroupName: string, updateId: string) =>
382 eks.describeUpdate({ name: clusterName, nodegroupName, updateId }).pipe(
383 Effect.flatMap(
384 Effect.fnUntraced(function* ({ update }) {
385 if (update?.status === "Successful") {
386 return update;
387 }
388 if (update?.status === "Failed" || update?.status === "Cancelled") {
389 return yield* new NodegroupOperationFailed({
390 message: `EKS node group update '${updateId}' failed with status '${update?.status}'`,
391 });
392 }
393 return yield* new NodegroupUpdateNotComplete({ status: update?.status });
394 }),
395 ),
396 Effect.retry({
397 while: (error) => error instanceof NodegroupUpdateNotComplete,
398 schedule: waitSchedule,
399 }),
400 );
401
402 return {
403 stables: ["nodegroupArn", "nodegroupName", "clusterName"],
404 // Enumerate every node group across the account/region. `listNodegroups`
405 // is cluster-scoped, so first enumerate all clusters, list each
406 // cluster's node groups, then hydrate each via `describeNodegroup`.
407 list: () =>
408 Effect.gen(function* () {
409 const clusterNames = yield* eks.listClusters.pages({}).pipe(
410 Stream.runCollect,
411 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.clusters ?? [])),
412 );
413
414 const perCluster = yield* Effect.forEach(
415 clusterNames,
416 (clusterName) =>
417 eks.listNodegroups.pages({ clusterName }).pipe(
418 Stream.runCollect,
419 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.nodegroups ?? [])),
420 Effect.flatMap((names) =>
421 Effect.forEach(
422 names,
423 (nodegroupName) => readNodegroup({ clusterName, nodegroupName }),
424 { concurrency: 5 },
425 ),
426 ),
427 ),
428 { concurrency: 5 },
429 );
430
431 return perCluster
432 .flat()
433 .filter((state): state is Nodegroup["Attributes"] => state !== undefined);
434 }),
435 diff: Effect.fn(function* ({ id, olds = {} as NodegroupProps, news }) {
436 if (!isResolved(news)) return;
437 if ((yield* toNodegroupName(id, olds)) !== (yield* toNodegroupName(id, news ?? {}))) {
438 return { action: "replace" } as const;
439 }
440 if (olds.clusterName !== news.clusterName) {
441 return { action: "replace" } as const;
442 }
443 if (olds.nodeRole !== news.nodeRole) {
444 return { action: "replace" } as const;
445 }
446 if (JSON.stringify(olds.subnets ?? []) !== JSON.stringify(news.subnets ?? [])) {
447 return { action: "replace" } as const;
448 }
449 if (
450 JSON.stringify(olds.instanceTypes ?? []) !== JSON.stringify(news.instanceTypes ?? [])
451 ) {
452 return { action: "replace" } as const;
453 }
454 if ((olds.amiType ?? undefined) !== (news.amiType ?? undefined)) {
455 return { action: "replace" } as const;
456 }
457 if ((olds.diskSize ?? undefined) !== (news.diskSize ?? undefined)) {
458 return { action: "replace" } as const;
459 }
460 if ((olds.capacityType ?? "ON_DEMAND") !== (news.capacityType ?? "ON_DEMAND")) {
461 return { action: "replace" } as const;
462 }
463 if (
464 JSON.stringify(olds.remoteAccess ?? undefined) !==
465 JSON.stringify(news.remoteAccess ?? undefined)
466 ) {
467 return { action: "replace" } as const;
468 }
469 }),
470 read: Effect.fn(function* ({ id, olds, output }) {
471 const clusterName = (output?.clusterName ?? (olds?.clusterName as string | undefined)) as
472 | string
473 | undefined;
474 if (!clusterName) return undefined;
475 const nodegroupName = output?.nodegroupName ?? (yield* toNodegroupName(id, olds ?? {}));
476 const state = yield* readNodegroup({ clusterName, nodegroupName });
477 if (!state) return undefined;
478 return (yield* hasAlchemyTags(id, state.tags)) ? state : Unowned(state);
479 }),
480 reconcile: Effect.fn(function* ({ id, news, session }) {
481 const clusterName = news.clusterName as string;
482 const nodegroupName = yield* toNodegroupName(id, news);
483 const desiredTags = {
484 ...(yield* createInternalTags(id)),
485 ...news.tags,
486 };
487
488 // Observe — cloud state is authoritative.
489 let state = yield* readNodegroup({ clusterName, nodegroupName });
490
491 // Ensure — create if missing; tolerate a create race, then wait for
492 // the node group to reach ACTIVE before syncing.
493 if (!state) {
494 yield* eks
495 .createNodegroup({
496 clusterName,
497 nodegroupName,
498 nodeRole: news.nodeRole as string,
499 subnets: news.subnets as string[],
500 instanceTypes: news.instanceTypes,
501 scalingConfig: news.scalingConfig,
502 amiType: news.amiType,
503 capacityType: news.capacityType,
504 diskSize: news.diskSize,
505 labels: news.labels,
506 taints: news.taints,
507 updateConfig: news.updateConfig,
508 remoteAccess: news.remoteAccess,
509 version: news.version,
510 releaseVersion: news.releaseVersion,
511 tags: desiredTags,
512 clientRequestToken: yield* toClientRequestToken(id, "create"),
513 })
514 .pipe(Effect.catchTag("ResourceInUseException", () => Effect.void));
515
516 yield* session.note(`Creating EKS node group ${nodegroupName}...`);
517 state = yield* waitForNodegroupActive(clusterName, nodegroupName);
518 }
519
520 // Sync mutable config (scaling, updateConfig, labels, taints) in a
521 // single updateNodegroupConfig call when anything changed.
522 const desiredLabels = normalizeLabels(news.labels);
523 const labelsDelta = diffLabels(state.labels, desiredLabels);
524 const taintsDelta = diffTaints(state.taints, news.taints ?? []);
525 const scalingChanged = scalingConfigChanged(state.scalingConfig, news.scalingConfig);
526 const updateCfgChanged = updateConfigChanged(state.updateConfig, news.updateConfig);
527 if (labelsDelta || taintsDelta || scalingChanged || updateCfgChanged) {
528 const configUpdate = yield* eks.updateNodegroupConfig({
529 clusterName,
530 nodegroupName,
531 scalingConfig: scalingChanged ? news.scalingConfig : undefined,
532 updateConfig: updateCfgChanged ? news.updateConfig : undefined,
533 labels: labelsDelta,
534 taints: taintsDelta,
535 clientRequestToken: yield* toClientRequestToken(id, "config"),
536 });
537 if (configUpdate.update?.id) {
538 yield* session.note(`Updating EKS node group config ${nodegroupName}...`);
539 yield* waitForUpdate(clusterName, nodegroupName, configUpdate.update.id);
540 state = (yield* waitForNodegroupActive(clusterName, nodegroupName)) ?? state;
541 }
542 }
543
544 // Sync version / release version via updateNodegroupVersion.
545 if (
546 (news.version && state.version !== news.version) ||
547 (news.releaseVersion && state.releaseVersion !== news.releaseVersion)
548 ) {
549 const versionUpdate = yield* eks.updateNodegroupVersion({
550 clusterName,
551 nodegroupName,
552 version: news.version,
553 releaseVersion: news.releaseVersion,
554 clientRequestToken: yield* toClientRequestToken(id, "version"),
555 });
556 if (versionUpdate.update?.id) {
557 yield* session.note(`Updating EKS node group version ${nodegroupName}...`);
558 yield* waitForUpdate(clusterName, nodegroupName, versionUpdate.update.id);
559 state = (yield* waitForNodegroupActive(clusterName, nodegroupName)) ?? state;
560 }
561 }
562
563 // Sync tags — diff observed cloud tags against desired.
564 const { removed, upsert } = diffTags(state.tags, desiredTags);
565 if (upsert.length > 0) {
566 yield* eks.tagResource({
567 resourceArn: state.nodegroupArn,
568 tags: Object.fromEntries(upsert.map((tag) => [tag.Key, tag.Value] as const)),
569 });
570 }
571 if (removed.length > 0) {
572 yield* eks.untagResource({
573 resourceArn: state.nodegroupArn,
574 tagKeys: removed,
575 });
576 }
577
578 yield* session.note(state.nodegroupArn);
579
580 const final = yield* readNodegroup({ clusterName, nodegroupName });
581 if (!final) {
582 return yield* new NodegroupUnexpectedResponse({
583 message: `EKS node group '${nodegroupName}' could not be read after reconcile`,
584 });
585 }
586 return final;
587 }),
588 delete: Effect.fn(function* ({ output }) {
589 yield* eks
590 .deleteNodegroup({
591 clusterName: output.clusterName,
592 nodegroupName: output.nodegroupName,
593 })
594 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
595 yield* waitForNodegroupDeleted(output.clusterName, output.nodegroupName);
596 }),
597 };
598 }),
599 );