@@ -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
372381func (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-
919891func RBGPredicate () predicate.Funcs {
920892 return predicate.Funcs {
921893 CreateFunc : func (e event.CreateEvent ) bool {
0 commit comments