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

main
ts 118 lines 4.56 KB
Raw
1 import type { Input } from "alchemy/Input";
2 import * as Namespace from "alchemy/Namespace";
3 /**
4 * Internal composite: a Kubernetes service account (bound onto the cluster)
5 * wired to EKS Pod Identity through an IAM role + `PodIdentityAssociation`.
6 * Un-exported — the workload platforms (`Kubernetes.Deployment`,
7 * `Kubernetes.Job`) provision the same triple through the aws-eks
8 * adapter; this helper remains for composition-style stacks that assemble
9 * the pieces from resources.
10 */
11 import * as Effect from "effect/Effect";
12 import type { PolicyDocument } from "../../IAM/Policy.ts";
13 import { Role, type Role as RoleResource } from "../../IAM/Role.ts";
14 import type { Cluster } from "../Cluster.ts";
15 import {
16 PodIdentityAssociation,
17 type PodIdentityAssociation as PodIdentityAssociationResource,
18 } from "../PodIdentityAssociation.ts";
19 import { clusterServiceAccount, namespaceNameOf, type ClusterObjectRef } from "./ClusterObject.ts";
20
21 export interface PodIdentityServiceAccountProps {
22 /** Target EKS cluster. */
23 cluster: Cluster;
24 /** Namespace name or namespace helper result. */
25 namespace: string | { name: string } | ClusterObjectRef;
26 /** Optional explicit service account name. Defaults to the logical id. */
27 serviceAccountName?: string;
28 /** Existing IAM role ARN to use for pod identity. */
29 roleArn?: string;
30 /** Optional role name when Alchemy creates the IAM role. */
31 roleName?: string;
32 /** Managed policy ARNs to attach when creating the IAM role. */
33 managedPolicyArns?: string[];
34 /** Inline policies to attach when creating the IAM role. */
35 inlinePolicies?: Record<string, PolicyDocument>;
36 /** Optional role description when Alchemy creates the IAM role. */
37 description?: string;
38 /** Disable session tags for the pod identity association. */
39 disableSessionTags?: boolean;
40 /** Optional target role ARN for chained role assumption. */
41 targetRoleArn?: string;
42 /** Optional inline session policy JSON. */
43 policy?: string;
44 /** Labels applied to the Kubernetes service account. */
45 labels?: Record<string, string>;
46 /** Annotations applied to the Kubernetes service account. */
47 annotations?: Record<string, string>;
48 /** Tags applied to AWS resources. */
49 tags?: Record<string, string>;
50 }
51
52 export interface PodIdentityServiceAccountResources {
53 /** Reference to the created Kubernetes service account. */
54 serviceAccount: ClusterObjectRef;
55 /** The Pod Identity association binding the role to the service account. */
56 podIdentityAssociation: PodIdentityAssociationResource;
57 /** The generated IAM role, or `undefined` when `roleArn` was supplied. */
58 role: RoleResource | undefined;
59 /** The ARN of the IAM role pods assume (generated or supplied). */
60 roleArn: Input<string>;
61 }
62
63 export const PodIdentityServiceAccount = (id: string, props: PodIdentityServiceAccountProps) =>
64 Namespace.push(
65 id,
66 Effect.gen(function* () {
67 const serviceAccountName = props.serviceAccountName ?? id;
68
69 const role = props.roleArn
70 ? undefined
71 : yield* Role("Role", {
72 roleName: props.roleName,
73 assumeRolePolicyDocument: {
74 Version: "2012-10-17",
75 Statement: [
76 {
77 Effect: "Allow",
78 Principal: {
79 Service: "pods.eks.amazonaws.com",
80 },
81 Action: ["sts:AssumeRole", "sts:TagSession"],
82 },
83 ],
84 },
85 description:
86 props.description ?? `Pod identity role for service account ${serviceAccountName}.`,
87 managedPolicyArns: props.managedPolicyArns,
88 inlinePolicies: props.inlinePolicies,
89 tags: props.tags,
90 });
91
92 const serviceAccount = yield* clusterServiceAccount("ServiceAccount", {
93 cluster: props.cluster,
94 namespace: props.namespace,
95 name: serviceAccountName,
96 labels: props.labels,
97 annotations: props.annotations,
98 });
99
100 const podIdentityAssociation = yield* PodIdentityAssociation("PodIdentityAssociation", {
101 clusterName: props.cluster.clusterName,
102 namespace: namespaceNameOf(props.namespace),
103 serviceAccount: serviceAccount.name,
104 roleArn: props.roleArn ?? role!.roleArn,
105 disableSessionTags: props.disableSessionTags,
106 targetRoleArn: props.targetRoleArn,
107 policy: props.policy,
108 tags: props.tags,
109 });
110
111 return {
112 serviceAccount,
113 podIdentityAssociation,
114 role,
115 roleArn: props.roleArn ?? role!.roleArn,
116 } satisfies PodIdentityServiceAccountResources;
117 }),
118 );