From bb0b3d2d4f07620db41d8f788dbb07ca62c53880 Mon Sep 17 00:00:00 2001
From: Christian Zheng <czheng@takima.fr>
Date: Wed, 2 Aug 2023 15:50:53 +0200
Subject: [PATCH] add back and db

---
 front/.gitignore => .gitignore                |  0
 front/Chart.yaml => Chart.yaml                |  0
 templates/_helpers.tpl                        | 53 ++++++++++++++++
 templates/api-config.yaml                     |  7 +++
 templates/api-deployment.yaml                 | 63 +++++++++++++++++++
 templates/api-ingress.yaml                    | 36 +++++++++++
 templates/api-service.yaml                    | 11 ++++
 .../templates => templates}/front-config.yaml |  2 +-
 .../front-deployment.yaml                     |  5 +-
 .../front-ingress.yaml                        |  2 +-
 .../front-service.yaml                        |  2 +-
 templates/pg-credentials.yaml                 | 12 ++++
 templates/pg-operator.yaml                    | 19 ++++++
 front/values.yaml => values.yaml              | 14 ++++-
 14 files changed, 221 insertions(+), 5 deletions(-)
 rename front/.gitignore => .gitignore (100%)
 rename front/Chart.yaml => Chart.yaml (100%)
 create mode 100644 templates/_helpers.tpl
 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
 rename {front/templates => templates}/front-config.yaml (97%)
 rename {front/templates => templates}/front-deployment.yaml (92%)
 rename {front/templates => templates}/front-ingress.yaml (98%)
 rename {front/templates => templates}/front-service.yaml (94%)
 create mode 100644 templates/pg-credentials.yaml
 create mode 100644 templates/pg-operator.yaml
 rename front/values.yaml => values.yaml (62%)

