Skip to content

Commit 14afc85

Browse files
leonardocearmrumnencia
authored
fix: make cluster reference immutable on Database, Pooler, Publication, Subscription and ScheduledBackup (cloudnative-pg#10743)
Add a CEL `self == oldSelf` validation rule to the `cluster` field of the Database, Pooler, Publication, Subscription and ScheduledBackup resources so that the target cluster cannot be changed after the resource is created. Repointing these objects at a different cluster has no well-defined semantics and previously left the controllers in an inconsistent state; rejecting the update at the API server makes the constraint explicit to users. Regenerate the affected CRD manifests and document the new constraint in the corresponding user guide sections. Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com> Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com> Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com> Co-authored-by: Armando Ruocco <armando.ruocco@enterprisedb.com> Co-authored-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
1 parent 7fe2eb2 commit 14afc85

14 files changed

Lines changed: 50 additions & 0 deletions

api/v1/database_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const (
6060
// +kubebuilder:validation:XValidation:rule="!has(self.icuRules) || self.localeProvider == 'icu'",message="icuRules is only available when localeProvider is set to `icu`"
6161
type DatabaseSpec struct {
6262
// The name of the PostgreSQL cluster hosting the database.
63+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="cluster reference is immutable after creation"
6364
ClusterRef corev1.LocalObjectReference `json:"cluster"`
6465

6566
// Ensure the PostgreSQL database is `present` or `absent` - defaults to "present".

api/v1/pooler_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ const (
8888
type PoolerSpec struct {
8989
// This is the cluster reference on which the Pooler will work.
9090
// Pooler name should never match with any cluster name within the same namespace.
91+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="cluster reference is immutable after creation"
9192
Cluster LocalObjectReference `json:"cluster"`
9293

9394
// Type of service to forward traffic to. Default: `rw`.

api/v1/publication_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141
// PublicationSpec defines the desired state of Publication
4242
type PublicationSpec struct {
4343
// The name of the PostgreSQL cluster that identifies the "publisher"
44+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="cluster reference is immutable after creation"
4445
ClusterRef corev1.LocalObjectReference `json:"cluster"`
4546

4647
// The name of the publication inside PostgreSQL

api/v1/scheduledbackup_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type ScheduledBackupSpec struct {
3939
Schedule string `json:"schedule"`
4040

4141
// The cluster to backup
42+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="cluster reference is immutable after creation"
4243
Cluster LocalObjectReference `json:"cluster"`
4344

4445
// Indicates which ownerReference should be put inside the created backup resources.<br />

api/v1/subscription_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141
// SubscriptionSpec defines the desired state of Subscription
4242
type SubscriptionSpec struct {
4343
// The name of the PostgreSQL cluster that identifies the "subscriber"
44+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="cluster reference is immutable after creation"
4445
ClusterRef corev1.LocalObjectReference `json:"cluster"`
4546

4647
// The name of the subscription inside PostgreSQL

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ spec:
8787
type: string
8888
type: object
8989
x-kubernetes-map-type: atomic
90+
x-kubernetes-validations:
91+
- message: cluster reference is immutable after creation
92+
rule: self == oldSelf
9093
collationVersion:
9194
description: |-
9295
Maps to the `COLLATION_VERSION` parameter of `CREATE DATABASE`. This

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ spec:
6565
required:
6666
- name
6767
type: object
68+
x-kubernetes-validations:
69+
- message: cluster reference is immutable after creation
70+
rule: self == oldSelf
6871
deploymentStrategy:
6972
description: The deployment strategy to use for pgbouncer to replace
7073
existing pods with new ones

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ spec:
7171
type: string
7272
type: object
7373
x-kubernetes-map-type: atomic
74+
x-kubernetes-validations:
75+
- message: cluster reference is immutable after creation
76+
rule: self == oldSelf
7477
dbname:
7578
description: |-
7679
The name of the database where the publication will be installed in

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ spec:
7272
required:
7373
- name
7474
type: object
75+
x-kubernetes-validations:
76+
- message: cluster reference is immutable after creation
77+
rule: self == oldSelf
7578
immediate:
7679
description: If the first backup has to be immediately start after
7780
creation or not

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ spec:
7171
type: string
7272
type: object
7373
x-kubernetes-map-type: atomic
74+
x-kubernetes-validations:
75+
- message: cluster reference is immutable after creation
76+
rule: self == oldSelf
7477
dbname:
7578
description: |-
7679
The name of the database where the publication will be installed in

0 commit comments

Comments
 (0)