From 72a353c1d0dc07c0f232f60bdda83e171ca4238b Mon Sep 17 00:00:00 2001
From: bmaignan <bmaignan@takima.fr>
Date: Wed, 14 Aug 2024 15:25:09 +0200
Subject: [PATCH] feat: add front/api/db

---
 templates/api/api-config.yml              |  8 +++
 templates/api/api-deployment.yml          | 61 +++++++++++++++++++++++
 templates/api/api-ingress.yml             | 25 ++++++++++
 templates/api/api-service.yml             | 13 +++++
 templates/database/pg-config.yml          |  9 ++++
 templates/database/pg-credentials.yml     |  8 +++
 templates/database/pg-deployment.yml      | 58 +++++++++++++++++++++
 templates/database/pg-pvc.yml             | 12 +++++
 templates/database/pg-service.yml         | 13 +++++
 templates/{ => www}/front-config.yaml     |  0
 templates/{ => www}/front-deployment.yaml |  0
 templates/{ => www}/front-ingress.yaml    |  2 +-
 templates/{ => www}/front-service.yaml    |  2 +-
 values.yaml                               | 17 ++++++-
 14 files changed, 224 insertions(+), 4 deletions(-)
 create mode 100644 templates/api/api-config.yml
 create mode 100644 templates/api/api-deployment.yml
 create mode 100644 templates/api/api-ingress.yml
 create mode 100644 templates/api/api-service.yml
 create mode 100644 templates/database/pg-config.yml
 create mode 100644 templates/database/pg-credentials.yml
 create mode 100644 templates/database/pg-deployment.yml
 create mode 100644 templates/database/pg-pvc.yml
 create mode 100644 templates/database/pg-service.yml
 rename templates/{ => www}/front-config.yaml (100%)
 rename templates/{ => www}/front-deployment.yaml (100%)
 rename templates/{ => www}/front-ingress.yaml (95%)
 rename templates/{ => www}/front-service.yaml (92%)