diff --git a/front/.gitignore b/.gitignore
similarity index 100%
rename from front/.gitignore
rename to .gitignore
diff --git a/front/Chart.yaml b/Chart.yaml
similarity index 100%
rename from front/Chart.yaml
rename to Chart.yaml
diff --git a/templates/_helpers.tpl b/templates/_helpers.tpl
new file mode 100644
index 0000000..e27fd56
--- /dev/null
+++ b/templates/_helpers.tpl
@@ -0,0 +1,53 @@
+{{/*
+Expand the name of the chart.
+*/}}
+{{- define "MyAppCtx.name" -}}
+{{- default .Chart.Name | trunc 63 | trimSuffix "-" }}
+{{- end }}
+
+
+{{/*
+Application image tag
+We select by default the Chart appVersion or an override in values
+*/}}
+{{- define "MyAppCtx.imageTag" }}
+{{- $name := default .Chart.AppVersion .Values.image.tag }}
+{{- printf "%s" $name }}
+{{- end }}
+
+
+{{/*
+Create a default fully qualified app name.
+We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
+*/}}
+{{- define "MyAppCtx.fullname" }}
+{{- $name := default .Chart.Name .Values.nameOverride -}}
+{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-"}}
+{{- end }}
+
+{{/*
+Create chart name and version as used by the chart label.
+*/}}
+{{- define "MyAppCtx.chart" -}}
+{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
+{{- end }}
+
+{{/*
+Common labels
+*/}}
+{{- define "MyAppCtx.labels" -}}
+helm.sh/chart: {{ include "MyAppCtx.chart" . }}
+{{ include "MyAppCtx.selectorLabels" . }}
+{{- if .Chart.AppVersion }}
+app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
+{{- end }}
+app.kubernetes.io/managed-by: {{ .Release.Service }}
+{{- end }}
+
+{{/*
+Selector labels
+*/}}
+{{- define "MyAppCtx.selectorLabels" -}}
+app.kubernetes.io/name: {{ include "MyAppCtx.name" . }}
+app.kubernetes.io/instance: {{ .Release.Name }}
+{{- end }}
diff --git a/templates/api-config.yaml b/templates/api-config.yaml
new file mode 100644
index 0000000..d84219d
--- /dev/null
+++ b/templates/api-config.yaml
@@ -0,0 +1,7 @@
+apiVersion: v1
+data:
+  db-endpoint: {{ .Values.api.db.endpoint }}
+
+kind: ConfigMap
+metadata:
+  name: api-config
diff --git a/templates/api-deployment.yaml b/templates/api-deployment.yaml
new file mode 100644
index 0000000..3048b1e
--- /dev/null
+++ b/templates/api-deployment.yaml
@@ -0,0 +1,63 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: {{ .Values.name }}-api
+  annotations: 
+    checksum/config: {{ include (print $.Template.BasePath "/api-config.yaml") . | sha256sum }}
+spec:
+  replicas: {{ .Values.api.replicaCount }}
+  selector:
+    matchLabels:
+      app: {{ .Values.name }}-api
+  template:
+    metadata:
+      labels:
+        app: {{ .Values.name }}-api
+    spec:
+      containers:
+        - env:
+            - name: DB_ENDPOINT
+              valueFrom:
+                configMapKeyRef:
+                  key: db-endpoint
+                  name: api-config
+            - name: POSTGRES_DB
+              value: {{ .Values.db.dbName }}
+            - name: POSTGRES_USER
+              valueFrom:
+                secretKeyRef:
+                  key: username
+                  name: hydra.acid-hydra-database.credentials.postgresql.acid.zalan.do
+            - name: POSTGRES_PASSWORD
+              valueFrom:
+                secretKeyRef:
+                  key: password
+                  name: hydra.acid-hydra-database.credentials.postgresql.acid.zalan.do
+          image: {{ .Values.api.image.repository }}:{{ .Values.api.image.tag }}
+          livenessProbe:
+            httpGet:
+              path: /actuator/health/liveness
+              port: 8080
+            initialDelaySeconds: 30
+            periodSeconds: 10
+          name: {{ .Values.name }}-api
+          ports:
+            - containerPort: 8080
+          readinessProbe:
+            httpGet:
+              path: /actuator/health/readiness
+              port: 8080
+            initialDelaySeconds: 30
+            periodSeconds: 5
+          resources:
+            limits:
+              cpu: '1'
+              memory: 256Mi
+            requests:
+              cpu: 100m
+              memory: 192Mi
+      imagePullSecrets:
+        - name: takima-school-registry
+      securityContext:
+        runAsGroup: 1001
+        runAsUser: 1001
diff --git a/templates/api-ingress.yaml b/templates/api-ingress.yaml
new file mode 100644
index 0000000..e675cf6
--- /dev/null
+++ b/templates/api-ingress.yaml
@@ -0,0 +1,36 @@
+apiVersion: networking.k8s.io/v1
+kind: Ingress
+metadata:
+  annotations:
+    kubernetes.io/ingress.class: nginx
+    {{- if .Values.api.ingress.tlsEnabled }}
+    kubernetes.io/tls-acme: 'true'
+    nginx.ingress.kubernetes.io/cors-allow-headers: Content-Type
+    nginx.ingress.kubernetes.io/cors-allow-methods: '*'
+    nginx.ingress.kubernetes.io/cors-allow-origin: {{ .Values.front.ingress.host }}
+    nginx.ingress.kubernetes.io/cors-expose-headers: X-Custom-Header
+    nginx.ingress.kubernetes.io/cors-max-age: '86400'
+    nginx.ingress.kubernetes.io/enable-cors: 'true'
+    {{- end }}
+  labels:
+    name: {{ .Values.name }}-api-ingress
+  name: {{ .Values.name }}-api-ingress
+spec:
+  rules:
+    - host: {{ .Values.api.ingress.host }}
+      http:
+        paths:
+          - backend:
+              service:
+                name: {{ .Values.name }}-api-service
+                port:
+                  number: 80
+            path: /
+            pathType: Prefix
+{{- if .Values.api.ingress.tlsEnabled }}
+  tls:
+    - hosts:
+        - {{ .Values.api.ingress.host }}
+      secretName: app-wildcard
+{{- end }}
+
diff --git a/templates/api-service.yaml b/templates/api-service.yaml
new file mode 100644
index 0000000..2c4563b
--- /dev/null
+++ b/templates/api-service.yaml
@@ -0,0 +1,11 @@
+apiVersion: v1
+kind: Service
+metadata:
+  name: {{ .Values.name }}-api-service
+spec:
+  ports:
+    - port: 80
+      protocol: TCP
+      targetPort: 8080
+  selector:
+    app: cdb-api
diff --git a/front/templates/front-config.yaml b/templates/front-config.yaml
similarity index 97%
rename from front/templates/front-config.yaml
rename to templates/front-config.yaml
index da89df7..f04d583 100644
--- a/front/templates/front-config.yaml
+++ b/templates/front-config.yaml
@@ -11,4 +11,4 @@ metadata:
   name: {{ .Values.name }}-front  
 data:  
   API_URL: {{ $apiUrl }}  
-{{- end }}
\ No newline at end of file
+{{- end }}
diff --git a/front/templates/front-deployment.yaml b/templates/front-deployment.yaml
similarity index 92%
rename from front/templates/front-deployment.yaml
rename to templates/front-deployment.yaml
index fa7c697..96220c9 100644
--- a/front/templates/front-deployment.yaml
+++ b/templates/front-deployment.yaml
@@ -3,6 +3,9 @@ apiVersion: apps/v1
 kind: Deployment
 metadata:
   name: {{ .Values.name }}-front
+  annotations:
+    checksum/config: {{ include (print $.Template.BasePath "/front-config.yaml") . | sha256sum }}
+
   labels:
     app: front
 spec:
@@ -65,4 +68,4 @@ spec:
               configMapKeyRef:
                 name: {{ .Values.name }}-front
                 key: API_URL
-{{- end }}
\ No newline at end of file
+{{- end }}
diff --git a/front/templates/front-ingress.yaml b/templates/front-ingress.yaml
similarity index 98%
rename from front/templates/front-ingress.yaml
rename to templates/front-ingress.yaml
index 8513bb5..2bafaad 100644
--- a/front/templates/front-ingress.yaml
+++ b/templates/front-ingress.yaml
@@ -23,4 +23,4 @@ spec:
       - {{ .Values.front.ingress.host }}
     secretName: app-wildcard
   {{- end }}
-{{- end }}
\ No newline at end of file
+{{- end }}
diff --git a/front/templates/front-service.yaml b/templates/front-service.yaml
similarity index 94%
rename from front/templates/front-service.yaml
rename to templates/front-service.yaml
index 8031d1a..1d8c47a 100644
--- a/front/templates/front-service.yaml
+++ b/templates/front-service.yaml
@@ -10,4 +10,4 @@ spec:
     - protocol: TCP
       port: 80
       targetPort: 8080
-{{- end }}
\ No newline at end of file
+{{- end }}
diff --git a/templates/pg-credentials.yaml b/templates/pg-credentials.yaml
new file mode 100644
index 0000000..210f92c
--- /dev/null
+++ b/templates/pg-credentials.yaml
@@ -0,0 +1,12 @@
+apiVersion: v1
+kind: Secret
+metadata:
+  name: hydra.acid-hydra-database.credentials.postgresql.acid.zalan.do
+  labels:
+    application: spilo
+    cluster-name: acid-hydra-database
+    team: acid
+type: Opaque
+data:
+  username: {{ .Values.db.username | b64enc }}
+  password: {{ .Values.db.password | b64enc }}
diff --git a/templates/pg-operator.yaml b/templates/pg-operator.yaml
new file mode 100644
index 0000000..03c361d
--- /dev/null
+++ b/templates/pg-operator.yaml
@@ -0,0 +1,19 @@
+apiVersion: "acid.zalan.do/v1"
+kind: postgresql
+metadata:
+  annotations:
+    checksum/config: {{ include (print $.Template.BasePath "/pg-credentials.yaml") . | sha256sum }}
+  name: {{ .Values.db.prefix }}-postgresql
+spec:
+  teamId: {{ .Values.db.prefix }}  # le team id doit matcher le préfixe dans le metadata.name, ici formation
+  volume:
+    size: 1Gi
+  numberOfInstances: 2
+  users:
+    {{ .Values.db.username }}:  # database owner
+      - superuser
+      - createdb
+  databases:
+    {{ .Values.db.dbName }}: {{ .Values.db.username }}  # dbname: owner
+  postgresql:
+    version: "14"
diff --git a/front/values.yaml b/values.yaml
similarity index 62%
rename from front/values.yaml
rename to values.yaml
index 0d4183a..7a07801 100644
--- a/front/values.yaml
+++ b/values.yaml
@@ -5,9 +5,15 @@
 name: cdb
 
 api:
+  image:
+    repository: registry.gitlab.com/takima-school/images/cdb/api
+    tag: latest
+  replicaCount: 1
   ingress:
-    tlsEnabled: false
+    tlsEnabled: true
     host: api.czheng.takima.school
+  db:
+    endpoint: cdb-postgresql:5432
 
 front:
   enabled: true
@@ -18,3 +24,9 @@ front:
   ingress:
     tlsEnabled: false
     host: www.czheng.takima.school
+
+db:
+  dbName: cdb-db
+  username: username
+  password: password
+  prefix: cdb
-- 
GitLab