Skip to content

Commit 671b7e3

Browse files
author
玖宇
committed
refactor logic and fix comments
1 parent 77eaaae commit 671b7e3

12 files changed

Lines changed: 1093 additions & 778 deletions

File tree

api/workloads/v1alpha1/rolebasedgroup_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ type CoordinationStrategy struct {
6969
}
7070

7171
type SegmentScheduling struct {
72-
// Segment defines the deployment segments and their replica counts for each role.
72+
// SegmentSize defines the deployment segments and their replica counts for each role.
7373
// Key is the role name, value is the number of replicas in this segment.
74-
Segment map[string]int `json:"segment"`
74+
SegmentSize map[string]int32 `json:"segment"`
7575

7676
// PartitionStrategy defines the strategy for deploying segments in a partitioned manner.
7777
// Determines whether to wait for the current segment to be ready before creating the next one.

api/workloads/v1alpha1/zz_generated.deepcopy.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client-go/applyconfiguration/workloads/v1alpha1/segmentscheduling.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/workloads.x-k8s.io_rolebasedgroups.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ spec:
103103
type: string
104104
segment:
105105
additionalProperties:
106+
format: int32
106107
type: integer
107108
description: |-
108-
Segment defines the deployment segments and their replica counts for each role.
109+
SegmentSize defines the deployment segments and their replica counts for each role.
109110
Key is the role name, value is the number of replicas in this segment.
110111
type: object
111112
required:

config/crd/bases/workloads.x-k8s.io_rolebasedgroupsets.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ spec:
118118
type: string
119119
segment:
120120
additionalProperties:
121+
format: int32
121122
type: integer
122123
description: |-
123-
Segment defines the deployment segments and their replica counts for each role.
124+
SegmentSize defines the deployment segments and their replica counts for each role.
124125
Key is the role name, value is the number of replicas in this segment.
125126
type: object
126127
required:

deploy/helm/rbgs/crds/workloads.x-k8s.io_rolebasedgroups.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ spec:
103103
type: string
104104
segment:
105105
additionalProperties:
106+
format: int32
106107
type: integer
107108
description: |-
108-
Segment defines the deployment segments and their replica counts for each role.
109+
SegmentSize defines the deployment segments and their replica counts for each role.
109110
Key is the role name, value is the number of replicas in this segment.
110111
type: object
111112
required:

deploy/helm/rbgs/crds/workloads.x-k8s.io_rolebasedgroupsets.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ spec:
118118
type: string
119119
segment:
120120
additionalProperties:
121+
format: int32
121122
type: integer
122123
description: |-
123-
Segment defines the deployment segments and their replica counts for each role.
124+
SegmentSize defines the deployment segments and their replica counts for each role.
124125
Key is the role name, value is the number of replicas in this segment.
125126
type: object
126127
required:

internal/controller/workloads/rolebasedgroup_controller.go

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"sigs.k8s.io/controller-runtime/pkg/reconcile"
5151
lwsv1 "sigs.k8s.io/lws/api/leaderworkerset/v1"
5252
workloadsv1alpha1 "sigs.k8s.io/rbgs/api/workloads/v1alpha1"
53+
"sigs.k8s.io/rbgs/pkg/coordination/segmentscheduling"
5354
"sigs.k8s.io/rbgs/pkg/dependency"
5455
"sigs.k8s.io/rbgs/pkg/reconciler"
5556
"sigs.k8s.io/rbgs/pkg/scale"
@@ -178,37 +179,38 @@ func (r *RoleBasedGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reque
178179
roleStatuses = append(roleStatuses, roleStatus)
179180
}
180181

181-
// Update the status based on the observed role statuses.
182-
if updateStatus {
183-
if err := r.updateRBGStatus(ctx, rbg, roleStatuses); err != nil {
184-
r.recorder.Eventf(
185-
rbg, corev1.EventTypeWarning, FailedUpdateStatus,
186-
"Failed to update status for %s: %v", rbg.Name, err,
187-
)
188-
return ctrl.Result{}, err
189-
}
190-
}
191-
192-
// Calculate the rolling update strategy for all coordination specifications in rbg.
193-
rollingUpdateStrategies, err := r.CalculateRollingUpdateForAllCoordination(rbg, roleStatuses)
182+
// [Coordination - SegmentScheduling] Calculate segment target replicas.
183+
segmentScheduler := segmentscheduling.NewSegmentScheduler(rbg, roleStatuses)
184+
segmentTargetReplicas, err := segmentScheduler.CalculateTargetReplicas()
194185
if err != nil {
195186
r.recorder.Eventf(
196187
rbg, corev1.EventTypeWarning, FailedUpdateStatus,
197-
"Failed to calculate rolling update strategy for %s: %v", rbg.Name, err,
188+
"Failed to calculate segment target replicas for %s: %v", rbg.Name, err,
198189
)
199190
return ctrl.Result{}, err
200191
}
201192

202-
// Calculate segment target replicas if segment scheduling is enabled
203-
segmentTargetReplicas, err := r.CalculateSegmentTargetReplicas(rbg, roleStatuses)
193+
// [Coordination - RollingUpdate] Calculate the rolling update strategy for all coordination specifications in rbg.
194+
rollingUpdateStrategies, err := r.CalculateRollingUpdateForAllCoordination(rbg, roleStatuses)
204195
if err != nil {
205196
r.recorder.Eventf(
206197
rbg, corev1.EventTypeWarning, FailedUpdateStatus,
207-
"Failed to calculate segment target replicas for %s: %v", rbg.Name, err,
198+
"Failed to calculate rolling update strategy for %s: %v", rbg.Name, err,
208199
)
209200
return ctrl.Result{}, err
210201
}
211202

203+
// Update the status based on the observed role statuses.
204+
if updateStatus {
205+
if err := r.updateRBGStatus(ctx, rbg, roleStatuses, segmentTargetReplicas); err != nil {
206+
r.recorder.Eventf(
207+
rbg, corev1.EventTypeWarning, FailedUpdateStatus,
208+
"Failed to update status for %s: %v", rbg.Name, err,
209+
)
210+
return ctrl.Result{}, err
211+
}
212+
}
213+
212214
// Reconcile roles, do create/update actions for roles.
213215
for _, roleList := range sortedRoles {
214216
var errs error
@@ -369,11 +371,21 @@ func (r *RoleBasedGroupReconciler) deleteOrphanRoles(ctx context.Context, rbg *w
369371
return errors.NewAggregate(errs)
370372
}
371373

374+
// updateRBGStatus updates the status of the RoleBasedGroup based on the current role statuses
375+
// Parameters:
376+
// - ctx: context for the operation
377+
// - rbg: the RoleBasedGroup resource to update
378+
// - roleStatuses: current status of all roles
379+
// - segmentTargetReplicas: pre-calculated segment target replicas (nil if segment scheduling is not active)
380+
// This is passed as parameter to avoid redundant calculation in the reconcile loop
372381
func (r *RoleBasedGroupReconciler) updateRBGStatus(
373-
ctx context.Context, rbg *workloadsv1alpha1.RoleBasedGroup, roleStatuses []workloadsv1alpha1.RoleStatus,
382+
ctx context.Context,
383+
rbg *workloadsv1alpha1.RoleBasedGroup,
384+
roleStatuses []workloadsv1alpha1.RoleStatus,
385+
segmentTargetReplicas map[string]int32,
374386
) error {
375387
// Check if segment scheduling is active
376-
segmentSchedulingActive := r.isSegmentSchedulingActive(rbg)
388+
segmentSchedulingActive := segmentscheduling.IsActive(rbg)
377389

378390
// update ready condition
379391
var rbgReady = true
@@ -382,21 +394,6 @@ func (r *RoleBasedGroupReconciler) updateRBGStatus(
382394
statusMap[rs.Name] = rs
383395
}
384396

385-
// Build target replicas map if segment scheduling is active
386-
var segmentTargetReplicas map[string]int32
387-
if segmentSchedulingActive {
388-
for _, coordination := range rbg.Spec.CoordinationRequirements {
389-
if coordination.Strategy != nil && coordination.Strategy.SegmentScheduling != nil {
390-
scheduler := reconciler.NewSegmentScheduler(rbg, &coordination, roleStatuses)
391-
var err error
392-
segmentTargetReplicas, err = scheduler.CalculateTargetReplicas()
393-
if err == nil && segmentTargetReplicas != nil {
394-
break
395-
}
396-
}
397-
}
398-
}
399-
400397
for _, role := range rbg.Spec.Roles {
401398
rs, ok := statusMap[role.Name]
402399
if !ok {
@@ -891,31 +888,6 @@ func calculateCoordinationUpdatedReplicasBound(maxSkew intstr.IntOrString, refUp
891888
return int32(lower), int32(upper)
892889
}
893890

894-
// CalculateSegmentTargetReplicas calculates the target replicas for each role based on segment scheduling
895-
func (r *RoleBasedGroupReconciler) CalculateSegmentTargetReplicas(
896-
rbg *workloadsv1alpha1.RoleBasedGroup,
897-
roleStatuses []workloadsv1alpha1.RoleStatus,
898-
) (map[string]int32, error) {
899-
// Check if any coordination has segment scheduling
900-
for _, coordination := range rbg.Spec.CoordinationRequirements {
901-
if coordination.Strategy != nil && coordination.Strategy.SegmentScheduling != nil {
902-
scheduler := reconciler.NewSegmentScheduler(rbg, &coordination, roleStatuses)
903-
return scheduler.CalculateTargetReplicas()
904-
}
905-
}
906-
return nil, nil
907-
}
908-
909-
// isSegmentSchedulingActive checks if any coordination has segment scheduling enabled
910-
func (r *RoleBasedGroupReconciler) isSegmentSchedulingActive(rbg *workloadsv1alpha1.RoleBasedGroup) bool {
911-
for _, coordination := range rbg.Spec.CoordinationRequirements {
912-
if coordination.Strategy != nil && coordination.Strategy.SegmentScheduling != nil {
913-
return true
914-
}
915-
}
916-
return false
917-
}
918-
919891
func RBGPredicate() predicate.Funcs {
920892
return predicate.Funcs{
921893
CreateFunc: func(e event.CreateEvent) bool {

0 commit comments

Comments
 (0)