-
Notifications
You must be signed in to change notification settings - Fork 403
/
raycluster_controller.go
1629 lines (1448 loc) · 71.6 KB
/
raycluster_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ray
import (
"context"
errstd "errors"
"fmt"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"time"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
configapi "github.com/ray-project/kuberay/ray-operator/apis/config/v1alpha1"
"github.com/ray-project/kuberay/ray-operator/controllers/ray/batchscheduler"
"github.com/ray-project/kuberay/ray-operator/controllers/ray/common"
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils"
"github.com/ray-project/kuberay/ray-operator/pkg/features"
batchv1 "k8s.io/api/batch/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/client-go/tools/record"
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
"github.com/go-logr/logr"
routev1 "github.com/openshift/api/route/v1"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
controller "sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
type reconcileFunc func(context.Context, *rayv1.RayCluster) error
var (
DefaultRequeueDuration = 2 * time.Second
// Definition of a index field for pod name
podUIDIndexField = "metadata.uid"
)
// getDiscoveryClient returns a discovery client for the current reconciler
func getDiscoveryClient(config *rest.Config) (*discovery.DiscoveryClient, error) {
return discovery.NewDiscoveryClientForConfig(config)
}
// Check where we are running. We are trying to distinguish here whether
// this is vanilla kubernetes cluster or Openshift
func getClusterType(ctx context.Context) bool {
logger := ctrl.LoggerFrom(ctx)
if os.Getenv("USE_INGRESS_ON_OPENSHIFT") == "true" {
// Environment is set to treat OpenShift cluster as Vanilla Kubernetes
return false
}
// The discovery package is used to discover APIs supported by a Kubernetes API server.
config, err := ctrl.GetConfig()
if err == nil && config != nil {
dclient, err := getDiscoveryClient(config)
if err == nil && dclient != nil {
apiGroupList, err := dclient.ServerGroups()
if err != nil {
logger.Info("Error while querying ServerGroups, assuming we're on Vanilla Kubernetes")
return false
}
for i := 0; i < len(apiGroupList.Groups); i++ {
if strings.HasSuffix(apiGroupList.Groups[i].Name, ".openshift.io") {
logger.Info("We detected being on OpenShift!")
return true
}
}
return false
}
logger.Info("Cannot retrieve a DiscoveryClient, assuming we're on Vanilla Kubernetes")
return false
}
logger.Info("Cannot retrieve config, assuming we're on Vanilla Kubernetes")
return false
}
// NewReconciler returns a new reconcile.Reconciler
func NewReconciler(ctx context.Context, mgr manager.Manager, options RayClusterReconcilerOptions, rayConfigs configapi.Configuration) *RayClusterReconciler {
if err := mgr.GetFieldIndexer().IndexField(ctx, &corev1.Pod{}, podUIDIndexField, func(rawObj client.Object) []string {
pod := rawObj.(*corev1.Pod)
return []string{string(pod.UID)}
}); err != nil {
panic(err)
}
isOpenShift := getClusterType(ctx)
// init the batch scheduler manager
schedulerMgr, err := batchscheduler.NewSchedulerManager(rayConfigs, mgr.GetConfig())
if err != nil {
// fail fast if the scheduler plugin fails to init
// prevent running the controller in an undefined state
panic(err)
}
// add schema to runtime
schedulerMgr.AddToScheme(mgr.GetScheme())
return &RayClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("raycluster-controller"),
BatchSchedulerMgr: schedulerMgr,
IsOpenShift: isOpenShift,
headSidecarContainers: options.HeadSidecarContainers,
workerSidecarContainers: options.WorkerSidecarContainers,
}
}
var _ reconcile.Reconciler = &RayClusterReconciler{}
// RayClusterReconciler reconciles a RayCluster object
type RayClusterReconciler struct {
client.Client
Scheme *k8sruntime.Scheme
Recorder record.EventRecorder
BatchSchedulerMgr *batchscheduler.SchedulerManager
headSidecarContainers []corev1.Container
workerSidecarContainers []corev1.Container
IsOpenShift bool
}
type RayClusterReconcilerOptions struct {
HeadSidecarContainers []corev1.Container
WorkerSidecarContainers []corev1.Container
}
// Reconcile reads that state of the cluster for a RayCluster object and makes changes based on it
// and what is in the RayCluster.Spec
// Automatically generate RBAC rules to allow the Controller to read and write workloads
// +kubebuilder:rbac:groups=ray.io,resources=rayclusters,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=ray.io,resources=rayclusters/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=ray.io,resources=rayclusters/finalizers,verbs=update
// +kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch;create;update;patch;delete;deletecollection
// +kubebuilder:rbac:groups=core,resources=pods/status,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=services/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;create;update
// +kubebuilder:rbac:groups=networking.k8s.io,resources=ingressclasses,verbs=get;list;watch
// +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;delete;patch
// +kubebuilder:rbac:groups=route.openshift.io,resources=routes,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=extensions,resources=ingresses,verbs=get;list;watch;create;update;delete;patch
// +kubebuilder:rbac:groups=core,resources=serviceaccounts,verbs=get;list;watch;create;delete
// +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=roles,verbs=get;list;watch;create;delete;update
// +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=rolebindings,verbs=get;list;watch;create;delete
// [WARNING]: There MUST be a newline after kubebuilder markers.
// Reconcile used to bridge the desired state with the current state
func (r *RayClusterReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
logger := ctrl.LoggerFrom(ctx)
var err error
// Try to fetch the RayCluster instance
instance := &rayv1.RayCluster{}
if err = r.Get(ctx, request.NamespacedName, instance); err == nil {
return r.rayClusterReconcile(ctx, request, instance)
}
// No match found
if errors.IsNotFound(err) {
logger.Info("Read request instance not found error!")
} else {
logger.Error(err, "Read request instance error!")
}
// Error reading the object - requeue the request.
return ctrl.Result{}, client.IgnoreNotFound(err)
}
func (r *RayClusterReconciler) deleteAllPods(ctx context.Context, filters common.AssociationOptions) (pods corev1.PodList, err error) {
logger := ctrl.LoggerFrom(ctx)
if err = r.List(ctx, &pods, filters.ToListOptions()...); err != nil {
return pods, err
}
active := 0
for _, pod := range pods.Items {
if pod.DeletionTimestamp.IsZero() {
active++
}
}
if active > 0 {
logger.Info("Deleting all Pods with labels", "filters", filters, "Number of active Pods", active)
return pods, r.DeleteAllOf(ctx, &corev1.Pod{}, filters.ToDeleteOptions()...)
}
return pods, nil
}
func (r *RayClusterReconciler) validateRayClusterStatus(instance *rayv1.RayCluster) error {
suspending := meta.IsStatusConditionTrue(instance.Status.Conditions, string(rayv1.RayClusterSuspending))
suspended := meta.IsStatusConditionTrue(instance.Status.Conditions, string(rayv1.RayClusterSuspended))
if suspending && suspended {
return errstd.New("invalid RayCluster State: rayv1.RayClusterSuspending and rayv1.RayClusterSuspended conditions should not be both true")
}
return nil
}
func (r *RayClusterReconciler) rayClusterReconcile(ctx context.Context, request ctrl.Request, instance *rayv1.RayCluster) (ctrl.Result, error) {
var reconcileErr error
logger := ctrl.LoggerFrom(ctx)
if err := r.validateRayClusterStatus(instance); err != nil {
logger.Error(err, "The RayCluster status is invalid")
r.Recorder.Eventf(instance, corev1.EventTypeWarning, string(utils.InvalidRayClusterStatus),
"The RayCluster status is invalid %s/%s, %v", instance.Namespace, instance.Name, err)
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
// Please do NOT modify `originalRayClusterInstance` in the following code.
originalRayClusterInstance := instance.DeepCopy()
// The `enableGCSFTRedisCleanup` is a feature flag introduced in KubeRay v1.0.0. It determines whether
// the Redis cleanup job should be activated. Users can disable the feature by setting the environment
// variable `ENABLE_GCS_FT_REDIS_CLEANUP` to `false`, and undertake the Redis storage namespace cleanup
// manually after the RayCluster CR deletion.
enableGCSFTRedisCleanup := strings.ToLower(os.Getenv(utils.ENABLE_GCS_FT_REDIS_CLEANUP)) != "false"
if enableGCSFTRedisCleanup && common.IsGCSFaultToleranceEnabled(*instance) {
if instance.DeletionTimestamp.IsZero() {
if !controllerutil.ContainsFinalizer(instance, utils.GCSFaultToleranceRedisCleanupFinalizer) {
logger.Info(
"GCS fault tolerance has been enabled. Implementing a finalizer to ensure that Redis is properly cleaned up once the RayCluster custom resource (CR) is deleted.",
"finalizer", utils.GCSFaultToleranceRedisCleanupFinalizer)
controllerutil.AddFinalizer(instance, utils.GCSFaultToleranceRedisCleanupFinalizer)
if err := r.Update(ctx, instance); err != nil {
err = fmt.Errorf("Failed to add the finalizer %s to the RayCluster: %w", utils.GCSFaultToleranceRedisCleanupFinalizer, err)
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
// Only start the RayCluster reconciliation after the finalizer is added.
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, nil
}
} else {
logger.Info(
fmt.Sprintf("The RayCluster with GCS enabled, %s, is being deleted. Start to handle the Redis cleanup finalizer %s.",
instance.Name, utils.GCSFaultToleranceRedisCleanupFinalizer),
"DeletionTimestamp", instance.ObjectMeta.DeletionTimestamp)
// Delete the head Pod if it exists.
headPods, err := r.deleteAllPods(ctx, common.RayClusterHeadPodsAssociationOptions(instance))
if err != nil {
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
// Delete all worker Pods if they exist.
if _, err = r.deleteAllPods(ctx, common.RayClusterWorkerPodsAssociationOptions(instance)); err != nil {
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
if len(headPods.Items) > 0 {
logger.Info(fmt.Sprintf(
"Wait for the head Pod %s to be terminated before initiating the Redis cleanup process. "+
"The storage namespace %s in Redis cannot be fully deleted if the GCS process on the head Pod is still writing to it.",
headPods.Items[0].Name, headPods.Items[0].Annotations[utils.RayExternalStorageNSAnnotationKey]))
// Requeue after 10 seconds because it takes much longer than DefaultRequeueDuration (2 seconds) for the head Pod to be terminated.
return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
}
// We can start the Redis cleanup process now because the head Pod has been terminated.
filterLabels := client.MatchingLabels{utils.RayClusterLabelKey: instance.Name, utils.RayNodeTypeLabelKey: string(rayv1.RedisCleanupNode)}
redisCleanupJobs := batchv1.JobList{}
if err := r.List(ctx, &redisCleanupJobs, client.InNamespace(instance.Namespace), filterLabels); err != nil {
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
if len(redisCleanupJobs.Items) != 0 {
// Check whether the Redis cleanup Job has been completed.
redisCleanupJob := redisCleanupJobs.Items[0]
logger.Info("Redis cleanup Job status", "Job name", redisCleanupJob.Name,
"Active", redisCleanupJob.Status.Active, "Succeeded", redisCleanupJob.Status.Succeeded, "Failed", redisCleanupJob.Status.Failed)
if condition, finished := utils.IsJobFinished(&redisCleanupJob); finished {
controllerutil.RemoveFinalizer(instance, utils.GCSFaultToleranceRedisCleanupFinalizer)
if err := r.Update(ctx, instance); err != nil {
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
switch condition {
case batchv1.JobComplete:
logger.Info(fmt.Sprintf(
"The Redis cleanup Job %s has been completed. "+
"The storage namespace %s in Redis has been fully deleted.",
redisCleanupJob.Name, redisCleanupJob.Annotations[utils.RayExternalStorageNSAnnotationKey]))
case batchv1.JobFailed:
logger.Info(fmt.Sprintf(
"The Redis cleanup Job %s has failed, requeue the RayCluster CR after 5 minute. "+
"You should manually delete the storage namespace %s in Redis and remove the RayCluster's finalizer. "+
"Please check https://docs.ray.io/en/master/cluster/kubernetes/user-guides/kuberay-gcs-ft.html for more details.",
redisCleanupJob.Name, redisCleanupJob.Annotations[utils.RayExternalStorageNSAnnotationKey]))
}
return ctrl.Result{}, nil
}
// the redisCleanupJob is still running
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, nil
}
redisCleanupJob := r.buildRedisCleanupJob(ctx, *instance)
if err := r.Create(ctx, &redisCleanupJob); err != nil {
if errors.IsAlreadyExists(err) {
logger.Info("Redis cleanup Job already exists. Requeue the RayCluster CR.")
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, nil
}
r.Recorder.Eventf(instance, corev1.EventTypeWarning, string(utils.FailedToCreateRedisCleanupJob),
"Failed to create Redis cleanup Job %s/%s, %v", redisCleanupJob.Namespace, redisCleanupJob.Name, err)
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
logger.Info("Created Redis cleanup Job", "name", redisCleanupJob.Name)
r.Recorder.Eventf(instance, corev1.EventTypeNormal, string(utils.CreatedRedisCleanupJob),
"Created Redis cleanup Job %s/%s", redisCleanupJob.Namespace, redisCleanupJob.Name)
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, nil
}
}
if instance.DeletionTimestamp != nil && !instance.DeletionTimestamp.IsZero() {
logger.Info("RayCluster is being deleted, just ignore", "cluster name", request.Name)
return ctrl.Result{}, nil
}
reconcileFuncs := []reconcileFunc{
r.reconcileAutoscalerServiceAccount,
r.reconcileAutoscalerRole,
r.reconcileAutoscalerRoleBinding,
r.reconcileIngress,
r.reconcileHeadService,
r.reconcileHeadlessService,
r.reconcileServeService,
r.reconcilePods,
}
for _, fn := range reconcileFuncs {
if reconcileErr = fn(ctx, instance); reconcileErr != nil {
funcName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
logger.Error(reconcileErr, "Error reconcile resources", "function name", funcName)
break
}
}
// Calculate the new status for the RayCluster. Note that the function will deep copy `instance` instead of mutating it.
newInstance, calculateErr := r.calculateStatus(ctx, instance, reconcileErr)
var updateErr error
var inconsistent bool
if calculateErr != nil {
logger.Info("Got error when calculating new status", "cluster name", request.Name, "error", calculateErr)
} else {
inconsistent, updateErr = r.updateRayClusterStatus(ctx, originalRayClusterInstance, newInstance)
}
// Return error based on order.
var err error
if reconcileErr != nil {
err = reconcileErr
} else if calculateErr != nil {
err = calculateErr
} else {
err = updateErr
}
// If the custom resource's status is updated, requeue the reconcile key.
// Without this behavior, atomic operations such as the suspend operation would need to wait for `RAYCLUSTER_DEFAULT_REQUEUE_SECONDS` to delete Pods
// after the condition rayv1.RayClusterSuspending is set to true.
if err != nil || inconsistent {
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
// Unconditionally requeue after the number of seconds specified in the
// environment variable RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV. If the
// environment variable is not set, requeue after the default value.
requeueAfterSeconds, err := strconv.Atoi(os.Getenv(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV))
if err != nil {
logger.Info(fmt.Sprintf("Environment variable %s is not set, using default value of %d seconds", utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV, utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS), "cluster name", request.Name)
requeueAfterSeconds = utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS
}
logger.Info("Unconditional requeue after", "cluster name", request.Name, "seconds", requeueAfterSeconds)
return ctrl.Result{RequeueAfter: time.Duration(requeueAfterSeconds) * time.Second}, nil
}
// Checks whether the old and new RayClusterStatus are inconsistent by comparing different fields. If the only
// differences between the old and new status are the `LastUpdateTime` and `ObservedGeneration` fields, the
// status update will not be triggered.
//
// TODO (kevin85421): The field `ObservedGeneration` is not being well-maintained at the moment. In the future,
// this field should be used to determine whether to update this CR or not.
func (r *RayClusterReconciler) inconsistentRayClusterStatus(ctx context.Context, oldStatus rayv1.RayClusterStatus, newStatus rayv1.RayClusterStatus) bool {
logger := ctrl.LoggerFrom(ctx)
if oldStatus.State != newStatus.State || oldStatus.Reason != newStatus.Reason { //nolint:staticcheck // https://github.com/ray-project/kuberay/pull/2288
logger.Info("inconsistentRayClusterStatus", "detect inconsistency", fmt.Sprintf(
"old State: %s, new State: %s, old Reason: %s, new Reason: %s",
oldStatus.State, newStatus.State, oldStatus.Reason, newStatus.Reason)) //nolint:staticcheck // https://github.com/ray-project/kuberay/pull/2288
return true
}
if oldStatus.ReadyWorkerReplicas != newStatus.ReadyWorkerReplicas ||
oldStatus.AvailableWorkerReplicas != newStatus.AvailableWorkerReplicas ||
oldStatus.DesiredWorkerReplicas != newStatus.DesiredWorkerReplicas ||
oldStatus.MinWorkerReplicas != newStatus.MinWorkerReplicas ||
oldStatus.MaxWorkerReplicas != newStatus.MaxWorkerReplicas {
logger.Info("inconsistentRayClusterStatus", "detect inconsistency", fmt.Sprintf(
"old ReadyWorkerReplicas: %d, new ReadyWorkerReplicas: %d, "+
"old AvailableWorkerReplicas: %d, new AvailableWorkerReplicas: %d, "+
"old DesiredWorkerReplicas: %d, new DesiredWorkerReplicas: %d, "+
"old MinWorkerReplicas: %d, new MinWorkerReplicas: %d, "+
"old MaxWorkerReplicas: %d, new MaxWorkerReplicas: %d",
oldStatus.ReadyWorkerReplicas, newStatus.ReadyWorkerReplicas,
oldStatus.AvailableWorkerReplicas, newStatus.AvailableWorkerReplicas,
oldStatus.DesiredWorkerReplicas, newStatus.DesiredWorkerReplicas,
oldStatus.MinWorkerReplicas, newStatus.MinWorkerReplicas,
oldStatus.MaxWorkerReplicas, newStatus.MaxWorkerReplicas))
return true
}
if !reflect.DeepEqual(oldStatus.Endpoints, newStatus.Endpoints) || !reflect.DeepEqual(oldStatus.Head, newStatus.Head) {
logger.Info("inconsistentRayClusterStatus", "detect inconsistency", fmt.Sprintf(
"old Endpoints: %v, new Endpoints: %v, old Head: %v, new Head: %v",
oldStatus.Endpoints, newStatus.Endpoints, oldStatus.Head, newStatus.Head))
return true
}
if !reflect.DeepEqual(oldStatus.Conditions, newStatus.Conditions) {
logger.Info("inconsistentRayClusterStatus", "old conditions", oldStatus.Conditions, "new conditions", newStatus.Conditions)
return true
}
return false
}
func (r *RayClusterReconciler) reconcileIngress(ctx context.Context, instance *rayv1.RayCluster) error {
logger := ctrl.LoggerFrom(ctx)
logger.Info("Reconciling Ingress")
if instance.Spec.HeadGroupSpec.EnableIngress == nil || !*instance.Spec.HeadGroupSpec.EnableIngress {
return nil
}
if r.IsOpenShift {
// This is open shift - create route
return r.reconcileRouteOpenShift(ctx, instance)
}
// plain vanilla kubernetes - create ingress
return r.reconcileIngressKubernetes(ctx, instance)
}
func (r *RayClusterReconciler) reconcileRouteOpenShift(ctx context.Context, instance *rayv1.RayCluster) error {
logger := ctrl.LoggerFrom(ctx)
headRoutes := routev1.RouteList{}
filterLabels := client.MatchingLabels{utils.RayClusterLabelKey: instance.Name}
if err := r.List(ctx, &headRoutes, client.InNamespace(instance.Namespace), filterLabels); err != nil {
return err
}
if len(headRoutes.Items) == 1 {
logger.Info("reconcileIngresses", "head service route found", headRoutes.Items[0].Name)
return nil
}
if len(headRoutes.Items) == 0 {
route, err := common.BuildRouteForHeadService(*instance)
if err != nil {
return err
}
if err := ctrl.SetControllerReference(instance, route, r.Scheme); err != nil {
return err
}
err = r.createHeadRoute(ctx, route, instance)
if err != nil {
return err
}
}
return nil
}
func (r *RayClusterReconciler) reconcileIngressKubernetes(ctx context.Context, instance *rayv1.RayCluster) error {
logger := ctrl.LoggerFrom(ctx)
headIngresses := networkingv1.IngressList{}
filterLabels := client.MatchingLabels{utils.RayClusterLabelKey: instance.Name}
if err := r.List(ctx, &headIngresses, client.InNamespace(instance.Namespace), filterLabels); err != nil {
return err
}
if len(headIngresses.Items) == 1 {
logger.Info("reconcileIngresses", "head service ingress found", headIngresses.Items[0].Name)
return nil
}
if len(headIngresses.Items) == 0 {
ingress, err := common.BuildIngressForHeadService(ctx, *instance)
if err != nil {
return err
}
if err := ctrl.SetControllerReference(instance, ingress, r.Scheme); err != nil {
return err
}
err = r.createHeadIngress(ctx, ingress, instance)
if err != nil {
return err
}
}
return nil
}
// Return nil only when the head service successfully created or already exists.
func (r *RayClusterReconciler) reconcileHeadService(ctx context.Context, instance *rayv1.RayCluster) error {
logger := ctrl.LoggerFrom(ctx)
services := corev1.ServiceList{}
filterLabels := client.MatchingLabels{utils.RayClusterLabelKey: instance.Name, utils.RayNodeTypeLabelKey: string(rayv1.HeadNode)}
if err := r.List(ctx, &services, client.InNamespace(instance.Namespace), filterLabels); err != nil {
return err
}
// Check if there's existing head service in the cluster.
if len(services.Items) != 0 {
if len(services.Items) == 1 {
logger.Info("reconcileHeadService", "1 head service found", services.Items[0].Name)
return nil
}
// This should never happen. This protects against the case that users manually create service with the same label.
if len(services.Items) > 1 {
logger.Info("reconcileHeadService", "Duplicate head service found", services.Items)
return fmt.Errorf("%d head service found %v", len(services.Items), services.Items)
}
} else {
// Create head service if there's no existing one in the cluster.
labels := make(map[string]string)
if val, ok := instance.Spec.HeadGroupSpec.Template.ObjectMeta.Labels[utils.KubernetesApplicationNameLabelKey]; ok {
labels[utils.KubernetesApplicationNameLabelKey] = val
}
annotations := make(map[string]string)
// TODO (kevin85421): KubeRay has already exposed the entire head service (#1040) to users.
// We may consider deprecating this field when we bump the CRD version.
for k, v := range instance.Spec.HeadServiceAnnotations {
annotations[k] = v
}
headSvc, err := common.BuildServiceForHeadPod(ctx, *instance, labels, annotations)
// TODO (kevin85421): Provide a detailed and actionable error message. For example, which port is missing?
if len(headSvc.Spec.Ports) == 0 {
logger.Info("Ray head service does not have any ports set up. Service specification: %v", headSvc.Spec)
return fmt.Errorf("Ray head service does not have any ports set up. Service specification: %v", headSvc.Spec)
}
if err != nil {
return err
}
if err := r.createService(ctx, headSvc, instance); err != nil {
return err
}
}
return nil
}
// Return nil only when the serve service successfully created or already exists.
func (r *RayClusterReconciler) reconcileServeService(ctx context.Context, instance *rayv1.RayCluster) error {
// Only reconcile the K8s service for Ray Serve when the "ray.io/enable-serve-service" annotation is set to true.
if enableServeServiceValue, exist := instance.Annotations[utils.EnableServeServiceKey]; !exist || enableServeServiceValue != utils.EnableServeServiceTrue {
return nil
}
// Retrieve the Service from the Kubernetes cluster with the name and namespace.
svc := &corev1.Service{}
err := r.Get(ctx, common.RayClusterServeServiceNamespacedName(instance), svc)
if err == nil {
// service exists, do nothing
return nil
} else if errors.IsNotFound(err) {
// Service does not exist, create it
svc, err = common.BuildServeServiceForRayCluster(ctx, *instance)
if err != nil {
return err
}
// Set the ownwer reference
if err := ctrl.SetControllerReference(instance, svc, r.Scheme); err != nil {
return err
}
// create service
return r.Create(ctx, svc)
}
return err
}
// Return nil only when the headless service for multi-host worker groups is successfully created or already exists.
func (r *RayClusterReconciler) reconcileHeadlessService(ctx context.Context, instance *rayv1.RayCluster) error {
// Check if there are worker groups with NumOfHosts > 1 in the cluster
isMultiHost := false
for _, workerGroup := range instance.Spec.WorkerGroupSpecs {
if workerGroup.NumOfHosts > 1 {
isMultiHost = true
break
}
}
if isMultiHost {
services := corev1.ServiceList{}
options := common.RayClusterHeadlessServiceListOptions(instance)
if err := r.List(ctx, &services, options...); err != nil {
return err
}
// Check if there's an existing headless service in the cluster.
if len(services.Items) != 0 {
// service exists, do nothing
return nil
}
// Create headless tpu worker service if there's no existing one in the cluster.
headlessSvc := common.BuildHeadlessServiceForRayCluster(*instance)
if err := r.createService(ctx, headlessSvc, instance); err != nil {
return err
}
}
return nil
}
func (r *RayClusterReconciler) reconcilePods(ctx context.Context, instance *rayv1.RayCluster) error {
logger := ctrl.LoggerFrom(ctx)
// if RayCluster is suspending, delete all pods and skip reconcile
suspendStatus := utils.FindRayClusterSuspendStatus(instance)
statusConditionGateEnabled := features.Enabled(features.RayClusterStatusConditions)
if suspendStatus == rayv1.RayClusterSuspending ||
(!statusConditionGateEnabled && instance.Spec.Suspend != nil && *instance.Spec.Suspend) {
if _, err := r.deleteAllPods(ctx, common.RayClusterAllPodsAssociationOptions(instance)); err != nil {
r.Recorder.Eventf(instance, corev1.EventTypeWarning, string(utils.FailedToDeletePod),
"Failed deleting Pods due to suspension for RayCluster %s/%s, %v",
instance.Namespace, instance.Name, err)
return errstd.Join(utils.ErrFailedDeleteAllPods, err)
}
r.Recorder.Eventf(instance, corev1.EventTypeNormal, string(utils.DeletedPod),
"Deleted Pods for RayCluster %s/%s due to suspension",
instance.Namespace, instance.Name)
return nil
}
if statusConditionGateEnabled {
if suspendStatus == rayv1.RayClusterSuspended {
return nil // stop reconcilePods because the cluster is suspended.
}
// (suspendStatus != rayv1.RayClusterSuspending) is always true here because it has been checked above.
if instance.Spec.Suspend != nil && *instance.Spec.Suspend {
return nil // stop reconcilePods because the cluster is going to suspend.
}
}
// check if all the pods exist
headPods := corev1.PodList{}
if err := r.List(ctx, &headPods, common.RayClusterHeadPodsAssociationOptions(instance).ToListOptions()...); err != nil {
return err
}
// check if the batch scheduler integration is enabled
// call the scheduler plugin if so
if r.BatchSchedulerMgr != nil {
if scheduler, err := r.BatchSchedulerMgr.GetSchedulerForCluster(); err == nil {
if err := scheduler.DoBatchSchedulingOnSubmission(ctx, instance); err != nil {
return err
}
} else {
return err
}
}
// Reconcile head Pod
if len(headPods.Items) == 1 {
headPod := headPods.Items[0]
logger.Info("reconcilePods", "Found 1 head Pod", headPod.Name, "Pod status", headPod.Status.Phase,
"Pod status reason", headPod.Status.Reason,
"Pod restart policy", headPod.Spec.RestartPolicy,
"Ray container terminated status", getRayContainerStateTerminated(headPod))
shouldDelete, reason := shouldDeletePod(headPod, rayv1.HeadNode)
logger.Info("reconcilePods", "head Pod", headPod.Name, "shouldDelete", shouldDelete, "reason", reason)
if shouldDelete {
if err := r.Delete(ctx, &headPod); err != nil {
r.Recorder.Eventf(instance, corev1.EventTypeWarning, string(utils.FailedToDeleteHeadPod),
"Failed deleting head Pod %s/%s; Pod status: %s; Pod restart policy: %s; Ray container terminated status: %v, %v",
headPod.Namespace, headPod.Name, headPod.Status.Phase, headPod.Spec.RestartPolicy, getRayContainerStateTerminated(headPod), err)
return errstd.Join(utils.ErrFailedDeleteHeadPod, err)
}
r.Recorder.Eventf(instance, corev1.EventTypeNormal, string(utils.DeletedHeadPod),
"Deleted head Pod %s/%s; Pod status: %s; Pod restart policy: %s; Ray container terminated status: %v",
headPod.Namespace, headPod.Name, headPod.Status.Phase, headPod.Spec.RestartPolicy, getRayContainerStateTerminated(headPod))
return errstd.New(reason)
}
} else if len(headPods.Items) == 0 {
// Create head Pod if it does not exist.
logger.Info("reconcilePods", "Found 0 head Pods; creating a head Pod for the RayCluster.", instance.Name)
common.CreatedClustersCounterInc(instance.Namespace)
if err := r.createHeadPod(ctx, *instance); err != nil {
common.FailedClustersCounterInc(instance.Namespace)
return errstd.Join(utils.ErrFailedCreateHeadPod, err)
}
common.SuccessfulClustersCounterInc(instance.Namespace)
} else if len(headPods.Items) > 1 {
logger.Info("reconcilePods", fmt.Sprintf("Found %d head Pods; deleting extra head Pods.", len(headPods.Items)), instance.Name)
// TODO (kevin85421): In-place update may not be a good idea.
itemLength := len(headPods.Items)
for index := 0; index < itemLength; index++ {
if headPods.Items[index].Status.Phase == corev1.PodRunning || headPods.Items[index].Status.Phase == corev1.PodPending {
// Remove the healthy pod at index i from the list of pods to delete
headPods.Items[index] = headPods.Items[len(headPods.Items)-1] // replace last element with the healthy head.
headPods.Items = headPods.Items[:len(headPods.Items)-1] // Truncate slice.
itemLength--
}
}
// delete all the extra head pod pods
for _, extraHeadPodToDelete := range headPods.Items {
if err := r.Delete(ctx, &extraHeadPodToDelete); err != nil {
return errstd.Join(utils.ErrFailedDeleteHeadPod, err)
}
}
}
// Reconcile worker pods now
for _, worker := range instance.Spec.WorkerGroupSpecs {
// workerReplicas will store the target number of pods for this worker group.
var workerReplicas int32 = utils.GetWorkerGroupDesiredReplicas(ctx, worker)
logger.Info("reconcilePods", "desired workerReplicas (always adhering to minReplicas/maxReplica)", workerReplicas, "worker group", worker.GroupName, "maxReplicas", worker.MaxReplicas, "minReplicas", worker.MinReplicas, "replicas", worker.Replicas)
workerPods := corev1.PodList{}
if err := r.List(ctx, &workerPods, common.RayClusterGroupPodsAssociationOptions(instance, worker.GroupName).ToListOptions()...); err != nil {
return err
}
// Delete unhealthy worker Pods.
deletedWorkers := make(map[string]struct{})
deleted := struct{}{}
numDeletedUnhealthyWorkerPods := 0
for _, workerPod := range workerPods.Items {
shouldDelete, reason := shouldDeletePod(workerPod, rayv1.WorkerNode)
logger.Info("reconcilePods", "worker Pod", workerPod.Name, "shouldDelete", shouldDelete, "reason", reason)
if shouldDelete {
numDeletedUnhealthyWorkerPods++
deletedWorkers[workerPod.Name] = deleted
if err := r.Delete(ctx, &workerPod); err != nil {
r.Recorder.Eventf(instance, corev1.EventTypeWarning, string(utils.FailedToDeleteWorkerPod),
"Failed deleting worker Pod %s/%s; Pod status: %s; Pod restart policy: %s; Ray container terminated status: %v, %v",
workerPod.Namespace, workerPod.Name, workerPod.Status.Phase, workerPod.Spec.RestartPolicy, getRayContainerStateTerminated(workerPod), err)
return errstd.Join(utils.ErrFailedDeleteWorkerPod, err)
}
r.Recorder.Eventf(instance, corev1.EventTypeNormal, string(utils.DeletedWorkerPod),
"Deleted worker Pod %s/%s; Pod status: %s; Pod restart policy: %s; Ray container terminated status: %v",
workerPod.Namespace, workerPod.Name, workerPod.Status.Phase, workerPod.Spec.RestartPolicy, getRayContainerStateTerminated(workerPod))
}
}
// If we delete unhealthy Pods, we will not create new Pods in this reconciliation.
if numDeletedUnhealthyWorkerPods > 0 {
return fmt.Errorf("Delete %d unhealthy worker Pods", numDeletedUnhealthyWorkerPods)
}
// Always remove the specified WorkersToDelete - regardless of the value of Replicas.
// Essentially WorkersToDelete has to be deleted to meet the expectations of the Autoscaler.
logger.Info("reconcilePods", "removing the pods in the scaleStrategy of", worker.GroupName)
for _, podsToDelete := range worker.ScaleStrategy.WorkersToDelete {
pod := corev1.Pod{}
pod.Name = podsToDelete
pod.Namespace = utils.GetNamespace(instance.ObjectMeta)
logger.Info("Deleting pod", "namespace", pod.Namespace, "name", pod.Name)
if err := r.Delete(ctx, &pod); err != nil {
if !errors.IsNotFound(err) {
logger.Info("reconcilePods", "Fail to delete Pod", pod.Name, "error", err)
r.Recorder.Eventf(instance, corev1.EventTypeWarning, string(utils.FailedToDeleteWorkerPod), "Failed deleting pod %s/%s, %v", pod.Namespace, pod.Name, err)
return errstd.Join(utils.ErrFailedDeleteWorkerPod, err)
}
logger.Info("reconcilePods", "The worker Pod has already been deleted", pod.Name)
} else {
deletedWorkers[pod.Name] = deleted
r.Recorder.Eventf(instance, corev1.EventTypeNormal, string(utils.DeletedWorkerPod), "Deleted pod %s/%s", pod.Namespace, pod.Name)
}
}
worker.ScaleStrategy.WorkersToDelete = []string{}
runningPods := corev1.PodList{}
for _, pod := range workerPods.Items {
if _, ok := deletedWorkers[pod.Name]; !ok {
runningPods.Items = append(runningPods.Items, pod)
}
}
// A replica can contain multiple hosts, so we need to calculate this based on the number of hosts per replica.
// If the user doesn't install the CRD with `NumOfHosts`, the zero value of `NumOfHosts`, which is 0, will be used.
// Hence, all workers will be deleted. Here, we set `NumOfHosts` to max(1, `NumOfHosts`) to avoid this situation.
if worker.NumOfHosts <= 0 {
worker.NumOfHosts = 1
}
numExpectedPods := int(workerReplicas * worker.NumOfHosts)
diff := numExpectedPods - len(runningPods.Items)
logger.Info("reconcilePods", "workerReplicas", workerReplicas, "NumOfHosts", worker.NumOfHosts, "runningPods", len(runningPods.Items), "diff", diff)
if diff > 0 {
// pods need to be added
logger.Info("reconcilePods", "Number workers to add", diff, "Worker group", worker.GroupName)
// create all workers of this group
for i := 0; i < diff; i++ {
logger.Info("reconcilePods", "creating worker for group", worker.GroupName, fmt.Sprintf("index %d", i), fmt.Sprintf("in total %d", diff))
if err := r.createWorkerPod(ctx, *instance, *worker.DeepCopy()); err != nil {
return errstd.Join(utils.ErrFailedCreateWorkerPod, err)
}
}
} else if diff == 0 {
logger.Info("reconcilePods", "all workers already exist for group", worker.GroupName)
continue
} else {
// diff < 0 indicates the need to delete some Pods to match the desired number of replicas. However,
// randomly deleting Pods is certainly not ideal. So, if autoscaling is enabled for the cluster, we
// will disable random Pod deletion, making Autoscaler the sole decision-maker for Pod deletions.
enableInTreeAutoscaling := (instance.Spec.EnableInTreeAutoscaling != nil) && (*instance.Spec.EnableInTreeAutoscaling)
// TODO (kevin85421): `enableRandomPodDelete` is a feature flag for KubeRay v0.6.0. If users want to use
// the old behavior, they can set the environment variable `ENABLE_RANDOM_POD_DELETE` to `true`. When the
// default behavior is stable enough, we can remove this feature flag.
enableRandomPodDelete := false
if enableInTreeAutoscaling {
if s := os.Getenv(utils.ENABLE_RANDOM_POD_DELETE); strings.ToLower(s) == "true" {
enableRandomPodDelete = true
}
}
// Case 1: If Autoscaler is disabled, we will always enable random Pod deletion no matter the value of the feature flag.
// Case 2: If Autoscaler is enabled, we will respect the value of the feature flag. If the feature flag environment variable
// is not set, we will disable random Pod deletion by default.
if !enableInTreeAutoscaling || enableRandomPodDelete {
// diff < 0 means that we need to delete some Pods to meet the desired number of replicas.
randomlyRemovedWorkers := -diff
logger.Info("reconcilePods", "Number workers to delete randomly", randomlyRemovedWorkers, "Worker group", worker.GroupName)
for i := 0; i < randomlyRemovedWorkers; i++ {
randomPodToDelete := runningPods.Items[i]
logger.Info("Randomly deleting Pod", "progress", fmt.Sprintf("%d / %d", i+1, randomlyRemovedWorkers), "with name", randomPodToDelete.Name)
if err := r.Delete(ctx, &randomPodToDelete); err != nil {
if !errors.IsNotFound(err) {
r.Recorder.Eventf(instance, corev1.EventTypeWarning, string(utils.FailedToDeleteWorkerPod), "Failed deleting Pod %s/%s, %v", randomPodToDelete.Namespace, randomPodToDelete.Name, err)
return errstd.Join(utils.ErrFailedDeleteWorkerPod, err)
}
logger.Info("reconcilePods", "The worker Pod has already been deleted", randomPodToDelete.Name)
}
r.Recorder.Eventf(instance, corev1.EventTypeNormal, string(utils.DeletedWorkerPod), "Deleted Pod %s/%s", randomPodToDelete.Namespace, randomPodToDelete.Name)
}
} else {
logger.Info(fmt.Sprintf("Random Pod deletion is disabled for cluster %s. The only decision-maker for Pod deletions is Autoscaler.", instance.Name))
}
}
}
return nil
}
// shouldDeletePod returns whether the Pod should be deleted and the reason
//
// @param pod: The Pod to be checked.
// @param nodeType: The type of the node that the Pod belongs to (head or worker).
//
// @return: shouldDelete (bool), reason (string)
// (1) shouldDelete: Whether the Pod should be deleted.
// (2) reason: The reason why the Pod should or should not be deleted.
func shouldDeletePod(pod corev1.Pod, nodeType rayv1.RayNodeType) (bool, string) {
// Based on the logic of the change of the status of the K8S pod, the following judgment is made.
// https://github.com/kubernetes/kubernetes/blob/3361895612dac57044d5dacc029d2ace1865479c/pkg/kubelet/kubelet_pods.go#L1556
// If the Pod's status is `Failed` or `Succeeded`, the Pod will not restart and we can safely delete it.
if pod.Status.Phase == corev1.PodFailed || pod.Status.Phase == corev1.PodSucceeded {
reason := fmt.Sprintf(
"The %s Pod %s status is %s which is a terminal state. "+
"KubeRay will delete the Pod and create new Pods in the next reconciliation if necessary.",
nodeType, pod.Name, pod.Status.Phase)
return true, reason
}
rayContainerTerminated := getRayContainerStateTerminated(pod)
if pod.Status.Phase == corev1.PodRunning && rayContainerTerminated != nil {
if pod.Spec.RestartPolicy == corev1.RestartPolicyNever {
reason := fmt.Sprintf(
"The Pod status of the %s Pod %s is %s, and the Ray container terminated status is %v. "+
"The container is unable to restart due to its restart policy %s, so KubeRay will delete it.",
nodeType, pod.Name, pod.Status.Phase, rayContainerTerminated, pod.Spec.RestartPolicy)
return true, reason
}
// If restart policy is set to `Always` or `OnFailure`, KubeRay will not delete the Pod.
reason := fmt.Sprintf(
"The Pod status of the %s Pod %s is %s, and the Ray container terminated status is %v. "+
"However, KubeRay will not delete the Pod because its restartPolicy is set to %s and it should be able to restart automatically.",
nodeType, pod.Name, pod.Status.Phase, rayContainerTerminated, pod.Spec.RestartPolicy)
return false, reason
}
// TODO (kevin85421): Consider deleting a Pod if its Ray container restarts excessively, as this might
// suggest an unhealthy Kubernetes node. Deleting and then recreating the Pod might allow it to be
// scheduled on a different node.
//
// (1) Head Pod:
// It's aggressive to delete a head Pod that is not in a terminated state (i.e., `Failed` or `Succeeded`).
// We should only delete a head Pod when GCS fault tolerance is enabled, and drain the head Pod before
// deleting it.
//
// (2) Worker Pod:
// Compared to deleting a head Pod, removing a worker Pod is less aggressive and aligns more closely with
// the behavior of the Ray Autoscaler. Nevertheless, we should still carefully drain the node before deleting
// the worker Pod. Enabling GCS fault tolerance might not be necessary when deleting worker Pods. Note that
// the Ray Autoscaler will not delete any worker Pods that have never been registered with the Ray cluster.
// Therefore, we may need to address the Ray Autoscaler's blind spots.
reason := fmt.Sprintf(
"KubeRay does not need to delete the %s Pod %s. The Pod status is %s, and the Ray container terminated status is %v.",
nodeType, pod.Name, pod.Status.Phase, rayContainerTerminated)
return false, reason
}
// `ContainerStatuses` does not guarantee the order of the containers. Therefore, we need to find the Ray
// container's status by name. See the following links for more details:
// (1) https://discuss.kubernetes.io/t/pod-spec-containers-and-pod-status-containerstatuses-can-have-a-different-order-why/25273
// (2) https://github.com/kubernetes/kubernetes/blob/03762cbcb52b2a4394e4d795f9d3517a78a5e1a2/pkg/api/v1/pod/util.go#L261-L268
func getRayContainerStateTerminated(pod corev1.Pod) *corev1.ContainerStateTerminated {
rayContainerName := pod.Spec.Containers[utils.RayContainerIndex].Name
for _, containerStatus := range pod.Status.ContainerStatuses {
if containerStatus.Name == rayContainerName {
return containerStatus.State.Terminated
}
}
// If the Ray container isn't found, we'll assume it hasn't terminated. This scenario
// typically arises during testing (`raycluster_controller_test.go`) as `envtest` lacks
// a Pod controller, preventing automatic Pod status updates.
return nil
}
func (r *RayClusterReconciler) createHeadIngress(ctx context.Context, ingress *networkingv1.Ingress, instance *rayv1.RayCluster) error {
logger := ctrl.LoggerFrom(ctx)
// making sure the name is valid
ingress.Name = utils.CheckName(ingress.Name)
if err := controllerutil.SetControllerReference(instance, ingress, r.Scheme); err != nil {
return err
}
if err := r.Create(ctx, ingress); err != nil {
if errors.IsAlreadyExists(err) {
logger.Info("Ingress already exists, no need to create")
return nil
}
r.Recorder.Eventf(instance, corev1.EventTypeWarning, string(utils.FailedToCreateIngress), "Failed creating ingress %s/%s, %v", ingress.Namespace, ingress.Name, err)
return err
}
logger.Info("Created ingress for RayCluster", "name", ingress.Name)
r.Recorder.Eventf(instance, corev1.EventTypeNormal, string(utils.CreatedIngress), "Created ingress %s/%s", ingress.Namespace, ingress.Name)
return nil
}
func (r *RayClusterReconciler) createHeadRoute(ctx context.Context, route *routev1.Route, instance *rayv1.RayCluster) error {
logger := ctrl.LoggerFrom(ctx)
// making sure the name is valid
route.Name = utils.CheckRouteName(ctx, route.Name, route.Namespace)
if err := r.Create(ctx, route); err != nil {
if errors.IsAlreadyExists(err) {
logger.Info("Route already exists, no need to create")
return nil
}
r.Recorder.Eventf(instance, corev1.EventTypeWarning, string(utils.FailedToCreateRoute), "Failed creating route %s/%s, %v", route.Namespace, route.Name, err)
return err
}
logger.Info("Created route for RayCluster", "name", route.Name)
r.Recorder.Eventf(instance, corev1.EventTypeNormal, string(utils.CreatedRoute), "Created route %s/%s", route.Namespace, route.Name)
return nil
}
func (r *RayClusterReconciler) createService(ctx context.Context, svc *corev1.Service, instance *rayv1.RayCluster) error {
logger := ctrl.LoggerFrom(ctx)
// making sure the name is valid
svc.Name = utils.CheckName(svc.Name)
if err := controllerutil.SetControllerReference(instance, svc, r.Scheme); err != nil {
return err
}
if err := r.Create(ctx, svc); err != nil {
r.Recorder.Eventf(instance, corev1.EventTypeWarning, string(utils.FailedToCreateService), "Failed creating service %s/%s, %v", svc.Namespace, svc.Name, err)
return err
}
logger.Info("Created service for RayCluster", "name", svc.Name)
r.Recorder.Eventf(instance, corev1.EventTypeNormal, string(utils.CreatedService), "Created service %s/%s", svc.Namespace, svc.Name)
return nil
}