Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Add support user-defined serve service ports #2670

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ray-operator/controllers/ray/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ func BuildServeService(ctx context.Context, rayService rayv1.RayService, rayClus
if serveService.ObjectMeta.Annotations == nil {
serveService.ObjectMeta.Annotations = make(map[string]string)
}

// Add port with name "serve" if it is already not added and ignore any custom ports
// Keeping this consistentent with adding only serve port in serve service
if len(ports) != 0 {
Expand All @@ -248,7 +247,8 @@ func BuildServeService(ctx context.Context, rayService rayv1.RayService, rayClus
ports := []corev1.ServicePort{}
for _, port := range serveService.Spec.Ports {
if port.Name == utils.ServingPortName {
svcPort := corev1.ServicePort{Name: port.Name, Port: port.Port}
svcPort := corev1.ServicePort{Name: port.Name, Port: port.Port, NodePort: port.NodePort,
TargetPort: port.TargetPort}
ports = append(ports, svcPort)
break
}
Expand Down
103 changes: 103 additions & 0 deletions ray-operator/controllers/ray/common/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/util/intstr"
)

var (
Expand Down Expand Up @@ -124,6 +126,33 @@ var (
},
},
}
instanceWithoutSvc = &rayv1.RayCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "raycluster-sample-nosvc",
Namespace: "default",
},
Spec: rayv1.RayClusterSpec{
HeadServiceAnnotations: map[string]string{
headServiceAnnotationKey1: headServiceAnnotationValue1,
headServiceAnnotationKey2: headServiceAnnotationValue2,
},
HeadGroupSpec: rayv1.HeadGroupSpec{
ServiceType: corev1.ServiceTypeClusterIP,
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "ray-head",
Ports: []corev1.ContainerPort{
{ContainerPort: 10001, Name: "client"},
},
},
},
},
},
},
},
}
Comment on lines +129 to +155
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use instanceForSvc directly? They are almost the same.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are different, instanceWithoutSvc does not include the "serve" container port for the service.

)

func TestBuildServiceForHeadPod(t *testing.T) {
Expand Down Expand Up @@ -652,6 +681,80 @@ func TestUserSpecifiedServeService(t *testing.T) {
validateNameAndNamespaceForUserSpecifiedService(svc, testRayServiceWithServeService.ObjectMeta.Namespace, userName, t)
}

func TestUserSpecifiedServeServicePorts(t *testing.T) {
// Use any RayService instance as a base for the test.
testRayServiceWithServeService := serviceInstance.DeepCopy()

userName := "user-custom-name"
userNamespace := "user-custom-namespace"
userLabels := map[string]string{"userLabelKey": "userLabelValue", utils.RayClusterLabelKey: "userClusterName"} // Override default cluster name
userAnnotations := map[string]string{"userAnnotationKey": "userAnnotationValue", "userAnnotationKey2": "userAnnotationValue2"}
// Specify serve service port.
userServePort := []corev1.ServicePort{{Name: "serve", Port: 12345, NodePort: 55555, TargetPort: intstr.IntOrString{IntVal: 12345}}}
userSelector := map[string]string{"userSelectorKey": "userSelectorValue", utils.RayClusterLabelKey: "userSelectorClusterName"}
// Specify a "LoadBalancer" type, which differs from the default "ClusterIP" type.
userType := corev1.ServiceTypeLoadBalancer

testRayServiceWithServeService.Spec.ServeService = &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: userName,
Namespace: userNamespace,
Labels: userLabels,
Annotations: userAnnotations,
},
Spec: corev1.ServiceSpec{
Ports: userServePort,
Selector: userSelector,
Type: userType,
},
}

svc, err := BuildServeServiceForRayService(context.Background(), *testRayServiceWithServeService, *instanceWithoutSvc)
if err != nil {
t.Errorf("failed to build serve service: %v", err)
}

// Check every annotation is in the service annotation
for k := range userAnnotations {
if _, ok := svc.ObjectMeta.Annotations[k]; !ok {
t.Errorf("Final labels should contain key=%s", k)
}
}

// Check that selectors only have default selectors
if len(svc.Spec.Selector) != 2 {
t.Errorf("Selectors should have just 2 keys %s and %s", utils.RayClusterLabelKey, utils.RayClusterServingServiceLabelKey)
}
if svc.Spec.Selector[utils.RayClusterLabelKey] != instanceWithoutSvc.Name {
t.Errorf("Serve Service selector key %s value didn't match expected value : expected value=%s, actual value=%s", utils.RayClusterLabelKey, instanceWithWrongSvc.Name, svc.Spec.Selector[utils.RayClusterLabelKey])
}
if svc.Spec.Selector[utils.RayClusterServingServiceLabelKey] != utils.EnableRayClusterServingServiceTrue {
t.Errorf("Serve Service selector key %s value didn't match expected value : expected value=%s, actual value=%s", utils.RayClusterServingServiceLabelKey, utils.EnableRayClusterServingServiceTrue, svc.Spec.Selector[utils.RayClusterServingServiceLabelKey])
}

// ports should have user define serve port
ports := svc.Spec.Ports
expectedPortName := utils.ServingPortName
expectedPortNumber := int32(12345)
expectedNodePortNumber := int32(55555)

for _, port := range ports {
if port.Name != utils.ServingPortName {
t.Fatalf("Expected `%v` but got `%v`", expectedPortName, port.Name)
}
if port.Port != expectedPortNumber {
t.Fatalf("Expected `%v` but got `%v`", expectedPortNumber, port.Port)
}
if port.NodePort != expectedNodePortNumber {
t.Fatalf("Expected `%v` but got `%v`", expectedNodePortNumber, port.NodePort)
}
}

validateServiceTypeForUserSpecifiedService(svc, userType, t)
validateLabelsForUserSpecifiedService(svc, userLabels, t)
validateNameAndNamespaceForUserSpecifiedService(svc, testRayServiceWithServeService.ObjectMeta.Namespace, userName, t)
}

func validateServiceTypeForUserSpecifiedService(svc *corev1.Service, userType corev1.ServiceType, t *testing.T) {
// Test that the user service type takes priority over the default service type (example: ClusterIP)
if svc.Spec.Type != userType {
Expand Down
Loading