Skip to content

Commit

Permalink
feat(clickhousedatabase): add 'databasename' to spec
Browse files Browse the repository at this point in the history
This makes it possible to use UTF-8 charset with ClickhouseDatabase resources.
  • Loading branch information
rriski committed Jun 18, 2024
1 parent 4883263 commit be1a74a
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 102 deletions.
18 changes: 18 additions & 0 deletions api/v1alpha1/clickhousedatabase_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ import (
)

// ClickhouseDatabaseSpec defines the desired state of ClickhouseDatabase
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.databaseName) || has(self.databaseName)", message="databaseName is required once set"
type ClickhouseDatabaseSpec struct {
ServiceDependant `json:",inline"`

// Specifies the Clickhouse database name. Defaults to `metadata.name` if omitted.
// Note: `metadata.name` is ASCII-only. For UTF-8 names, use `spec.databaseName`, but ASCII is advised for compatibility.
// +kubebuilder:validation:MaxLength=63
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
DatabaseName string `json:"databaseName,omitempty"`
}

// ClickhouseDatabaseStatus defines the observed state of ClickhouseDatabase
Expand All @@ -21,6 +28,7 @@ type ClickhouseDatabaseStatus struct {
// +kubebuilder:subresource:status

// ClickhouseDatabase is the Schema for the databases API
// +kubebuilder:printcolumn:name="Database name",type="string",JSONPath=".spec.databaseName"
// +kubebuilder:printcolumn:name="Service Name",type="string",JSONPath=".spec.serviceName"
// +kubebuilder:printcolumn:name="Project",type="string",JSONPath=".spec.project"
type ClickhouseDatabase struct {
Expand All @@ -33,6 +41,16 @@ type ClickhouseDatabase struct {

var _ AivenManagedObject = &ClickhouseDatabase{}

func (in *ClickhouseDatabase) GetName() string {
// Default to Spec.Username and use ObjectMeta.Name if empty.
// ObjectMeta.Name doesn't support UTF-8 characters, Spec.Username does.
name := in.Spec.DatabaseName
if name == "" {
name = in.ObjectMeta.Name
}
return name
}

func (*ClickhouseDatabase) NoSecret() bool {
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ spec:
scope: Namespaced
versions:
- additionalPrinterColumns:
- jsonPath: .spec.databaseName
name: Database name
type: string
- jsonPath: .spec.serviceName
name: Service Name
type: string
Expand Down Expand Up @@ -59,6 +62,15 @@ spec:
- key
- name
type: object
databaseName:
description: |-
Specifies the Clickhouse database name. Defaults to `metadata.name` if omitted.
Note: `metadata.name` is ASCII-only. For UTF-8 names, use `spec.databaseName`, but ASCII is advised for compatibility.
maxLength: 63
type: string
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
project:
description: Identifies the project this resource belongs to
maxLength: 63
Expand All @@ -81,6 +93,9 @@ spec:
- project
- serviceName
type: object
x-kubernetes-validations:
- message: databaseName is required once set
rule: "!has(oldSelf.databaseName) || has(self.databaseName)"
status:
description: ClickhouseDatabaseStatus defines the observed state of ClickhouseDatabase
properties:
Expand Down
15 changes: 15 additions & 0 deletions config/crd/bases/aiven.io_clickhousedatabases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ spec:
scope: Namespaced
versions:
- additionalPrinterColumns:
- jsonPath: .spec.databaseName
name: Database name
type: string
- jsonPath: .spec.serviceName
name: Service Name
type: string
Expand Down Expand Up @@ -59,6 +62,15 @@ spec:
- key
- name
type: object
databaseName:
description: |-
Specifies the Clickhouse database name. Defaults to `metadata.name` if omitted.
Note: `metadata.name` is ASCII-only. For UTF-8 names, use `spec.databaseName`, but ASCII is advised for compatibility.
maxLength: 63
type: string
x-kubernetes-validations:
- message: Value is immutable
rule: self == oldSelf
project:
description: Identifies the project this resource belongs to
maxLength: 63
Expand All @@ -81,6 +93,9 @@ spec:
- project
- serviceName
type: object
x-kubernetes-validations:
- message: databaseName is required once set
rule: "!has(oldSelf.databaseName) || has(self.databaseName)"
status:
description: ClickhouseDatabaseStatus defines the observed state of ClickhouseDatabase
properties:
Expand Down
11 changes: 7 additions & 4 deletions controllers/clickhousedatabase_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ func (h *ClickhouseDatabaseHandler) createOrUpdate(ctx context.Context, avn *aiv
return err
}

_, err = avn.ClickhouseDatabase.Get(ctx, db.Spec.Project, db.Spec.ServiceName, db.Name)
dbName := db.GetName()
_, err = avn.ClickhouseDatabase.Get(ctx, db.Spec.Project, db.Spec.ServiceName, dbName)
if isNotFound(err) {
err = avn.ClickhouseDatabase.Create(ctx, db.Spec.Project, db.Spec.ServiceName, db.Name)
err = avn.ClickhouseDatabase.Create(ctx, db.Spec.Project, db.Spec.ServiceName, dbName)
}

if err != nil {
Expand All @@ -79,7 +80,8 @@ func (h *ClickhouseDatabaseHandler) delete(ctx context.Context, avn *aiven.Clien
return false, err
}

err = avn.ClickhouseDatabase.Delete(ctx, db.Spec.Project, db.Spec.ServiceName, db.Name)
dbName := db.GetName()
err = avn.ClickhouseDatabase.Delete(ctx, db.Spec.Project, db.Spec.ServiceName, dbName)
if err != nil && !isNotFound(err) {
return false, err
}
Expand All @@ -93,7 +95,8 @@ func (h *ClickhouseDatabaseHandler) get(ctx context.Context, avn *aiven.Client,
return nil, err
}

_, err = avn.ClickhouseDatabase.Get(ctx, db.Spec.Project, db.Spec.ServiceName, db.Name)
dbName := db.GetName()
_, err = avn.ClickhouseDatabase.Get(ctx, db.Spec.Project, db.Spec.ServiceName, dbName)
if err != nil {
return nil, err
}
Expand Down
13 changes: 11 additions & 2 deletions docs/docs/api-reference/clickhouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ title: "Clickhouse"
key: token

connInfoSecretTarget:
name: clickhouse-secret
prefix: MY_SECRET_PREFIX_
name: my-clickhouse
annotations:
foo: bar
labels:
baz: egg

tags:
env: test
instance: foo

userConfig:
ip_filter:
- network: 0.0.0.0/32
description: bar
- network: 10.20.0.0/16

project: my-aiven-project
cloudName: google-europe-west1
plan: startup-16
Expand Down
5 changes: 4 additions & 1 deletion docs/docs/api-reference/clickhousedatabase.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ title: "ClickhouseDatabase"
name: aiven-token
key: token

project: aiven-project-name
project: my-aiven-project
serviceName: my-clickhouse
databaseName: my-db
```

## ClickhouseDatabase {: #ClickhouseDatabase }
Expand Down Expand Up @@ -44,6 +45,8 @@ ClickhouseDatabaseSpec defines the desired state of ClickhouseDatabase.
**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).
- [`databaseName`](#spec.databaseName-property){: name='spec.databaseName-property'} (string, Immutable, MaxLength: 63). Specifies the Clickhouse database name. Defaults to `metadata.name` if omitted.
Note: `metadata.name` is ASCII-only. For UTF-8 names, use `spec.databaseName`, but ASCII is advised for compatibility.

## authSecretRef {: #spec.authSecretRef }

Expand Down
13 changes: 11 additions & 2 deletions docs/docs/api-reference/examples/clickhouse.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ spec:
key: token

connInfoSecretTarget:
name: clickhouse-secret
prefix: MY_SECRET_PREFIX_
name: my-clickhouse
annotations:
foo: bar
labels:
baz: egg

tags:
env: test
instance: foo

userConfig:
ip_filter:
- network: 0.0.0.0/32
description: bar
- network: 10.20.0.0/16

project: my-aiven-project
cloudName: google-europe-west1
plan: startup-16
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/api-reference/examples/clickhousedatabase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ spec:
name: aiven-token
key: token

project: aiven-project-name
project: my-aiven-project
serviceName: my-clickhouse
databaseName: my-db
Loading

0 comments on commit be1a74a

Please sign in to comment.