Skip to content

Commit

Permalink
chore: support sharding component hscale to rerender
Browse files Browse the repository at this point in the history
  • Loading branch information
sophon-zt committed Jan 17, 2025
1 parent 5be65e3 commit 2789c3a
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 7 deletions.
7 changes: 4 additions & 3 deletions apis/apps/v1alpha1/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ spec:
- vscale
- hscale
- tls
- shardingHScale
type: string
type: array
x-kubernetes-list-type: set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17918,6 +17918,7 @@ spec:
- vscale
- hscale
- tls
- shardingHScale
type: string
type: array
x-kubernetes-list-type: set
Expand Down
1 change: 1 addition & 0 deletions config/crd/bases/apps.kubeblocks.io_configurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ spec:
- vscale
- hscale
- tls
- shardingHScale
type: string
type: array
x-kubernetes-list-type: set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ spec:
- vscale
- hscale
- tls
- shardingHScale
type: string
type: array
x-kubernetes-list-type: set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17918,6 +17918,7 @@ spec:
- vscale
- hscale
- tls
- shardingHScale
type: string
type: array
x-kubernetes-list-type: set
Expand Down
1 change: 1 addition & 0 deletions deploy/helm/crds/apps.kubeblocks.io_configurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ spec:
- vscale
- hscale
- tls
- shardingHScale
type: string
type: array
x-kubernetes-list-type: set
Expand Down
2 changes: 2 additions & 0 deletions docs/developer_docs/api-reference/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -24453,6 +24453,8 @@ StatefulSetSpec
<td></td>
</tr><tr><td><p>&#34;vscale&#34;</p></td>
<td></td>
</tr><tr><td><p>&#34;shardingHScale&#34;</p></td>
<td></td>
</tr></tbody>
</table>
<h3 id="apps.kubeblocks.io/v1alpha1.ResourceMeta">ResourceMeta
Expand Down
1 change: 1 addition & 0 deletions pkg/constant/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ const (
ComponentResourcePayload = "component-resource"
ReplicasPayload = "replicas"
BinaryVersionPayload = "binary-version"
ShardingPayload = "sharding"
)
40 changes: 39 additions & 1 deletion pkg/controller/configuration/config_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"context"
"fmt"
"slices"
"strconv"
"strings"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -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]
Expand All @@ -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
}
Expand All @@ -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)
}
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/controller/configuration/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2789c3a

Please sign in to comment.