Skip to content

Commit f18b1a4

Browse files
authored
refactor httpproxy httpclient (#4010)
Signed-off-by: You-Cheng Lin (Owen) <mses010108@gmail.com>
1 parent 3129b87 commit f18b1a4

File tree

7 files changed

+24
-40
lines changed

7 files changed

+24
-40
lines changed

ray-operator/apis/config/v1alpha1/configuration_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ func (config Configuration) GetDashboardClient(mgr manager.Manager) func(rayClus
8888
return utils.GetRayDashboardClientFunc(mgr, config.UseKubernetesProxy)
8989
}
9090

91-
func (config Configuration) GetHttpProxyClient(mgr manager.Manager) func() utils.RayHttpProxyClientInterface {
91+
func (config Configuration) GetHttpProxyClient(mgr manager.Manager) func(hostIp, podNamespace, podName string, port int) utils.RayHttpProxyClientInterface {
9292
return utils.GetRayHttpProxyClientFunc(mgr, config.UseKubernetesProxy)
9393
}

ray-operator/controllers/ray/rayservice_controller.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type RayServiceReconciler struct {
5353
ServeConfigs *lru.Cache
5454
RayClusterDeletionTimestamps cmap.ConcurrentMap[string, time.Time]
5555
dashboardClientFunc func(rayCluster *rayv1.RayCluster, url string) (utils.RayDashboardClientInterface, error)
56-
httpProxyClientFunc func() utils.RayHttpProxyClientInterface
56+
httpProxyClientFunc func(hostIp, podNamespace, podName string, port int) utils.RayHttpProxyClientInterface
5757
}
5858

5959
// NewRayServiceReconciler returns a new reconcile.Reconciler
@@ -983,13 +983,10 @@ func (r *RayServiceReconciler) updateHeadPodServeLabel(ctx context.Context, rayS
983983
return fmt.Errorf("found 0 head. cluster name %s, namespace %v", rayClusterInstance.Name, rayClusterInstance.Namespace)
984984
}
985985

986-
client := r.httpProxyClientFunc()
987-
client.InitClient()
988-
989986
rayContainer := headPod.Spec.Containers[utils.RayContainerIndex]
990987
servingPort := utils.FindContainerPort(&rayContainer, utils.ServingPortName, utils.DefaultServingPort)
991-
client.SetHostIp(headPod.Status.PodIP, headPod.Namespace, headPod.Name, servingPort)
992988

989+
client := r.httpProxyClientFunc(headPod.Status.PodIP, headPod.Namespace, headPod.Name, servingPort)
993990
if headPod.Labels == nil {
994991
headPod.Labels = make(map[string]string)
995992
}

ray-operator/controllers/ray/rayservice_controller_unit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ func TestLabelHeadPodForServeStatus(t *testing.T) {
671671
Client: fakeClient,
672672
Recorder: &record.FakeRecorder{},
673673
Scheme: newScheme,
674-
httpProxyClientFunc: func() utils.RayHttpProxyClientInterface {
674+
httpProxyClientFunc: func(_, _, _ string, _ int) utils.RayHttpProxyClientInterface {
675675
return fakeRayHttpProxyClient
676676
},
677677
}

ray-operator/controllers/ray/suite_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func (testProvider TestClientProvider) GetDashboardClient(_ manager.Manager) fun
5757
}
5858
}
5959

60-
func (testProvider TestClientProvider) GetHttpProxyClient(_ manager.Manager) func() utils.RayHttpProxyClientInterface {
61-
return func() utils.RayHttpProxyClientInterface {
60+
func (testProvider TestClientProvider) GetHttpProxyClient(_ manager.Manager) func(hostIp, podNamespace, podName string, port int) utils.RayHttpProxyClientInterface {
61+
return func(_, _, _ string, _ int) utils.RayHttpProxyClientInterface {
6262
return fakeRayHttpProxyClient
6363
}
6464
}

ray-operator/controllers/ray/utils/fake_httpproxy_httpclient.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ type FakeRayHttpProxyClient struct {
99
IsHealthy bool
1010
}
1111

12-
func (fc *FakeRayHttpProxyClient) InitClient() {}
13-
14-
func (fc *FakeRayHttpProxyClient) SetHostIp(_, _, _ string, _ int) {}
15-
1612
func (fc *FakeRayHttpProxyClient) CheckProxyActorHealth(_ context.Context) error {
1713
if !fc.IsHealthy {
1814
return fmt.Errorf("fake proxy actor is not healthy")

ray-operator/controllers/ray/utils/httpproxy_httpclient.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,15 @@ import (
66
"io"
77
"net/http"
88
"time"
9-
10-
ctrl "sigs.k8s.io/controller-runtime"
119
)
1210

1311
type RayHttpProxyClientInterface interface {
14-
InitClient()
1512
CheckProxyActorHealth(ctx context.Context) error
16-
SetHostIp(hostIp, podNamespace, podName string, port int)
17-
}
18-
19-
func GetRayHttpProxyClientFunc(mgr ctrl.Manager, useKubernetesProxy bool) func() RayHttpProxyClientInterface {
20-
return func() RayHttpProxyClientInterface {
21-
return &RayHttpProxyClient{
22-
mgr: mgr,
23-
useKubernetesProxy: useKubernetesProxy,
24-
}
25-
}
2613
}
2714

2815
type RayHttpProxyClient struct {
29-
client *http.Client
30-
mgr ctrl.Manager
31-
httpProxyURL string
32-
useKubernetesProxy bool
16+
client *http.Client
17+
httpProxyURL string
3318
}
3419

3520
func (r *RayHttpProxyClient) InitClient() {
@@ -38,15 +23,6 @@ func (r *RayHttpProxyClient) InitClient() {
3823
}
3924
}
4025

41-
func (r *RayHttpProxyClient) SetHostIp(hostIp, podNamespace, podName string, port int) {
42-
if r.useKubernetesProxy {
43-
r.client = r.mgr.GetHTTPClient()
44-
r.httpProxyURL = fmt.Sprintf("%s/api/v1/namespaces/%s/pods/%s:%d/proxy/", r.mgr.GetConfig().Host, podNamespace, podName, port)
45-
}
46-
47-
r.httpProxyURL = fmt.Sprintf("http://%s:%d/", hostIp, port)
48-
}
49-
5026
// CheckProxyActorHealth checks the health status of the Ray Serve proxy actor.
5127
func (r *RayHttpProxyClient) CheckProxyActorHealth(ctx context.Context) error {
5228
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.httpProxyURL+RayServeProxyHealthPath, nil)

ray-operator/controllers/ray/utils/util.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ func EnvVarByName(envName string, envVars []corev1.EnvVar) (corev1.EnvVar, bool)
641641

642642
type ClientProvider interface {
643643
GetDashboardClient(mgr manager.Manager) func(rayCluster *rayv1.RayCluster, url string) (RayDashboardClientInterface, error)
644-
GetHttpProxyClient(mgr manager.Manager) func() RayHttpProxyClientInterface
644+
GetHttpProxyClient(mgr manager.Manager) func(hostIp, podNamespace, podName string, port int) RayHttpProxyClientInterface
645645
}
646646

647647
func ManagedByExternalController(controllerName *string) *string {
@@ -785,3 +785,18 @@ func GetRayDashboardClientFunc(mgr manager.Manager, useKubernetesProxy bool) fun
785785
}, nil
786786
}
787787
}
788+
789+
func GetRayHttpProxyClientFunc(mgr manager.Manager, useKubernetesProxy bool) func(hostIp, podNamespace, podName string, port int) RayHttpProxyClientInterface {
790+
return func(hostIp, podNamespace, podName string, port int) RayHttpProxyClientInterface {
791+
if useKubernetesProxy {
792+
return &RayHttpProxyClient{
793+
client: mgr.GetHTTPClient(),
794+
httpProxyURL: fmt.Sprintf("%s/api/v1/namespaces/%s/pods/%s:%d/proxy/", mgr.GetConfig().Host, podNamespace, podName, port),
795+
}
796+
}
797+
return &RayHttpProxyClient{
798+
client: &http.Client{Timeout: 2 * time.Second},
799+
httpProxyURL: fmt.Sprintf("http://%s:%d/", hostIp, port),
800+
}
801+
}
802+
}

0 commit comments

Comments
 (0)