Skip to content

Commit

Permalink
feat(clickhousedatabase): add a new kind
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Mar 26, 2024
1 parent a5e5b06 commit 2823fce
Show file tree
Hide file tree
Showing 29 changed files with 1,062 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

- Add `ClickhouseDatabase` kind
- 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.
Expand Down
16 changes: 16 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: aiven.io
layout:
- go.kubebuilder.io/v3
Expand Down Expand Up @@ -247,4 +251,16 @@ resources:
defaulting: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: aiven.io
kind: ClickhouseDatabase
path: github.com/aiven/aiven-operator/api/v1alpha1
version: v1alpha1
webhooks:
defaulting: true
validation: true
webhookVersion: v1
version: "3"
76 changes: 76 additions & 0 deletions api/v1alpha1/clickhousedatabase_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2024 Aiven, Helsinki, Finland. https://aiven.io/

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ClickhouseDatabaseSpec defines the desired state of ClickhouseDatabase
type ClickhouseDatabaseSpec 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"
// Clickhouse service to link the database to
ServiceName string `json:"serviceName"`

// 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.
TerminationProtection *bool `json:"terminationProtection,omitempty"`

// Authentication reference to Aiven token in a secret
AuthSecretRef *AuthSecretReference `json:"authSecretRef,omitempty"`
}

// ClickhouseDatabaseStatus defines the observed state of ClickhouseDatabase
type ClickhouseDatabaseStatus struct {
// Conditions represent the latest available observations of an ClickhouseDatabase state
Conditions []metav1.Condition `json:"conditions"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// ClickhouseDatabase is the Schema for the databases API
// +kubebuilder:printcolumn:name="Service Name",type="string",JSONPath=".spec.serviceName"
// +kubebuilder:printcolumn:name="Project",type="string",JSONPath=".spec.project"
type ClickhouseDatabase struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ClickhouseDatabaseSpec `json:"spec,omitempty"`
Status ClickhouseDatabaseStatus `json:"status,omitempty"`
}

var _ AivenManagedObject = &ClickhouseDatabase{}

func (*ClickhouseDatabase) NoSecret() bool {
return false
}

func (in *ClickhouseDatabase) AuthSecretRef() *AuthSecretReference {
return in.Spec.AuthSecretRef
}

func (in *ClickhouseDatabase) Conditions() *[]metav1.Condition {
return &in.Status.Conditions
}

// +kubebuilder:object:root=true

// ClickhouseDatabaseList contains a list of ClickhouseDatabase
type ClickhouseDatabaseList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ClickhouseDatabase `json:"items"`
}

func init() {
SchemeBuilder.Register(&ClickhouseDatabase{}, &ClickhouseDatabaseList{})
}
58 changes: 58 additions & 0 deletions api/v1alpha1/clickhousedatabase_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2024 Aiven, Helsinki, Finland. https://aiven.io/

package v1alpha1

import (
"errors"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

// log is for logging in this package.
var clickhousedatabaselog = logf.Log.WithName("clickhousedatabase-resource")

func (in *ClickhouseDatabase) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(in).
Complete()
}

//+kubebuilder:webhook:path=/mutate-aiven-io-v1alpha1-clickhousedatabase,mutating=true,failurePolicy=fail,groups=aiven.io,resources=clickhousedatabases,verbs=create;update,versions=v1alpha1,name=mclickhousedatabase.kb.io,sideEffects=none,admissionReviewVersions=v1

var _ webhook.Defaulter = &ClickhouseDatabase{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (in *ClickhouseDatabase) Default() {
clickhousedatabaselog.Info("default", "name", in.Name)
}

//+kubebuilder:webhook:verbs=create;update;delete,path=/validate-aiven-io-v1alpha1-clickhousedatabase,mutating=false,failurePolicy=fail,groups=aiven.io,resources=clickhousedatabases,versions=v1alpha1,name=vclickhousedatabase.kb.io,sideEffects=none,admissionReviewVersions=v1

var _ webhook.Validator = &ClickhouseDatabase{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (in *ClickhouseDatabase) ValidateCreate() error {
clickhousedatabaselog.Info("validate create", "name", in.Name)

return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (in *ClickhouseDatabase) ValidateUpdate(old runtime.Object) error {
clickhousedatabaselog.Info("validate update", "name", in.Name)

return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (in *ClickhouseDatabase) ValidateDelete() error {
clickhousedatabaselog.Info("validate delete", "name", in.Name)

if in.Spec.TerminationProtection != nil && *in.Spec.TerminationProtection {
return errors.New("cannot delete ClickhouseDatabase, termination protection is on")
}
return nil
}
3 changes: 3 additions & 0 deletions api/v1alpha1/setup_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func SetupWebhooks(mgr ctrl.Manager) error {
if err := (&ClickhouseUser{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("webhook ClickhouseUser: %w", err)
}
if err := (&ClickhouseDatabase{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("webhook ClickhouseDatabase: %w", err)
}
if err := (&MySQL{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("webhook MySQL: %w", err)
}
Expand Down
106 changes: 106 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2823fce

Please sign in to comment.