@@ -514,21 +514,31 @@ func (r *RayServiceReconciler) reconcileRayCluster(ctx context.Context, rayServi
514
514
if shouldUpdateCluster (rayServiceInstance , activeRayCluster , true ) {
515
515
// TODO(kevin85421): We should not reconstruct the cluster to update it. This will cause issues if autoscaler is enabled.
516
516
logger .Info ("Updating the active RayCluster instance" , "clusterName" , activeRayCluster .Name )
517
- if activeRayCluster , err = constructRayClusterForRayService (rayServiceInstance , activeRayCluster .Name , r .Scheme ); err != nil {
517
+ goalCluster , err := constructRayClusterForRayService (rayServiceInstance , activeRayCluster .Name , r .Scheme )
518
+ if err != nil {
518
519
return nil , nil , err
519
520
}
520
- err = r .updateRayClusterInstance (ctx , rayServiceInstance , activeRayCluster )
521
+ modifyRayCluster (ctx , activeRayCluster , goalCluster )
522
+ if err = r .Update (ctx , activeRayCluster ); err != nil {
523
+ r .Recorder .Eventf (rayServiceInstance , corev1 .EventTypeWarning , string (utils .FailedToUpdateRayCluster ), "Failed to update the active RayCluster %s/%s: %v" , activeRayCluster .Namespace , activeRayCluster .Name , err )
524
+ }
525
+ r .Recorder .Eventf (rayServiceInstance , corev1 .EventTypeNormal , string (utils .UpdatedRayCluster ), "Updated the active RayCluster %s/%s" , activeRayCluster .Namespace , activeRayCluster .Name )
521
526
return activeRayCluster , pendingRayCluster , err
522
527
}
523
528
524
529
if shouldUpdateCluster (rayServiceInstance , pendingRayCluster , false ) {
525
530
// TODO(kevin85421): We should not reconstruct the cluster to update it. This will cause issues if autoscaler is enabled.
526
531
logger .Info ("Updating the pending RayCluster instance" , "clusterName" , pendingRayCluster .Name )
527
- if pendingRayCluster , err = constructRayClusterForRayService (rayServiceInstance , pendingRayCluster .Name , r .Scheme ); err != nil {
532
+ goalCluster , err := constructRayClusterForRayService (rayServiceInstance , pendingRayCluster .Name , r .Scheme )
533
+ if err != nil {
528
534
return nil , nil , err
529
535
}
530
- err = r .updateRayClusterInstance (ctx , rayServiceInstance , pendingRayCluster )
531
- return activeRayCluster , pendingRayCluster , err
536
+ modifyRayCluster (ctx , pendingRayCluster , goalCluster )
537
+ if err = r .Update (ctx , pendingRayCluster ); err != nil {
538
+ r .Recorder .Eventf (rayServiceInstance , corev1 .EventTypeWarning , string (utils .FailedToUpdateRayCluster ), "Failed to update the pending RayCluster %s/%s: %v" , pendingRayCluster .Namespace , pendingRayCluster .Name , err )
539
+ }
540
+ r .Recorder .Eventf (rayServiceInstance , corev1 .EventTypeNormal , string (utils .UpdatedRayCluster ), "Updated the pending RayCluster %s/%s" , pendingRayCluster .Namespace , pendingRayCluster .Name )
541
+ return activeRayCluster , pendingRayCluster , nil
532
542
}
533
543
534
544
return activeRayCluster , pendingRayCluster , nil
@@ -695,41 +705,30 @@ func shouldPrepareNewCluster(ctx context.Context, rayServiceInstance *rayv1.RayS
695
705
return isZeroDowntimeUpgradeEnabled (ctx , rayServiceInstance .Spec .UpgradeStrategy )
696
706
}
697
707
698
- // updateRayClusterInstance updates the RayCluster instance.
699
- func (r * RayServiceReconciler ) updateRayClusterInstance (ctx context.Context , rayServiceInstance * rayv1.RayService , rayClusterInstance * rayv1.RayCluster ) error {
708
+ // `modifyRayCluster` updates `currentCluster` in place based on `goalCluster`. `currentCluster` is the
709
+ // current RayCluster retrieved from the informer cache, and `goalCluster` is the target state of the
710
+ // RayCluster derived from the RayService spec.
711
+ func modifyRayCluster (ctx context.Context , currentCluster , goalCluster * rayv1.RayCluster ) {
700
712
logger := ctrl .LoggerFrom (ctx )
701
- logger .Info ("updateRayClusterInstance" , "Name" , rayClusterInstance .Name , "Namespace" , rayClusterInstance .Namespace )
702
-
703
- // Fetch the current state of the RayCluster
704
- currentRayCluster , err := r .getRayClusterByNamespacedName (ctx , client.ObjectKey {
705
- Namespace : rayClusterInstance .Namespace ,
706
- Name : rayClusterInstance .Name ,
707
- })
708
- if err != nil {
709
- err = fmt .Errorf ("failed to get the current state of RayCluster, namespace: %s, name: %s: %w" , rayClusterInstance .Namespace , rayClusterInstance .Name , err )
710
- return err
711
- }
712
713
713
- if currentRayCluster == nil {
714
- logger .Info ("RayCluster not found, possibly deleted" , "Namespace" , rayClusterInstance .Namespace , "Name" , rayClusterInstance .Name )
715
- return nil
714
+ if currentCluster .Name != goalCluster .Name || currentCluster .Namespace != goalCluster .Namespace {
715
+ panic (fmt .Sprintf (
716
+ "currentCluster and goalCluster have different names or namespaces: " +
717
+ "%s/%s != %s/%s" ,
718
+ currentCluster .Namespace ,
719
+ currentCluster .Name ,
720
+ goalCluster .Namespace ,
721
+ goalCluster .Name ,
722
+ ))
716
723
}
724
+ logger .Info ("updateRayClusterInstance" , "Name" , goalCluster .Name , "Namespace" , goalCluster .Namespace )
717
725
718
726
// Update the fetched RayCluster with new changes
719
- currentRayCluster .Spec = rayClusterInstance .Spec
727
+ currentCluster .Spec = goalCluster .Spec
720
728
721
729
// Update the labels and annotations
722
- currentRayCluster .Labels = rayClusterInstance .Labels
723
- currentRayCluster .Annotations = rayClusterInstance .Annotations
724
-
725
- // Update the RayCluster
726
- err = r .Update (ctx , currentRayCluster )
727
- if err != nil {
728
- r .Recorder .Eventf (rayServiceInstance , corev1 .EventTypeWarning , string (utils .FailedToUpdateRayCluster ), "Failed to update the RayCluster %s/%s: %v" , currentRayCluster .Namespace , currentRayCluster .Name , err )
729
- } else {
730
- r .Recorder .Eventf (rayServiceInstance , corev1 .EventTypeNormal , string (utils .UpdatedRayCluster ), "Updated the RayCluster %s/%s" , currentRayCluster .Namespace , currentRayCluster .Name )
731
- }
732
- return err
730
+ currentCluster .Labels = goalCluster .Labels
731
+ currentCluster .Annotations = goalCluster .Annotations
733
732
}
734
733
735
734
func (r * RayServiceReconciler ) createRayClusterInstance (ctx context.Context , rayServiceInstance * rayv1.RayService ) (* rayv1.RayCluster , error ) {
0 commit comments