Skip to content

Commit

Permalink
Refactor Clickhouse Webhook (#1228)
Browse files Browse the repository at this point in the history
Signed-off-by: SK Ali Arman <[email protected]>
  • Loading branch information
sheikh-arman authored May 31, 2024
1 parent 0494903 commit f50d9c1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 56 deletions.
20 changes: 10 additions & 10 deletions apis/kubedb/v1alpha2/clickhouse_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{}
}
Expand Down
86 changes: 43 additions & 43 deletions apis/kubedb/v1alpha2/clickhouse_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -99,13 +115,40 @@ 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"),
r.Name,
"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,
Expand All @@ -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"),
Expand All @@ -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
}
Expand Down Expand Up @@ -302,6 +303,5 @@ func (r *ClickHouse) validateVolumesMountPaths(podTemplate *ofst.PodTemplateSpec
}
}
}

return nil
}
12 changes: 9 additions & 3 deletions apis/kubedb/v1alpha2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f50d9c1

Please sign in to comment.