Skip to content

Commit 82a016c

Browse files
leonardocegbartolinifcanovaimnenciagabriele-wolfox
authored
feat(databases): declarative management of extensions and schemas (cloudnative-pg#7062)
This patch introduces the `extensions` and `schemas` stanzas in the Database resource to declaratively create, modify, and drop PostgreSQL extensions and schemas within a database. Closes: cloudnative-pg#6292 ## Release Notes **Declarative management of extensions and schemas**: Introduced the `extensions` and `schemas` stanzas in the Database resource to declaratively create, modify, and drop PostgreSQL extensions and schemas within a database. Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com> Signed-off-by: Gabriele Bartolini <gabriele.bartolini@enterprisedb.com> Signed-off-by: Francesco Canovai <francesco.canovai@enterprisedb.com> Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com> Co-authored-by: Gabriele Bartolini <gabriele.bartolini@enterprisedb.com> Co-authored-by: Francesco Canovai <francesco.canovai@enterprisedb.com> Co-authored-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com> Co-authored-by: Gabriele Quaresima <gabriele.quaresima@enterprisedb.com>
1 parent 163dbaf commit 82a016c

24 files changed

+1681
-23
lines changed

.wordlist-en-custom.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ DataBackupConfiguration
122122
DataBase
123123
DataDurabilityLevel
124124
DataSource
125+
DatabaseObjectSpec
126+
DatabaseObjectStatus
125127
DatabaseReclaimPolicy
126128
DatabaseRoleRef
127129
DatabaseSpec
@@ -155,6 +157,8 @@ EnvVar
155157
EphemeralVolumeSource
156158
EphemeralVolumesSizeLimit
157159
EphemeralVolumesSizeLimitConfiguration
160+
ExtensionSpec
161+
ExtensionStatus
158162
ExternalCluster
159163
FQDN
160164
Fei
@@ -414,6 +418,7 @@ ScheduledBackupList
414418
ScheduledBackupSpec
415419
ScheduledBackupStatus
416420
ScheduledBackups
421+
SchemaSpec
417422
Scorsolini
418423
Seccomp
419424
SeccompProfile

api/v1/database_funcs.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,13 @@ func (dbList *DatabaseList) MustHaveManagedResourceExclusivity(reference *Databa
7575
pointers := toSliceWithPointers(dbList.Items)
7676
return ensureManagedResourceExclusivity(reference, pointers)
7777
}
78+
79+
// GetEnsure gets the ensure status of the resource
80+
func (dbObject DatabaseObjectSpec) GetEnsure() EnsureOption {
81+
return dbObject.Ensure
82+
}
83+
84+
// GetName gets the name of the resource
85+
func (dbObject DatabaseObjectSpec) GetName() string {
86+
return dbObject.Name
87+
}

api/v1/database_types.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,59 @@ type DatabaseSpec struct {
162162
// +kubebuilder:default:=retain
163163
// +optional
164164
ReclaimPolicy DatabaseReclaimPolicy `json:"databaseReclaimPolicy,omitempty"`
165+
166+
// The list of schemas to be managed in the database
167+
// +optional
168+
Schemas []SchemaSpec `json:"schemas,omitempty"`
169+
170+
// The list of extensions to be managed in the database
171+
// +optional
172+
Extensions []ExtensionSpec `json:"extensions,omitempty"`
173+
}
174+
175+
// DatabaseObjectSpec contains the fields which are common to every
176+
// database object
177+
type DatabaseObjectSpec struct {
178+
// Name of the extension/schema
179+
Name string `json:"name"`
180+
181+
// Specifies whether an extension/schema should be present or absent in
182+
// the database. If set to `present`, the extension/schema will be
183+
// created if it does not exist. If set to `absent`, the
184+
// extension/schema will be removed if it exists.
185+
// +kubebuilder:default:="present"
186+
// +kubebuilder:validation:Enum=present;absent
187+
// +optional
188+
Ensure EnsureOption `json:"ensure"`
189+
}
190+
191+
// SchemaSpec configures a schema in a database
192+
type SchemaSpec struct {
193+
// Common fields
194+
DatabaseObjectSpec `json:",inline"`
195+
196+
// The role name of the user who owns the schema inside PostgreSQL.
197+
// It maps to the `AUTHORIZATION` parameter of `CREATE SCHEMA` and the
198+
// `OWNER TO` command of `ALTER SCHEMA`.
199+
Owner string `json:"owner,omitempty"`
200+
}
201+
202+
// ExtensionSpec configures an extension in a database
203+
type ExtensionSpec struct {
204+
// Common fields
205+
DatabaseObjectSpec `json:",inline"`
206+
207+
// The version of the extension to install. If empty, the operator will
208+
// install the default version (whatever is specified in the
209+
// extension's control file)
210+
Version string `json:"version,omitempty"`
211+
212+
// The name of the schema in which to install the extension's objects,
213+
// in case the extension allows its contents to be relocated. If not
214+
// specified (default), and the extension's control file does not
215+
// specify a schema either, the current default object creation schema
216+
// is used.
217+
Schema string `json:"schema,omitempty"`
165218
}
166219

167220
// DatabaseStatus defines the observed state of Database
@@ -178,6 +231,28 @@ type DatabaseStatus struct {
178231
// Message is the reconciliation output message
179232
// +optional
180233
Message string `json:"message,omitempty"`
234+
235+
// Schemas is the status of the managed schemas
236+
// +optional
237+
Schemas []DatabaseObjectStatus `json:"schemas,omitempty"`
238+
239+
// Extensions is the status of the managed extensions
240+
// +optional
241+
Extensions []DatabaseObjectStatus `json:"extensions,omitempty"`
242+
}
243+
244+
// DatabaseObjectStatus is the status of the managed database objects
245+
type DatabaseObjectStatus struct {
246+
// The name of the object
247+
Name string `json:"name"`
248+
249+
// True of the object has been installed successfully in
250+
// the database
251+
Applied bool `json:"applied"`
252+
253+
// Message is the object reconciliation message
254+
// +optional
255+
Message string `json:"message,omitempty"`
181256
}
182257

183258
// +genclient

api/v1/zz_generated.deepcopy.go

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/postgresql.cnpg.io_databases.yaml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,43 @@ spec:
124124
- present
125125
- absent
126126
type: string
127+
extensions:
128+
description: The list of extensions to be managed in the database
129+
items:
130+
description: ExtensionSpec configures an extension in a database
131+
properties:
132+
ensure:
133+
default: present
134+
description: |-
135+
Specifies whether an extension/schema should be present or absent in
136+
the database. If set to `present`, the extension/schema will be
137+
created if it does not exist. If set to `absent`, the
138+
extension/schema will be removed if it exists.
139+
enum:
140+
- present
141+
- absent
142+
type: string
143+
name:
144+
description: Name of the extension/schema
145+
type: string
146+
schema:
147+
description: |-
148+
The name of the schema in which to install the extension's objects,
149+
in case the extension allows its contents to be relocated. If not
150+
specified (default), and the extension's control file does not
151+
specify a schema either, the current default object creation schema
152+
is used.
153+
type: string
154+
version:
155+
description: |-
156+
The version of the extension to install. If empty, the operator will
157+
install the default version (whatever is specified in the
158+
extension's control file)
159+
type: string
160+
required:
161+
- name
162+
type: object
163+
type: array
127164
icuLocale:
128165
description: |-
129166
Maps to the `ICU_LOCALE` parameter of `CREATE DATABASE`. This
@@ -203,6 +240,35 @@ spec:
203240
Maps to the `OWNER TO` command of `ALTER DATABASE`.
204241
The role name of the user who owns the database inside PostgreSQL.
205242
type: string
243+
schemas:
244+
description: The list of schemas to be managed in the database
245+
items:
246+
description: SchemaSpec configures a schema in a database
247+
properties:
248+
ensure:
249+
default: present
250+
description: |-
251+
Specifies whether an extension/schema should be present or absent in
252+
the database. If set to `present`, the extension/schema will be
253+
created if it does not exist. If set to `absent`, the
254+
extension/schema will be removed if it exists.
255+
enum:
256+
- present
257+
- absent
258+
type: string
259+
name:
260+
description: Name of the extension/schema
261+
type: string
262+
owner:
263+
description: |-
264+
The role name of the user who owns the schema inside PostgreSQL.
265+
It maps to the `AUTHORIZATION` parameter of `CREATE SCHEMA` and the
266+
`OWNER TO` command of `ALTER SCHEMA`.
267+
type: string
268+
required:
269+
- name
270+
type: object
271+
type: array
206272
tablespace:
207273
description: |-
208274
Maps to the `TABLESPACE` parameter of `CREATE DATABASE`.
@@ -242,6 +308,28 @@ spec:
242308
applied:
243309
description: Applied is true if the database was reconciled correctly
244310
type: boolean
311+
extensions:
312+
description: Extensions is the status of the managed extensions
313+
items:
314+
description: DatabaseObjectStatus is the status of the managed database
315+
objects
316+
properties:
317+
applied:
318+
description: |-
319+
True of the object has been installed successfully in
320+
the database
321+
type: boolean
322+
message:
323+
description: Message is the object reconciliation message
324+
type: string
325+
name:
326+
description: The name of the object
327+
type: string
328+
required:
329+
- applied
330+
- name
331+
type: object
332+
type: array
245333
message:
246334
description: Message is the reconciliation output message
247335
type: string
@@ -251,6 +339,28 @@ spec:
251339
desired state that was synchronized
252340
format: int64
253341
type: integer
342+
schemas:
343+
description: Schemas is the status of the managed schemas
344+
items:
345+
description: DatabaseObjectStatus is the status of the managed database
346+
objects
347+
properties:
348+
applied:
349+
description: |-
350+
True of the object has been installed successfully in
351+
the database
352+
type: boolean
353+
message:
354+
description: Message is the object reconciliation message
355+
type: string
356+
name:
357+
description: The name of the object
358+
type: string
359+
required:
360+
- applied
361+
- name
362+
type: object
363+
type: array
254364
type: object
255365
required:
256366
- metadata

0 commit comments

Comments
 (0)