diff --git a/templates/api/api-config.yml b/templates/api/api-config.yml
new file mode 100644
index 0000000..5d37ada
--- /dev/null
+++ b/templates/api/api-config.yml
@@ -0,0 +1,8 @@
+{{- if .Values.api.enabled }}  
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ .Values.name }}-api  
+data:
+  DB_ENDPOINT: {{ .Values.name }}-pg:{{ .Values.pg.port }}
+{{- end }}
\ No newline at end of file
diff --git a/templates/api/api-deployment.yml b/templates/api/api-deployment.yml
new file mode 100644
index 0000000..3f2297e
--- /dev/null
+++ b/templates/api/api-deployment.yml
@@ -0,0 +1,61 @@
+{{- if .Values.api.enabled }}
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: {{ .Values.name }}-api
+  labels:
+    app: api
+spec:
+  replicas: {{ .Values.api.replicaCount }}
+  selector:
+    matchLabels:
+      app: api
+  template:
+    metadata:
+      labels:
+        app: api
+    spec:
+      imagePullSecrets:
+      - name: takima-school-registry
+      containers:
+      - name: api
+        image: {{ .Values.api.image.repository }}:{{ .Values.api.image.tag }}
+        resources:
+          requests:
+            memory: "192Mi"
+            cpu: "100m"
+          limits:
+            memory: "256Mi"
+            cpu: 2
+        livenessProbe:
+          httpGet:
+            path: "/actuator/health/liveness"
+            port: 8080
+        readinessProbe:
+          httpGet:
+            path: "/actuator/health/readiness"
+            port: 8080
+        ports:
+        - containerPort: 8080
+        env:
+        - name: POSTGRES_PASSWORD
+          valueFrom:
+            secretKeyRef:
+              name: {{ .Values.name }}-pg
+              key: password
+        - name: POSTGRES_USER
+          valueFrom:
+            secretKeyRef:
+              name: {{ .Values.name }}-pg
+              key: username
+        - name: POSTGRES_DB
+          valueFrom:
+            configMapKeyRef:
+              name: {{ .Values.name }}-pg
+              key: POSTGRES_DB
+        - name: DB_ENDPOINT
+          valueFrom:
+            configMapKeyRef:
+              name: {{ .Values.name }}-api
+              key: DB_ENDPOINT
+{{- end }}
\ No newline at end of file
diff --git a/templates/api/api-ingress.yml b/templates/api/api-ingress.yml
new file mode 100644
index 0000000..9b4b7d7
--- /dev/null
+++ b/templates/api/api-ingress.yml
@@ -0,0 +1,25 @@
+{{- if .Values.api.enabled }}
+apiVersion: networking.k8s.io/v1
+kind: Ingress
+metadata:
+  name: {{ .Values.name }}-api
+spec:
+  ingressClassName: nginx
+  rules:
+    - host: {{ .Values.api.ingress.host }}
+      http:
+        paths:
+          - backend:
+              service:
+                name: {{ .Values.name }}-api
+                port:
+                  number: 8080
+            path: /
+            pathType: Prefix
+  {{- if .Values.api.ingress.tlsEnabled }}
+  tls:
+    - hosts:
+      - {{ .Values.api.ingress.host }}
+      secretName: app-wildcard
+  {{- end }}
+{{- end }}
\ No newline at end of file
diff --git a/templates/api/api-service.yml b/templates/api/api-service.yml
new file mode 100644
index 0000000..6b48384
--- /dev/null
+++ b/templates/api/api-service.yml
@@ -0,0 +1,13 @@
+{{- if .Values.api.enabled }}
+apiVersion: v1
+kind: Service
+metadata:
+  name: {{ .Values.name }}-api
+spec:
+  selector:
+    app: api
+  ports:
+    - protocol: TCP
+      port: 8080
+      targetPort: 8080
+{{- end }}
\ No newline at end of file
diff --git a/templates/database/pg-config.yml b/templates/database/pg-config.yml
new file mode 100644
index 0000000..e686864
--- /dev/null
+++ b/templates/database/pg-config.yml
@@ -0,0 +1,9 @@
+{{- if .Values.pg.enabled }}  
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ .Values.name }}-pg
+data:
+  POSTGRES_DB: "cdb-db"
+  DB_PATH: "/var/lib/postgresql/data/pgdata"
+{{- end }}
\ No newline at end of file
diff --git a/templates/database/pg-credentials.yml b/templates/database/pg-credentials.yml
new file mode 100644
index 0000000..c30b359
--- /dev/null
+++ b/templates/database/pg-credentials.yml
@@ -0,0 +1,8 @@
+apiVersion: v1
+kind: Secret
+metadata:
+  name: {{ .Values.name }}-pg
+type: Opaque
+data:
+  username: cm9vdA== # root
+  password: cm9vdA== # root
\ No newline at end of file
diff --git a/templates/database/pg-deployment.yml b/templates/database/pg-deployment.yml
new file mode 100644
index 0000000..054e5b9
--- /dev/null
+++ b/templates/database/pg-deployment.yml
@@ -0,0 +1,58 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: {{ .Values.name }}-pg
+  labels:
+    app: pg
+spec:
+  replicas: {{ .Values.pg.replicaCount }}
+  selector:
+    matchLabels:
+      app: pg
+  template:
+    metadata:
+      labels:
+        app: pg
+    spec:
+      imagePullSecrets:
+      - name: takima-school-registry
+      volumes:
+      - name: pg-data
+        persistentVolumeClaim:
+          claimName: pg-db
+      containers:
+      - name: pg
+        image: {{ .Values.pg.image.repository }}:{{ .Values.pg.image.tag }}
+        volumeMounts:
+        - mountPath: /var/lib/postgresql/data
+          name: pg-data
+        resources:
+          requests:
+            memory: "192Mi"
+            cpu: "100m"
+          limits:
+            memory: "256Mi"
+            cpu: 1
+        ports:
+        - containerPort: 5432
+        env:
+        - name: POSTGRES_PASSWORD
+          valueFrom:
+            secretKeyRef:
+              name: {{ .Values.name }}-pg
+              key: password
+        - name: POSTGRES_USER
+          valueFrom:
+            secretKeyRef:
+              name: {{ .Values.name }}-pg
+              key: username
+        - name: POSTGRES_DB
+          valueFrom:
+            configMapKeyRef:
+              name: {{ .Values.name }}-pg
+              key: POSTGRES_DB
+        - name: PGDATA
+          valueFrom:
+            configMapKeyRef:
+              name: {{ .Values.name }}-pg
+              key: DB_PATH
\ No newline at end of file
diff --git a/templates/database/pg-pvc.yml b/templates/database/pg-pvc.yml
new file mode 100644
index 0000000..5cd2ea7
--- /dev/null
+++ b/templates/database/pg-pvc.yml
@@ -0,0 +1,12 @@
+apiVersion: v1
+kind: PersistentVolumeClaim
+metadata:
+  name: {{ .Values.name }}-pg
+spec:
+  storageClassName: gp2
+  accessModes:
+  - ReadWriteOnce
+  volumeMode: Filesystem
+  resources:
+    requests:
+      storage: 3Gi
\ No newline at end of file
diff --git a/templates/database/pg-service.yml b/templates/database/pg-service.yml
new file mode 100644
index 0000000..792a71c
--- /dev/null
+++ b/templates/database/pg-service.yml
@@ -0,0 +1,13 @@
+{{- if .Values.api.enabled }}
+apiVersion: v1
+kind: Service
+metadata:
+  name: {{ .Values.name }}-pg
+spec:
+  selector:
+    app: pg
+  ports:
+    - protocol: TCP
+      port: 5432
+      targetPort: 5432
+{{- end }}
\ No newline at end of file
diff --git a/templates/front-config.yaml b/templates/www/front-config.yaml
similarity index 100%
rename from templates/front-config.yaml
rename to templates/www/front-config.yaml
diff --git a/templates/front-deployment.yaml b/templates/www/front-deployment.yaml
similarity index 100%
rename from templates/front-deployment.yaml
rename to templates/www/front-deployment.yaml
diff --git a/templates/front-ingress.yaml b/templates/www/front-ingress.yaml
similarity index 95%
rename from templates/front-ingress.yaml
rename to templates/www/front-ingress.yaml
index 297d800..746dbfc 100644
--- a/templates/front-ingress.yaml
+++ b/templates/www/front-ingress.yaml
@@ -13,7 +13,7 @@ spec:
           service:
             name: {{ .Values.name }}-front
             port:
