Skip to content

Commit

Permalink
Implemented recovery.method=import
Browse files Browse the repository at this point in the history
  • Loading branch information
itay-grudev committed Nov 6, 2024
1 parent d103e8b commit 9849711
Show file tree
Hide file tree
Showing 22 changed files with 496 additions and 31 deletions.
57 changes: 29 additions & 28 deletions charts/paradedb/templates/_bootstrap.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
bootstrap:
initdb:
{{- with .Values.cluster.initdb }}
{{- with (omit . "postInitSQL" "postInitApplicationSQL" "postInitTemplateSQL" "owner") }}
{{- with (omit . "postInitSQL" "postInitApplicationSQL" "postInitTemplateSQL" "owner" "import") }}
{{- . | toYaml | nindent 4 }}
{{- end }}
{{- end }}
Expand Down Expand Up @@ -70,33 +70,34 @@ bootstrap:
{{- end }}

externalClusters:
- name: pgBaseBackupSource
connectionParameters:
host: {{ .Values.recovery.pgBaseBackup.source.host | quote }}
port: {{ .Values.recovery.pgBaseBackup.source.port | quote }}
user: {{ .Values.recovery.pgBaseBackup.source.username | quote }}
dbname: {{ .Values.recovery.pgBaseBackup.source.database | quote }}
sslmode: {{ .Values.recovery.pgBaseBackup.source.sslMode | quote }}
{{- if .Values.recovery.pgBaseBackup.source.passwordSecret.name }}
password:
name: {{ default (printf "%s-pg-basebackup-password" (include "cluster.fullname" .)) .Values.recovery.pgBaseBackup.source.passwordSecret.name }}
key: {{ .Values.recovery.pgBaseBackup.source.passwordSecret.key }}
{{- end }}
{{- if .Values.recovery.pgBaseBackup.source.sslKeySecret.name }}
sslKey:
name: {{ .Values.recovery.pgBaseBackup.source.sslKeySecret.name }}
key: {{ .Values.recovery.pgBaseBackup.source.sslKeySecret.key }}
{{- end }}
{{- if .Values.recovery.pgBaseBackup.source.sslCertSecret.name }}
sslCert:
name: {{ .Values.recovery.pgBaseBackup.source.sslCertSecret.name }}
key: {{ .Values.recovery.pgBaseBackup.source.sslCertSecret.key }}
{{- end }}
{{- if .Values.recovery.pgBaseBackup.source.sslRootCertSecret.name }}
sslRootCert:
name: {{ .Values.recovery.pgBaseBackup.source.sslRootCertSecret.name }}
key: {{ .Values.recovery.pgBaseBackup.source.sslRootCertSecret.key }}
{{- end }}
{{- include "cluster.externalSourceCluster" (list "pgBaseBackupSource" .Values.recovery.pgBaseBackup.source) | nindent 2 }}

{{- else if eq .Values.recovery.method "import" }}
initdb:
{{- with .Values.cluster.initdb }}
{{- with (omit . "owner" "import") }}
{{- . | toYaml | nindent 4 }}
{{- end }}
{{- end }}
{{- if .Values.cluster.initdb.owner }}
owner: {{ tpl .Values.cluster.initdb.owner . }}
{{- end }}
import:
source:
externalCluster: importSource
type: {{ .Values.recovery.import.type }}
databases: {{ .Values.recovery.import.databases | toJson }}
{{ with .Values.recovery.import.roles }}
roles: {{ . | toJson }}
{{- end }}
{{ with .Values.recovery.import.postImportApplicationSQL }}
postImportApplicationSQL:
{{- . | toYaml | nindent 6 }}
{{- end }}
schemaOnly: {{ .Values.recovery.import.schemaOnly }}

externalClusters:
{{- include "cluster.externalSourceCluster" (list "importSource" .Values.recovery.import.source) | nindent 2 }}

