The best way to control Claude Code, Codex and more from any computer or phone

main
ts 99 lines 2.84 KB
Raw
1 import {
2 kubernetesObjectKey,
3 toKubernetesObjectRef,
4 type KubernetesObjectDefinition,
5 type KubernetesObjectMetadata,
6 } from "@alchemy.run/kubernetes/Objects";
7 /**
8 * Internal helpers for binding Kubernetes objects onto an `AWS.EKS.Cluster`
9 * through its `KubernetesObjectBinding` contract. The cluster's reconciler
10 * server-side-applies every bound object after the control plane is ACTIVE.
11 * Not exported from the EKS index — the public path for raw manifests is
12 * `Kubernetes.Manifest`.
13 */
14 import * as Effect from "effect/Effect";
15 import type { Cluster } from "../Cluster.ts";
16
17 export interface ClusterObjectProps {
18 /** Target EKS cluster that will own this Kubernetes object. */
19 cluster: Cluster;
20 /** Kubernetes API version. */
21 apiVersion: string;
22 /** Kubernetes kind. */
23 kind: string;
24 /** Object metadata. `name` defaults to the logical id. */
25 metadata?: Omit<KubernetesObjectMetadata, "name"> & {
26 name?: string;
27 };
28 /** Extra top-level fields merged into the final Kubernetes object. */
29 body?: Record<string, unknown>;
30 }
31
32 export interface ClusterObjectRef {
33 cluster: Cluster;
34 apiVersion: string;
35 kind: string;
36 name: string;
37 namespace: string | undefined;
38 key: string;
39 object: KubernetesObjectDefinition;
40 }
41
42 export const kubernetesBindingSid = (object: KubernetesObjectDefinition) =>
43 `Kubernetes.Object(${kubernetesObjectKey(toKubernetesObjectRef(object))})`;
44
45 export const ClusterObject = Effect.fn(function* (id: string, props: ClusterObjectProps) {
46 const object = {
47 apiVersion: props.apiVersion,
48 kind: props.kind,
49 metadata: {
50 name: props.metadata?.name ?? id,
51 namespace: props.metadata?.namespace,
52 labels: props.metadata?.labels,
53 annotations: props.metadata?.annotations,
54 },
55 ...props.body,
56 } satisfies KubernetesObjectDefinition;
57
58 yield* props.cluster.bind(kubernetesBindingSid(object), {
59 type: "kubernetes-object",
60 object,
61 });
62
63 const ref = toKubernetesObjectRef(object);
64
65 return {
66 cluster: props.cluster,
67 apiVersion: ref.apiVersion,
68 kind: ref.kind,
69 name: ref.name,
70 namespace: ref.namespace,
71 key: kubernetesObjectKey(ref),
72 object,
73 } satisfies ClusterObjectRef;
74 });
75
76 export const namespaceNameOf = (namespace: string | { name: string } | ClusterObjectRef): string =>
77 typeof namespace === "string" ? namespace : namespace.name;
78
79 export const clusterServiceAccount = (
80 id: string,
81 props: {
82 cluster: Cluster;
83 namespace: string | { name: string } | ClusterObjectRef;
84 name?: string;
85 labels?: Record<string, string>;
86 annotations?: Record<string, string>;
87 },
88 ) =>
89 ClusterObject(id, {
90 cluster: props.cluster,
91 apiVersion: "v1",
92 kind: "ServiceAccount",
93 metadata: {
94 name: props.name,
95 namespace: namespaceNameOf(props.namespace),
96 labels: props.labels,
97 annotations: props.annotations,
98 },
99 });