Skip to content

Commit

Permalink
[RayCluster controller] Add headServiceAnnotations field to RayCluste…
Browse files Browse the repository at this point in the history
…r CR (ray-project#841)

Adds headServiceAnnotations field to RayCluster CR.

Signed-off-by: Kornel Csernai <[email protected]>
  • Loading branch information
cskornel-doordash authored Dec 16, 2022
1 parent 1ec19a9 commit e5c1bed
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 11 deletions.
4 changes: 4 additions & 0 deletions helm-chart/kuberay-operator/crds/ray.io_rayclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5709,6 +5709,10 @@ spec:
- serviceType
- template
type: object
headServiceAnnotations:
additionalProperties:
type: string
type: object
rayVersion:
description: RayVersion is the version of ray being used. this affects
the command used to start ray
Expand Down
4 changes: 4 additions & 0 deletions helm-chart/kuberay-operator/crds/ray.io_rayjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5973,6 +5973,10 @@ spec:
- serviceType
- template
type: object
headServiceAnnotations:
additionalProperties:
type: string
type: object
rayVersion:
description: RayVersion is the version of ray being used. this
affects the command used to start ray
Expand Down
4 changes: 4 additions & 0 deletions helm-chart/kuberay-operator/crds/ray.io_rayservices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5959,6 +5959,10 @@ spec:
- serviceType
- template
type: object
headServiceAnnotations:
additionalProperties:
type: string
type: object
rayVersion:
description: RayVersion is the version of ray being used. this
affects the command used to start ray
Expand Down
3 changes: 2 additions & 1 deletion ray-operator/apis/ray/v1alpha1/raycluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type RayClusterSpec struct {
// EnableInTreeAutoscaling indicates whether operator should create in tree autoscaling configs
EnableInTreeAutoscaling *bool `json:"enableInTreeAutoscaling,omitempty"`
// AutoscalerOptions specifies optional configuration for the Ray autoscaler.
AutoscalerOptions *AutoscalerOptions `json:"autoscalerOptions,omitempty"`
AutoscalerOptions *AutoscalerOptions `json:"autoscalerOptions,omitempty"`
HeadServiceAnnotations map[string]string `json:"headServiceAnnotations,omitempty"`
}

// HeadGroupSpec are the spec for the head pod
Expand Down
7 changes: 7 additions & 0 deletions ray-operator/apis/ray/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ray-operator/config/crd/bases/ray.io_rayclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5709,6 +5709,10 @@ spec:
- serviceType
- template
type: object
headServiceAnnotations:
additionalProperties:
type: string
type: object
rayVersion:
description: RayVersion is the version of ray being used. this affects
the command used to start ray
Expand Down
4 changes: 4 additions & 0 deletions ray-operator/config/crd/bases/ray.io_rayjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5973,6 +5973,10 @@ spec:
- serviceType
- template
type: object
headServiceAnnotations:
additionalProperties:
type: string
type: object
rayVersion:
description: RayVersion is the version of ray being used. this
affects the command used to start ray
Expand Down
4 changes: 4 additions & 0 deletions ray-operator/config/crd/bases/ray.io_rayservices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5959,6 +5959,10 @@ spec:
- serviceType
- template
type: object
headServiceAnnotations:
additionalProperties:
type: string
type: object
rayVersion:
description: RayVersion is the version of ray being used. this
affects the command used to start ray
Expand Down
3 changes: 3 additions & 0 deletions ray-operator/config/samples/ray_v1alpha1_rayservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,6 @@ spec:
requests:
cpu: "500m"
memory: "2Gi"
headServiceAnnotations: {}
# annotations passed on for the Head Service
# service_key: "service_value"
15 changes: 10 additions & 5 deletions ray-operator/controllers/ray/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func HeadServiceLabels(cluster rayiov1alpha1.RayCluster) map[string]string {

// BuildServiceForHeadPod Builds the service for a pod. Currently, there is only one service that allows
// the worker nodes to connect to the head node.
func BuildServiceForHeadPod(cluster rayiov1alpha1.RayCluster, labels map[string]string) (*corev1.Service, error) {
func BuildServiceForHeadPod(cluster rayiov1alpha1.RayCluster, labels map[string]string, annotations map[string]string) (*corev1.Service, error) {
if labels == nil {
labels = make(map[string]string)
}
Expand All @@ -34,11 +34,16 @@ func BuildServiceForHeadPod(cluster rayiov1alpha1.RayCluster, labels map[string]
}
}

if annotations == nil {
annotations = make(map[string]string)
}

service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: utils.GenerateServiceName(cluster.Name),
Namespace: cluster.Namespace,
Labels: labels,
Name: utils.GenerateServiceName(cluster.Name),
Namespace: cluster.Namespace,
Labels: labels,
Annotations: annotations,
},
Spec: corev1.ServiceSpec{
Selector: labels,
Expand All @@ -61,7 +66,7 @@ func BuildServiceForHeadPod(cluster rayiov1alpha1.RayCluster, labels map[string]
// the worker nodes to connect to the head node.
// RayService controller updates the service whenever a new RayCluster serves the traffic.
func BuildHeadServiceForRayService(rayService rayiov1alpha1.RayService, rayCluster rayiov1alpha1.RayCluster) (*corev1.Service, error) {
service, err := BuildServiceForHeadPod(rayCluster, nil)
service, err := BuildServiceForHeadPod(rayCluster, nil, nil)
if err != nil {
return nil, err
}
Expand Down
17 changes: 15 additions & 2 deletions ray-operator/controllers/ray/common/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var instanceWithWrongSvc = &rayiov1alpha1.RayCluster{
}

func TestBuildServiceForHeadPod(t *testing.T) {
svc, err := BuildServiceForHeadPod(*instanceWithWrongSvc, nil)
svc, err := BuildServiceForHeadPod(*instanceWithWrongSvc, nil, nil)
assert.Nil(t, err)

actualResult := svc.Spec.Selector[RayClusterLabelKey]
Expand Down Expand Up @@ -96,7 +96,8 @@ func TestBuildServiceForHeadPod(t *testing.T) {
func TestBuildServiceForHeadPodWithAppNameLabel(t *testing.T) {
labels := make(map[string]string)
labels[KubernetesApplicationNameLabelKey] = "testname"
svc, err := BuildServiceForHeadPod(*instanceWithWrongSvc, labels)

svc, err := BuildServiceForHeadPod(*instanceWithWrongSvc, labels, nil)
assert.Nil(t, err)

actualResult := svc.Spec.Selector[KubernetesApplicationNameLabelKey]
Expand All @@ -113,3 +114,15 @@ func TestBuildServiceForHeadPodWithAppNameLabel(t *testing.T) {
t.Fatalf("Expected `%v` but got `%v`", expectedLength, actualLength)
}
}

func TestBuildServiceForHeadPodWithAnnotations(t *testing.T) {
annotations := make(map[string]string)
annotations["key1"] = "testvalue1"
annotations["key2"] = "testvalue2"
svc, err := BuildServiceForHeadPod(*instanceWithWrongSvc, nil, annotations)
assert.Nil(t, err)

if !reflect.DeepEqual(svc.ObjectMeta.Annotations, annotations) {
t.Fatalf("Expected `%v` but got `%v`", annotations, svc.ObjectMeta.Annotations)
}
}
6 changes: 5 additions & 1 deletion ray-operator/controllers/ray/raycluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,11 @@ func (r *RayClusterReconciler) reconcileServices(instance *rayiov1alpha1.RayClus
if val, ok := instance.Spec.HeadGroupSpec.Template.ObjectMeta.Labels[common.KubernetesApplicationNameLabelKey]; ok {
labels[common.KubernetesApplicationNameLabelKey] = val
}
raySvc, err = common.BuildServiceForHeadPod(*instance, labels)
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func setupTest(t *testing.T) {
},
}

headService, err := common.BuildServiceForHeadPod(*testRayCluster, nil)
headService, err := common.BuildServiceForHeadPod(*testRayCluster, nil, nil)
if err != nil {
t.Errorf("failed to build head service: %v", err)
}
Expand Down Expand Up @@ -904,7 +904,7 @@ func TestGetHeadServiceIP(t *testing.T) {
defer tearDown(t)

headServiceIP := "1.2.3.4"
headService, err := common.BuildServiceForHeadPod(*testRayCluster, nil)
headService, err := common.BuildServiceForHeadPod(*testRayCluster, nil, nil)
if err != nil {
t.Errorf("failed to build head service: %v", err)
}
Expand Down

0 comments on commit e5c1bed

Please sign in to comment.