Skip to content

Commit

Permalink
[Bug] Fix null map handling in BuildServiceForHeadPod function (#1095)
Browse files Browse the repository at this point in the history
Fix null map handling in `BuildServiceForHeadPod` function
  • Loading branch information
architkulkarni authored Jun 2, 2023
1 parent 70ef243 commit c420135
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ray-operator/controllers/ray/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func BuildServiceForHeadPod(cluster rayv1alpha1.RayCluster, labels map[string]st

// For the Labels field, merge labels_for_service with custom HeadService labels.
// If there are overlaps, ignore the custom HeadService labels.
if headService.ObjectMeta.Labels == nil {
headService.ObjectMeta.Labels = make(map[string]string)
}
for k, v := range labels_for_service {
headService.ObjectMeta.Labels[k] = v
}
Expand All @@ -79,6 +82,9 @@ func BuildServiceForHeadPod(cluster rayv1alpha1.RayCluster, labels map[string]st

// Merge annotations with custom HeadService annotations. If there are overlaps,
// ignore the custom HeadService annotations.
if headService.ObjectMeta.Annotations == nil {
headService.ObjectMeta.Annotations = make(map[string]string)
}
for k, v := range annotations {
// if the key is present, log a warning message
if _, ok := headService.ObjectMeta.Annotations[k]; ok {
Expand Down
15 changes: 15 additions & 0 deletions ray-operator/controllers/ray/common/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,21 @@ func TestUserSpecifiedHeadService(t *testing.T) {
}
}

func TestNilMapDoesntErrorInUserSpecifiedHeadService(t *testing.T) {
// Use any RayCluster instance as a base for the test.
testRayClusterWithHeadService := instanceWithWrongSvc.DeepCopy()

// Set user-specified head service with many nil fields.
testRayClusterWithHeadService.Spec.HeadGroupSpec.HeadService = &corev1.Service{
ObjectMeta: metav1.ObjectMeta{},
}

_, err := BuildServiceForHeadPod(*testRayClusterWithHeadService, nil, nil)
if err != nil {
t.Errorf("failed to build head service: %v", err)
}
}

func TestBuildServiceForHeadPodPortsOrder(t *testing.T) {
svc1, err1 := BuildServiceForHeadPod(*instanceWithWrongSvc, nil, nil)
svc2, err2 := BuildServiceForHeadPod(*instanceWithWrongSvc, nil, nil)
Expand Down

0 comments on commit c420135

Please sign in to comment.