Skip to content

Remove fmt.Println, convert to log #3718

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

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/association.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ func RayClusterHeadlessServiceListOptions(instance *rayv1.RayCluster) []client.L
}
}

func RayClusterHeadServiceListOptions(instance *rayv1.RayCluster) []client.ListOption {
func RayClusterHeadServiceListOptions(ctx context.Context, instance *rayv1.RayCluster) []client.ListOption {
return []client.ListOption{
client.InNamespace(instance.Namespace),
client.MatchingLabels(map[string]string{
utils.RayClusterLabelKey: instance.Name,
utils.RayNodeTypeLabelKey: string(rayv1.HeadNode),
utils.RayIDLabelKey: utils.CheckLabel(utils.GenerateIdentifier(instance.Name, rayv1.HeadNode)),
utils.RayIDLabelKey: utils.CheckLabel(ctx, utils.GenerateIdentifier(instance.Name, rayv1.HeadNode)),
}),
}
}
Expand Down
6 changes: 4 additions & 2 deletions ray-operator/controllers/ray/common/association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,24 @@ func TestRayClusterHeadlessServiceListOptions(t *testing.T) {
}

func TestRayClusterHeadServiceListOptions(t *testing.T) {
ctx := context.Background()

instance := rayv1.RayCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "raycluster",
Namespace: "test-ns",
},
}

labels := HeadServiceLabels(instance)
labels := HeadServiceLabels(ctx, instance)
delete(labels, utils.KubernetesCreatedByLabelKey)
delete(labels, utils.KubernetesApplicationNameLabelKey)

