-
Notifications
You must be signed in to change notification settings - Fork 339
/
raycluster_controller.go
1111 lines (994 loc) · 42.8 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"
"fmt"
"os"
"strconv"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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"
rbacv1 "k8s.io/api/rbac/v1"
rayiov1alpha1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1alpha1"
"k8s.io/client-go/tools/record"
"github.com/go-logr/logr"
_ "k8s.io/api/apps/v1beta1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
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/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
var (
DefaultRequeueDuration = 2 * time.Second
PrioritizeWorkersToDelete bool
ForcedClusterUpgrade bool
EnableBatchScheduler bool
)
// NewReconciler returns a new reconcile.Reconciler
func NewReconciler(mgr manager.Manager) *RayClusterReconciler {
return &RayClusterReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("controllers").WithName("RayCluster"),
Recorder: mgr.GetEventRecorderFor("raycluster-controller"),
BatchSchedulerMgr: batchscheduler.NewSchedulerManager(mgr.GetConfig()),
}
}
var _ reconcile.Reconciler = &RayClusterReconciler{}
// RayClusterReconciler reconciles a RayCluster object
type RayClusterReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Recorder record.EventRecorder
BatchSchedulerMgr *batchscheduler.SchedulerManager
}
// 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
// +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=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) {
var err error
// Try to fetch the Event instance
event := &corev1.Event{}
if err = r.Get(context.TODO(), request.NamespacedName, event); err == nil {
return r.eventReconcile(request, event)
}
// Try to fetch the RayCluster instance
instance := &rayiov1alpha1.RayCluster{}
if err = r.Get(context.TODO(), request.NamespacedName, instance); err == nil {
return r.rayClusterReconcile(request, instance)
}
// No match found
if errors.IsNotFound(err) {
r.Log.Info("Read request instance not found error!", "name", request.NamespacedName)
} else {
r.Log.Error(err, "Read request instance error!")
}
// Error reading the object - requeue the request.
return ctrl.Result{}, client.IgnoreNotFound(err)
}
func (r *RayClusterReconciler) eventReconcile(request ctrl.Request, event *corev1.Event) (ctrl.Result, error) {
var unhealthyPod *corev1.Pod
pods := corev1.PodList{}
// we only care about pod events
if event.InvolvedObject.Kind != "Pod" || event.Type != "Warning" || event.Reason != "Unhealthy" ||
!strings.Contains(event.Message, "Readiness probe failed") {
return ctrl.Result{}, nil
}
_ = r.Log.WithValues("event", request.NamespacedName)
r.Log.Info("reconcile RayCluster Event", "event name", request.Name)
if err := r.List(context.TODO(), &pods,
client.InNamespace(event.InvolvedObject.Namespace)); err != nil {
return ctrl.Result{}, err
}
for _, item := range pods.Items {
if item.Name == event.InvolvedObject.Name {
if unhealthyPod != nil {
return ctrl.Result{}, fmt.Errorf("duplicate pod found")
}
unhealthyPod = &item
}
}
if unhealthyPod == nil || unhealthyPod.Annotations == nil {
r.Log.Info("pod not found or no valid annotations", "pod name", event.InvolvedObject.Name)
return ctrl.Result{}, nil
}
if enabledString, ok := unhealthyPod.Annotations[common.RayFTEnabledAnnotationKey]; ok {
if strings.ToLower(enabledString) != "true" {
r.Log.Info("FT not enabled skipping event reconcile for pod.", "pod name", unhealthyPod.Name)
return ctrl.Result{}, nil
}
} else {
r.Log.Info("HAEnabled annotation not found", "pod name", unhealthyPod.Name)
return ctrl.Result{}, nil
}
if !utils.IsRunningAndReady(unhealthyPod) {
if v, ok := unhealthyPod.Annotations[common.RayNodeHealthStateAnnotationKey]; !ok || v != common.PodUnhealthy {
updatedPod := unhealthyPod.DeepCopy()
updatedPod.Annotations[common.RayNodeHealthStateAnnotationKey] = common.PodUnhealthy
r.Log.Info("mark pod unhealthy and need for a rebuild", "pod", unhealthyPod)
if err := r.Update(context.TODO(), updatedPod); err != nil {
return ctrl.Result{}, err
}
}
}
return ctrl.Result{}, nil
}
func (r *RayClusterReconciler) rayClusterReconcile(request ctrl.Request, instance *rayiov1alpha1.RayCluster) (ctrl.Result, error) {
_ = r.Log.WithValues("raycluster", request.NamespacedName)
r.Log.Info("reconciling RayCluster", "cluster name", request.Name)
if instance.DeletionTimestamp != nil && !instance.DeletionTimestamp.IsZero() {
r.Log.Info("RayCluster is being deleted, just ignore", "cluster name", request.Name)
return ctrl.Result{}, nil
}
if err := r.reconcileAutoscalerServiceAccount(instance); err != nil {
if updateErr := r.updateClusterState(instance, rayiov1alpha1.Failed); updateErr != nil {
r.Log.Error(updateErr, "RayCluster update state error", "cluster name", request.Name)
}
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
if err := r.reconcileAutoscalerRole(instance); err != nil {
if updateErr := r.updateClusterState(instance, rayiov1alpha1.Failed); updateErr != nil {
r.Log.Error(updateErr, "RayCluster update state error", "cluster name", request.Name)
}
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
if err := r.reconcileAutoscalerRoleBinding(instance); err != nil {
if updateErr := r.updateClusterState(instance, rayiov1alpha1.Failed); updateErr != nil {
r.Log.Error(updateErr, "RayCluster update state error", "cluster name", request.Name)
}
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
if err := r.reconcileIngress(instance); err != nil {
if updateErr := r.updateClusterState(instance, rayiov1alpha1.Failed); updateErr != nil {
r.Log.Error(updateErr, "RayCluster update state error", "cluster name", request.Name)
}
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
if err := r.reconcileServices(instance, common.HeadService); err != nil {
if updateErr := r.updateClusterState(instance, rayiov1alpha1.Failed); updateErr != nil {
r.Log.Error(updateErr, "RayCluster update state error", "cluster name", request.Name)
}
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
if common.IsAgentServiceEnabled(instance) {
// Reconcile agent service only when enabled in annotation.
if err := r.reconcileServices(instance, common.AgentService); err != nil {
if updateErr := r.updateClusterState(instance, rayiov1alpha1.Failed); updateErr != nil {
r.Log.Error(updateErr, "RayCluster update state error", "cluster name", request.Name)
}
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
}
if err := r.reconcilePods(instance); err != nil {
if updateErr := r.updateClusterState(instance, rayiov1alpha1.Failed); updateErr != nil {
r.Log.Error(updateErr, "RayCluster update state error", "cluster name", request.Name)
}
if updateErr := r.updateClusterReason(instance, err.Error()); updateErr != nil {
r.Log.Error(updateErr, "RayCluster update reason error", "cluster name", request.Name)
}
r.Recorder.Event(instance, corev1.EventTypeWarning, string(rayiov1alpha1.PodReconciliationError), err.Error())
return ctrl.Result{RequeueAfter: DefaultRequeueDuration}, err
}
// update the status if needed
if err := r.updateStatus(instance); err != nil {
if errors.IsNotFound(err) {
r.Log.Info("Update status not found error", "cluster name", request.Name)
} else {
r.Log.Error(err, "Update status error", "cluster name", request.Name)
}
}
// 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.
var requeueAfterSeconds int
requeueAfterSeconds, err := strconv.Atoi(os.Getenv(common.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV))
if err != nil {
r.Log.Info(fmt.Sprintf("Environment variable %s is not set, using default value of %d seconds", common.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV, common.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS), "cluster name", request.Name)
requeueAfterSeconds = common.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS
}
r.Log.Info("Unconditional requeue after", "cluster name", request.Name, "seconds", requeueAfterSeconds)
return ctrl.Result{RequeueAfter: time.Duration(requeueAfterSeconds) * time.Second}, nil
}
func (r *RayClusterReconciler) reconcileIngress(instance *rayiov1alpha1.RayCluster) error {
if instance.Spec.HeadGroupSpec.EnableIngress == nil || !*instance.Spec.HeadGroupSpec.EnableIngress {
return nil
}
headIngresses := networkingv1.IngressList{}
filterLabels := client.MatchingLabels{common.RayClusterLabelKey: instance.Name}
if err := r.List(context.TODO(), &headIngresses, client.InNamespace(instance.Namespace), filterLabels); err != nil {
return err
}
if headIngresses.Items != nil && len(headIngresses.Items) == 1 {
r.Log.Info("reconcileIngresses", "head service ingress found", headIngresses.Items[0].Name)
return nil
}
if headIngresses.Items == nil || len(headIngresses.Items) == 0 {
ingress, err := common.BuildIngressForHeadService(*instance)
if err != nil {
return err
}
if err := ctrl.SetControllerReference(instance, ingress, r.Scheme); err != nil {
return err
}
err = r.createHeadIngress(ingress, instance)
if err != nil {
return err
}
}
return nil
}
func (r *RayClusterReconciler) reconcileServices(instance *rayiov1alpha1.RayCluster, serviceType common.ServiceType) error {
services := corev1.ServiceList{}
var filterLabels client.MatchingLabels
if serviceType == common.HeadService {
filterLabels = client.MatchingLabels{common.RayClusterLabelKey: instance.Name}
} else if serviceType == common.AgentService {
filterLabels = client.MatchingLabels{common.RayClusterDashboardServiceLabelKey: utils.GenerateDashboardAgentLabel(instance.Name)}
}
if err := r.List(context.TODO(), &services, client.InNamespace(instance.Namespace), filterLabels); err != nil {
return err
}
if services.Items != nil {
if len(services.Items) == 1 {
r.Log.Info("reconcileServices ", string(serviceType)+" service found", services.Items[0].Name)
// TODO: compare diff and reconcile the object
// For example. ServiceType might be changed or port might be modified
return nil
}
// This should never happen.
// We add the protection here just in case controller has race issue or user manually create service with same label.
if len(services.Items) > 1 {
r.Log.Info("reconcileServices ", "Duplicates "+string(serviceType)+" service found", len(services.Items))
return nil
}
}
// Create head service if there's no existing one in the cluster.
if services.Items == nil || len(services.Items) == 0 {
var raySvc *corev1.Service
var err error
if serviceType == common.HeadService {
labels := make(map[string]string)
if val, ok := instance.Spec.HeadGroupSpec.Template.ObjectMeta.Labels[common.KubernetesApplicationNameLabelKey]; ok {
labels[common.KubernetesApplicationNameLabelKey] = val
}
annotations := make(map[string]string)
for k, v := range instance.Spec.HeadServiceAnnotations {
annotations[k] = v
}
raySvc, err = common.BuildServiceForHeadPod(*instance, labels, annotations)
} else if serviceType == common.AgentService {
raySvc, err = common.BuildDashboardService(*instance)
}
if raySvc == nil {
r.Log.Info("reconcileServices ", "Cannot create un-support service type ", serviceType)
return nil
}
if len(raySvc.Spec.Ports) == 0 {
r.Log.Info("reconcileServices ", "Ray service has no ports set up.", raySvc.Spec)
return nil
}
if err != nil {
return err
}
err = r.createService(raySvc, instance)
// if the service cannot be created we return the error and requeue
if err != nil {
return err
}
}
return nil
}
func (r *RayClusterReconciler) reconcilePods(instance *rayiov1alpha1.RayCluster) error {
// check if all the pods exist
headPods := corev1.PodList{}
filterLabels := client.MatchingLabels{common.RayClusterLabelKey: instance.Name, common.RayNodeTypeLabelKey: string(rayiov1alpha1.HeadNode)}
if err := r.List(context.TODO(), &headPods, client.InNamespace(instance.Namespace), filterLabels); err != nil {
return err
}
if EnableBatchScheduler {
if scheduler, err := r.BatchSchedulerMgr.GetSchedulerForCluster(instance); err == nil {
if err := scheduler.DoBatchSchedulingOnSubmission(instance); err != nil {
return err
}
} else {
return err
}
}
// Reconcile head Pod
if len(headPods.Items) == 1 {
headPod := headPods.Items[0]
r.Log.Info("reconcilePods ", "head pod found", headPod.Name)
if headPod.Status.Phase == corev1.PodRunning || headPod.Status.Phase == corev1.PodPending {
r.Log.Info("reconcilePods", "head pod is up and running... checking workers", headPod.Name)
} else if headPod.Status.Phase == corev1.PodFailed && strings.Contains(headPod.Status.Reason, "Evicted") {
// Handle evicted pod
r.Log.Info("reconcilePods", "head pod has been evicted and controller needs to replace the pod", headPod.Name)
if err := r.Delete(context.TODO(), &headPod); err != nil {
return err
}
} else {
return fmt.Errorf("head pod %s is not running nor pending", headPod.Name)
}
}
if len(headPods.Items) == 0 || headPods.Items == nil {
// create head pod
r.Log.Info("reconcilePods ", "creating head pod for cluster", instance.Name)
common.CreatedClustersCounterInc(instance.Namespace)
if err := r.createHeadPod(*instance); err != nil {
common.FailedClustersCounterInc(instance.Namespace)
return err
}
common.SuccessfulClustersCounterInc(instance.Namespace)
} else if len(headPods.Items) > 1 {
r.Log.Info("reconcilePods ", "more than 1 head pod found for cluster", instance.Name)
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(context.TODO(), &extraHeadPodToDelete); err != nil {
return err
}
}
} else {
// we have exactly one head pod running
if headPods.Items[0].Annotations != nil {
if v, ok := headPods.Items[0].Annotations[common.RayNodeHealthStateAnnotationKey]; ok && v == common.PodUnhealthy {
if err := r.Delete(context.TODO(), &headPods.Items[0]); err != nil {
return err
}
r.Log.Info(fmt.Sprintf("need to delete unhealthy head pod %s", headPods.Items[0].Name))
// we are deleting the head pod now, let's reconcile again later
return nil
}
}
}
if ForcedClusterUpgrade {
if len(headPods.Items) == 1 {
// head node amount is exactly 1, but we need to check if it has been changed
res := utils.PodNotMatchingTemplate(headPods.Items[0], instance.Spec.HeadGroupSpec.Template)
if res {
r.Log.Info(fmt.Sprintf("need to delete old head pod %s", headPods.Items[0].Name))
if err := r.Delete(context.TODO(), &headPods.Items[0]); err != nil {
return err
}
return nil
}
}
// check if WorkerGroupSpecs has been changed and we need to kill worker pods
for _, worker := range instance.Spec.WorkerGroupSpecs {
workerPods := corev1.PodList{}
filterLabels = client.MatchingLabels{common.RayClusterLabelKey: instance.Name, common.RayNodeGroupLabelKey: worker.GroupName}
if err := r.List(context.TODO(), &workerPods, client.InNamespace(instance.Namespace), filterLabels); err != nil {
return err
}
updatedWorkerPods := false
for _, item := range workerPods.Items {
if utils.PodNotMatchingTemplate(item, worker.Template) {
r.Log.Info(fmt.Sprintf("need to delete old worker pod %s", item.Name))
if err := r.Delete(context.TODO(), &item); err != nil {
r.Log.Info(fmt.Sprintf("error deleting worker pod %s", item.Name))
return err
}
updatedWorkerPods = true
}
}
if updatedWorkerPods {
return nil
}
}
}
// 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
// Always honor MaxReplicas if it is set:
// If MaxReplicas is set and Replicas > MaxReplicas, use MaxReplicas as the
// effective target replica count and log the discrepancy.
// See https://github.com/ray-project/kuberay/issues/560.
if worker.MaxReplicas != nil && *worker.MaxReplicas < *worker.Replicas {
workerReplicas = *worker.MaxReplicas
r.Log.Info(
fmt.Sprintf(
"Replicas for worker group %s (%d) is greater than maxReplicas (%d). Using maxReplicas (%d) as the target replica count.",
worker.GroupName, *worker.Replicas, *worker.MaxReplicas, *worker.MaxReplicas,
),
)
} else {
workerReplicas = *worker.Replicas
}
workerPods := corev1.PodList{}
filterLabels = client.MatchingLabels{common.RayClusterLabelKey: instance.Name, common.RayNodeGroupLabelKey: worker.GroupName}
if err := r.List(context.TODO(), &workerPods, client.InNamespace(instance.Namespace), filterLabels); err != nil {
return err
}
// delete the worker pod if it is marked unhealthy
for _, workerPod := range workerPods.Items {
if workerPod.Annotations == nil {
continue
}
if v, ok := workerPod.Annotations[common.RayNodeHealthStateAnnotationKey]; ok && v == common.PodUnhealthy {
r.Log.Info(fmt.Sprintf("deleting unhealthy worker pod %s", workerPod.Name))
if err := r.Delete(context.TODO(), &workerPod); err != nil {
return err
}
// we are deleting one worker pod now, let's reconcile again later
return nil
}
}
runningPods := corev1.PodList{}
for _, aPod := range workerPods.Items {
if (aPod.Status.Phase == corev1.PodRunning || aPod.Status.Phase == corev1.PodPending) && aPod.ObjectMeta.DeletionTimestamp == nil {
runningPods.Items = append(runningPods.Items, aPod)
}
}
r.updateLocalWorkersToDelete(&worker, runningPods.Items)
diff := workerReplicas - int32(len(runningPods.Items))
if PrioritizeWorkersToDelete {
// Always remove the specified WorkersToDelete - regardless of the value of Replicas.
// Essentially WorkersToDelete has to be deleted to meet the expectations of the Autoscaler.
r.Log.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)
r.Log.Info("Deleting pod", "namespace", pod.Namespace, "name", pod.Name)
if err := r.Delete(context.TODO(), &pod); err != nil {
if !errors.IsNotFound(err) {
return err
}
r.Log.Info("reconcilePods", "unable to delete worker ", pod.Name)
} else {
diff++
r.Recorder.Eventf(instance, corev1.EventTypeNormal, "Deleted", "Deleted pod %s", pod.Name)
}
}
worker.ScaleStrategy.WorkersToDelete = []string{}
}
// Once we remove the feature flag and commit to those changes, the code below can be cleaned up
// It will end being a simple: "if diff > 0 { } else { }"
if diff > 0 {
// pods need to be added
r.Log.Info("reconcilePods", "add workers for group", worker.GroupName)
// create all workers of this group
var i int32
for i = 0; i < diff; i++ {
r.Log.Info("reconcilePods", "creating worker for group", worker.GroupName, fmt.Sprintf("index %d", i), fmt.Sprintf("in total %d", diff))
if err := r.createWorkerPod(*instance, *worker.DeepCopy()); err != nil {
return err
}
}
} else if diff == 0 {
r.Log.Info("reconcilePods", "all workers already exist for group", worker.GroupName)
continue
} else if -diff == int32(len(worker.ScaleStrategy.WorkersToDelete)) {
r.Log.Info("reconcilePods", "removing all 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)
r.Log.Info("Deleting pod", "namespace", pod.Namespace, "name", pod.Name)
if err := r.Delete(context.TODO(), &pod); err != nil {
if !errors.IsNotFound(err) {
return err
}
r.Log.Info("reconcilePods", "workers specified to delete was already deleted ", pod.Name)
}
r.Recorder.Eventf(instance, corev1.EventTypeNormal, "Deleted", "Deleted pod %s", pod.Name)
}
continue
} else {
// diff < 0 and not the same absolute value as int32(len(worker.ScaleStrategy.WorkersToDelete)
// we need to scale down
workersToRemove := int32(len(runningPods.Items)) - workerReplicas
randomlyRemovedWorkers := workersToRemove - int32(len(worker.ScaleStrategy.WorkersToDelete))
// we only need to scale down the workers in the ScaleStrategy
r.Log.Info("reconcilePods", "removing all 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)
r.Log.Info("Deleting pod", "namespace", pod.Namespace, "name", pod.Name)
if err := r.Delete(context.TODO(), &pod); err != nil {
if !errors.IsNotFound(err) {
return err
}
r.Log.Info("reconcilePods", "workers specified to delete was already deleted ", pod.Name)
}
r.Recorder.Eventf(instance, corev1.EventTypeNormal, "Deleted", "Deleted pod %s", pod.Name)
}
// remove the remaining pods not part of the scaleStrategy
i := 0
if int(randomlyRemovedWorkers) > 0 {
for _, randomPodToDelete := range runningPods.Items {
found := false
for _, podsToDelete := range worker.ScaleStrategy.WorkersToDelete {
if randomPodToDelete.Name == podsToDelete {
found = true
break
}
}
if !found {
r.Log.Info("Randomly deleting pod ", "index ", i, "/", randomlyRemovedWorkers, "with name", randomPodToDelete.Name)
if err := r.Delete(context.TODO(), &randomPodToDelete); err != nil {
if !errors.IsNotFound(err) {
return err
}
r.Log.Info("reconcilePods", "workers specified to delete was already deleted ", randomPodToDelete.Name)
}
r.Recorder.Eventf(instance, corev1.EventTypeNormal, "Deleted", "Deleted pod %s", randomPodToDelete.Name)
// increment the number of deleted pods
i++
if i >= int(randomlyRemovedWorkers) {
break
}
}
}
}
}
}
return nil
}
func (r *RayClusterReconciler) updateLocalWorkersToDelete(worker *rayiov1alpha1.WorkerGroupSpec, runningItems []corev1.Pod) {
var actualWorkersToDelete []string
itemMap := make(map[string]int)
// Create a map for quick lookup.
for _, item := range runningItems {
itemMap[item.Name] = 1
}
// Build actualWorkersToDelete to only include running items.
for _, workerToDelete := range worker.ScaleStrategy.WorkersToDelete {
if _, ok := itemMap[workerToDelete]; ok {
actualWorkersToDelete = append(actualWorkersToDelete, workerToDelete)
}
}
worker.ScaleStrategy.WorkersToDelete = actualWorkersToDelete
}
func (r *RayClusterReconciler) createHeadIngress(ingress *networkingv1.Ingress, instance *rayiov1alpha1.RayCluster) error {
// 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(context.TODO(), ingress); err != nil {
if errors.IsAlreadyExists(err) {
r.Log.Info("Ingress already exists, no need to create")
return nil
}
r.Log.Error(err, "Ingress create error!", "Ingress.Error", err)
return err
}
r.Log.Info("Ingress created successfully", "ingress name", ingress.Name)
r.Recorder.Eventf(instance, corev1.EventTypeNormal, "Created", "Created ingress %s", ingress.Name)
return nil
}
func (r *RayClusterReconciler) createService(raySvc *corev1.Service, instance *rayiov1alpha1.RayCluster) error {
// making sure the name is valid
raySvc.Name = utils.CheckName(raySvc.Name)
// Set controller reference
if err := controllerutil.SetControllerReference(instance, raySvc, r.Scheme); err != nil {
return err
}
if err := r.Create(context.TODO(), raySvc); err != nil {
if errors.IsAlreadyExists(err) {
r.Log.Info("Pod service already exist, no need to create")
return nil
}
r.Log.Error(err, "Pod Service create error!", "Pod.Service.Error", err)
return err
}
r.Log.Info("Pod Service created successfully", "service name", raySvc.Name)
r.Recorder.Eventf(instance, corev1.EventTypeNormal, "Created", "Created service %s", raySvc.Name)
return nil
}
func (r *RayClusterReconciler) createHeadPod(instance rayiov1alpha1.RayCluster) error {
// build the pod then create it
pod := r.buildHeadPod(instance)
podIdentifier := types.NamespacedName{
Name: pod.Name,
Namespace: pod.Namespace,
}
if EnableBatchScheduler {
if scheduler, err := r.BatchSchedulerMgr.GetSchedulerForCluster(&instance); err == nil {
scheduler.AddMetadataToPod(&instance, &pod)
} else {
return err
}
}
r.Log.Info("createHeadPod", "head pod with name", pod.GenerateName)
if err := r.Create(context.TODO(), &pod); err != nil {
if errors.IsAlreadyExists(err) {
fetchedPod := corev1.Pod{}
// the pod might be in terminating state, we need to check
if errPod := r.Get(context.TODO(), podIdentifier, &fetchedPod); errPod == nil {
if fetchedPod.DeletionTimestamp != nil {
r.Log.Error(errPod, "create pod error!", "pod is in a terminating state, we will wait until it is cleaned up", podIdentifier)
return err
}
}
r.Log.Info("Creating pod", "Pod already exists", pod.Name)
} else {
return err
}
}
r.Recorder.Eventf(&instance, corev1.EventTypeNormal, "Created", "Created head pod %s", pod.Name)
return nil
}
func (r *RayClusterReconciler) createWorkerPod(instance rayiov1alpha1.RayCluster, worker rayiov1alpha1.WorkerGroupSpec) error {
// build the pod then create it
pod := r.buildWorkerPod(instance, worker)
podIdentifier := types.NamespacedName{
Name: pod.Name,
Namespace: pod.Namespace,
}
if EnableBatchScheduler {
if scheduler, err := r.BatchSchedulerMgr.GetSchedulerForCluster(&instance); err == nil {
scheduler.AddMetadataToPod(&instance, &pod)
} else {
return err
}
}
replica := pod
if err := r.Create(context.TODO(), &replica); err != nil {
if errors.IsAlreadyExists(err) {
fetchedPod := corev1.Pod{}
// the pod might be in terminating state, we need to check
if errPod := r.Get(context.TODO(), podIdentifier, &fetchedPod); errPod == nil {
if fetchedPod.DeletionTimestamp != nil {
r.Log.Error(errPod, "create pod error!", "pod is in a terminating state, we will wait until it is cleaned up", podIdentifier)
return err
}
}
r.Log.Info("Creating pod", "Pod already exists", pod.Name)
} else {
r.Log.Error(fmt.Errorf("createWorkerPod error"), "error creating pod", "pod", pod, "err = ", err)
return err
}
}
r.Log.Info("Created pod", "Pod ", pod.GenerateName)
r.Recorder.Eventf(&instance, corev1.EventTypeNormal, "Created", "Created worker pod %s", pod.Name)
return nil
}
// Build head instance pod(s).
func (r *RayClusterReconciler) buildHeadPod(instance rayiov1alpha1.RayCluster) corev1.Pod {
podName := strings.ToLower(instance.Name + common.DashSymbol + string(rayiov1alpha1.HeadNode) + common.DashSymbol)
podName = utils.CheckName(podName) // making sure the name is valid
svcName := utils.GenerateServiceName(instance.Name)
svcName = utils.CheckName(svcName)
// The Ray head port used by workers to connect to the cluster (GCS server port for Ray >= 1.11.0, Redis port for older Ray.)
headPort := common.GetHeadPort(instance.Spec.HeadGroupSpec.RayStartParams)
autoscalingEnabled := instance.Spec.EnableInTreeAutoscaling
podConf := common.DefaultHeadPodTemplate(instance, instance.Spec.HeadGroupSpec, podName, svcName, headPort)
r.Log.Info("head pod labels", "labels", podConf.Labels)
creatorName := getCreator(instance)
pod := common.BuildPod(podConf, rayiov1alpha1.HeadNode, instance.Spec.HeadGroupSpec.RayStartParams, svcName, headPort, autoscalingEnabled, creatorName)
// Set raycluster instance as the owner and controller
if err := controllerutil.SetControllerReference(&instance, &pod, r.Scheme); err != nil {
r.Log.Error(err, "Failed to set controller reference for raycluster pod")
}
return pod
}
func getCreator(instance rayiov1alpha1.RayCluster) string {
if instance.Labels == nil {
return ""
}
creatorName, exist := instance.Labels[common.KubernetesCreatedByLabelKey]
if !exist {
return ""
}
return creatorName
}
// Build worker instance pods.
func (r *RayClusterReconciler) buildWorkerPod(instance rayiov1alpha1.RayCluster, worker rayiov1alpha1.WorkerGroupSpec) corev1.Pod {
podName := strings.ToLower(instance.Name + common.DashSymbol + string(rayiov1alpha1.WorkerNode) + common.DashSymbol + worker.GroupName + common.DashSymbol)
podName = utils.CheckName(podName) // making sure the name is valid
svcName := utils.GenerateServiceName(instance.Name)
svcName = utils.CheckName(svcName)
// The Ray head port used by workers to connect to the cluster (GCS server port for Ray >= 1.11.0, Redis port for older Ray.)
headPort := common.GetHeadPort(instance.Spec.HeadGroupSpec.RayStartParams)
autoscalingEnabled := instance.Spec.EnableInTreeAutoscaling
podTemplateSpec := common.DefaultWorkerPodTemplate(instance, worker, podName, svcName, headPort)
creatorName := getCreator(instance)
pod := common.BuildPod(podTemplateSpec, rayiov1alpha1.WorkerNode, worker.RayStartParams, svcName, headPort, autoscalingEnabled, creatorName)
// Set raycluster instance as the owner and controller
if err := controllerutil.SetControllerReference(&instance, &pod, r.Scheme); err != nil {
r.Log.Error(err, "Failed to set controller reference for raycluster pod")
}
return pod
}
// SetupWithManager builds the reconciler.
func (r *RayClusterReconciler) SetupWithManager(mgr ctrl.Manager, reconcileConcurrency int) error {
b := ctrl.NewControllerManagedBy(mgr).
Named("raycluster-controller").
For(&rayiov1alpha1.RayCluster{}, builder.WithPredicates(predicate.Or(
predicate.GenerationChangedPredicate{},
predicate.LabelChangedPredicate{},
predicate.AnnotationChangedPredicate{},
))).
Watches(&source.Kind{Type: &corev1.Event{}}, &handler.EnqueueRequestForObject{}).
Owns(&corev1.Pod{}).
Owns(&corev1.Service{})
if EnableBatchScheduler {
b = batchscheduler.ConfigureReconciler(b)
}
return b.
WithOptions(controller.Options{MaxConcurrentReconciles: reconcileConcurrency}).
Complete(r)
}
func (r *RayClusterReconciler) updateStatus(instance *rayiov1alpha1.RayCluster) error {
runtimePods := corev1.PodList{}
filterLabels := client.MatchingLabels{common.RayClusterLabelKey: instance.Name}
if err := r.List(context.TODO(), &runtimePods, client.InNamespace(instance.Namespace), filterLabels); err != nil {
return err
}
count := utils.CalculateAvailableReplicas(runtimePods)
if instance.Status.AvailableWorkerReplicas != count {
instance.Status.AvailableWorkerReplicas = count
}
count = utils.CalculateDesiredReplicas(instance)
if instance.Status.DesiredWorkerReplicas != count {
instance.Status.DesiredWorkerReplicas = count
}
count = utils.CalculateMinReplicas(instance)
if instance.Status.MinWorkerReplicas != count {
instance.Status.MinWorkerReplicas = count
}
count = utils.CalculateMaxReplicas(instance)
if instance.Status.MaxWorkerReplicas != count {
instance.Status.MaxWorkerReplicas = count
}
// validation for the RayStartParam for the state.
isValid, err := common.ValidateHeadRayStartParams(instance.Spec.HeadGroupSpec)
if err != nil {
r.Recorder.Event(instance, corev1.EventTypeWarning, string(rayiov1alpha1.RayConfigError), err.Error())
}
// only in invalid status that we update the status to unhealthy.
if !isValid {
instance.Status.State = rayiov1alpha1.Unhealthy
} else {
if utils.CheckAllPodsRunnning(runtimePods) {
instance.Status.State = rayiov1alpha1.Ready
}
}
if err := r.updateEndpoints(instance); err != nil {
return err
}
if err := r.updateHeadInfo(instance); err != nil {
return err
}
timeNow := metav1.Now()
instance.Status.LastUpdateTime = &timeNow
if err := r.Status().Update(context.Background(), instance); err != nil {
return err
}
return nil
}
// Best effort to obtain the ip of the head node.
func (r *RayClusterReconciler) getHeadPodIP(instance *rayiov1alpha1.RayCluster) (string, error) {
runtimePods := corev1.PodList{}
filterLabels := client.MatchingLabels{common.RayClusterLabelKey: instance.Name, common.RayNodeTypeLabelKey: string(rayiov1alpha1.HeadNode)}
if err := r.List(context.TODO(), &runtimePods, client.InNamespace(instance.Namespace), filterLabels); err != nil {
r.Log.Error(err, "Failed to list pods while getting head pod ip.")
return "", err
}
if len(runtimePods.Items) != 1 {
r.Log.Info(fmt.Sprintf("Found %d head pods. cluster name %s, filter labels %v", len(runtimePods.Items), instance.Name, filterLabels))
return "", nil
}
return runtimePods.Items[0].Status.PodIP, nil
}
func (r *RayClusterReconciler) getHeadServiceIP(instance *rayiov1alpha1.RayCluster) (string, error) {
runtimeServices := corev1.ServiceList{}
filterLabels := client.MatchingLabels(common.HeadServiceLabels(*instance))
if err := r.List(context.TODO(), &runtimeServices, client.InNamespace(instance.Namespace), filterLabels); err != nil {
return "", err
}
if len(runtimeServices.Items) < 1 {
return "", fmt.Errorf("unable to find head service. cluster name %s, filter labels %v", instance.Name, filterLabels)
} else if len(runtimeServices.Items) > 1 {
return "", fmt.Errorf("found multiple head services. cluster name %s, filter labels %v", instance.Name, filterLabels)
} else if runtimeServices.Items[0].Spec.ClusterIP == "" {
return "", fmt.Errorf("head service IP is empty. cluster name %s, filter labels %v", instance.Name, filterLabels)
}
return runtimeServices.Items[0].Spec.ClusterIP, nil
}
func (r *RayClusterReconciler) updateEndpoints(instance *rayiov1alpha1.RayCluster) error {
// TODO: (@scarlet25151) There may be several K8s Services for a RayCluster.
// We assume we can find the right one by filtering Services with appropriate label selectors
// and picking the first one. We may need to select by name in the future if the Service naming is stable.
rayHeadSvc := corev1.ServiceList{}
filterLabels := client.MatchingLabels{
common.RayClusterLabelKey: instance.Name,
common.RayNodeTypeLabelKey: "head",
}
if err := r.List(context.TODO(), &rayHeadSvc, client.InNamespace(instance.Namespace), filterLabels); err != nil {
return err
}
if len(rayHeadSvc.Items) != 0 {
svc := rayHeadSvc.Items[0]
if instance.Status.Endpoints == nil {
instance.Status.Endpoints = map[string]string{}
}
for _, port := range svc.Spec.Ports {
if len(port.Name) == 0 {
r.Log.Info("updateStatus", "service port's name is empty. Not adding it to RayCluster status.endpoints", port)
continue
}
if port.NodePort != 0 {
instance.Status.Endpoints[port.Name] = fmt.Sprintf("%d", port.NodePort)
} else if port.TargetPort.IntVal != 0 {
instance.Status.Endpoints[port.Name] = fmt.Sprintf("%d", port.TargetPort.IntVal)
} else if port.TargetPort.StrVal != "" {
instance.Status.Endpoints[port.Name] = port.TargetPort.StrVal
} else {
r.Log.Info("updateStatus", "service port's targetPort is empty. Not adding it to RayCluster status.endpoints", port)
}
}
} else {
r.Log.Info("updateEndpoints", "unable to find a Service for this RayCluster. Not adding RayCluster status.endpoints", instance.Name, "Service selectors", filterLabels)
}
return nil
}
func (r *RayClusterReconciler) updateHeadInfo(instance *rayiov1alpha1.RayCluster) error {
if ip, err := r.getHeadPodIP(instance); err != nil {
return err
} else {
instance.Status.Head.PodIP = ip
}
if ip, err := r.getHeadServiceIP(instance); err != nil {
return err
} else {
instance.Status.Head.ServiceIP = ip
}
return nil
}
func (r *RayClusterReconciler) reconcileAutoscalerServiceAccount(instance *rayiov1alpha1.RayCluster) error {
if instance.Spec.EnableInTreeAutoscaling == nil || !*instance.Spec.EnableInTreeAutoscaling {
return nil
}
serviceAccount := &corev1.ServiceAccount{}
namespacedName := types.NamespacedName{Namespace: instance.Namespace, Name: utils.GetHeadGroupServiceAccountName(instance)}
if err := r.Get(context.TODO(), namespacedName, serviceAccount); err != nil {
if !errors.IsNotFound(err) {
return err
}
// Create service account for autoscaler if there's no existing one in the cluster.
serviceAccount, err := common.BuildServiceAccount(instance)
if err != nil {
return err
}
// making sure the name is valid
serviceAccount.Name = utils.CheckName(serviceAccount.Name)
// Set controller reference