1import * as EKS from "@/AWS/EKS";
2import * as Lambda from "@/AWS/Lambda";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import { HttpServerRequest } from "effect/http/HttpServerRequest";
7import * as HttpServerResponse from "effect/http/HttpServerResponse";
8import path from "pathe";
9
10const main = path.resolve(import.meta.dirname, "handler.ts");
11
12export class EksTestFunction extends Lambda.Function<Lambda.Function>()(
13 "EksTestFunction",
14) {}
15
16/**
17 * Account-level EKS bindings fixture. The cluster-scoped bindings
18 * (`DescribeCluster`, `ListNodegroups`, …) require a live EKS control plane
19 * (~10+ minutes to provision) and are exercised by the env-gated
20 * `ClusterBindings.test.ts` fixture instead. The five account-scoped
21 * discovery/catalog bindings need no cluster at all, so this fixture proves
22 * their init + IAM + runtime wiring ungated.
23 */
24export default EksTestFunction.make(
25 {
26 main,
27 functionUrl: true,
28 timeout: Duration.seconds(30),
29 },
30 Effect.gen(function* () {
31 const listClusters = yield* EKS.ListClusters();
32 const listAccessPolicies = yield* EKS.ListAccessPolicies();
33 const describeClusterVersions = yield* EKS.DescribeClusterVersions();
34 const describeAddonVersions = yield* EKS.DescribeAddonVersions();
35 const describeAddonConfiguration = yield* EKS.DescribeAddonConfiguration();
36
37 const bound = {
38 listClusters,
39 listAccessPolicies,
40 describeClusterVersions,
41 describeAddonVersions,
42 describeAddonConfiguration,
43 };
44
45 return {
46 fetch: Effect.gen(function* () {
47 const request = yield* HttpServerRequest;
48 const url = new URL(request.originalUrl);
49 const pathname = url.pathname;
50
51 if (request.method === "GET" && pathname === "/bindings") {
52 return yield* HttpServerResponse.json({
53 bound: Object.keys(bound),
54 });
55 }
56
57 if (request.method === "GET" && pathname === "/clusters") {
58 const { clusters } = yield* listClusters();
59 return yield* HttpServerResponse.json({
60 count: (clusters ?? []).length,
61 });
62 }
63
64 if (request.method === "GET" && pathname === "/access-policies") {
65 const { accessPolicies } = yield* listAccessPolicies();
66 return yield* HttpServerResponse.json({
67 count: (accessPolicies ?? []).length,
68 names: (accessPolicies ?? []).map((policy) => policy.name),
69 });
70 }
71
72 if (request.method === "GET" && pathname === "/cluster-versions") {
73 const { clusterVersions } = yield* describeClusterVersions({
74 defaultOnly: true,
75 });
76 return yield* HttpServerResponse.json({
77 count: (clusterVersions ?? []).length,
78 defaultVersion: clusterVersions?.[0]?.clusterVersion,
79 });
80 }
81
82 if (request.method === "GET" && pathname === "/addon-versions") {
83 const { addons } = yield* describeAddonVersions({
84 addonName: "vpc-cni",
85 maxResults: 1,
86 });
87 return yield* HttpServerResponse.json({
88 count: (addons ?? []).length,
89 addonName: addons?.[0]?.addonName,
90 });
91 }
92
93 // Chains two catalog bindings: resolve a live vpc-cni version via
94 // DescribeAddonVersions, then fetch that version's configuration
95 // JSON schema via DescribeAddonConfiguration.
96 if (request.method === "GET" && pathname === "/addon-schema") {
97 const { addons } = yield* describeAddonVersions({
98 addonName: "vpc-cni",
99 maxResults: 1,
100 });
101 const addonVersion = addons?.[0]?.addonVersions?.[0]?.addonVersion;
102 if (!addonVersion) {
103 return yield* HttpServerResponse.json(
104 { error: "no vpc-cni addon version found" },
105 { status: 404 },
106 );
107 }
108 const { configurationSchema } = yield* describeAddonConfiguration({
109 addonName: "vpc-cni",
110 addonVersion,
111 });
112 return yield* HttpServerResponse.json({
113 addonVersion,
114 hasSchema:
115 typeof configurationSchema === "string" &&
116 configurationSchema.length > 0,
117 });
118 }
119
120 return yield* HttpServerResponse.json(
121 { error: "Not found", method: request.method, pathname },
122 { status: 404 },
123 );
124 }).pipe(Effect.orDie),
125 };
126 }).pipe(
127 Effect.provide(
128 Layer.mergeAll(
129 EKS.ListClustersHttp,
130 EKS.ListAccessPoliciesHttp,
131 EKS.DescribeClusterVersionsHttp,
132 EKS.DescribeAddonVersionsHttp,
133 EKS.DescribeAddonConfigurationHttp,
134 ),
135 ),
136 ),
137);