Skip to content

Commit bb31661

Browse files
authored
[RayService] Refactor updateRayClusterInstance (#2875)
1 parent 5b8e9c6 commit bb31661

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

ray-operator/controllers/ray/rayservice_controller.go

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -514,21 +514,31 @@ func (r *RayServiceReconciler) reconcileRayCluster(ctx context.Context, rayServi
514514
if shouldUpdateCluster(rayServiceInstance, activeRayCluster, true) {
515515
// TODO(kevin85421): We should not reconstruct the cluster to update it. This will cause issues if autoscaler is enabled.
516516
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 {
518519
return nil, nil, err
519520
}
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)
521526
return activeRayCluster, pendingRayCluster, err
522527
}
523528

524529
if shouldUpdateCluster(rayServiceInstance, pendingRayCluster, false) {
525530
// TODO(kevin85421): We should not reconstruct the cluster to update it. This will cause issues if autoscaler is enabled.
526531
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 {
528534
return nil, nil, err
529535
}
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
532542
}
533543

534544
return activeRayCluster, pendingRayCluster, nil
@@ -695,41 +705,30 @@ func shouldPrepareNewCluster(ctx context.Context, rayServiceInstance *rayv1.RayS
695705
return isZeroDowntimeUpgradeEnabled(ctx, rayServiceInstance.Spec.UpgradeStrategy)
696706
}
697707

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) {
700712
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-
}
712713

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+
))
716723
}
724+
logger.Info("updateRayClusterInstance", "Name", goalCluster.Name, "Namespace", goalCluster.Namespace)
717725

718726
// Update the fetched RayCluster with new changes
719-
currentRayCluster.Spec = rayClusterInstance.Spec
727+
currentCluster.Spec = goalCluster.Spec
720728

721729
// 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
733732
}
734733

735734
func (r *RayServiceReconciler) createRayClusterInstance(ctx context.Context, rayServiceInstance *rayv1.RayService) (*rayv1.RayCluster, error) {

0 commit comments

Comments
 (0)