diff --git a/apis/apps/v1alpha1/type.go b/apis/apps/v1alpha1/type.go index 47734adce49..2fe2a455b60 100644 --- a/apis/apps/v1alpha1/type.go +++ b/apis/apps/v1alpha1/type.go @@ -207,12 +207,13 @@ type ComponentConfigSpec struct { // RerenderResourceType defines the resource requirements for a component. // +enum -// +kubebuilder:validation:Enum={vscale,hscale,tls} +// +kubebuilder:validation:Enum={vscale,hscale,tls,shardingHScale} type RerenderResourceType string const ( - ComponentVScaleType RerenderResourceType = "vscale" - ComponentHScaleType RerenderResourceType = "hscale" + ComponentVScaleType RerenderResourceType = "vscale" + ComponentHScaleType RerenderResourceType = "hscale" + ShardingComponentHScaleType RerenderResourceType = "shardingHScale" ) // MergedPolicy defines how to merge external imported templates into component templates. diff --git a/config/crd/bases/apps.kubeblocks.io_clusterdefinitions.yaml b/config/crd/bases/apps.kubeblocks.io_clusterdefinitions.yaml index 2e7ce33926d..0d2e5a68a13 100644 --- a/config/crd/bases/apps.kubeblocks.io_clusterdefinitions.yaml +++ b/config/crd/bases/apps.kubeblocks.io_clusterdefinitions.yaml @@ -589,6 +589,7 @@ spec: - vscale - hscale - tls + - shardingHScale type: string type: array x-kubernetes-list-type: set diff --git a/config/crd/bases/apps.kubeblocks.io_componentdefinitions.yaml b/config/crd/bases/apps.kubeblocks.io_componentdefinitions.yaml index fe6bec4e932..dbe357e099c 100644 --- a/config/crd/bases/apps.kubeblocks.io_componentdefinitions.yaml +++ b/config/crd/bases/apps.kubeblocks.io_componentdefinitions.yaml @@ -17918,6 +17918,7 @@ spec: - vscale - hscale - tls + - shardingHScale type: string type: array x-kubernetes-list-type: set diff --git a/config/crd/bases/apps.kubeblocks.io_configurations.yaml b/config/crd/bases/apps.kubeblocks.io_configurations.yaml index 90310656942..869c96905f5 100644 --- a/config/crd/bases/apps.kubeblocks.io_configurations.yaml +++ b/config/crd/bases/apps.kubeblocks.io_configurations.yaml @@ -271,6 +271,7 @@ spec: - vscale - hscale - tls + - shardingHScale type: string type: array x-kubernetes-list-type: set diff --git a/controllers/apps/component/transformer_component_parameters.go b/controllers/apps/component/transformer_component_parameters.go index d4b5f7c8531..6af22cd1c92 100644 --- a/controllers/apps/component/transformer_component_parameters.go +++ b/controllers/apps/component/transformer_component_parameters.go @@ -24,7 +24,7 @@ import ( appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1" cfgcore "github.com/apecloud/kubeblocks/pkg/configuration/core" - "github.com/apecloud/kubeblocks/pkg/controller/configuration" + configctrl "github.com/apecloud/kubeblocks/pkg/controller/configuration" "github.com/apecloud/kubeblocks/pkg/controller/graph" ) @@ -46,7 +46,7 @@ func (c *componentRelatedParametersTransformer) Transform(ctx graph.TransformCon } configNew := config.DeepCopy() - updated, err := configuration.UpdateConfigPayload(&configNew.Spec, synthesizedComp) + updated, err := configctrl.UpdateConfigPayload(&configNew.Spec, synthesizedComp, configctrl.ResolveShardingReference(transCtx.Component, transCtx.Cluster)) if err != nil { return err } diff --git a/deploy/helm/crds/apps.kubeblocks.io_clusterdefinitions.yaml b/deploy/helm/crds/apps.kubeblocks.io_clusterdefinitions.yaml index 2e7ce33926d..0d2e5a68a13 100644 --- a/deploy/helm/crds/apps.kubeblocks.io_clusterdefinitions.yaml +++ b/deploy/helm/crds/apps.kubeblocks.io_clusterdefinitions.yaml @@ -589,6 +589,7 @@ spec: - vscale - hscale - tls + - shardingHScale type: string type: array x-kubernetes-list-type: set diff --git a/deploy/helm/crds/apps.kubeblocks.io_componentdefinitions.yaml b/deploy/helm/crds/apps.kubeblocks.io_componentdefinitions.yaml index fe6bec4e932..dbe357e099c 100644 --- a/deploy/helm/crds/apps.kubeblocks.io_componentdefinitions.yaml +++ b/deploy/helm/crds/apps.kubeblocks.io_componentdefinitions.yaml @@ -17918,6 +17918,7 @@ spec: - vscale - hscale - tls + - shardingHScale type: string type: array x-kubernetes-list-type: set diff --git a/deploy/helm/crds/apps.kubeblocks.io_configurations.yaml b/deploy/helm/crds/apps.kubeblocks.io_configurations.yaml index 90310656942..869c96905f5 100644 --- a/deploy/helm/crds/apps.kubeblocks.io_configurations.yaml +++ b/deploy/helm/crds/apps.kubeblocks.io_configurations.yaml @@ -271,6 +271,7 @@ spec: - vscale - hscale - tls + - shardingHScale type: string type: array x-kubernetes-list-type: set diff --git a/docs/developer_docs/api-reference/cluster.md b/docs/developer_docs/api-reference/cluster.md index 1fb5b6f1892..ff43a546400 100644 --- a/docs/developer_docs/api-reference/cluster.md +++ b/docs/developer_docs/api-reference/cluster.md @@ -24453,6 +24453,8 @@ StatefulSetSpec

"vscale"

+

"shardingHScale"

+

ResourceMeta diff --git a/pkg/constant/payload.go b/pkg/constant/payload.go index 8de78bfb441..a80d3086b65 100644 --- a/pkg/constant/payload.go +++ b/pkg/constant/payload.go @@ -23,4 +23,5 @@ const ( ComponentResourcePayload = "component-resource" ReplicasPayload = "replicas" BinaryVersionPayload = "binary-version" + ShardingPayload = "sharding" ) diff --git a/pkg/controller/configuration/config_utils.go b/pkg/controller/configuration/config_utils.go index b287d6e6579..16e80527f81 100644 --- a/pkg/controller/configuration/config_utils.go +++ b/pkg/controller/configuration/config_utils.go @@ -23,6 +23,7 @@ import ( "context" "fmt" "slices" + "strconv" "strings" corev1 "k8s.io/api/core/v1" @@ -269,7 +270,7 @@ func findPortByPortName(container corev1.Container) (int32, bool) { } // UpdateConfigPayload updates the configuration payload -func UpdateConfigPayload(config *appsv1alpha1.ConfigurationSpec, component *component.SynthesizedComponent) (bool, error) { +func UpdateConfigPayload(config *appsv1alpha1.ConfigurationSpec, component *component.SynthesizedComponent, sharding *appsv1.ClusterSharding) (bool, error) { updated := false for i := range config.ConfigItemDetails { configSpec := &config.ConfigItemDetails[i] @@ -290,10 +291,25 @@ func UpdateConfigPayload(config *appsv1alpha1.ConfigurationSpec, component *comp } updated = updated || ret } + // check sharding h-scale operation + if sharding != nil && enableShardingHScaleTrigger(configSpec.ConfigSpec) { + ret, err := intctrlutil.CheckAndPatchPayload(configSpec, constant.ShardingPayload, resolveShardingResource(sharding)) + if err != nil { + return false, err + } + updated = updated || ret + } } return updated, nil } +func resolveShardingResource(sharding *appsv1.ClusterSharding) map[string]string { + return map[string]string{ + "shards": strconv.Itoa(int(sharding.Shards)), + "replicas": strconv.Itoa(int(sharding.Template.Replicas)), + } +} + func validRerenderResources(configSpec *appsv1alpha1.ComponentConfigSpec) bool { return configSpec != nil && len(configSpec.ReRenderResourceTypes) != 0 } @@ -302,6 +318,10 @@ func enableHScaleTrigger(configSpec *appsv1alpha1.ComponentConfigSpec) bool { return validRerenderResources(configSpec) && slices.Contains(configSpec.ReRenderResourceTypes, appsv1alpha1.ComponentHScaleType) } +func enableShardingHScaleTrigger(configSpec *appsv1alpha1.ComponentConfigSpec) bool { + return validRerenderResources(configSpec) && slices.Contains(configSpec.ReRenderResourceTypes, appsv1alpha1.ShardingComponentHScaleType) +} + func enableVScaleTrigger(configSpec *appsv1alpha1.ComponentConfigSpec) bool { return validRerenderResources(configSpec) && slices.Contains(configSpec.ReRenderResourceTypes, appsv1alpha1.ComponentVScaleType) } @@ -313,3 +333,21 @@ func configSetFromComponent(templates []appsv1.ComponentConfigSpec) []string { } return configSet } + +func ResolveShardingReference(component *appsv1.Component, cluster *appsv1.Cluster) *appsv1.ClusterSharding { + if len(cluster.Spec.Shardings) == 0 || len(component.Labels) == 0 { + return nil + } + + clusterCompName := component.Labels[constant.KBAppShardingNameLabelKey] + if clusterCompName == "" { + return nil + } + + for i, comp := range cluster.Spec.Shardings { + if comp.Name == clusterCompName { + return &cluster.Spec.Shardings[i] + } + } + return nil +} diff --git a/pkg/controller/configuration/pipeline.go b/pkg/controller/configuration/pipeline.go index cc93e65166c..f75ed7bae84 100644 --- a/pkg/controller/configuration/pipeline.go +++ b/pkg/controller/configuration/pipeline.go @@ -100,7 +100,7 @@ func (p *pipeline) UpdateConfiguration() *pipeline { if intctrlutil.SetControllerReference(p.ctx.Component, expectedConfiguration) != nil { return } - _, _ = UpdateConfigPayload(&expectedConfiguration.Spec, p.ctx.SynthesizedComponent) + _, _ = UpdateConfigPayload(&expectedConfiguration.Spec, p.ctx.SynthesizedComponent, ResolveShardingReference(p.ctx.Component, p.ctx.Cluster)) existingConfiguration := appsv1alpha1.Configuration{} err = p.ResourceFetcher.Client.Get(p.Context, client.ObjectKeyFromObject(expectedConfiguration), &existingConfiguration)