-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cluster): Recovery using pg_basebackup (#252)
feat(cluster): Recovery using pg_basebackup (#252) --------- Signed-off-by: Pieter van der Giessen <[email protected]> Signed-off-by: Itay Grudev <[email protected]> Co-authored-by: Itay Grudev <[email protected]>
- Loading branch information
1 parent
17cb83c
commit c14ed18
Showing
18 changed files
with
409 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
mode: "recovery" | ||
|
||
recovery: | ||
method: "pg_basebackup" | ||
pgBaseBackup: | ||
sourceHost: "source-db.foo.com" | ||
sourceUsername: "streaming_replica" | ||
existingPasswordSecret: "source-db-replica-password" | ||
|
||
cluster: | ||
instances: 1 | ||
|
||
backups: | ||
enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
charts/cluster/templates/recovery-pg_basebackup-password.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{{- if and (eq .Values.mode "recovery") (eq .Values.recovery.method "pg_basebackup") .Values.recovery.pgBaseBackup.source.passwordSecret.create }} | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: {{ default (printf "%s-pg-basebackup-password" (include "cluster.fullname" .)) .Values.recovery.pgBaseBackup.source.passwordSecret.name }} | ||
data: | ||
{{ .Values.recovery.pgBaseBackup.source.passwordSecret.key }}: {{ required ".Values.recovery.pgBaseBackup.source.passwordSecret.value required when creating a password secret." .Values.recovery.pgBaseBackup.source.passwordSecret.value | b64enc | quote }} | ||
{{- end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
charts/cluster/test/postgresql-pg_basebackup/00-source-cluster-assert.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: postgresql.cnpg.io/v1 | ||
kind: Cluster | ||
metadata: | ||
name: source-cluster | ||
status: | ||
readyInstances: 1 |
5 changes: 5 additions & 0 deletions
5
charts/cluster/test/postgresql-pg_basebackup/00-source-cluster.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mode: "standalone" | ||
cluster: | ||
instances: 1 | ||
backups: | ||
enabled: false |
6 changes: 6 additions & 0 deletions
6
charts/cluster/test/postgresql-pg_basebackup/01-data_write-assert.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: data-write | ||
status: | ||
succeeded: 1 |
30 changes: 30 additions & 0 deletions
30
charts/cluster/test/postgresql-pg_basebackup/01-data_write.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: data-write | ||
spec: | ||
template: | ||
spec: | ||
restartPolicy: OnFailure | ||
containers: | ||
- name: data-write | ||
env: | ||
- name: DB_USER | ||
valueFrom: | ||
secretKeyRef: | ||
name: source-cluster-superuser | ||
key: username | ||
- name: DB_PASS | ||
valueFrom: | ||
secretKeyRef: | ||
name: source-cluster-superuser | ||
key: password | ||
- name: DB_URI | ||
value: postgres://$(DB_USER):$(DB_PASS)@source-cluster-rw:5432 | ||
image: alpine:3.19 | ||
command: ['sh', '-c'] | ||
args: | ||
- | | ||
apk --no-cache add postgresql-client kubectl | ||
psql "$DB_URI" -c "CREATE DATABASE mygooddb;" | ||
psql "$DB_URI/mygooddb" -c "CREATE TABLE mygoodtable (id serial PRIMARY KEY);" |
6 changes: 6 additions & 0 deletions
6
charts/cluster/test/postgresql-pg_basebackup/02-pg_basebackup-cluster-assert.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: postgresql.cnpg.io/v1 | ||
kind: Cluster | ||
metadata: | ||
name: pg-basebackup-cluster | ||
status: | ||
readyInstances: 2 |
21 changes: 21 additions & 0 deletions
21
charts/cluster/test/postgresql-pg_basebackup/02-pg_basebackup-cluster.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
mode: "recovery" | ||
recovery: | ||
method: "pg_basebackup" | ||
pgBaseBackup: | ||
source: | ||
host: "source-cluster-rw" | ||
database: "mygooddb" | ||
username: "streaming_replica" | ||
sslMode: "require" | ||
sslKeySecret: | ||
name: source-cluster-replication | ||
key: tls.key | ||
sslCertSecret: | ||
name: source-cluster-replication | ||
key: tls.crt | ||
|
||
cluster: | ||
instances: 2 | ||
|
||
backups: | ||
enabled: false |
6 changes: 6 additions & 0 deletions
6
charts/cluster/test/postgresql-pg_basebackup/03-data_test-assert.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: data-test | ||
status: | ||
succeeded: 1 |
23 changes: 23 additions & 0 deletions
23
charts/cluster/test/postgresql-pg_basebackup/03-data_test.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: data-test | ||
spec: | ||
template: | ||
spec: | ||
restartPolicy: OnFailure | ||
containers: | ||
- name: data-test | ||
env: | ||
- name: DB_URI | ||
valueFrom: | ||
secretKeyRef: | ||
name: pg-basebackup-cluster-superuser | ||
key: uri | ||
image: alpine:3.19 | ||
command: ['sh', '-c'] | ||
args: | ||
- | | ||
apk --no-cache add postgresql-client | ||
DB_URI=$(echo $DB_URI | sed "s|/\*|/|" ) | ||
test "$(psql "${DB_URI}mygooddb" -t -c 'SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = $$mygoodtable$$)' --csv -q 2>/dev/null)" = "t" |
64 changes: 64 additions & 0 deletions
64
charts/cluster/test/postgresql-pg_basebackup/chainsaw-test.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
## | ||
# This is a test that provisions a regular (non CNPG) PostgreSQL cluster and attempts to perform a pg_basebackup recovery. | ||
apiVersion: chainsaw.kyverno.io/v1alpha1 | ||
kind: Test | ||
metadata: | ||
name: postgresql-pg-basebackup | ||
spec: | ||
timeouts: | ||
apply: 1s | ||
assert: 2m | ||
cleanup: 1m | ||
steps: | ||
- name: Install the external PostgreSQL cluster | ||
try: | ||
- script: | ||
content: | | ||
helm upgrade \ | ||
--install \ | ||
--namespace $NAMESPACE \ | ||
--values ./00-source-cluster.yaml \ | ||
--wait \ | ||
source ../../ | ||
- assert: | ||
file: ./00-source-cluster-assert.yaml | ||
- apply: | ||
file: ./01-data_write.yaml | ||
- assert: | ||
file: ./01-data_write-assert.yaml | ||
- name: Install the pg_basebackup cluster | ||
timeouts: | ||
assert: 5m | ||
try: | ||
- script: | ||
content: | | ||
helm upgrade \ | ||
--install \ | ||
--namespace $NAMESPACE \ | ||
--values ./02-pg_basebackup-cluster.yaml \ | ||
--wait \ | ||
pg-basebackup ../../ | ||
- assert: | ||
file: ./02-pg_basebackup-cluster-assert.yaml | ||
catch: | ||
- describe: | ||
apiVersion: postgresql.cnpg.io/v1 | ||
kind: Cluster | ||
- name: Verify the data from step 1 exists | ||
try: | ||
- apply: | ||
file: ./03-data_test.yaml | ||
- assert: | ||
file: ./03-data_test-assert.yaml | ||
catch: | ||
- describe: | ||
apiVersion: batch/v1 | ||
kind: Job | ||
- podLogs: | ||
selector: batch.kubernetes.io/job-name=data-test | ||
- name: Cleanup | ||
try: | ||
- script: | ||
content: | | ||
helm uninstall --namespace $NAMESPACE source | ||
helm uninstall --namespace $NAMESPACE pg-basebackup |
Oops, something went wrong.