DodoApp: Support postgresql config

Change-Id: I491553c709385b99d0bc1326dd967bd73c4fec6a
diff --git a/core/installer/app_configs/app_base.cue b/core/installer/app_configs/app_base.cue
index 9860c6c..4954bef 100644
--- a/core/installer/app_configs/app_base.cue
+++ b/core/installer/app_configs/app_base.cue
@@ -118,6 +118,11 @@
             "\(name)": #Image & image
         }
     }
+    for _, value in _postgresql {
+        for name, image in value.out.images {
+            "\(name)": #Image & image
+        }
+    }
 }
 
 charts: {}
@@ -134,6 +139,13 @@
             }
         }
     }
+    for _, value in _postgresql {
+        for name, chart in value.out.charts {
+            "\(name)": #Chart & chart & {
+                name: name
+            }
+        }
+    }
 }
 charts: {
 	volume: {
@@ -144,6 +156,104 @@
 	}
 }
 
+#PostgreSQL: {
+	name: string
+	version: "15.3"
+	initSQL: string | *""
+	size: string | *"1Gi"
+
+	_size: size
+	_volumeClaimName: "postgresql"
+
+	out: {
+		images: {
+			postgres: #Image & {
+				repository: "library"
+				name: "postgres"
+				tag: version
+				pullPolicy: "IfNotPresent"
+			}
+		}
+		charts: {
+			volume: #Chart & {
+				kind: "GitRepository"
+				address: "https://code.v1.dodo.cloud/helm-charts"
+				branch: "main"
+				path: "charts/volumes"
+			}
+			postgres: #Chart & {
+				kind: "GitRepository"
+				address: "https://code.v1.dodo.cloud/helm-charts"
+				branch: "main"
+				path: "charts/postgresql"
+			}
+		}
+		charts: {
+			for key, value in charts {
+				"\(key)": #Chart & value & {
+					name: key
+				}
+			}
+		}
+		helm: {
+			"volume-name": {
+				chart: charts.volume
+				values: {
+					name: _volumeClaimName
+					size: _size
+				}
+			}
+			postgres: {
+				chart: charts.postgres
+				values: {
+					fullnameOverride: "postgres"
+					image: {
+						registry: images.postgres.registry
+						repository: images.postgres.imageName
+						tag: images.postgres.tag
+						pullPolicy: images.postgres.pullPolicy
+					}
+					auth: {
+						username: "postgres"
+						password: "postgres"
+						database: "postgres"
+					}
+					service: {
+						type: "ClusterIP"
+						port: 5432
+					}
+					primary: {
+						persistence: existingClaim: _volumeClaimName
+						if initSQL != "" {
+							initdb: scripts: "init.sql": initSQL
+						}
+						securityContext: {
+							enabled: true
+							fsGroup: 0
+						}
+						containerSecurityContext: {
+							enabled: true
+							runAsUser: 0
+						}
+					}
+					volumePermissions: securityContext: runAsUser: 0
+				}
+			}
+		}
+	}
+}
+
+_ingressValidate: {}
+
+postgresql: {}
+_postgresql: {
+	for key, value in postgresql {
+		"\(key)": #PostgreSQL & value & {
+			name: key
+		}
+	}
+}
+
 localCharts: {
 	for key, _ in charts {
 		"\(key)": {
@@ -184,6 +294,13 @@
 			}
 		}
 	}
+	for key, value in _postgresql {
+		for post, postValue in value.out.helm {
+			"\(key)-\(post)": #Helm & postValue & {
+				name: "\(key)-\(post)"
+			}
+		}
+	}
 }
 
 #HelmRelease: {
diff --git a/core/installer/app_configs/app_global_env.cue b/core/installer/app_configs/app_global_env.cue
index 26e8a99..c53df4a 100644
--- a/core/installer/app_configs/app_global_env.cue
+++ b/core/installer/app_configs/app_global_env.cue
@@ -128,7 +128,6 @@
 }
 
 ingress: {}
-
 _ingressValidate: {
 	for key, value in ingress {
 		"\(key)": #Ingress & value
diff --git a/core/installer/app_configs/app_global_infra.cue b/core/installer/app_configs/app_global_infra.cue
index 937d490..b3069d0 100644
--- a/core/installer/app_configs/app_global_infra.cue
+++ b/core/installer/app_configs/app_global_infra.cue
@@ -19,7 +19,3 @@
 }
 
 networks: #Networks
-
-ingress: {}
-_ingressValidate: {}
-
diff --git a/core/installer/app_configs/dodo_app.cue b/core/installer/app_configs/dodo_app.cue
index 2a605e0..8a67645 100644
--- a/core/installer/app_configs/dodo_app.cue
+++ b/core/installer/app_configs/dodo_app.cue
@@ -24,6 +24,10 @@
 	...
 }
 
+#PostgreSQLs: {
+	...
+}
+
 app: {
 	volumes: {
 		for key, value in volumes {
@@ -32,6 +36,13 @@
 			}
 		}
 	}
+	postgresql: {
+		for key, value in postgresql {
+			"\(key)": #PostgreSQL & value & {
+				name: key
+			}
+		}
+	}
 }
 
 #Command: {
@@ -50,6 +61,7 @@
 	run: string | *"main.go"
 	ingress: #AppIngress
 	volumes: #Volumes
+	postgresql: #PostgreSQLs
 	port: int | *8080
 	rootDir: _appDir
 
@@ -66,6 +78,18 @@
 			for k, v in volumes {
 				"DODO_VOLUME_\(strings.ToUpper(k))=/dodo-volume/\(v.name)"
 			}
+			for k, v in postgresql {
+				"DODO_POSTGRESQL_\(strings.ToUpper(k))_ADDRESS=\(v.name).\(release.namespace).svc.cluster.local"
+			}
+			for k, v in postgresql {
+				"DODO_POSTGRESQL_\(strings.ToUpper(k))_USERNAME=postgres"
+			}
+			for k, v in postgresql {
+				"DODO_POSTGRESQL_\(strings.ToUpper(k))_PASSWORD=postgres"
+			}
+			for k, v in postgresql {
+				"DODO_POSTGRESQL_\(strings.ToUpper(k))_DATABASE=postgres"
+			}
 	    ]
 	}]
 }
@@ -88,6 +112,7 @@
 	type: _hugoLatest
 	ingress: #AppIngress
 	volumes: {}
+	postgresql: {}
 	port: int | *8080
 	rootDir: _appDir
 
@@ -115,6 +140,7 @@
 	type: "php:8.2-apache"
 	ingress: #AppIngress
 	volumes: {}
+	postgresql: {}
 	port: int | *80
 	rootDir: "/var/www/html"
 
@@ -168,6 +194,7 @@
 }
 
 volumes: app.volumes
+postgresql: app.postgresql
 
 helm: {
 	app: {
diff --git a/core/installer/app_configs/testapp.cue b/core/installer/app_configs/testapp.cue
index 8201ff6..7ab6829 100644
--- a/core/installer/app_configs/testapp.cue
+++ b/core/installer/app_configs/testapp.cue
@@ -6,7 +6,16 @@
 		subdomain: "testapp"
 		auth: enabled: false
 	}
-	volumes: data: size: "1Gi"
+	volumes: {
+		data: {
+			size: "1Gi"
+		}
+	}
+	postgresql: {
+		foo: {
+			size: "2Gi"
+		}
+	}
 }
 
 // do create app --type=go[1.22.0] [--run-cmd=(*default main.go)]