From f50d9c109494685a7fc788075c3343df6e94bf98 Mon Sep 17 00:00:00 2001 From: SK Ali Arman Date: Fri, 31 May 2024 18:29:02 +0600 Subject: [PATCH] Refactor Clickhouse Webhook (#1228) Signed-off-by: SK Ali Arman --- apis/kubedb/v1alpha2/clickhouse_helpers.go | 20 ++--- apis/kubedb/v1alpha2/clickhouse_webhook.go | 86 +++++++++++----------- apis/kubedb/v1alpha2/constants.go | 12 ++- 3 files changed, 62 insertions(+), 56 deletions(-) diff --git a/apis/kubedb/v1alpha2/clickhouse_helpers.go b/apis/kubedb/v1alpha2/clickhouse_helpers.go index a433ce269b..bc770126bb 100644 --- a/apis/kubedb/v1alpha2/clickhouse_helpers.go +++ b/apis/kubedb/v1alpha2/clickhouse_helpers.go @@ -200,16 +200,6 @@ func (c *ClickHouse) ResourceSingular() string { } func (c *ClickHouse) SetDefaults() { - if c.Spec.Replicas == nil { - c.Spec.Replicas = pointer.Int32P(1) - } - if c.Spec.TerminationPolicy == "" { - c.Spec.TerminationPolicy = TerminationPolicyDelete - } - if c.Spec.StorageType == "" { - c.Spec.StorageType = StorageTypeDurable - } - var chVersion catalog.ClickHouseVersion err := DefaultClient.Get(context.TODO(), types.NamespacedName{ Name: c.Spec.Version, @@ -257,6 +247,16 @@ func (c *ClickHouse) SetDefaults() { } c.Spec.ClusterTopology.Cluster = clusters } else { + if c.Spec.Replicas == nil { + c.Spec.Replicas = pointer.Int32P(1) + } + if c.Spec.TerminationPolicy == "" { + c.Spec.TerminationPolicy = TerminationPolicyDelete + } + if c.Spec.StorageType == "" { + c.Spec.StorageType = StorageTypeDurable + } + if c.Spec.PodTemplate == nil { c.Spec.PodTemplate = &ofst.PodTemplateSpec{} } diff --git a/apis/kubedb/v1alpha2/clickhouse_webhook.go b/apis/kubedb/v1alpha2/clickhouse_webhook.go index b27090f9f3..a658f05b9c 100644 --- a/apis/kubedb/v1alpha2/clickhouse_webhook.go +++ b/apis/kubedb/v1alpha2/clickhouse_webhook.go @@ -79,6 +79,22 @@ func (r *ClickHouse) ValidateDelete() (admission.Warnings, error) { func (r *ClickHouse) ValidateCreateOrUpdate() error { var allErr field.ErrorList + + if r.Spec.Version == "" { + allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("version"), + r.Name, + "spec.version' is missing")) + return apierrors.NewInvalid(schema.GroupKind{Group: "ClickHouse.kubedb.com", Kind: "ClickHouse"}, r.Name, allErr) + } else { + err := r.ValidateVersion(r) + if err != nil { + allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("version"), + r.Spec.Version, + err.Error())) + return apierrors.NewInvalid(schema.GroupKind{Group: "ClickHouse.kubedb.com", Kind: "ClickHouse"}, r.Name, allErr) + } + } + if r.Spec.ClusterTopology != nil { clusterName := map[string]bool{} clusters := r.Spec.ClusterTopology.Cluster @@ -99,6 +115,21 @@ func (r *ClickHouse) ValidateCreateOrUpdate() error { "cluster name is duplicated, use different cluster name")) } clusterName[cluster.Name] = true + + allErr = r.validateClusterStorageType(cluster, allErr) + + err := r.validateVolumes(cluster.PodTemplate) + if err != nil { + allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("clusterTopology").Child("podTemplate").Child("spec").Child("volumes"), + r.Name, + err.Error())) + } + err = r.validateVolumesMountPaths(cluster.PodTemplate) + if err != nil { + allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("clusterTopology").Child("podTemplate").Child("spec").Child("volumeMounts"), + r.Name, + err.Error())) + } } if r.Spec.PodTemplate != nil { allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("podTemplate"), @@ -106,6 +137,18 @@ func (r *ClickHouse) ValidateCreateOrUpdate() error { "PodTemplate should be nil in clusterTopology")) } + if r.Spec.Replicas != nil { + allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("replica"), + r.Name, + "replica should be nil in clusterTopology")) + } + + if r.Spec.StorageType != "" { + allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("storageType"), + r.Name, + "StorageType should be empty in clusterTopology")) + } + if r.Spec.Storage != nil { allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("storage"), r.Name, @@ -126,22 +169,6 @@ func (r *ClickHouse) ValidateCreateOrUpdate() error { r.Name, "number of replicas can't be greater than 1 in standalone mode")) } - - } - - if r.Spec.Version == "" { - allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("version"), - r.Name, - "spec.version' is missing")) - } else { - err := r.ValidateVersion(r) - if err != nil { - allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("version"), - r.Spec.Version, - err.Error())) - } - } - if r.Spec.ClusterTopology == nil { err := r.validateVolumes(r.Spec.PodTemplate) if err != nil { allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("podTemplate").Child("spec").Child("volumes"), @@ -154,36 +181,10 @@ func (r *ClickHouse) ValidateCreateOrUpdate() error { r.Name, err.Error())) } - } - - if r.Spec.ClusterTopology != nil { - clusters := r.Spec.ClusterTopology.Cluster - for _, cluster := range clusters { - allErr = r.validateClusterStorageType(cluster, allErr) - err := r.validateVolumes(cluster.PodTemplate) - if err != nil { - allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("clusterTopology").Child("podTemplate").Child("spec").Child("volumes"), - r.Name, - err.Error())) - } - err = r.validateVolumesMountPaths(cluster.PodTemplate) - if err != nil { - allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("clusterTopology").Child("podTemplate").Child("spec").Child("volumeMounts"), - r.Name, - err.Error())) - } - } - } else { allErr = r.validateStandaloneStorageType(r.Spec.StorageType, r.Spec.Storage, allErr) } - //if r.Spec.ConfigSecret != nil && r.Spec.ConfigSecret.Name == "" { - // allErr = append(allErr, field.Invalid(field.NewPath("spec").Child("configSecret").Child("name"), - // r.Name, - // "ConfigSecret Name can not be empty")) - //} - if len(allErr) == 0 { return nil } @@ -302,6 +303,5 @@ func (r *ClickHouse) validateVolumesMountPaths(podTemplate *ofst.PodTemplateSpec } } } - return nil } diff --git a/apis/kubedb/v1alpha2/constants.go b/apis/kubedb/v1alpha2/constants.go index e58a6230be..d70d61c370 100644 --- a/apis/kubedb/v1alpha2/constants.go +++ b/apis/kubedb/v1alpha2/constants.go @@ -1254,15 +1254,21 @@ const ( ClickHouseContainerName = "clickhouse" ClickHouseInitContainerName = "clickhouse-init" - ClickHouseClusterConfigFileName = "cluster-config.yaml" - ClickHouseTempConfigDir = "/ch-tmp/config" - ClickHouseTempDir = "/ch-tmp" + ClickHouseClusterConfigFile = "cluster-config.yaml" + ClickHouseTempConfigDir = "/ch-tmp/config" + ClickHouseTempDir = "/ch-tmp" ClickHouseUserConfigDir = "/etc/clickhouse-server/user.d" ClickHouseMacrosFileName = "macros.yaml" ClickHouseStandalone = "standalone" ClickHouseCluster = "cluster" + + ClickHouseHealthCheckerDatabase = "kubedb_system_db" + ClickHouseHealthCheckerTable = "kubedb_system_table" + + ClickHouseServerConfigFile = "server-config.yaml" + ClickHouseKeeperFileConfig = "keeper-config.yaml" ) // Resource kind related constants