From ab147f31dc666d229f45c1206a3e9586fa5f8a83 Mon Sep 17 00:00:00 2001 From: Kjeld Schouten-Lebbing Date: Tue, 28 Feb 2023 11:11:16 +0100 Subject: [PATCH] feat(common): allow postgres -> cnpg migration (#329) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** Add functionality to migrate old postgresql databases to cnpg. This is not guaranteed(!) to work for every app and every database, but it offers a semi-decent option to at least TRY to migrate, instead of forcing users to do this manually **โš™๏ธ Type of change** - [x] โš™๏ธ Feature/App addition - [ ] ๐Ÿช› Bugfix - [ ] โš ๏ธ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [x] ๐Ÿ”ƒ Refactor of current code **๐Ÿงช How Has This Been Tested?** **๐Ÿ“ƒ Notes:** See: https://cloudnative-pg.io/documentation/1.18/bootstrap/ **โœ”๏ธ Checklist:** - [x] โš–๏ธ My code follows the style guidelines of this project - [x] ๐Ÿ‘€ I have performed a self-review of my own code - [x] #๏ธโƒฃ I have commented my code, particularly in hard-to-understand areas - [x] ๐Ÿ“„ I have made corresponding changes to the documentation - [x] โš ๏ธ My changes generate no new warnings - [x] ๐Ÿงช I have added tests to this description that prove my fix is effective or that my feature works - [x] โฌ†๏ธ I increased versions for any altered app according to semantic versioning **โž• App addition** If this PR is an app addition please make sure you have done the following. - [x] ๐Ÿชž I have opened a PR on [truecharts/containers](https://github.com/truecharts/containers) adding the container to TrueCharts mirror repo. - [ ] ๐Ÿ–ผ๏ธ I have added an icon in the Chart's root directory called `icon.png` --- _Please don't blindly check all the boxes. Read them and only check those that apply. Those checkboxes are there for the reviewer to see what is this all about and the status of this PR with a quick glance._ --- library/common-test/Chart.yaml | 2 +- library/common/Chart.yaml | 2 +- .../common/templates/class/_cnpgCluster.tpl | 21 +++++++++++++++++++ library/common/templates/spawner/_cnpg.tpl | 14 +++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/library/common-test/Chart.yaml b/library/common-test/Chart.yaml index 832e4b6f..8686baeb 100644 --- a/library/common-test/Chart.yaml +++ b/library/common-test/Chart.yaml @@ -3,7 +3,7 @@ appVersion: "" dependencies: - name: common repository: file://../common - version: ~12.1.0 + version: ~12.2.0 deprecated: false description: Helper chart to test different use cases of the common library home: https://github.com/truecharts/apps/tree/master/charts/library/common-test diff --git a/library/common/Chart.yaml b/library/common/Chart.yaml index 0750be71..3214ccfc 100644 --- a/library/common/Chart.yaml +++ b/library/common/Chart.yaml @@ -15,4 +15,4 @@ maintainers: name: common sources: null type: library -version: 12.1.0 +version: 12.2.0 diff --git a/library/common/templates/class/_cnpgCluster.tpl b/library/common/templates/class/_cnpgCluster.tpl index b96b6821..0095707a 100644 --- a/library/common/templates/class/_cnpgCluster.tpl +++ b/library/common/templates/class/_cnpgCluster.tpl @@ -31,8 +31,18 @@ spec: superuserSecret: name: {{ $cnpgClusterName }}-superuser + {{- $basename := include "tc.v1.common.lib.chart.names.fullname" $ -}} + {{- $fetchname := printf "%s-dbcreds" $basename -}} + {{- $olddbprevious1 := lookup "v1" "Secret" .Release.Namespace $fetchname }} + {{- $olddbprevious2 := lookup "v1" "Secret" .Release.Namespace "dbcreds" }} + bootstrap: + {{- if and $.Values.postgresql.enabled ( or $olddbprevious1 $olddbprevious2 ) $.Release.IsUpgrade }} + pg_basebackup: + source: old-db + {{- else }} initdb: + {{- end }} database: {{ $values.database | default "app" }} owner: {{ $values.user | default "app" }} secret: @@ -69,4 +79,15 @@ spec: inProgress: false reusePVC: on + externalClusters: + {{- if and $.Values.postgresql.enabled ( or $olddbprevious1 $olddbprevious2 ) $.Release.IsUpgrade }} + - name: old-db + connectionParameters: + host: {{ (printf "%v-%v" .Release.Name "postgresql" ) }} + user: postgres + password: + name: {{ $cnpgClusterName }}-superuser + key: password + {{- end }} + {{- end -}} diff --git a/library/common/templates/spawner/_cnpg.tpl b/library/common/templates/spawner/_cnpg.tpl index 90bc0fd5..9123805b 100644 --- a/library/common/templates/spawner/_cnpg.tpl +++ b/library/common/templates/spawner/_cnpg.tpl @@ -26,11 +26,21 @@ {{- include "tc.v1.common.class.cnpg.pooler" $ -}} {{- end }} + {{- $basename := include "tc.v1.common.lib.chart.names.fullname" $ -}} + {{- $fetchname := printf "%s-dbcreds" $basename -}} + {{- $olddbprevious1 := lookup "v1" "Secret" $.Release.Namespace $fetchname }} + {{- $olddbprevious2 := lookup "v1" "Secret" $.Release.Namespace "dbcreds" }} + + {{/* Inject the required secrets */}} {{- $dbPass := "" }} {{- $dbprevious := lookup "v1" "Secret" $.Release.Namespace ( printf "%s-user" $cnpgValues.name ) }} {{- if $dbprevious }} {{- $dbPass = ( index $dbprevious.data "user-password" ) | b64dec }} + {{- else if and $.Values.postgresql.enabled $olddbprevious1 $.Release.IsUpgrade }} + {{- $dbPass = ( index $olddbprevious1.data "postgresql-password" ) | b64dec }} + {{- else if and $.Values.postgresql.enabled $olddbprevious2 $.Release.IsUpgrade }} + {{- $dbPass = ( index $olddbprevious2.data "postgresql-password" ) | b64dec }} {{- else }} {{- $dbPass = $cnpgValues.password | default ( randAlphaNum 62 ) }} {{- end }} @@ -39,6 +49,10 @@ {{- $pgprevious := lookup "v1" "Secret" $.Release.Namespace ( printf "%s-superuser" $cnpgValues.name ) }} {{- if $pgprevious }} {{- $pgPass = ( index $dbprevious.data "superuser-password" ) | b64dec }} + {{- else if and $.Values.postgresql.enabled $olddbprevious1 $.Release.IsUpgrade }} + {{- $pgPass = ( index $olddbprevious1.data "postgresql-postgres-password" ) | b64dec }} + {{- else if and $.Values.postgresql.enabled $olddbprevious2 $.Release.IsUpgrade }} + {{- $pgPass = ( index $olddbprevious2.data "postgresql-postgres-password" ) | b64dec }} {{- else }} {{- $pgPass = $cnpgValues.superUserPassword | default ( randAlphaNum 62 ) }} {{- end }}