blob: fcea7c38533afa77f282be871e48e4c5ebedb490 [file] [log] [blame]
gio5f2f1002025-03-20 18:38:48 +04001import { AppNode, Env, GatewayHttpsNode, Message, MessageType, NodeType, ServiceType, VolumeType } from "./state";
2
3export type AuthDisabled = {
4 enabled: false;
5};
6
7export type AuthEnabled = {
8 enabled: true;
9 groups: string[];
10 noAuthPathPatterns: string[];
11};
12
13export type Auth = AuthDisabled | AuthEnabled;
14
15export type Ingress = {
16 network: string;
17 subdomain: string;
18 port: { name: string; } | { value: string; };
19 auth: Auth;
20};
21
22export type Domain = {
23 network: string;
24 subdomain: string;
25};
26
27export type PortValue = {
28 name: string;
29} | {
30 value: number;
31};
32
33export type PortDomain = Domain & {
34 port: PortValue;
35}
36
37export type Service = {
38 type: ServiceType;
39 name: string;
40 source: {
41 repository: string;
42 branch: string;
43 rootDir: string;
44 };
45 ports?: {
46 name: string;
47 value: number;
48 protocol: "TCP" | "UDP";
49 }[];
50 env?: {
51 name: string;
52 alias?: string;
53 }[]
gio218e8132025-04-22 17:11:58 +000054 ingress?: Ingress[];
gio5f2f1002025-03-20 18:38:48 +040055 expose?: PortDomain[];
56 volume?: string[];
57};
58
59export type Volume = {
60 name: string;
61 accessMode: VolumeType;
62 size: string;
63};
64
65export type PostgreSQL = {
66 name: string;
67 size: string;
68 expose?: Domain[];
69};
70
71export type MongoDB = {
72 name: string;
73 size: string;
74 expose?: Domain[];
75};
76
77export type Config = {
78 service?: Service[];
79 volume?: Volume[];
80 postgresql?: PostgreSQL[];
81 mongodb?: MongoDB[];
82};
83
84export function generateDodoConfig(nodes: AppNode[], env: Env): Config | null {
85 try {
86 const networkMap = new Map(env.networks.map((n) => [n.domain, n.name]));
87 const ingressNodes = nodes.filter((n) => n.type === "gateway-https").filter((n) => n.data.https !== undefined);
88 const tcpNodes = nodes.filter((n) => n.type === "gateway-tcp").filter((n) => n.data.exposed !== undefined);
89 const findExpose = (n: AppNode): PortDomain[] => {
90 return n.data.ports.map((p) => [n.id, p.id, p.name]).flatMap((sp) => {
91 return tcpNodes.flatMap((i) => (i.data.exposed || []).filter((t) => t.serviceId === sp[0] && t.portId === sp[1]).map(() => ({
92 network: networkMap.get(i.data.network!)!,
93 subdomain: i.data.subdomain!,
94 port: { name: sp[2] },
95 })));
96 });
97 };
98 return {
99 service: nodes.filter((n) => n.type === "app").map((n): Service => {
100 return {
101 type: n.data.type,
102 name: n.data.label,
103 source: {
104 repository: nodes.filter((i) => i.type === "github").find((i) => i.id === n.data.repository.id)!.data.address,
105 branch: n.data.repository.branch,
106 rootDir: n.data.repository.rootDir,
107 },
108 ports: (n.data.ports || []).map((p) => ({
109 name: p.name,
110 value: p.value,
111 protocol: "TCP", // TODO(gio)
112 })),
113 env: (n.data.envVars || []).filter((e) => "name" in e).map((e) => ({
114 name: e.name,
115 alias: "alias" in e ? e.alias : undefined,
116 })),
gio218e8132025-04-22 17:11:58 +0000117 ingress: ingressNodes.filter((i) => i.data.https!.serviceId === n.id).map((i: GatewayHttpsNode): Ingress => ({
118 network: networkMap.get(i.data.network!)!,
119 subdomain: i.data.subdomain!,
120 port: {
121 name: n.data.ports.find((p) => p.id === i.data.https!.portId)!.name,
122 },
123 auth: { enabled: false },
124 })),
gio5f2f1002025-03-20 18:38:48 +0400125 expose: findExpose(n),
126 };
127 }),
128 volume: nodes.filter((n) => n.type === "volume").map((n): Volume => ({
129 name: n.data.label,
130 accessMode: n.data.type,
131 size: n.data.size,
132 })),
133 postgresql: nodes.filter((n) => n.type === "postgresql").map((n): PostgreSQL => ({
134 name: n.data.label,
135 size: "1Gi", // TODO(gio)
136 expose: findExpose(n).map((e) => ({ network: e.network, subdomain: e.subdomain })),
137 })),
138 mongodb: nodes.filter((n) => n.type === "mongodb").map((n): MongoDB => ({
139 name: n.data.label,
140 size: "1Gi", // TODO(gio)
141 expose: findExpose(n).map((e) => ({ network: e.network, subdomain: e.subdomain })),
142 })),
143 };
144 } catch (e) {
145 console.log(e);
146 return null;
147 }
148}
149
150export interface Validator {
151 (nodes: AppNode[]): Message[];
152}
153
154function CombineValidators(...v: Validator[]): Validator {
155 return (n) => v.flatMap((v) => v(n));
156}
157
158function MessageTypeToNumber(t: MessageType) {
159 switch (t) {
160 case "FATAL": return 0;
161 case "WARNING": return 1;
162 case "INFO": return 2;
163 }
164}
165
166function NodeTypeToNumber(t?: NodeType) {
167 switch (t) {
168 case "github": return 0;
169 case "app": return 1;
170 case "volume": return 2;
171 case "postgresql": return 3;
172 case "mongodb": return 4;
173 case "gateway-https": return 5;
174 case undefined: return 100;
175 }
176}
177
178function SortingValidator(v: Validator): Validator {
179 return (n) => {
180 const nt = new Map(n.map((n) => [n.id, NodeTypeToNumber(n.type)]))
181 return v(n).sort((a, b) => {
182 const at = MessageTypeToNumber(a.type);
183 const bt = MessageTypeToNumber(b.type);
184 if (a.nodeId === undefined && b.nodeId === undefined) {
185 if (at !== bt) {
186 return at - bt;
187 }
188 return a.id.localeCompare(b.id);
189 }
190 if (a.nodeId === undefined) {
191 return -1;
192 }
193 if (b.nodeId === undefined) {
194 return 1;
195 }
196 if (a.nodeId === b.nodeId) {
197 if (at !== bt) {
198 return at - bt;
199 }
200 return a.id.localeCompare(b.id);
201 }
202 const ant = nt.get(a.id)!;
203 const bnt = nt.get(b.id)!;
204 if (ant !== bnt) {
205 return ant - bnt;
206 }
207 return a.id.localeCompare(b.id);
208 });
209 };
210}
211
212export function CreateValidators(): Validator {
213 return SortingValidator(
214 CombineValidators(
215 EmptyValidator,
216 GitRepositoryValidator,
217 ServiceValidator,
218 GatewayHTTPSValidator,
219 GatewayTCPValidator,
220 )
221 );
222}
223
224function EmptyValidator(nodes: AppNode[]): Message[] {
225 if (nodes.length > 0) {
226 return [];
227 }
228 return [{
229 id: "no-nodes",
230 type: "FATAL",
231 message: "Start by importing application source code",
232 onHighlight: (store) => store.setHighlightCategory("repository", true),
233 onLooseHighlight: (store) => store.setHighlightCategory("repository", false),
234 }];
235}
236
237function GitRepositoryValidator(nodes: AppNode[]): Message[] {
238 const git = nodes.filter((n) => n.type === "github");
239 const noAddress: Message[] = git.filter((n) => n.data == null || n.data.address == null || n.data.address === "").map((n) => ({
240 id: `${n.id}-no-address`,
241 type: "FATAL",
242 nodeId: n.id,
243 message: "Configure repository address",
244 onHighlight: (store) => store.updateNode(n.id, { selected: true }),
245 onLooseHighlight: (store) => store.updateNode(n.id, { selected: false }),
246 } satisfies Message));
247 const noApp = git.filter((n) => !nodes.some((i) => i.type === "app" && i.data?.repository?.id === n.id)).map((n) => ({
248 id: `${n.id}-no-app`,
249 type: "WARNING",
250 nodeId: n.id,
251 message: "Connect to service",
252 onHighlight: (store) => store.setHighlightCategory("Services", true),
253 onLooseHighlight: (store) => store.setHighlightCategory("Services", false),
254} satisfies Message));
255 return noAddress.concat(noApp);
256}
257
258function ServiceValidator(nodes: AppNode[]): Message[] {
259 const apps = nodes.filter((n) => n.type === "app");
260 const noName = apps.filter((n) => n.data == null || n.data.label == null || n.data.label === "").map((n): Message => ({
261 id: `${n.id}-no-name`,
262 type: "FATAL",
263 nodeId: n.id,
264 message: "Name the service",
265 onHighlight: (store) => store.updateNode(n.id, { selected: true }),
266 onLooseHighlight: (store) => store.updateNode(n.id, { selected: false }),
267 onClick: (store) => {
268 store.updateNode(n.id, { selected: true });
269 store.updateNodeData<"app">(n.id, {
270 activeField: "name" ,
271 });
272 },
273 }));
274 const noSource = apps.filter((n) => n.data == null || n.data.repository == null || n.data.repository.id === "").map((n): Message => ({
275 id: `${n.id}-no-repo`,
276 type: "FATAL",
277 nodeId: n.id,
278 message: "Connect to source repository",
279 onHighlight: (store) => store.updateNode(n.id, { selected: true }),
280 onLooseHighlight: (store) => store.updateNode(n.id, { selected: false }),
281 }));
282 const noRuntime = apps.filter((n) => n.data == null || n.data.type == null).map((n): Message => ({
283 id: `${n.id}-no-runtime`,
284 type: "FATAL",
285 nodeId: n.id,
286 message: "Choose runtime",
287 onHighlight: (store) => store.updateNode(n.id, { selected: true }),
288 onLooseHighlight: (store) => store.updateNode(n.id, { selected: false }),
289 onClick: (store) => {
290 store.updateNode(n.id, { selected: true });
291 store.updateNodeData<"app">(n.id, {
292 activeField: "type" ,
293 });
294 },
295 }));
296 const noPorts = apps.filter((n) => n.data == null || n.data.ports == null || n.data.ports.length === 0).map((n): Message => ({
297 id: `${n.id}-no-ports`,
298 type: "INFO",
299 nodeId: n.id,
300 message: "Expose ports",
301 onHighlight: (store) => store.updateNode(n.id, { selected: true }),
302 onLooseHighlight: (store) => store.updateNode(n.id, { selected: false }),
303 }));
304 const noIngress = apps.flatMap((n): Message[] => {
305 if (n.data == null) {
306 return [];
307 }
308 return (n.data.ports || []).filter((p) => !nodes.filter((i) => i.type === "gateway-https").some((i) => {
309 if (i.data && i.data.https && i.data.https.serviceId === n.id && i.data.https.portId === p.id) {
310 return true;
311 }
312 return false;
313 })).map((p): Message => ({
314 id: `${n.id}-${p.id}-no-ingress`,
315 type: "WARNING",
316 nodeId: n.id,
317 message: `Connect to ingress: ${p.name} - ${p.value}`,
318 onHighlight: (store) => {
319 store.updateNode(n.id, { selected: true });
320 store.setHighlightCategory("gateways", true);
321 },
322 onLooseHighlight: (store) => {
323 store.updateNode(n.id, { selected: false });
324 store.setHighlightCategory("gateways", false);
325 },
326 }));
327 });
328 const multipleIngress = apps.filter((n) => n.data != null && n.data.ports != null).flatMap((n) => n.data.ports.map((p): Message | undefined => {
329 const ing = nodes.filter((i) => i.type === "gateway-https").filter((i) => i.data && i.data.https && i.data.https.serviceId === n.id && i.data.https.portId === p.id);
330 if (ing.length < 2) {
331 return undefined;
332 }
333 return {
334 id: `${n.id}-${p.id}-multiple-ingress`,
335 type: "FATAL",
336 nodeId: n.id,
337 message: `Can not expose same port using multiple ingresses: ${p.name} - ${p.value}`,
338 onHighlight: (store) => store.updateNode(n.id, { selected: true }),
339 onLooseHighlight: (store) => store.updateNode(n.id, { selected: false }),
340 };
341 })).filter((m) => m !== undefined);
342 return noName.concat(noSource).concat(noRuntime).concat(noPorts).concat(noIngress).concat(multipleIngress);
343}
344
345function GatewayHTTPSValidator(nodes: AppNode[]): Message[] {
346 const ing = nodes.filter((n) => n.type === "gateway-https");
347 const noNetwork: Message[] = ing.filter((n) => n.data == null || n.data.network == null || n.data.network == "" || n.data.subdomain == null || n.data.subdomain == "").map((n): Message => ({
348 id: `${n.id}-no-network`,
349 type: "FATAL",
350 nodeId: n.id,
351 message: "Network and subdomain must be defined",
352 onHighlight: (store) => store.updateNode(n.id, { selected: true }),
353 onLooseHighlight: (store) => store.updateNode(n.id, { selected: false }),
354 }));
355 const notConnected: Message[] = ing.filter((n) => n.data == null || n.data.https == null || n.data.https.serviceId == null || n.data.https.serviceId == "" || n.data.https.portId == null || n.data.https.portId == "").map((n) => ({
356 id: `${n.id}-not-connected`,
357 type: "FATAL",
358 nodeId: n.id,
359 message: "Connect to a service port",
360 onHighlight: (store) => store.updateNode(n.id, { selected: true }),
361 onLooseHighlight: (store) => store.updateNode(n.id, { selected: false }),
362 }));
363 return noNetwork.concat(notConnected);
364}
365
366function GatewayTCPValidator(nodes: AppNode[]): Message[] {
367 const ing = nodes.filter((n) => n.type === "gateway-tcp");
368 const noNetwork: Message[] = ing.filter((n) => n.data == null || n.data.network == null || n.data.network == "" || n.data.subdomain == null || n.data.subdomain == "").map((n): Message => ({
369 id: `${n.id}-no-network`,
370 type: "FATAL",
371 nodeId: n.id,
372 message: "Network and subdomain must be defined",
373 onHighlight: (store) => store.updateNode(n.id, { selected: true }),
374 onLooseHighlight: (store) => store.updateNode(n.id, { selected: false }),
375 }));
376 const notConnected: Message[] = ing.filter((n) => n.data == null || n.data.exposed == null || n.data.exposed.length === 0).map((n) => ({
377 id: `${n.id}-not-connected`,
378 type: "FATAL",
379 nodeId: n.id,
380 message: "Connect to a service port",
381 onHighlight: (store) => store.updateNode(n.id, { selected: true }),
382 onLooseHighlight: (store) => store.updateNode(n.id, { selected: false }),
383 }));
384 return noNetwork.concat(notConnected);
385}