expected := []client.ListOption{
client.InNamespace(instance.Namespace),
client.MatchingLabels(labels),
}
result := RayClusterHeadServiceListOptions(&instance)
result := RayClusterHeadServiceListOptions(ctx, &instance)
if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected %v, got %v", expected, result)
}
Expand Down
2 changes: 1 addition & 1 deletion ray-operator/controllers/ray/common/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func BuildIngressForHeadService(ctx context.Context, cluster rayv1.RayCluster) (

labels := map[string]string{
utils.RayClusterLabelKey: cluster.Name,
utils.RayIDLabelKey: utils.CheckLabel(utils.GenerateIdentifier(cluster.Name, rayv1.HeadNode)),
utils.RayIDLabelKey: utils.CheckLabel(ctx, utils.GenerateIdentifier(cluster.Name, rayv1.HeadNode)),
utils.KubernetesApplicationNameLabelKey: utils.ApplicationName,
utils.KubernetesCreatedByLabelKey: utils.ComponentName,
}
Expand Down
10 changes: 5 additions & 5 deletions ray-operator/controllers/ray/common/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func DefaultHeadPodTemplate(ctx context.Context, instance rayv1.RayCluster, head
if podTemplate.Labels == nil {
podTemplate.Labels = make(map[string]string)
}
podTemplate.Labels = labelPod(rayv1.HeadNode, instance.Name, utils.RayNodeHeadGroupLabelValue, instance.Spec.HeadGroupSpec.Template.ObjectMeta.Labels)
podTemplate.Labels = labelPod(ctx, rayv1.HeadNode, instance.Name, utils.RayNodeHeadGroupLabelValue, instance.Spec.HeadGroupSpec.Template.ObjectMeta.Labels)
headSpec.RayStartParams = setMissingRayStartParams(ctx, headSpec.RayStartParams, rayv1.HeadNode, headPort, "")

initTemplateAnnotations(instance, &podTemplate)
Expand All @@ -184,7 +184,7 @@ func DefaultHeadPodTemplate(ctx context.Context, instance rayv1.RayCluster, head
headSpec.RayStartParams["no-monitor"] = "true"
// set custom service account with proper roles bound.
// utils.CheckName clips the name to match the behavior of reconcileAutoscalerServiceAccount
podTemplate.Spec.ServiceAccountName = utils.CheckName(utils.GetHeadGroupServiceAccountName(&instance))
podTemplate.Spec.ServiceAccountName = utils.CheckName(ctx, utils.GetHeadGroupServiceAccountName(&instance))
// Use the same image as Ray head container by default.
autoscalerImage := podTemplate.Spec.Containers[utils.RayContainerIndex].Image
// inject autoscaler container into head pod
Expand Down Expand Up @@ -311,7 +311,7 @@ func DefaultWorkerPodTemplate(ctx context.Context, instance rayv1.RayCluster, wo
if podTemplate.Labels == nil {
podTemplate.Labels = make(map[string]string)
}
podTemplate.Labels = labelPod(rayv1.WorkerNode, instance.Name, workerSpec.GroupName, workerSpec.Template.ObjectMeta.Labels)
podTemplate.Labels = labelPod(ctx, rayv1.WorkerNode, instance.Name, workerSpec.GroupName, workerSpec.Template.ObjectMeta.Labels)
workerSpec.RayStartParams = setMissingRayStartParams(ctx, workerSpec.RayStartParams, rayv1.WorkerNode, headPort, fqdnRayIP)

initTemplateAnnotations(instance, &podTemplate)
Expand Down Expand Up @@ -599,13 +599,13 @@ func getAutoscalerContainerIndex(pod corev1.Pod) (autoscalerContainerIndex int)

// labelPod returns the labels for selecting the resources
// belonging to the given RayCluster CR name.
func labelPod(rayNodeType rayv1.RayNodeType, rayClusterName string, groupName string, overrideLabels map[string]string) map[string]string {
func labelPod(ctx context.Context, rayNodeType rayv1.RayNodeType, rayClusterName string, groupName string, overrideLabels map[string]string) map[string]string {
labels := map[string]string{
utils.RayNodeLabelKey: "yes",
utils.RayClusterLabelKey: rayClusterName,
utils.RayNodeTypeLabelKey: string(rayNodeType),
utils.RayNodeGroupLabelKey: groupName,
utils.RayIDLabelKey: utils.CheckLabel(utils.GenerateIdentifier(rayClusterName, rayNodeType)),
utils.RayIDLabelKey: utils.CheckLabel(ctx, utils.GenerateIdentifier(rayClusterName, rayNodeType)),
utils.KubernetesApplicationNameLabelKey: utils.ApplicationName,
utils.KubernetesCreatedByLabelKey: utils.ComponentName,
}
Expand Down
2 changes: 1 addition & 1 deletion ray-operator/controllers/ray/common/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ func TestHeadPodTemplate_WithAutoscalingEnabled(t *testing.T) {
// Repeat ServiceAccountName check with long cluster name.
cluster.Name = longString(t) // 200 chars long
podTemplateSpec = DefaultHeadPodTemplate(ctx, *cluster, cluster.Spec.HeadGroupSpec, podName, "6379")
assert.Equal(t, shortString(t), podTemplateSpec.Spec.ServiceAccountName)
assert.Equal(t, shortString(ctx, t), podTemplateSpec.Spec.ServiceAccountName)
}

func TestDefaultHeadPodTemplate_Autoscaling(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions ray-operator/controllers/ray/common/rbac.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"context"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -56,7 +57,7 @@ func BuildRole(cluster *rayv1.RayCluster) (*rbacv1.Role, error) {
}

// BuildRole
func BuildRoleBinding(cluster *rayv1.RayCluster) (*rbacv1.RoleBinding, error) {
func BuildRoleBinding(ctx context.Context, cluster *rayv1.RayCluster) (*rbacv1.RoleBinding, error) {
serviceAccountName := utils.GetHeadGroupServiceAccountName(cluster)
rb := &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -72,15 +73,15 @@ func BuildRoleBinding(cluster *rayv1.RayCluster) (*rbacv1.RoleBinding, error) {
{
Kind: rbacv1.ServiceAccountKind,
// Clip name for consistency with the function reconcileAutoscalerServiceAccount.
Name: utils.CheckName(serviceAccountName),
Name: utils.CheckName(ctx, serviceAccountName),
Namespace: cluster.Namespace,
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: "Role",
// Clip name for consistency with the function reconcileAutoscalerRole.
Name: utils.CheckName(cluster.Name),
Name: utils.CheckName(ctx, cluster.Name),
},
}

Expand Down
9 changes: 6 additions & 3 deletions ray-operator/controllers/ray/common/rbac_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -13,6 +14,8 @@ import (

// Test subject and role ref names in the function BuildRoleBinding.
func TestBuildRoleBindingSubjectAndRoleRefName(t *testing.T) {
ctx := context.Background()

tests := []struct {
name string
input *rayv1.RayCluster
Expand Down Expand Up @@ -70,15 +73,15 @@ func TestBuildRoleBindingSubjectAndRoleRefName(t *testing.T) {
},
},
want: []string{
shortString(t), // 50 chars long, truncated by utils.CheckName
shortString(t),
shortString(ctx, t), // 50 chars long, truncated by utils.CheckName
shortString(ctx, t),
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
rb, err := BuildRoleBinding(tc.input)
rb, err := BuildRoleBinding(ctx, tc.input)
require.NoError(t, err)
got := []string{rb.Subjects[0].Name, rb.RoleRef.Name}
assert.Equal(t, tc.want, got)
Expand Down
5 changes: 3 additions & 2 deletions ray-operator/controllers/ray/common/route.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"context"
routev1 "github.com/openshift/api/route/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand All @@ -11,10 +12,10 @@ import (

// BuildRouteForHeadService Builds the Route (OpenShift) for head service dashboard.
// This is used to expose dashboard and remote submit service apis or external traffic.
func BuildRouteForHeadService(cluster rayv1.RayCluster) (*routev1.Route, error) {
func BuildRouteForHeadService(ctx context.Context, cluster rayv1.RayCluster) (*routev1.Route, error) {
labels := map[string]string{
utils.RayClusterLabelKey: cluster.Name,
utils.RayIDLabelKey: utils.CheckLabel(utils.GenerateIdentifier(cluster.Name, rayv1.HeadNode)),
utils.RayIDLabelKey: utils.CheckLabel(ctx, utils.GenerateIdentifier(cluster.Name, rayv1.HeadNode)),
utils.KubernetesApplicationNameLabelKey: utils.ApplicationName,
utils.KubernetesCreatedByLabelKey: utils.ComponentName,
}
Expand Down
3 changes: 2 additions & 1 deletion ray-operator/controllers/ray/common/route_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -41,7 +42,7 @@ var instanceWithRouteEnabled = &rayv1.RayCluster{
}

func TestBuildRouteForHeadService(t *testing.T) {
route, err := BuildRouteForHeadService(*instanceWithRouteEnabled)
route, err := BuildRouteForHeadService(context.Background(), *instanceWithRouteEnabled)
require.NoError(t, err)

// Test name
Expand Down
8 changes: 4 additions & 4 deletions ray-operator/controllers/ray/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func getEnableRayHeadClusterIPService() bool {
}

// HeadServiceLabels returns the default labels for a cluster's head service.
func HeadServiceLabels(cluster rayv1.RayCluster) map[string]string {
func HeadServiceLabels(ctx context.Context, cluster rayv1.RayCluster) map[string]string {
return map[string]string{
utils.RayClusterLabelKey: cluster.Name,
utils.RayNodeTypeLabelKey: string(rayv1.HeadNode),
utils.RayIDLabelKey: utils.CheckLabel(utils.GenerateIdentifier(cluster.Name, rayv1.HeadNode)),
utils.RayIDLabelKey: utils.CheckLabel(ctx, utils.GenerateIdentifier(cluster.Name, rayv1.HeadNode)),
utils.KubernetesApplicationNameLabelKey: utils.ApplicationName,
utils.KubernetesCreatedByLabelKey: utils.ComponentName,
}
Expand All @@ -39,7 +39,7 @@ func BuildServiceForHeadPod(ctx context.Context, cluster rayv1.RayCluster, label
labels = make(map[string]string)
}

defaultLabels := HeadServiceLabels(cluster)
defaultLabels := HeadServiceLabels(ctx, cluster)

// selector consists of *only* the keys in defaultLabels, updated with the values in labels if they exist
selector := make(map[string]string)
Expand Down Expand Up @@ -161,7 +161,7 @@ func BuildHeadServiceForRayService(ctx context.Context, rayService rayv1.RayServ
utils.RayOriginatedFromCRNameLabelKey: rayService.Name,
utils.RayOriginatedFromCRDLabelKey: utils.RayOriginatedFromCRDLabelValue(utils.RayServiceCRD),
utils.RayNodeTypeLabelKey: string(rayv1.HeadNode),
utils.RayIDLabelKey: utils.CheckLabel(utils.GenerateIdentifier(rayService.Name, rayv1.HeadNode)),
utils.RayIDLabelKey: utils.CheckLabel(ctx, utils.GenerateIdentifier(rayService.Name, rayv1.HeadNode)),
}

return service, nil
Expand Down
3 changes: 2 additions & 1 deletion ray-operator/controllers/ray/common/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ func TestGetServicePortsWithMetricsPort(t *testing.T) {
}

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

Expand Down Expand Up @@ -332,7 +333,7 @@ func TestUserSpecifiedHeadService(t *testing.T) {

// The selector field should only use the keys from the five default labels. The values should be updated with the values from the template labels.
// The user-provided HeadService labels should be ignored for the purposes of the selector field. The user-provided Selector field should be ignored.
defaultLabels := HeadServiceLabels(*testRayClusterWithHeadService)
defaultLabels := HeadServiceLabels(ctx, *testRayClusterWithHeadService)
// Make sure this test isn't spuriously passing. Check that RayClusterLabelKey is in the default labels.
if _, ok := defaultLabels[utils.RayClusterLabelKey]; !ok {
t.Errorf("utils.RayClusterLabelKey=%s should be in the default labels", utils.RayClusterLabelKey)
Expand Down
5 changes: 3 additions & 2 deletions ray-operator/controllers/ray/common/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"bytes"
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -23,8 +24,8 @@ func longString(t *testing.T) string {

// Clip the above string using utils.CheckName
// to a string of length 50.
func shortString(t *testing.T) string {
result := utils.CheckName(longString(t))
func shortString(ctx context.Context, t *testing.T) string {
result := utils.CheckName(ctx, longString(t))
// Confirm length.
assert.Len(t, result, 50)
return result
Expand Down
26 changes: 13 additions & 13 deletions ray-operator/controllers/ray/raycluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func (r *RayClusterReconciler) reconcileRouteOpenShift(ctx context.Context, inst
}

if len(headRoutes.Items) == 0 {
route, err := common.BuildRouteForHeadService(*instance)
route, err := common.BuildRouteForHeadService(ctx, *instance)
if err != nil {
return err
}
Expand Down Expand Up @@ -499,7 +499,7 @@ func (r *RayClusterReconciler) reconcileIngressKubernetes(ctx context.Context, i
func (r *RayClusterReconciler) reconcileHeadService(ctx context.Context, instance *rayv1.RayCluster) error {
logger := ctrl.LoggerFrom(ctx)
services := corev1.ServiceList{}
filterLabels := common.RayClusterHeadServiceListOptions(instance)
filterLabels := common.RayClusterHeadServiceListOptions(ctx, instance)

if err := r.List(ctx, &services, filterLabels...); err != nil {
return err
Expand Down Expand Up @@ -933,7 +933,7 @@ func (r *RayClusterReconciler) createHeadIngress(ctx context.Context, ingress *n
logger := ctrl.LoggerFrom(ctx)

// making sure the name is valid
ingress.Name = utils.CheckName(ingress.Name)
ingress.Name = utils.CheckName(ctx, ingress.Name)
if err := controllerutil.SetControllerReference(instance, ingress, r.Scheme); err != nil {
return err
}
Expand Down Expand Up @@ -1144,7 +1144,7 @@ func (r *RayClusterReconciler) buildRedisCleanupJob(ctx context.Context, instanc
pod.Spec.RestartPolicy = corev1.RestartPolicyNever

// Trim the job name to ensure it is within the 63-character limit.
jobName := utils.TrimJobName(fmt.Sprintf("%s-%s", instance.Name, "redis-cleanup"))
jobName := utils.TrimJobName(ctx, fmt.Sprintf("%s-%s", instance.Name, "redis-cleanup"))

redisCleanupJob := batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -1369,15 +1369,15 @@ func (r *RayClusterReconciler) calculateStatus(ctx context.Context, instance *ra

func (r *RayClusterReconciler) getHeadServiceIPAndName(ctx context.Context, instance *rayv1.RayCluster) (string, string, error) {
runtimeServices := corev1.ServiceList{}
if err := r.List(ctx, &runtimeServices, common.RayClusterHeadServiceListOptions(instance)...); err != nil {
if err := r.List(ctx, &runtimeServices, common.RayClusterHeadServiceListOptions(ctx, instance)...); 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, common.RayClusterHeadServiceListOptions(instance))
return "", "", fmt.Errorf("unable to find head service. cluster name %s, filter labels %v", instance.Name, common.RayClusterHeadServiceListOptions(ctx, instance))
} else if len(runtimeServices.Items) > 1 {
return "", "", fmt.Errorf("found multiple head services. cluster name %s, filter labels %v", instance.Name, common.RayClusterHeadServiceListOptions(instance))
return "", "", fmt.Errorf("found multiple head services. cluster name %s, filter labels %v", instance.Name, common.RayClusterHeadServiceListOptions(ctx, instance))
} else if runtimeServices.Items[0].Spec.ClusterIP == "" {
return "", "", fmt.Errorf("head service IP is empty. cluster name %s, filter labels %v", instance.Name, common.RayClusterHeadServiceListOptions(instance))
return "", "", fmt.Errorf("head service IP is empty. cluster name %s, filter labels %v", instance.Name, common.RayClusterHeadServiceListOptions(ctx, instance))
} else if runtimeServices.Items[0].Spec.ClusterIP == corev1.ClusterIPNone {
// We return Head Pod IP if the Head service is headless.
headPod, err := common.GetRayClusterHeadPod(ctx, r, instance)
Expand All @@ -1399,7 +1399,7 @@ func (r *RayClusterReconciler) updateEndpoints(ctx context.Context, instance *ra
// 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 := common.RayClusterHeadServiceListOptions(instance)
filterLabels := common.RayClusterHeadServiceListOptions(ctx, instance)
if err := r.List(ctx, &rayHeadSvc, filterLabels...); err != nil {
return err
}
Expand Down Expand Up @@ -1488,7 +1488,7 @@ func (r *RayClusterReconciler) reconcileAutoscalerServiceAccount(ctx context.Con
}

// making sure the name is valid
serviceAccount.Name = utils.CheckName(serviceAccount.Name)
serviceAccount.Name = utils.CheckName(ctx, serviceAccount.Name)

// Set controller reference
if err := controllerutil.SetControllerReference(instance, serviceAccount, r.Scheme); err != nil {
Expand Down Expand Up @@ -1531,7 +1531,7 @@ func (r *RayClusterReconciler) reconcileAutoscalerRole(ctx context.Context, inst
}

// making sure the name is valid
role.Name = utils.CheckName(role.Name)
role.Name = utils.CheckName(ctx, role.Name)
// Set controller reference
if err := controllerutil.SetControllerReference(instance, role, r.Scheme); err != nil {
return err
Expand Down Expand Up @@ -1567,13 +1567,13 @@ func (r *RayClusterReconciler) reconcileAutoscalerRoleBinding(ctx context.Contex
}

// Create role bindings for autoscaler if there's no existing one in the cluster.
roleBinding, err := common.BuildRoleBinding(instance)
roleBinding, err := common.BuildRoleBinding(ctx, instance)
if err != nil {
return err
}

// making sure the name is valid
roleBinding.Name = utils.CheckName(roleBinding.Name)
roleBinding.Name = utils.CheckName(ctx, roleBinding.Name)
// Set controller reference
if err := controllerutil.SetControllerReference(instance, roleBinding, r.Scheme); err != nil {
return err
Expand Down
Loading