-              number: 80
+              number: 8080
         path: /
         pathType: Prefix
   {{- if .Values.front.ingress.tlsEnabled }}
diff --git a/templates/front-service.yaml b/templates/www/front-service.yaml
similarity index 92%
rename from templates/front-service.yaml
rename to templates/www/front-service.yaml
index 8031d1a..3bda6c5 100644
--- a/templates/front-service.yaml
+++ b/templates/www/front-service.yaml
@@ -8,6 +8,6 @@ spec:
     app: front
   ports:
     - protocol: TCP
-      port: 80
+      port: 8080
       targetPort: 8080
 {{- end }}
\ No newline at end of file
diff --git a/values.yaml b/values.yaml
index abb218b..9bf9372 100644
--- a/values.yaml
+++ b/values.yaml
@@ -4,9 +4,22 @@
 # We have only done front for now
 name: cdb
 
+pg:
+  enabled: true
+  port: 5432
+  image:
+    repository: registry.takima.io/school/proxy/postgres
+    tag: latest
+  replicaCount: 1
+
 api:
+  enabled: true
+  image:
+    repository: registry.gitlab.com/takima-school/images/cdb/api
+    tag: latest
+  replicaCount: 1
   ingress:
-    tlsEnabled: false
+    tlsEnabled: true
     host: api.bmaignan.takima.school
 
 front:
@@ -16,5 +29,5 @@ front:
     tag: latest
   replicaCount: 1
   ingress:
-    tlsEnabled: false
+    tlsEnabled: true
     host: www.bmaignan.takima.school
\ No newline at end of file
-- 
GitLab