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