-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: h-scale opsRequest validation considers replicas and shards limit #8834
base: main
Are you sure you want to change the base?
Changes from all commits
7c692a5
bb29330
f8b2a1d
43f0698
bf1e1e9
6508e25
4671a9d
24d3e31
fa1449e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,7 +323,7 @@ func compareQuantity(requestQuantity, limitQuantity *resource.Quantity) bool { | |
} | ||
|
||
// validateHorizontalScaling validates api when spec.type is HorizontalScaling | ||
func (r *OpsRequest) validateHorizontalScaling(_ context.Context, _ client.Client, cluster *appsv1.Cluster) error { | ||
func (r *OpsRequest) validateHorizontalScaling(ctx context.Context, cli client.Client, cluster *appsv1.Cluster) error { | ||
horizontalScalingList := r.Spec.HorizontalScalingList | ||
if len(horizontalScalingList) == 0 { | ||
return notEmptyError("spec.horizontalScaling") | ||
|
@@ -338,15 +338,41 @@ func (r *OpsRequest) validateHorizontalScaling(_ context.Context, _ client.Clien | |
return err | ||
} | ||
for _, comSpec := range cluster.Spec.ComponentSpecs { | ||
// Default values if no limit is found | ||
minNum, maxNum := 0, 16384 | ||
if comSpec.ComponentDef != "" { | ||
compDef := &appsv1.ComponentDefinition{} | ||
if err := cli.Get(ctx, client.ObjectKey{Name: comSpec.ComponentDef, Namespace: r.Namespace}, compDef); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should only be obtained when h-scale. |
||
return err | ||
} | ||
if compDef != nil && compDef.Spec.ReplicasLimit != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The compDef must not be nil. |
||
minNum = int(compDef.Spec.ReplicasLimit.MinReplicas) | ||
maxNum = int(compDef.Spec.ReplicasLimit.MaxReplicas) | ||
} | ||
} | ||
if hScale, ok := hScaleMap[comSpec.Name]; ok { | ||
if err := r.validateHorizontalScalingSpec(hScale, comSpec, cluster.Name, false); err != nil { | ||
if err := r.validateHorizontalScalingSpec(hScale, comSpec, cluster.Name, false, maxNum, minNum); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
for _, spec := range cluster.Spec.Shardings { | ||
// Default values if no limit is found | ||
minNum, maxNum := 0, 2048 | ||
if spec.ShardingDef != "" { | ||
shardingDef := &appsv1.ShardingDefinition{} | ||
if err := cli.Get(ctx, types.NamespacedName{Name: spec.ShardingDef, Namespace: r.Namespace}, shardingDef); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
return err | ||
} | ||
|
||
if shardingDef != nil && shardingDef.Spec.ShardsLimit != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
minNum = int(shardingDef.Spec.ShardsLimit.MinShards) | ||
maxNum = int(shardingDef.Spec.ShardsLimit.MaxShards) | ||
} | ||
} | ||
|
||
if hScale, ok := hScaleMap[spec.Name]; ok { | ||
if err := r.validateHorizontalScalingSpec(hScale, spec.Template, cluster.Name, true); err != nil { | ||
if err := r.validateHorizontalScalingSpec(hScale, spec.Template, cluster.Name, true, maxNum, minNum); err != nil { | ||
return err | ||
} | ||
} | ||
|
@@ -364,7 +390,7 @@ func (r *OpsRequest) CountOfflineOrOnlineInstances(clusterName, componentName st | |
return offlineOrOnlineInsCountMap | ||
} | ||
|
||
func (r *OpsRequest) validateHorizontalScalingSpec(hScale HorizontalScaling, compSpec appsv1.ClusterComponentSpec, clusterName string, isSharding bool) error { | ||
func (r *OpsRequest) validateHorizontalScalingSpec(hScale HorizontalScaling, compSpec appsv1.ClusterComponentSpec, clusterName string, isSharding bool, maxReplicasOrShards int, minReplicasOrShards int) error { | ||
scaleIn := hScale.ScaleIn | ||
scaleOut := hScale.ScaleOut | ||
if hScale.Shards != nil { | ||
|
@@ -374,6 +400,12 @@ func (r *OpsRequest) validateHorizontalScalingSpec(hScale HorizontalScaling, com | |
if scaleOut != nil || scaleIn != nil { | ||
return fmt.Errorf(`shards field cannot be used together with scaleOut or scaleIn for the component "%s"`, hScale.ComponentName) | ||
} | ||
if int(*hScale.Shards) < minReplicasOrShards { | ||
return fmt.Errorf(`the number of shards after horizontal scale violates the shards limit "%s"`, hScale.ComponentName) | ||
} | ||
if int(*hScale.Shards) > maxReplicasOrShards { | ||
return fmt.Errorf(`the number of shards after horizontal scale violates the shards limit "%s"`, hScale.ComponentName) | ||
} | ||
return nil | ||
} | ||
if lastCompConfiguration, ok := r.Status.LastConfiguration.Components[hScale.ComponentName]; ok { | ||
|
@@ -438,6 +470,19 @@ func (r *OpsRequest) validateHorizontalScalingSpec(hScale HorizontalScaling, com | |
if replicaChanger.ReplicaChanges != nil && allReplicaChanges > *replicaChanger.ReplicaChanges { | ||
return fmt.Errorf(`%s "replicaChanges" can't be less than the sum of "replicaChanges" for specified instance templates`, msgPrefix) | ||
} | ||
|
||
var replicaChange int32 | ||
if replicaChanger.ReplicaChanges != nil { | ||
replicaChange = *replicaChanger.ReplicaChanges | ||
} else { | ||
replicaChange = allReplicaChanges | ||
} | ||
if isScaleIn && int(compSpec.Replicas)-int(replicaChange) < minReplicasOrShards { | ||
return fmt.Errorf(`the number of replicas after scaling down violates the replica limit for component "%s"`, hScale.ComponentName) | ||
} | ||
if !isScaleIn && int(compSpec.Replicas)+int(replicaChange) > maxReplicasOrShards { | ||
return fmt.Errorf(`the number of replicas after scaling up violates the replica limit for component "%s"`, hScale.ComponentName) | ||
} | ||
return nil | ||
} | ||
if scaleIn != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default min replicas limit is 1, but not 0.