{{- else }}
recovery:
Expand Down
33 changes: 33 additions & 0 deletions charts/paradedb/templates/_external_source_cluster.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{- define "cluster.externalSourceCluster" -}}
{{- $name := first . -}}
{{- $config := last . -}}
- name: {{ first . }}
connectionParameters:
host: {{ $config.host | quote }}
port: {{ $config.port | quote }}
user: {{ $config.username | quote }}
{{- with $config.database }}
dbname: {{ . | quote }}
{{- end }}
sslmode: {{ $config.sslMode | quote }}
{{- if $config.passwordSecret.name }}
password:
name: {{ $config.passwordSecret.name }}
key: {{ $config.passwordSecret.key }}
{{- end }}
{{- if $config.sslKeySecret.name }}
sslKey:
name: {{ $config.sslKeySecret.name }}
key: {{ $config.sslKeySecret.key }}
{{- end }}
{{- if $config.sslCertSecret.name }}
sslCert:
name: {{ $config.sslCertSecret.name }}
key: {{ $config.sslCertSecret.key }}
{{- end }}
{{- if $config.sslRootCertSecret.name }}
sslRootCert:
name: {{ $config.sslRootCertSecret.name }}
key: {{ $config.sslRootCertSecret.key }}
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ spec:
- CREATE TABLE mytable (id serial PRIMARY KEY, name VARCHAR(255));
superuserSecret:
name: supersecret-secret
key: password
enableSuperuserAccess: true
certificates:
serverCASecret: ca-secret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ cluster:
replicationTLSSecret: replication-tls-secret
clientCASecret: client-ca-secret
enableSuperuserAccess: true
superuserSecret: supersecret-secret
superuserSecret:
name: supersecret-secret
key: password
roles:
- name: dante
ensure: present
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: source-paradedb
status:
readyInstances: 1
9 changes: 9 additions & 0 deletions charts/paradedb/test/postgresql-import/00-source-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type: postgresql
mode: "standalone"
cluster:
instances: 1
superuserSecret: source-paradedb-superuser
storage:
size: 256Mi
backups:
enabled: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Secret
metadata:
name: source-paradedb-superuser
type: Opaque
data:
username: "cG9zdGdyZXM="
password: "cG9zdGdyZXM="
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
31 changes: 31 additions & 0 deletions charts/paradedb/test/postgresql-import/01-data_write.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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-paradedb-superuser
key: username
- name: DB_PASS
valueFrom:
secretKeyRef:
name: source-paradedb-superuser
key: password
- name: DB_URI
value: postgres://$(DB_USER):$(DB_PASS)@source-paradedb-rw:5432
image: alpine:3.19
command: ['sh', '-c']
args:
- |
apk --no-cache add postgresql-client
psql "$DB_URI" -c "CREATE DATABASE mygooddb;"
psql "$DB_URI/mygooddb" -c "CREATE TABLE mygoodtable (id serial PRIMARY KEY);"
psql "$DB_URI/mygooddb" -c "INSERT INTO mygoodtable VALUES (314159265);"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: import-paradedb
status:
readyInstances: 2
30 changes: 30 additions & 0 deletions charts/paradedb/test/postgresql-import/02-import-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
type: postgresql
mode: "recovery"
recovery:
method: "import"
import:
type: "microservice"
databases: [ "mygooddb" ]
source:
host: "source-paradedb-rw"
username: "postgres"
passwordSecret:
name: source-paradedb-superuser
key: password
sslMode: "require"
sslKeySecret:
name: source-paradedb-replication
key: tls.key
sslCertSecret:
name: source-paradedb-replication
key: tls.crt

cluster:
instances: 2
storage:
size: 256Mi
initdb:
database: mygooddb

backups:
enabled: false
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
24 changes: 24 additions & 0 deletions charts/paradedb/test/postgresql-import/03-data_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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: import-paradedb-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"
test "$(psql "${DB_URI}mygooddb" -t -c 'SELECT EXISTS (SELECT FROM mygoodtable WHERE id = 314159265)' --csv -q 2>/dev/null)" = "t"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: import-schemaonly-paradedb
status:
readyInstances: 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
type: postgresql
mode: "recovery"
recovery:
method: "import"
import:
type: "microservice"
databases: [ "mygooddb" ]
schemaOnly: true
source:
host: "source-paradedb-rw"
username: "postgres"
passwordSecret:
name: source-paradedb-superuser
key: password
sslMode: "require"
sslKeySecret:
name: source-paradedb-replication
key: tls.key
sslCertSecret:
name: source-paradedb-replication
key: tls.crt

cluster:
instances: 2
storage:
size: 256Mi
initdb:
database: mygooddb

backups:
enabled: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: batch/v1
kind: Job
metadata:
name: data-test-schemaonly
status:
succeeded: 1
24 changes: 24 additions & 0 deletions charts/paradedb/test/postgresql-import/05-data_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: batch/v1
kind: Job
metadata:
name: data-test-schemaonly
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: data-test
env:
- name: DB_URI
valueFrom:
secretKeyRef:
name: import-schemaonly-paradedb-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"
test "$(psql "${DB_URI}mygooddb" -t -c 'SELECT EXISTS (SELECT FROM mygoodtable WHERE id = 314159265)' --csv -q 2>/dev/null)" = "f"
Loading

0 comments on commit 9849711

Please sign in to comment.