From e52e28fa3c753c046c13e9a87b7854a6cc101f1b Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Date: Sun, 13 Dec 2020 23:39:37 +0500 Subject: [PATCH] Use unique name for backup --- test/nextcloud/1.0.0/templates/_postgres.tpl | 8 +++++++ .../templates/backup-postgres-config.yaml | 15 +++++++++++++ .../1.0.0/templates/backup-postgres-hook.yaml | 21 +++++-------------- .../nextcloud/1.0.0/templates/deployment.yaml | 2 +- .../templates/nuke-deployments-hook.yaml | 3 +++ 5 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 test/nextcloud/1.0.0/templates/backup-postgres-config.yaml diff --git a/test/nextcloud/1.0.0/templates/_postgres.tpl b/test/nextcloud/1.0.0/templates/_postgres.tpl index bb0ef0d0d0d..8889f105714 100644 --- a/test/nextcloud/1.0.0/templates/_postgres.tpl +++ b/test/nextcloud/1.0.0/templates/_postgres.tpl @@ -13,4 +13,12 @@ app.kubernetes.io/name: {{ include "nextcloud.name" . }}-postgres app.kubernetes.io/instance: {{ .Release.Name }}-postgres {{- end }} +{{/* +Retrieve postgres backup name +This will return a unique name based on revision and chart numbers specified. +*/}} +{{- define "postgres.backupName" -}} +{{- $upgradeDict := .Values.ixChartContext.upgradeMetadata -}} +{{- printf "postgres-backup-from-%s-to-%s-revision-%d" $upgradeDict.oldChartVersion $upgradeDict.newChartVersion (int64 $upgradeDict.preUpgradeRevision) -}} +{{- end }} diff --git a/test/nextcloud/1.0.0/templates/backup-postgres-config.yaml b/test/nextcloud/1.0.0/templates/backup-postgres-config.yaml new file mode 100644 index 00000000000..c9ccf66dbaa --- /dev/null +++ b/test/nextcloud/1.0.0/templates/backup-postgres-config.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: "postgres-backup-hook-config-map" + annotations: + rollme: {{ randAlphaNum 5 | quote }} +data: + entrypoint.sh: |- + #!/bin/sh + cmd="/docker-entrypoint.sh postgres" + eval "${cmd}" & disown; + until pg_isready; do + sleep 5; + done; + pg_dump -U $POSTGRES_USER -d {{ template "postgres.DatabaseName" . }} > /postgres_backups/$BACKUP_NAME; diff --git a/test/nextcloud/1.0.0/templates/backup-postgres-hook.yaml b/test/nextcloud/1.0.0/templates/backup-postgres-hook.yaml index 5c02335f47e..5601f8b089c 100644 --- a/test/nextcloud/1.0.0/templates/backup-postgres-hook.yaml +++ b/test/nextcloud/1.0.0/templates/backup-postgres-hook.yaml @@ -1,19 +1,4 @@ -apiVersion: v1 -kind: ConfigMap -metadata: - name: "postgres-backup-hook-config-map" -data: - entrypoint.sh: |- - #!/bin/sh - cmd="/docker-entrypoint.sh postgres" - eval "${cmd}" & disown; - until pg_isready; do - sleep 5; - done; - pg_dump -U $POSTGRES_USER -d {{ template "postgres.DatabaseName"}} > /postgres_backups/update_dump; - ---- - +{{- if .Values.ixChartContext.isUpgrade -}} apiVersion: batch/v1 kind: Job metadata: @@ -22,6 +7,7 @@ metadata: "helm.sh/hook": pre-upgrade "helm.sh/hook-weight": "1" "helm.sh/hook-delete-policy": hook-succeeded + rollme: {{ randAlphaNum 5 | quote }} spec: template: metadata: @@ -44,6 +30,8 @@ spec: secretKeyRef: name: db-details key: db-password + - name: BACKUP_NAME + value: {{ template "postgres.backupName" . }} volumeMounts: - name: postgres-data mountPath: /var/lib/postgresql/data @@ -66,3 +54,4 @@ spec: configMap: defaultMode: 0700 name: "postgres-backup-hook-config-map" +{{- end -}} diff --git a/test/nextcloud/1.0.0/templates/deployment.yaml b/test/nextcloud/1.0.0/templates/deployment.yaml index effca727e56..643e9826ea6 100644 --- a/test/nextcloud/1.0.0/templates/deployment.yaml +++ b/test/nextcloud/1.0.0/templates/deployment.yaml @@ -49,7 +49,7 @@ spec: - name: POSTGRES_HOST value: {{ template "nextcloud.fullname" . }}-postgres:5432 - name: POSTGRES_DB - value: {{ template "postgres.DatabaseName" }} + value: {{ template "postgres.DatabaseName" . }} - name: POSTGRES_USER valueFrom: secretKeyRef: diff --git a/test/nextcloud/1.0.0/templates/nuke-deployments-hook.yaml b/test/nextcloud/1.0.0/templates/nuke-deployments-hook.yaml index dbab1f52e5c..7307db6c1d6 100644 --- a/test/nextcloud/1.0.0/templates/nuke-deployments-hook.yaml +++ b/test/nextcloud/1.0.0/templates/nuke-deployments-hook.yaml @@ -1,3 +1,4 @@ +{{- if .Values.ixChartContext.isUpgrade -}} apiVersion: batch/v1 kind: Job metadata: @@ -6,6 +7,7 @@ metadata: "helm.sh/hook": pre-upgrade "helm.sh/hook-weight": "-1" "helm.sh/hook-delete-policy": hook-succeeded + rollme: {{ randAlphaNum 5 | quote }} spec: template: metadata: @@ -17,3 +19,4 @@ spec: - name: kubectl image: "bitnami/kubectl:1.19" command: ["kubectl", "delete" , "deployment", "{{ template "nextcloud.fullname" . }}", "{{ template "nextcloud.fullname" . }}-postgres"] +{{- end -}}