diff --git a/ray-operator/apis/config/v1alpha1/configuration_types.go b/ray-operator/apis/config/v1alpha1/configuration_types.go index 73ccee14523..dec9e8addf5 100644 --- a/ray-operator/apis/config/v1alpha1/configuration_types.go +++ b/ray-operator/apis/config/v1alpha1/configuration_types.go @@ -88,6 +88,6 @@ func (config Configuration) GetDashboardClient(mgr manager.Manager) func(rayClus return utils.GetRayDashboardClientFunc(mgr, config.UseKubernetesProxy) } -func (config Configuration) GetHttpProxyClient(mgr manager.Manager) func() utils.RayHttpProxyClientInterface { +func (config Configuration) GetHttpProxyClient(mgr manager.Manager) func(hostIp, podNamespace, podName string, port int) utils.RayHttpProxyClientInterface { return utils.GetRayHttpProxyClientFunc(mgr, config.UseKubernetesProxy) } diff --git a/ray-operator/controllers/ray/rayservice_controller.go b/ray-operator/controllers/ray/rayservice_controller.go index 31940973c8a..b008b0870f0 100644 --- a/ray-operator/controllers/ray/rayservice_controller.go +++ b/ray-operator/controllers/ray/rayservice_controller.go @@ -53,7 +53,7 @@ type RayServiceReconciler struct { ServeConfigs *lru.Cache RayClusterDeletionTimestamps cmap.ConcurrentMap[string, time.Time] dashboardClientFunc func(rayCluster *rayv1.RayCluster, url string) (utils.RayDashboardClientInterface, error) - httpProxyClientFunc func() utils.RayHttpProxyClientInterface + httpProxyClientFunc func(hostIp, podNamespace, podName string, port int) utils.RayHttpProxyClientInterface } // NewRayServiceReconciler returns a new reconcile.Reconciler @@ -983,13 +983,10 @@ func (r *RayServiceReconciler) updateHeadPodServeLabel(ctx context.Context, rayS return fmt.Errorf("found 0 head. cluster name %s, namespace %v", rayClusterInstance.Name, rayClusterInstance.Namespace) } - client := r.httpProxyClientFunc() - client.InitClient() - rayContainer := headPod.Spec.Containers[utils.RayContainerIndex] servingPort := utils.FindContainerPort(&rayContainer, utils.ServingPortName, utils.DefaultServingPort) - client.SetHostIp(headPod.Status.PodIP, headPod.Namespace, headPod.Name, servingPort) + client := r.httpProxyClientFunc(headPod.Status.PodIP, headPod.Namespace, headPod.Name, servingPort) if headPod.Labels == nil { headPod.Labels = make(map[string]string) } diff --git a/ray-operator/controllers/ray/rayservice_controller_unit_test.go b/ray-operator/controllers/ray/rayservice_controller_unit_test.go index e0e04d18b21..e5824b9b264 100644 --- a/ray-operator/controllers/ray/rayservice_controller_unit_test.go +++ b/ray-operator/controllers/ray/rayservice_controller_unit_test.go @@ -671,7 +671,7 @@ func TestLabelHeadPodForServeStatus(t *testing.T) { Client: fakeClient, Recorder: &record.FakeRecorder{}, Scheme: newScheme, - httpProxyClientFunc: func() utils.RayHttpProxyClientInterface { + httpProxyClientFunc: func(_, _, _ string, _ int) utils.RayHttpProxyClientInterface { return fakeRayHttpProxyClient }, } diff --git a/ray-operator/controllers/ray/suite_test.go b/ray-operator/controllers/ray/suite_test.go index 1d45dfffcea..5df54b8b54b 100644 --- a/ray-operator/controllers/ray/suite_test.go +++ b/ray-operator/controllers/ray/suite_test.go @@ -57,8 +57,8 @@ func (testProvider TestClientProvider) GetDashboardClient(_ manager.Manager) fun } } -func (testProvider TestClientProvider) GetHttpProxyClient(_ manager.Manager) func() utils.RayHttpProxyClientInterface { - return func() utils.RayHttpProxyClientInterface { +func (testProvider TestClientProvider) GetHttpProxyClient(_ manager.Manager) func(hostIp, podNamespace, podName string, port int) utils.RayHttpProxyClientInterface { + return func(_, _, _ string, _ int) utils.RayHttpProxyClientInterface { return fakeRayHttpProxyClient } } diff --git a/ray-operator/controllers/ray/utils/fake_httpproxy_httpclient.go b/ray-operator/controllers/ray/utils/fake_httpproxy_httpclient.go index ccd0f99bda6..90a39db25a0 100644 --- a/ray-operator/controllers/ray/utils/fake_httpproxy_httpclient.go +++ b/ray-operator/controllers/ray/utils/fake_httpproxy_httpclient.go @@ -9,10 +9,6 @@ type FakeRayHttpProxyClient struct { IsHealthy bool } -func (fc *FakeRayHttpProxyClient) InitClient() {} - -func (fc *FakeRayHttpProxyClient) SetHostIp(_, _, _ string, _ int) {} - func (fc *FakeRayHttpProxyClient) CheckProxyActorHealth(_ context.Context) error { if !fc.IsHealthy { return fmt.Errorf("fake proxy actor is not healthy") diff --git a/ray-operator/controllers/ray/utils/httpproxy_httpclient.go b/ray-operator/controllers/ray/utils/httpproxy_httpclient.go index 3a37f476ab5..e9c53b38128 100644 --- a/ray-operator/controllers/ray/utils/httpproxy_httpclient.go +++ b/ray-operator/controllers/ray/utils/httpproxy_httpclient.go @@ -6,30 +6,15 @@ import ( "io" "net/http" "time" - - ctrl "sigs.k8s.io/controller-runtime" ) type RayHttpProxyClientInterface interface { - InitClient() CheckProxyActorHealth(ctx context.Context) error - SetHostIp(hostIp, podNamespace, podName string, port int) -} - -func GetRayHttpProxyClientFunc(mgr ctrl.Manager, useKubernetesProxy bool) func() RayHttpProxyClientInterface { - return func() RayHttpProxyClientInterface { - return &RayHttpProxyClient{ - mgr: mgr, - useKubernetesProxy: useKubernetesProxy, - } - } } type RayHttpProxyClient struct { - client *http.Client - mgr ctrl.Manager - httpProxyURL string - useKubernetesProxy bool + client *http.Client + httpProxyURL string } func (r *RayHttpProxyClient) InitClient() { @@ -38,15 +23,6 @@ func (r *RayHttpProxyClient) InitClient() { } } -func (r *RayHttpProxyClient) SetHostIp(hostIp, podNamespace, podName string, port int) { - if r.useKubernetesProxy { - r.client = r.mgr.GetHTTPClient() - r.httpProxyURL = fmt.Sprintf("%s/api/v1/namespaces/%s/pods/%s:%d/proxy/", r.mgr.GetConfig().Host, podNamespace, podName, port) - } - - r.httpProxyURL = fmt.Sprintf("http://%s:%d/", hostIp, port) -} - // CheckProxyActorHealth checks the health status of the Ray Serve proxy actor. func (r *RayHttpProxyClient) CheckProxyActorHealth(ctx context.Context) error { req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.httpProxyURL+RayServeProxyHealthPath, nil) diff --git a/ray-operator/controllers/ray/utils/util.go b/ray-operator/controllers/ray/utils/util.go index 64f7717af0e..4308c0afb76 100644 --- a/ray-operator/controllers/ray/utils/util.go +++ b/ray-operator/controllers/ray/utils/util.go @@ -641,7 +641,7 @@ func EnvVarByName(envName string, envVars []corev1.EnvVar) (corev1.EnvVar, bool) type ClientProvider interface { GetDashboardClient(mgr manager.Manager) func(rayCluster *rayv1.RayCluster, url string) (RayDashboardClientInterface, error) - GetHttpProxyClient(mgr manager.Manager) func() RayHttpProxyClientInterface + GetHttpProxyClient(mgr manager.Manager) func(hostIp, podNamespace, podName string, port int) RayHttpProxyClientInterface } func ManagedByExternalController(controllerName *string) *string { @@ -785,3 +785,18 @@ func GetRayDashboardClientFunc(mgr manager.Manager, useKubernetesProxy bool) fun }, nil } } + +func GetRayHttpProxyClientFunc(mgr manager.Manager, useKubernetesProxy bool) func(hostIp, podNamespace, podName string, port int) RayHttpProxyClientInterface { + return func(hostIp, podNamespace, podName string, port int) RayHttpProxyClientInterface { + if useKubernetesProxy { + return &RayHttpProxyClient{ + client: mgr.GetHTTPClient(), + httpProxyURL: fmt.Sprintf("%s/api/v1/namespaces/%s/pods/%s:%d/proxy/", mgr.GetConfig().Host, podNamespace, podName, port), + } + } + return &RayHttpProxyClient{ + client: &http.Client{Timeout: 2 * time.Second}, + httpProxyURL: fmt.Sprintf("http://%s:%d/", hostIp, port), + } + } +}