From 34f73702b2dfacb5eed5cc7525e9f3c5cae8c4b2 Mon Sep 17 00:00:00 2001 From: Elise SOUVANNAVONG <esouvannavong@takima.fr> Date: Thu, 23 May 2024 15:13:42 +0200 Subject: [PATCH] solution bonus --- templates/_helpers.tpl | 101 ++++++++++++++++++++++++++++++++ templates/api-config.yaml | 5 +- templates/api-deployment.yaml | 35 ++++++----- templates/api-ingress.yaml | 5 +- templates/api-service.yaml | 6 +- templates/front-config.yaml | 2 +- templates/front-deployment.yaml | 15 +++-- templates/front-ingress.yaml | 5 +- templates/front-service.yaml | 6 +- templates/pg-config.yaml | 3 +- templates/pg-credentials.yaml | 3 +- templates/pg-deployment.yaml | 30 ++++++---- templates/pg-pvc.yaml | 15 +++++ templates/pg-service.yaml | 8 +-- values.prod.yaml | 40 +++++++++++++ values.staging.yaml | 40 +++++++++++++ values.yaml | 30 +++++++--- 17 files changed, 284 insertions(+), 65 deletions(-) create mode 100644 templates/_helpers.tpl create mode 100644 templates/pg-pvc.yaml create mode 100644 values.prod.yaml create mode 100644 values.staging.yaml diff --git a/templates/_helpers.tpl b/templates/_helpers.tpl new file mode 100644 index 0000000..a5a0106 --- /dev/null +++ b/templates/_helpers.tpl @@ -0,0 +1,101 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "AppCtx.chartName" -}} +{{- default .Chart.Name | trunc 24 | trimSuffix "-" }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "AppCtx.chartNameVersion" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 30 chars in order to leave room for suffixes (because some Kubernetes name fields are limited to 63 chars by the DNS naming spec). +*/}} +{{- define "AppCtx.name" }} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- printf "%s" $name | trunc 30 | trimSuffix "-"}} +{{- end }} + +{{/* +Create the API name +*/}} +{{- define "AppCtx.apiName" }} +{{- printf "%s-api" (include "AppCtx.name" .) | trunc 63 }} +{{- end }} + +{{/* +Create the Front name +*/}} +{{- define "AppCtx.frontName" }} +{{- printf "%s-front" (include "AppCtx.name" .) | trunc 63 }} +{{- end }} + +{{/* +Create the DB name +*/}} +{{- define "AppCtx.dbName" }} +{{- printf "%s-db" (include "AppCtx.name" .) | trunc 63 }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "AppCtx.selectorLabels" -}} +app.kubernetes.io/name: {{ include "AppCtx.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "AppCtx.labels" -}} +helm.sh/chart: {{ include "AppCtx.chartName" . }} +{{ include "AppCtx.selectorLabels" . }} +app.kubernetes.io/part-of: {{ include "AppCtx.chartName" . }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{- define "AppCtx.apiSelectorLabels" -}} +{{ include "AppCtx.selectorLabels" . }} +app.kubernetes.io/component: api +{{- end }} + +{{- define "AppCtx.apiLabels" -}} +{{ include "AppCtx.apiSelectorLabels" . }} +app.kubernetes.io/part-of: {{ include "AppCtx.chartName" . }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +app/language: java +app/version: {{ .Values.api.image.tag }} +{{- end }} + + +{{- define "AppCtx.frontSelectorLabels" -}} +{{ include "AppCtx.selectorLabels" . }} +app.kubernetes.io/component: front +{{- end }} + +{{- define "AppCtx.frontLabels" -}} +{{ include "AppCtx.frontSelectorLabels" . }} +app.kubernetes.io/part-of: {{ include "AppCtx.chartName" . }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +app/language: javascript +app/version: {{ .Values.front.image.tag }} +{{- end }} + +{{- define "AppCtx.dbSelectorLabels" -}} +{{ include "AppCtx.selectorLabels" . }} +app.kubernetes.io/component: db +{{- end }} + +{{- define "AppCtx.dbLabels" -}} +{{ include "AppCtx.dbSelectorLabels" . }} +app.kubernetes.io/part-of: {{ include "AppCtx.chartName" . }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +app/language: postgresql +app/version: {{ .Values.db.image.tag }} +{{- end }} diff --git a/templates/api-config.yaml b/templates/api-config.yaml index 8a1d62f..39d56d6 100644 --- a/templates/api-config.yaml +++ b/templates/api-config.yaml @@ -2,8 +2,9 @@ apiVersion: v1 kind: ConfigMap metadata: - name: {{.Values.name }}-api + name: {{ template "AppCtx.apiName" . }} + labels: {{ include "AppCtx.apiLabels" . | nindent 4 }} data: - DB_ENDPOINT: "{{ .Values.name }}:5432" + DB_ENDPOINT: "{{ template "AppCtx.dbName" . }}:5432" DB_NAME: {{ .Values.db.name }} {{- end }} \ No newline at end of file diff --git a/templates/api-deployment.yaml b/templates/api-deployment.yaml index 65d2d59..baf0af5 100644 --- a/templates/api-deployment.yaml +++ b/templates/api-deployment.yaml @@ -2,18 +2,17 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: {{.Values.name }}-api - labels: - app: api + name: {{ template "AppCtx.apiName" . }} + labels: {{ include "AppCtx.apiLabels" . | nindent 4 }} spec: replicas: {{ .Values.api.replicaCount }} selector: - matchLabels: - app: api + matchLabels: {{ include "AppCtx.apiSelectorLabels" . | nindent 8 }} template: metadata: - labels: - app: api + labels: {{ include "AppCtx.apiLabels" . | nindent 10 }} + annotations: + checksum/config: {{ include (print $.Template.BasePath "/api-config.yaml") . | sha256sum }} spec: securityContext: runAsUser: 1001 @@ -25,19 +24,19 @@ spec: image: {{ .Values.api.image.repository }}:{{ .Values.api.image.tag }} resources: requests: - memory: "192M" - cpu: "0.2" + memory: {{ .Values.api.requests.memory }} + cpu: {{ .Values.api.requests.cpu }} limits: - memory: "256M" - cpu: "1" + memory: {{ .Values.api.limits.memory }} + cpu: {{ .Values.api.limits.cpu }} startupProbe: httpGet: path: /actuator/health port: 8080 - initialDelaySeconds: 15 - periodSeconds: 3 + initialDelaySeconds: {{ .Values.api.startupProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.api.startupProbe.periodSeconds }} successThreshold: 1 - failureThreshold: 5 + failureThreshold: {{ .Values.api.startupProbe.failureThreshold }} livenessProbe: httpGet: path: /actuator/health/liveness @@ -60,21 +59,21 @@ spec: - name: DB_ENDPOINT valueFrom: configMapKeyRef: - name: {{.Values.name }}-api + name: {{ template "AppCtx.apiName" . }} key: DB_ENDPOINT - name: POSTGRES_DB valueFrom: configMapKeyRef: - name: {{.Values.name }}-api + name: {{ template "AppCtx.apiName" . }} key: DB_NAME - name: POSTGRES_USER valueFrom: secretKeyRef: - name: {{ .Values.name }}-db # Nom du secret + name: {{ template "AppCtx.dbName" . }}-credentials # Nom du secret key: pg_username # nom de la clef dans le config map - name: POSTGRES_PASSWORD valueFrom: secretKeyRef: - name: {{ .Values.name }}-db # Nom du secret + name: {{ template "AppCtx.dbName" . }}-credentials # Nom du secret key: pg_password {{- end }} \ No newline at end of file diff --git a/templates/api-ingress.yaml b/templates/api-ingress.yaml index 607b85c..c8bb6b2 100644 --- a/templates/api-ingress.yaml +++ b/templates/api-ingress.yaml @@ -2,7 +2,8 @@ apiVersion: networking.k8s.io/v1 kind: Ingress metadata: - name: {{.Values.name }}-api + name: {{ template "AppCtx.apiName" . }} + labels: {{ include "AppCtx.apiLabels" . | nindent 4 }} spec: ingressClassName: nginx rules: @@ -11,7 +12,7 @@ spec: paths: - backend: service: - name: {{.Values.name }}-api + name: {{ template "AppCtx.apiName" . }} port: number: 80 path: / diff --git a/templates/api-service.yaml b/templates/api-service.yaml index 596b7b3..0f55be5 100644 --- a/templates/api-service.yaml +++ b/templates/api-service.yaml @@ -2,10 +2,10 @@ apiVersion: v1 kind: Service metadata: - name: {{.Values.name }}-api + name: {{ template "AppCtx.apiName" . }} + labels: {{ include "AppCtx.apiLabels" . | nindent 8 }} spec: - selector: - app: api + selector: {{ include "AppCtx.apiSelectorLabels" . | nindent 4 }} ports: - protocol: TCP port: 80 diff --git a/templates/front-config.yaml b/templates/front-config.yaml index da89df7..5d5fd45 100644 --- a/templates/front-config.yaml +++ b/templates/front-config.yaml @@ -8,7 +8,7 @@ apiVersion: v1 kind: ConfigMap metadata: - name: {{ .Values.name }}-front + name: {{ template "AppCtx.frontName" . }} data: API_URL: {{ $apiUrl }} {{- end }} \ No newline at end of file diff --git a/templates/front-deployment.yaml b/templates/front-deployment.yaml index fa7c697..6e98e4b 100644 --- a/templates/front-deployment.yaml +++ b/templates/front-deployment.yaml @@ -2,18 +2,17 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: {{ .Values.name }}-front - labels: - app: front + name: {{ template "AppCtx.frontName" . }} + labels: {{ include "AppCtx.frontLabels" . | nindent 4 }} spec: replicas: {{ .Values.front.replicaCount }} selector: - matchLabels: - app: front + matchLabels: {{ include "AppCtx.frontSelectorLabels" . | nindent 8 }} template: metadata: - labels: - app: front + labels: {{ include "AppCtx.frontLabels" . | nindent 10 }} + annotations: + checksum/config: {{ include (print $.Template.BasePath "/front-config.yaml") . | sha256sum }} spec: imagePullSecrets: - name: takima-school-registry @@ -63,6 +62,6 @@ spec: - name: API_URL valueFrom: configMapKeyRef: - name: {{ .Values.name }}-front + name: {{ template "AppCtx.frontName" . }} key: API_URL {{- end }} \ No newline at end of file diff --git a/templates/front-ingress.yaml b/templates/front-ingress.yaml index 297d800..9d8648d 100644 --- a/templates/front-ingress.yaml +++ b/templates/front-ingress.yaml @@ -2,7 +2,8 @@ apiVersion: networking.k8s.io/v1 kind: Ingress metadata: - name: {{ .Values.name }}-front + name: {{ template "AppCtx.frontName" . }} + labels: {{ include "AppCtx.frontLabels" . | nindent 4 }} spec: ingressClassName: nginx rules: @@ -11,7 +12,7 @@ spec: paths: - backend: service: - name: {{ .Values.name }}-front + name: {{ template "AppCtx.frontName" . }} port: number: 80 path: / diff --git a/templates/front-service.yaml b/templates/front-service.yaml index 8031d1a..29c8200 100644 --- a/templates/front-service.yaml +++ b/templates/front-service.yaml @@ -2,10 +2,10 @@ apiVersion: v1 kind: Service metadata: - name: {{ .Values.name }}-front + name: {{ template "AppCtx.frontName" . }} + labels: {{ include "AppCtx.frontLabels" . | nindent 8 }} spec: - selector: - app: front + selector: {{ include "AppCtx.frontSelectorLabels" . | nindent 4 }} ports: - protocol: TCP port: 80 diff --git a/templates/pg-config.yaml b/templates/pg-config.yaml index c493cdb..9704903 100644 --- a/templates/pg-config.yaml +++ b/templates/pg-config.yaml @@ -2,7 +2,8 @@ apiVersion: v1 kind: ConfigMap metadata: - name: {{ .Values.name }}-db + name: {{ template "AppCtx.dbName" . }} + labels: {{ include "AppCtx.dbLabels" . | nindent 4 }} data: db_name: {{ .Values.db.name }} db_path: "/var/lib/postgresql/data/pgdata" diff --git a/templates/pg-credentials.yaml b/templates/pg-credentials.yaml index 2c483f5..69b2d6e 100644 --- a/templates/pg-credentials.yaml +++ b/templates/pg-credentials.yaml @@ -2,7 +2,8 @@ apiVersion: v1 kind: Secret metadata: - name: {{ .Values.name }}-db + name: {{ template "AppCtx.dbName" . }}-credentials + labels: {{ include "AppCtx.dbLabels" . | nindent 4 }} type: Opaque data: pg_username: YWRtaW4= # user: admin diff --git a/templates/pg-deployment.yaml b/templates/pg-deployment.yaml index e7564bc..c4fd65a 100644 --- a/templates/pg-deployment.yaml +++ b/templates/pg-deployment.yaml @@ -2,18 +2,17 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: {{ .Values.name }}-db - labels: - app: pg + name: {{ template "AppCtx.dbName" . }} + labels: {{ include "AppCtx.dbLabels" . | nindent 4 }} spec: replicas: 1 selector: - matchLabels: - app: pg + matchLabels: {{ include "AppCtx.dbSelectorLabels" . | nindent 8 }} template: metadata: - labels: - app: pg + labels: {{ include "AppCtx.dbLabels" . | nindent 10 }} + annotations: + checksum/config: {{ include (print $.Template.BasePath "/front-config.yaml") . | sha256sum }} spec: containers: - name: postgres @@ -24,23 +23,30 @@ spec: - name: POSTGRES_DB valueFrom: configMapKeyRef: - name: {{ .Values.name }}-db # Nom du configmap + name: {{ template "AppCtx.dbName" . }} # Nom du configmap key: db_name # nom de la clef dans le config map contenant le nom de la DB - name: PGDATA valueFrom: configMapKeyRef: - name: {{ .Values.name }}-db # Nom du configmap + name: {{ template "AppCtx.dbName" . }} # Nom du configmap key: db_path # nom de la clef dans le configMap contenant path ou installer la db dans le volume persistant - name: POSTGRES_USER valueFrom: secretKeyRef: - name: {{ .Values.name }}-db # Nom du secret + name: {{ template "AppCtx.dbName" . }}-credentials # Nom du secret key: pg_username # nom de la clef dans le secret - name: POSTGRES_PASSWORD valueFrom: secretKeyRef: - name: {{ .Values.name }}-db # Nom du secret + name: {{ template "AppCtx.dbName" . }}-credentials # Nom du secret key: pg_password # nom de la clef dans le secret contenant le password + volumeMounts: + - mountPath: /var/lib/postgresql/data + name: pg-data imagePullSecrets: - name: takima-school-registry -{{- end }} \ No newline at end of file + volumes: + - name: pg-data + persistentVolumeClaim: + claimName: {{ template "AppCtx.dbName" . }} +{{- end }} diff --git a/templates/pg-pvc.yaml b/templates/pg-pvc.yaml new file mode 100644 index 0000000..1ba7294 --- /dev/null +++ b/templates/pg-pvc.yaml @@ -0,0 +1,15 @@ +{{- if .Values.db.enabled }} +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: {{ template "AppCtx.dbName" . }} + labels: {{ include "AppCtx.dbLabels" . | nindent 4 }} +spec: + accessModes: + - ReadWriteOnce + volumeMode: Filesystem + resources: + requests: + storage: {{ .Values.db.pvc.size }} + storageClassName: {{ .Values.db.pvc.storageClass }} +{{- end }} diff --git a/templates/pg-service.yaml b/templates/pg-service.yaml index eb3a81c..547fd0c 100644 --- a/templates/pg-service.yaml +++ b/templates/pg-service.yaml @@ -2,13 +2,11 @@ apiVersion: v1 kind: Service metadata: - name: {{ .Values.name }}-db - labels: - app: pg + name: {{ template "AppCtx.dbName" . }} + labels: {{ include "AppCtx.dbLabels" . | nindent 4 }} spec: + selector: {{ include "AppCtx.dbSelectorLabels" . | nindent 4 }} type: ClusterIP ports: - port: 5432 - selector: - app: pg {{- end}} diff --git a/values.prod.yaml b/values.prod.yaml new file mode 100644 index 0000000..933c6a0 --- /dev/null +++ b/values.prod.yaml @@ -0,0 +1,40 @@ +# Default values for CDB app. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +nameOverride: mycdb-prod + +api: + image: + tag: latest + replicaCount: 2 + requests: + memory: "192M" + cpu: "0.2" + limits: + memory: "256M" + cpu: "1" + startupProbe: + initialDelaySeconds: 20 + periodSeconds: 3 + failureThreshold: 5 + ingress: + tlsEnabled: true + host: api.esouvannavong.takima.school + +front: + image: + tag: latest + replicaCount: 1 + ingress: + tlsEnabled: true + host: www.esouvannavong.takima.school + +db: + image: + tag: latest + pvc: + storageClass: gp2 + size: 512Mi + credentials: + user: cdb + pwd: cdb123 \ No newline at end of file diff --git a/values.staging.yaml b/values.staging.yaml new file mode 100644 index 0000000..9beddb6 --- /dev/null +++ b/values.staging.yaml @@ -0,0 +1,40 @@ +# Default values for CDB app. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +nameOverride: mycdb-staging + +api: + image: + tag: latest + replicaCount: 1 + requests: + memory: "128M" + cpu: "0.1" + limits: + memory: "256M" + cpu: "1" + startupProbe: + initialDelaySeconds: 20 + periodSeconds: 3 + failureThreshold: 5 + ingress: + tlsEnabled: true + host: api-staging.esouvannavong.takima.school + +front: + image: + tag: latest + replicaCount: 1 + ingress: + tlsEnabled: true + host: www-staging.esouvannavong.takima.school + +db: + image: + tag: latest + pvc: + storageClass: gp2 + size: 512Mi + credentials: + user: cdb + pwd: cdb123 diff --git a/values.yaml b/values.yaml index 888cb06..a2e6670 100644 --- a/values.yaml +++ b/values.yaml @@ -1,8 +1,7 @@ # Default values for CDB app. # This is a YAML-formatted file. # Declare variables to be passed into your templates. -# We have only done front for now -name: cdb +nameOverride: mycdb api: enabled: true @@ -10,9 +9,19 @@ api: repository: registry.gitlab.com/takima-school/images/cdb/api tag: latest replicaCount: 1 + requests: + memory: "192M" + cpu: "0.2" + limits: + memory: "256M" + cpu: "1" + startupProbe: + initialDelaySeconds: 30 + periodSeconds: 3 + failureThreshold: 5 ingress: - tlsEnabled: true - host: api.esouvannavong.takima.school + tlsEnabled: false + host: api.jlebloas.takima.school front: enabled: true @@ -21,12 +30,19 @@ front: tag: latest replicaCount: 1 ingress: - tlsEnabled: true - host: www.esouvannavong.takima.school + tlsEnabled: false + host: www.jlebloas.takima.school db: enabled: true name: cdb-db image: repository: registry.takima.io/school/proxy/postgres - tag: latest \ No newline at end of file + tag: latest + pvc: + storageClass: gp2 + size: 512Mi + credentials: + user: cdb + pwd: cdb123 + -- GitLab