From 21f0240bcf25060d1a446541414b872347e884b4 Mon Sep 17 00:00:00 2001
From: Anto RAKOTOVAO <arakotovao@takima.fr>
Date: Fri, 9 Feb 2024 16:05:33 +0100
Subject: [PATCH] API and data templates

---
 .gitignore                    |  1 +
 templates/api-config.yaml     |  6 ++++
 templates/api-deployment.yaml | 53 ++++++++++++++++++++++++++++++++++
 templates/api-ingress.yaml    | 18 ++++++++++++
 templates/api-service.yaml    | 11 +++++++
 templates/pg-config.yaml      |  7 +++++
 templates/pg-credentials.yaml |  8 ++++++
 templates/pg-deployment.yaml  | 54 +++++++++++++++++++++++++++++++++++
 templates/pg-pvc.yaml         | 12 ++++++++
 templates/pg-service.yml      | 11 +++++++
 values.yaml                   | 18 ++++++++++--
 11 files changed, 197 insertions(+), 2 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 templates/api-config.yaml
 create mode 100644 templates/api-deployment.yaml
 create mode 100644 templates/api-ingress.yaml
 create mode 100644 templates/api-service.yaml
 create mode 100644 templates/pg-config.yaml
 create mode 100644 templates/pg-credentials.yaml
 create mode 100644 templates/pg-deployment.yaml
 create mode 100644 templates/pg-pvc.yaml
 create mode 100644 templates/pg-service.yml

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..849ddff
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+dist/
diff --git a/templates/api-config.yaml b/templates/api-config.yaml
new file mode 100644
index 0000000..85da321
--- /dev/null
+++ b/templates/api-config.yaml
@@ -0,0 +1,6 @@
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ .Values.name }}-api-config
+data:
+  DB_ENDPOINT: {{ .Values.name }}-pg:{{ .Values.pg.port }}
\ No newline at end of file
diff --git a/templates/api-deployment.yaml b/templates/api-deployment.yaml
new file mode 100644
index 0000000..6fe51dd
--- /dev/null
+++ b/templates/api-deployment.yaml
@@ -0,0 +1,53 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  creationTimestamp: null
+  labels:
+    app: api
+  name: {{ .Values.name }}-api
+spec:
+  replicas: {{ .Values.api.replicaCount }}
+  selector:
+    matchLabels:
+      app: api
+  strategy: {}
+  template:
+    metadata:
+      creationTimestamp: null
+      labels:
+        app: api
+    spec:
+      containers:
+      - image: {{ .Values.api.image.repository }}:{{ .Values.api.image.tag }}
+        name: api
+        resources:
+          limits:
+            memory: 256M
+            cpu: 1
+          requests:
+            memory: 192M
+            cpu: 100m
+        env:
+        - name: DB_ENDPOINT # Vrai key de la variable d'env. Peut-être différent de la valeur dans le configmap
+          valueFrom:
+            configMapKeyRef:
+              name: {{ .Values.name }}-api-config # Nom du configmap
+              key: DB_ENDPOINT  # nom de la clef dans le configmap
+        - name: POSTGRES_DB  # Vrai key de la variable d'env. Peut-être différent de la valeur dans le configmap
+          valueFrom:
+            configMapKeyRef:
+              name: {{ .Values.name }}-pg-config
+              key: POSTGRES_DB  
+        - name: POSTGRES_USER  
+          valueFrom:
+            secretKeyRef:
+              name: {{ .Values.name }}-pg-credentials
+              key: POSTGRES_USER 
+        - name: POSTGRES_PASSWORD
+          valueFrom:
+            secretKeyRef:
+              name: {{ .Values.name }}-pg-credentials
+              key: POSTGRES_PASSWORD
+      imagePullSecrets: 
+        - name: takima-school-registry # à variabiliser 
+status: {}
diff --git a/templates/api-ingress.yaml b/templates/api-ingress.yaml
new file mode 100644
index 0000000..5511ed1
--- /dev/null
+++ b/templates/api-ingress.yaml
@@ -0,0 +1,18 @@
+apiVersion: networking.k8s.io/v1
+kind: Ingress
+metadata:
+ annotations:
+   kubernetes.io/ingress.class: nginx
+ name: {{ .Values.name }}-api
+spec:
+ rules:
+ - host: {{ .Values.api.ingress.host }}
+   http:
+     paths:
+     - backend:
+         service:
+           name: {{ .Values.name }}-api
+           port:
+             number: {{ .Values.api.port }}
+       path: /
+       pathType: Prefix
diff --git a/templates/api-service.yaml b/templates/api-service.yaml
new file mode 100644
index 0000000..1b42e6e
--- /dev/null
+++ b/templates/api-service.yaml
@@ -0,0 +1,11 @@
+apiVersion: v1
+kind: Service
+metadata:
+  name: {{ .Values.name }}-api
+spec:
+  selector:
+    app: api
+  ports:
+    - protocol: TCP
+      port: {{ .Values.api.port }}
+      targetPort: {{ .Values.api.targetPort }}
diff --git a/templates/pg-config.yaml b/templates/pg-config.yaml
new file mode 100644
index 0000000..fc7c694
--- /dev/null
+++ b/templates/pg-config.yaml
@@ -0,0 +1,7 @@
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ .Values.name }}-pg-config  
+data:
+  POSTGRES_DB: {{ .Values.name }}-db
+  db_path: "/var/lib/postgresql/data/pgdata"
\ No newline at end of file
diff --git a/templates/pg-credentials.yaml b/templates/pg-credentials.yaml
new file mode 100644
index 0000000..756e918
--- /dev/null
+++ b/templates/pg-credentials.yaml
@@ -0,0 +1,8 @@
+apiVersion: v1
+kind: Secret
+metadata:
+  name: {{ .Values.name }}-pg-credentials
+type: Opaque
+data:
+  POSTGRES_USER: YXJha290b3Zhbw==
+  POSTGRES_PASSWORD: MTIzNDU2
\ No newline at end of file
diff --git a/templates/pg-deployment.yaml b/templates/pg-deployment.yaml
new file mode 100644
index 0000000..78280da
--- /dev/null
+++ b/templates/pg-deployment.yaml
@@ -0,0 +1,54 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  creationTimestamp: null
+  labels:
+    app: pg
+  name: {{ .Values.name }}-pg
+spec:
+  replicas: {{ .Values.pg.replicaCount }}
+  selector:
+    matchLabels:
+      app: pg
+  strategy: {}
+  template:
+    metadata:
+      creationTimestamp: null
+      labels:
+        app: pg
+    spec:
+      containers:
+      - image: {{ .Values.pg.image.repository }}:{{ .Values.pg.image.tag }}
+        name: postgres
+        resources: {}
+        env:
+        - name: POSTGRES_DB  
+          valueFrom:
+            configMapKeyRef:
+              name: {{ .Values.name }}-pg-config 
+              key: POSTGRES_DB  
+        - name: POSTGRES_USER  
+          valueFrom:
+            secretKeyRef:
+              name: {{ .Values.name }}-pg-credentials
+              key: POSTGRES_USER  
+        - name: POSTGRES_PASSWORD  
+          valueFrom:
+            secretKeyRef:
+              name: {{ .Values.name }}-pg-credentials
+              key: POSTGRES_PASSWORD     
+        - name: PGDATA
+          valueFrom:
+            configMapKeyRef:
+              name: {{ .Values.name }}-pg-config  
+              key: db_path    
+        volumeMounts:
+        - mountPath: /var/lib/postgresql/data
+          name: {{ .Values.name }}-pg-data
+      imagePullSecrets:
+        - name: takima-school-registry
+      volumes:
+      - name: {{ .Values.name }}-pg-data
+        persistentVolumeClaim:
+          claimName: {{ .Values.name }}-pg
+status: {}
diff --git a/templates/pg-pvc.yaml b/templates/pg-pvc.yaml
new file mode 100644
index 0000000..1ebf06b
--- /dev/null
+++ b/templates/pg-pvc.yaml
@@ -0,0 +1,12 @@
+apiVersion: v1
+kind: PersistentVolumeClaim
+metadata:
+  name: {{ .Values.name }}-pg
+spec:
+  storageClassName: gp2
+  accessModes:
+  - ReadWriteOnce
+  volumeMode: Filesystem
+  resources:
+    requests:
+      storage: 3Gi
diff --git a/templates/pg-service.yml b/templates/pg-service.yml
new file mode 100644
index 0000000..ada173c
--- /dev/null
+++ b/templates/pg-service.yml
@@ -0,0 +1,11 @@
+apiVersion: v1
+kind: Service
+metadata:
+  name: {{ .Values.name }}-pg
+spec:
+  selector:
+    app: pg
+  ports:
+    - protocol: TCP
+      port: {{ .Values.pg.port }}
+      targetPort: {{ .Values.pg.targetPort }}
diff --git a/values.yaml b/values.yaml
index 9ab42ef..4f00f1a 100644
--- a/values.yaml
+++ b/values.yaml
@@ -7,7 +7,13 @@ name: cdb
 api:
   ingress:
     tlsEnabled: false
-    host: api.to-replace.takima.school
+    host: api.cdb.arakotovao.takima.school
+  replicaCount: 1
+  image:
+    repository: registry.gitlab.com/takima-school/images/cdb/api
+    tag: latest
+  port: 80
+  targetPort: 8080
 
 front:
   enabled: true
@@ -17,4 +23,12 @@ front:
   replicaCount: 1
   ingress:
     tlsEnabled: false
-    host: www.to-replace.takima.school
\ No newline at end of file
+    host: www.arakotovao.takima.school
+
+pg:
+  image:
+    repository: registry.takima.io/school/proxy/postgres
+    tag: latest
+  replicaCount: 1
+  port: 80
+  targetPort: 5432
\ No newline at end of file
-- 
GitLab