From e659fc1d175d82d29d5ff9000ccfb50a45b77e74 Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Date: Sat, 30 Jan 2021 14:33:05 +0500 Subject: [PATCH] Update postgres backup configuration to use library chart --- test/nextcloud/1.1.0/templates/_postgres.tpl | 29 +++++++++++---- .../1.1.0/templates/backup-postgres-hook.yaml | 36 ++++++------------- .../templates/nuke-deployments-hook.yaml | 6 ++-- .../1.1.0/templates/postgres-deployment.yaml | 12 ++----- 4 files changed, 39 insertions(+), 44 deletions(-) diff --git a/test/nextcloud/1.1.0/templates/_postgres.tpl b/test/nextcloud/1.1.0/templates/_postgres.tpl index 038a680c80..e9244da240 100644 --- a/test/nextcloud/1.1.0/templates/_postgres.tpl +++ b/test/nextcloud/1.1.0/templates/_postgres.tpl @@ -5,18 +5,12 @@ Get Nextloud Postgres Database Name {{- print "nextcloud" -}} {{- end -}} -{{/* -Postgres Selector labels -*/}} -{{- define "nextcloud.postgres.selectorLabels" -}} -app.kubernetes.io/name: {{ include "nextcloud.name" . }}-postgres -app.kubernetes.io/instance: {{ .Release.Name }}-postgres -{{- end }} {{- define "postgres.imageName" -}} {{- print "postgres:13.1" -}} {{- end -}} + {{/* Retrieve postgres backup name This will return a unique name based on revision and chart numbers specified. @@ -26,3 +20,24 @@ This will return a unique name based on revision and chart numbers specified. {{- printf "postgres-backup-from-%s-to-%s-revision-%d" $upgradeDict.oldChartVersion $upgradeDict.newChartVersion (int64 $upgradeDict.preUpgradeRevision) -}} {{- end }} + +{{/* +Retrieve postgres credentials for environment variables configuration +*/}} +{{- define "postgres.envVariableConfiguration" -}} +{{ $envList := list }} +{{ $envList = mustAppend $envList (dict "name" "POSTGRES_USER" "valueFromSecret" true "secretName" "db-details" "secretKey" "db-user") }} +{{ $envList = mustAppend $envList (dict "name" "POSTGRES_PASSWORD" "valueFromSecret" true "secretName" "db-details" "secretKey" "db-password") }} +{{ include "common.containers.environmentVariables" (dict "environmentVariables" $envList) }} +{{- end -}} + + +{{/* +Retrieve postgres volume configuration +*/}} +{{- define "postgres.volumeConfiguration" -}} +{{ $vols := list }} +{{ $vols = mustAppend $vols (dict "name" "postgres-data" "emptyDirVolumes" .Values.emptyDirVolumes "hostPathEnabled" false "pathField" nil "datasetName" (.Values.postgresDataVolume | default dict).datasetName ) }} +{{ $vols = mustAppend $vols (dict "name" "postgres-backup" "emptyDirVolumes" .Values.emptyDirVolumes "hostPathEnabled" false "pathField" nil "datasetName" (.Values.postgresBackupVolume | default dict).datasetName ) }} +{{ include "common.storage.volumesConfiguration" (dict "ixVolumes" .Values.ixVolumes "volumes" $vols) }} +{{- end -}} diff --git a/test/nextcloud/1.1.0/templates/backup-postgres-hook.yaml b/test/nextcloud/1.1.0/templates/backup-postgres-hook.yaml index 3f6c6dc366..2286116f6b 100644 --- a/test/nextcloud/1.1.0/templates/backup-postgres-hook.yaml +++ b/test/nextcloud/1.1.0/templates/backup-postgres-hook.yaml @@ -1,4 +1,6 @@ {{- if .Values.ixChartContext.isUpgrade -}} +{{ $values := . }} +{{ $_ := set $values "nameSuffix" "postgres" }} apiVersion: batch/v1 kind: Job metadata: @@ -14,24 +16,14 @@ spec: name: "pre-upgrade-hook2" spec: restartPolicy: Never - serviceAccountName: "{{ template "nextcloud.serviceAccountName" . }}" + serviceAccountName: "{{ template "common.names.serviceAccountName" . }}" containers: - name: {{ .Chart.Name }}-postgres-backup image: {{ template "postgres.imageName" . }} imagePullPolicy: {{ .Values.image.pullPolicy }} - env: - - name: POSTGRES_USER - valueFrom: - secretKeyRef: - name: db-details - key: db-user - - name: POSTGRES_PASSWORD - valueFrom: - secretKeyRef: - name: db-details - key: db-password - - name: BACKUP_NAME - value: {{ template "postgres.backupName" . }} + env: {{ include "postgres.envVariableConfiguration" $values | nindent 10 }} + - name: BACKUP_NAME + value: {{ template "postgres.backupName" . }} volumeMounts: - name: postgres-data mountPath: /var/lib/postgresql/data @@ -43,15 +35,9 @@ spec: subPath: entrypoint.sh command: - "/bin/backup_entrypoint.sh" - volumes: - - name: postgres-data - hostPath: - path: {{ template "configuredPostgresHostPath" . }} - - name: postgres-backup - hostPath: - path: {{ template "configuredBackupPostgresHostPath" . }} - - name: backup-script-configmap - configMap: - defaultMode: 0700 - name: "postgres-backup-hook-config-map" + volumes: {{ include "postgres.volumeConfiguration" $values | nindent 8 }} + - name: backup-script-configmap + configMap: + defaultMode: 0700 + name: "postgres-backup-hook-config-map" {{- end -}} diff --git a/test/nextcloud/1.1.0/templates/nuke-deployments-hook.yaml b/test/nextcloud/1.1.0/templates/nuke-deployments-hook.yaml index 7307db6c1d..b1e0ef1768 100644 --- a/test/nextcloud/1.1.0/templates/nuke-deployments-hook.yaml +++ b/test/nextcloud/1.1.0/templates/nuke-deployments-hook.yaml @@ -1,4 +1,6 @@ {{- if .Values.ixChartContext.isUpgrade -}} +{{ $values := . }} +{{ $_ := set $values "nameSuffix" "postgres" }} apiVersion: batch/v1 kind: Job metadata: @@ -14,9 +16,9 @@ spec: name: "pre-upgrade-hook1" spec: restartPolicy: Never - serviceAccountName: "{{ template "nextcloud.serviceAccountName" . }}" + serviceAccountName: "{{ template "common.names.serviceAccountName" . }}" containers: - name: kubectl image: "bitnami/kubectl:1.19" - command: ["kubectl", "delete" , "deployment", "{{ template "nextcloud.fullname" . }}", "{{ template "nextcloud.fullname" . }}-postgres"] + command: ["kubectl", "delete" , "deployment", "{{ template "common.names.fullname" . }}", "{{ template "common.names.fullname" $values }}"] {{- end -}} diff --git a/test/nextcloud/1.1.0/templates/postgres-deployment.yaml b/test/nextcloud/1.1.0/templates/postgres-deployment.yaml index 2b1699f8f4..0c6e480c3a 100644 --- a/test/nextcloud/1.1.0/templates/postgres-deployment.yaml +++ b/test/nextcloud/1.1.0/templates/postgres-deployment.yaml @@ -20,11 +20,7 @@ spec: - name: {{ .Chart.Name }}-postgres image: {{ template "postgres.imageName" . }} imagePullPolicy: {{ .Values.image.pullPolicy }} - env: - {{ $envList := list }} - {{ $envList = mustAppend $envList (dict "name" "POSTGRES_USER" "valueFromSecret" true "secretName" "db-details" "secretKey" "db-user") }} - {{ $envList = mustAppend $envList (dict "name" "POSTGRES_PASSWORD" "valueFromSecret" true "secretName" "db-details" "secretKey" "db-password") }} - {{ include "common.containers.environmentVariables" (dict "environmentVariables" $envList) | nindent 10 }} + env: {{ include "postgres.envVariableConfiguration" $values | nindent 10 }} volumeMounts: - name: postgres-data mountPath: /var/lib/postgresql/data @@ -34,8 +30,4 @@ spec: - name: postgres-tcp containerPort: 5432 protocol: TCP - volumes: - {{ $vols := list }} - {{ $vols = mustAppend $vols (dict "name" "postgres-data" "emptyDirVolumes" .Values.emptyDirVolumes "hostPathEnabled" false "pathField" nil "datasetName" (.Values.postgresDataVolume | default dict).datasetName ) }} - {{ $vols = mustAppend $vols (dict "name" "postgres-backup" "emptyDirVolumes" .Values.emptyDirVolumes "hostPathEnabled" false "pathField" nil "datasetName" (.Values.postgresBackupVolume | default dict).datasetName ) }} - {{ include "common.storage.volumesConfiguration" (dict "ixVolumes" .Values.ixVolumes "volumes" $vols) | nindent 8 }} + volumes: {{ include "postgres.volumeConfiguration" $values | nindent 8 }}