Skip to content

Commit 9fc7be1

Browse files
authored
feat(cluster): Database, extension and schema management (#612)
Signed-off-by: Itay Grudev <itay@verito.digital>
1 parent 8b9b076 commit 9fc7be1

File tree

7 files changed

+289
-0
lines changed

7 files changed

+289
-0
lines changed

charts/cluster/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ refer to the [CloudNativePG Documentation](https://cloudnative-pg.io/documentat
194194
| cluster.walStorage.enabled | bool | `false` | |
195195
| cluster.walStorage.size | string | `"1Gi"` | |
196196
| cluster.walStorage.storageClass | string | `""` | |
197+
| databases | list | `[]` | |
197198
| fullnameOverride | string | `""` | Override the full name of the chart |
198199
| imageCatalog.create | bool | `true` | Whether to provision an image catalog. If imageCatalog.images is empty this option will be ignored. |
199200
| imageCatalog.images | list | `[]` | List of images to be provisioned in an image catalog. |
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{{- range .Values.databases }}
2+
---
3+
apiVersion: postgresql.cnpg.io/v1
4+
kind: Database
5+
metadata:
6+
name: {{ include "cluster.fullname" $ }}-{{ .name | replace "_" "-" }}
7+
namespace: {{ include "cluster.namespace" $ }}
8+
{{- with $.Values.cluster.annotations }}
9+
annotations:
10+
{{- toYaml . | nindent 4 }}
11+
{{- end }}
12+
labels:
13+
{{- include "cluster.labels" $ | nindent 4 }}
14+
{{- with $.Values.cluster.additionalLabels }}
15+
{{ toYaml . | nindent 4 }}
16+
{{- end }}
17+
spec:
18+
name: {{ .name }}
19+
cluster:
20+
name: {{ include "cluster.fullname" $ }}
21+
ensure: {{ .ensure | default "present" }}
22+
owner: {{ .owner }}
23+
template: {{ .template | default "template1" }}
24+
encoding: {{ .encoding | default "UTF8" }}
25+
databaseReclaimPolicy: {{ .databaseReclaimPolicy | default "retain" }}
26+
{{- with .isTemplate }}
27+
isTemplate: {{ . }}
28+
{{- end }}
29+
{{- with .allowConnections }}
30+
allowConnections: {{ . }}
31+
{{- end }}
32+
{{- with .connectionLimit }}
33+
connectionLimit: {{ . }}
34+
{{- end }}
35+
{{- with .tablespace }}
36+
tablespace: {{ . }}
37+
{{- end }}
38+
{{- with .locale }}
39+
locale: {{ . }}
40+
{{- end }}
41+
{{- with .localeProvider }}
42+
localeProvider: {{ . }}
43+
{{- end }}
44+
{{- with .localeCollate }}
45+
localeCollate: {{ . }}
46+
{{- end }}
47+
{{- with .localeCType }}
48+
localeCType: {{ . }}
49+
{{- end }}
50+
{{- with .icuLocale }}
51+
icuLocale: {{ . }}
52+
{{- end }}
53+
{{- with .icuRules }}
54+
icuRules: {{ . }}
55+
{{- end }}
56+
{{- with .builtinLocale }}
57+
builtinLocale: {{ . }}
58+
{{- end }}
59+
{{- with .collationVersion }}
60+
collationVersion: {{ . | quote }}
61+
{{- end }}
62+
{{- with .schemas }}
63+
schemas:
64+
{{- range . }}
65+
- name: {{ .name }}
66+
owner: {{ .owner }}
67+
ensure: {{ .ensure | default "present" }}
68+
{{- end }}
69+
{{- end }}
70+
{{- with .extensions }}
71+
extensions:
72+
{{- range . }}
73+
- name: {{ .name }}
74+
{{- with .version }}
75+
version: {{ . }}
76+
{{- end }}
77+
{{- with .schema }}
78+
schema: {{ . }}
79+
{{- end }}
80+
ensure: {{ .ensure | default "present" }}
81+
{{- end }}
82+
{{- end }}
83+
{{- end }}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
apiVersion: postgresql.cnpg.io/v1
2+
kind: Database
3+
metadata:
4+
name: database-parameters-cluster-default-db
5+
spec:
6+
name: default_db
7+
cluster:
8+
name: database-parameters-cluster
9+
ensure: present
10+
owner: test-owner
11+
template: template1
12+
encoding: UTF8
13+
databaseReclaimPolicy: retain
14+
---
15+
apiVersion: postgresql.cnpg.io/v1
16+
kind: Database
17+
metadata:
18+
name: database-parameters-cluster-test-db-icu
19+
spec:
20+
name: test-db-icu
21+
cluster:
22+
name: database-parameters-cluster
23+
ensure: present
24+
owner: test-owner
25+
template: template0
26+
encoding: UTF16
27+
connectionLimit: 100
28+
tablespace: test-space
29+
databaseReclaimPolicy: delete
30+
isTemplate: true
31+
locale: "en_GB.utf8"
32+
localeProvider: icu
33+
localeCollate: "en_GB.utf8"
34+
localeCType: "en_GB.utf8"
35+
icuLocale: "en_GB"
36+
icuRules: "en_GB"
37+
collationVersion: "1"
38+
schemas:
39+
- name: test-schema
40+
owner: test-owner
41+
ensure: absent
42+
extensions:
43+
- name: pg_search
44+
ensure: absent
45+
version: "0.15.21"
46+
schema: test-schema
47+
48+
---
49+
apiVersion: postgresql.cnpg.io/v1
50+
kind: Database
51+
metadata:
52+
name: database-parameters-cluster-test-db-builtin
53+
spec:
54+
name: test-db-builtin
55+
cluster:
56+
name: database-parameters-cluster
57+
ensure: present
58+
owner: test-owner
59+
template: template0
60+
encoding: UTF16
61+
connectionLimit: 100
62+
tablespace: test-space
63+
databaseReclaimPolicy: delete
64+
isTemplate: true
65+
locale: "en_GB.utf8"
66+
localeProvider: builtin
67+
localeCollate: "en_GB.utf8"
68+
localeCType: "en_GB.utf8"
69+
builtinLocale: "en_GB.utf8"
70+
collationVersion: "1"
71+
schemas:
72+
- name: test-schema
73+
owner: test-owner
74+
ensure: absent
75+
extensions:
76+
- name: pg_search
77+
ensure: absent
78+
version: "0.15.21"
79+
schema: test-schema
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
type: postgresql
2+
version:
3+
postgresql: "17"
4+
5+
cluster:
6+
instances: 1
7+
8+
databases:
9+
- name: default_db
10+
ensure: present
11+
owner: test-owner
12+
13+
- name: test-db-icu
14+
ensure: present
15+
owner: test-owner
16+
template: template0
17+
encoding: UTF16
18+
connectionLimit: 100
19+
tablespace: test-space
20+
databaseReclaimPolicy: delete
21+
isTemplate: true
22+
locale: "en_GB.utf8"
23+
localeProvider: icu
24+
localeCollate: "en_GB.utf8"
25+
localeCType: "en_GB.utf8"
26+
icuLocale: "en_GB"
27+
icuRules: "en_GB"
28+
collationVersion: "1"
29+
schemas:
30+
- name: test-schema
31+
owner: test-owner
32+
ensure: absent
33+
extensions:
34+
- name: pg_search
35+
ensure: absent
36+
version: "0.15.21"
37+
schema: test-schema
38+
39+
- name: test-db-builtin
40+
ensure: present
41+
owner: test-owner
42+
template: template0
43+
encoding: UTF16
44+
connectionLimit: 100
45+
tablespace: test-space
46+
databaseReclaimPolicy: delete
47+
isTemplate: true
48+
locale: "en_GB.utf8"
49+
localeProvider: builtin
50+
localeCollate: "en_GB.utf8"
51+
localeCType: "en_GB.utf8"
52+
builtinLocale: "en_GB.utf8"
53+
collationVersion: "1"
54+
schemas:
55+
- name: test-schema
56+
owner: test-owner
57+
ensure: absent
58+
extensions:
59+
- name: pg_search
60+
ensure: absent
61+
version: "0.15.21"
62+
schema: test-schema
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apiVersion: chainsaw.kyverno.io/v1alpha1
2+
kind: Test
3+
metadata:
4+
name: database-management
5+
spec:
6+
timeouts:
7+
apply: 1s
8+
assert: 300s
9+
cleanup: 60s
10+
steps:
11+
- name: database-parameters
12+
timeouts:
13+
apply: 1s
14+
assert: 5s
15+
cleanup: 30s
16+
try:
17+
- script:
18+
content: |
19+
helm upgrade \
20+
--install \
21+
--namespace $NAMESPACE \
22+
--values ./01-database-parameters.yaml \
23+
--wait \
24+
database-parameters ../../
25+
- assert:
26+
file: ./01-database-parameters-assert.yaml
27+
- name: cleanup
28+
try:
29+
- script:
30+
content: |
31+
helm uninstall --namespace $NAMESPACE database-parameters

charts/cluster/values.schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@
342342
}
343343
}
344344
},
345+
"databases": {
346+
"type": "array"
347+
},
345348
"fullnameOverride": {
346349
"type": "string"
347350
},

