Skip to content

Commit

Permalink
refactor(database): use crd validation rules
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Mar 25, 2024
1 parent f49cc45 commit d64772d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 37 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## [MAJOR.MINOR.PATCH] - YYYY-MM-DD

- Perform upgrade tasks to check if PG service can be upgraded before updating service
- Replace `Database` kind validations and default values with CRD validation rules
- Perform upgrade tasks to check if PG service can be upgraded before updating the service
- Expose project CA certificate to service secrets: `REDIS_CA_CERT`, `MYSQL_CA_CERT`, etc.
- Add `KafkaTopic` field `config.local_retention_bytes`, type `integer`: local.retention.bytes value
- Add `KafkaTopic` field `config.local_retention_ms`, type `integer`: local.retention.ms value
Expand Down
6 changes: 6 additions & 0 deletions api/v1alpha1/database_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ import (
type DatabaseSpec struct {
// +kubebuilder:validation:MaxLength=63
// +kubebuilder:validation:Format="^[a-zA-Z0-9_-]*$"
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
// Project to link the database to
Project string `json:"project"`

// +kubebuilder:validation:MaxLength=63
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
// PostgreSQL service to link the database to
ServiceName string `json:"serviceName"`

// +kubebuilder:validation:MaxLength=128
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
// +kubebuilder:default=en_US.UTF-8
// Default string sort order (LC_COLLATE) of the database. Default value: en_US.UTF-8
LcCollate string `json:"lcCollate,omitempty"`

// +kubebuilder:validation:MaxLength=128
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
// +kubebuilder:default=en_US.UTF-8
// Default character classification (LC_CTYPE) of the database. Default value: en_US.UTF-8
LcCtype string `json:"lcCtype,omitempty"`

Expand Down
26 changes: 0 additions & 26 deletions api/v1alpha1/database_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ var _ webhook.Defaulter = &Database{}
// Default implements webhook.Defaulter so a webhook will be registered for the type
func (in *Database) Default() {
databaselog.Info("default", "name", in.Name)

const defaultLC = "en_US.UTF-8"

if in.Spec.LcCtype == "" {
in.Spec.LcCtype = defaultLC
}

if in.Spec.LcCollate == "" {
in.Spec.LcCollate = defaultLC
}
}

//+kubebuilder:webhook:verbs=create;update;delete,path=/validate-aiven-io-v1alpha1-database,mutating=false,failurePolicy=fail,groups=aiven.io,resources=databases,versions=v1alpha1,name=vdatabase.kb.io,sideEffects=none,admissionReviewVersions=v1
Expand All @@ -54,22 +44,6 @@ func (in *Database) ValidateCreate() error {
func (in *Database) ValidateUpdate(old runtime.Object) error {
databaselog.Info("validate update", "name", in.Name)

if in.Spec.Project != old.(*Database).Spec.Project {
return errors.New("cannot update a Database, project field is immutable and cannot be updated")
}

if in.Spec.ServiceName != old.(*Database).Spec.ServiceName {
return errors.New("cannot update a Database, service_name field is immutable and cannot be updated")
}

if in.Spec.LcCollate != old.(*Database).Spec.LcCollate {
return errors.New("cannot update a Database, lc_collate field is immutable and cannot be updated")
}

if in.Spec.LcCtype != old.(*Database).Spec.LcCtype {
return errors.New("cannot update a Database, lc_ctype field is immutable and cannot be updated")
}

return nil
}

Expand Down
14 changes: 14 additions & 0 deletions charts/aiven-operator-crds/templates/aiven.io_databases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,40 @@ spec:
- name
type: object
lcCollate:
default: en_US.UTF-8
description:
"Default string sort order (LC_COLLATE) of the database.
Default value: en_US.UTF-8"
maxLength: 128
type: string
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
lcCtype:
default: en_US.UTF-8
description:
"Default character classification (LC_CTYPE) of the database.
Default value: en_US.UTF-8"
maxLength: 128
type: string
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
project:
description: Project to link the database to
format: ^[a-zA-Z0-9_-]*$
maxLength: 63
type: string
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
serviceName:
description: PostgreSQL service to link the database to
maxLength: 63
type: string
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
terminationProtection:
description:
It is a Kubernetes side deletion protections, which prevents
Expand Down
14 changes: 14 additions & 0 deletions config/crd/bases/aiven.io_databases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,40 @@ spec:
- name
type: object
lcCollate:
default: en_US.UTF-8
description:
"Default string sort order (LC_COLLATE) of the database.
Default value: en_US.UTF-8"
maxLength: 128
type: string
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
lcCtype:
default: en_US.UTF-8
description:
"Default character classification (LC_CTYPE) of the database.
Default value: en_US.UTF-8"
maxLength: 128
type: string
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
project:
description: Project to link the database to
format: ^[a-zA-Z0-9_-]*$
maxLength: 63
type: string
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
serviceName:
description: PostgreSQL service to link the database to
maxLength: 63
type: string
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
terminationProtection:
description:
It is a Kubernetes side deletion protections, which prevents
Expand Down
8 changes: 4 additions & 4 deletions docs/docs/api-reference/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ DatabaseSpec defines the desired state of Database.

**Required**

- [`project`](#spec.project-property){: name='spec.project-property'} (string, MaxLength: 63, Format: `^[a-zA-Z0-9_-]*$`). Project to link the database to.
- [`serviceName`](#spec.serviceName-property){: name='spec.serviceName-property'} (string, MaxLength: 63). PostgreSQL service to link the database to.
- [`project`](#spec.project-property){: name='spec.project-property'} (string, Immutable, MaxLength: 63, Format: `^[a-zA-Z0-9_-]*$`). Project to link the database to.
- [`serviceName`](#spec.serviceName-property){: name='spec.serviceName-property'} (string, Immutable, MaxLength: 63). PostgreSQL service to link the database to.

**Optional**

- [`authSecretRef`](#spec.authSecretRef-property){: name='spec.authSecretRef-property'} (object). Authentication reference to Aiven token in a secret. See below for [nested schema](#spec.authSecretRef).
- [`lcCollate`](#spec.lcCollate-property){: name='spec.lcCollate-property'} (string, MaxLength: 128). Default string sort order (LC_COLLATE) of the database. Default value: en_US.UTF-8.
- [`lcCtype`](#spec.lcCtype-property){: name='spec.lcCtype-property'} (string, MaxLength: 128). Default character classification (LC_CTYPE) of the database. Default value: en_US.UTF-8.
- [`lcCollate`](#spec.lcCollate-property){: name='spec.lcCollate-property'} (string, Immutable, MaxLength: 128). Default string sort order (LC_COLLATE) of the database. Default value: en_US.UTF-8.
- [`lcCtype`](#spec.lcCtype-property){: name='spec.lcCtype-property'} (string, Immutable, MaxLength: 128). Default character classification (LC_CTYPE) of the database. Default value: en_US.UTF-8.
- [`terminationProtection`](#spec.terminationProtection-property){: name='spec.terminationProtection-property'} (boolean). It is a Kubernetes side deletion protections, which prevents the database from being deleted by Kubernetes. It is recommended to enable this for any production databases containing critical data.

## authSecretRef {: #spec.authSecretRef }
Expand Down
8 changes: 2 additions & 6 deletions tests/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ spec:
project: %[1]s
serviceName: %[2]s
lcCtype: en_US.UTF-8
lcCollate: en_US.UTF-8
`, project, pgName, dbName, cloudName)
}

Expand Down Expand Up @@ -87,9 +83,9 @@ func TestDatabase(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, dbName, db.GetName())
assert.Equal(t, dbAvn.DatabaseName, db.GetName())
assert.Equal(t, "en_US.UTF-8", db.Spec.LcCtype)
assert.Equal(t, "en_US.UTF-8", db.Spec.LcCtype) // the default value
assert.Equal(t, dbAvn.LcType, db.Spec.LcCtype)
assert.Equal(t, "en_US.UTF-8", db.Spec.LcCollate)
assert.Equal(t, "en_US.UTF-8", db.Spec.LcCollate) // the default value
assert.Equal(t, dbAvn.LcCollate, db.Spec.LcCollate)

// We need to validate deletion,
Expand Down

0 comments on commit d64772d

Please sign in to comment.