charts/cluster/values.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,36 @@ backups:
474474
# -- Retention policy for backups
475475
retentionPolicy: "30d"
476476

477+
##
478+
# Database management configuration
479+
databases: []
480+
# - name: app # -- Name of the database to be created.
481+
# ensure: present # -- Ensure the PostgreSQL database is present or absent - defaults to "present".
482+
# owner: app # -- Owner of the database, defaults to the value of the `name` key.
483+
# template: template1 # -- Maps to the TEMPLATE parameter.
484+
# encoding: UTF8 # -- Maps to the ENCODING parameter.
485+
# connectionLimit: -1 # -- Maps to the CONNECTION LIMIT parameter. -1 (the default) means no limit.
486+
# tablespace: "" # -- Maps to the TABLESPACE parameter and ALTER DATABASE.
487+
# databaseReclaimPolicy: retain # -- One of: retain / delete (retain by default).
488+
# schemas: [] # -- List of schemas to be created in the database.
489+
# # - name: myschema
490+
# # owner: app # -- Owner of the schema, defaults to the database owner.
491+
# # ensure: present # -- Ensure the PostgreSQL schema is present or absent - defaults to "present".
492+
# extensions: [] # -- List of extensions to be created in the database.
493+
# # - name: pg_search
494+
# # ensure: present # -- Ensure the PostgreSQL extension is present or absent - defaults to "present".
495+
# # version: "0.15.21" # -- Version of the extension to be installed, if not specified the latest version will be used.
496+
# # schema: "" # -- Schema where the extension will be installed, if not specified the extensions or current default object creation schema will be used.
497+
# isTemplate: false # -- Maps to the IS_TEMPLATE parameter. If true, the database is considered a template for new databases.
498+
# locale: "" # -- Maps to the LC_COLLATE and LC_CTYPE parameters
499+
# localeProvider: "" # -- Maps to the LOCALE_PROVIDER parameter. Available from PostgreSQL 16.
500+
# localeCollate: "" # -- Maps to the LC_COLLATE parameter
501+
# localeCType: "" # -- Maps to the LC_CTYPE parameter
502+
# icuLocale: "" # -- Maps to the ICU_LOCALE parameter. Available from PostgreSQL 15.
503+
# icuRules: "" # -- Maps to the ICU_RULES parameter. Available from PostgreSQL 16.
504+
# builtinLocale: "" # -- Maps to the BUILTIN_LOCALE parameter. Available from PostgreSQL 17.
505+
# collationVersion: "" # -- Maps to the COLLATION_VERSION parameter.
506+
477507
imageCatalog:
478508
# -- Whether to provision an image catalog. If imageCatalog.images is empty this option will be ignored.
479509
create: true

0 commit comments

Comments
 (0)