Skip to content

Commit e37b70c

Browse files
authored
[refactor][7/N] Make dashboard http client a new package (#4057)
* update Signed-off-by: You-Cheng Lin (Owen) <[email protected]> * make member private by create a init func Signed-off-by: You-Cheng Lin (Owen) <[email protected]> * add comment Signed-off-by: You-Cheng Lin (Owen) <[email protected]> --------- Signed-off-by: You-Cheng Lin (Owen) <[email protected]>
1 parent 491c488 commit e37b70c

File tree

12 files changed

+49
-28
lines changed

12 files changed

+49
-28
lines changed

apiserver/pkg/server/ray_job_submission_service_server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
api "github.com/ray-project/kuberay/proto/go_client"
2121
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
2222
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils"
23+
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils/dashboardclient"
2324
utiltypes "github.com/ray-project/kuberay/ray-operator/controllers/ray/utils/types"
2425
)
2526

@@ -33,7 +34,7 @@ type RayJobSubmissionServiceServer struct {
3334
api.UnimplementedRayJobSubmissionServiceServer
3435
options *RayJobSubmissionServiceServerOptions
3536
clusterServer *ClusterServer
36-
dashboardClientFunc func(rayCluster *rayv1.RayCluster, url string) (utils.RayDashboardClientInterface, error)
37+
dashboardClientFunc func(rayCluster *rayv1.RayCluster, url string) (dashboardclient.RayDashboardClientInterface, error)
3738
log logr.Logger
3839
}
3940

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
99
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils"
10+
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils/dashboardclient"
1011
)
1112

1213
//+kubebuilder:object:root=true
@@ -84,7 +85,7 @@ type Configuration struct {
8485
EnableMetrics bool `json:"enableMetrics,omitempty"`
8586
}
8687

87-
func (config Configuration) GetDashboardClient(mgr manager.Manager) func(rayCluster *rayv1.RayCluster, url string) (utils.RayDashboardClientInterface, error) {
88+
func (config Configuration) GetDashboardClient(mgr manager.Manager) func(rayCluster *rayv1.RayCluster, url string) (dashboardclient.RayDashboardClientInterface, error) {
8889
return utils.GetRayDashboardClientFunc(mgr, config.UseKubernetesProxy)
8990
}
9091

ray-operator/controllers/ray/rayjob_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ray-project/kuberay/ray-operator/controllers/ray/common"
2929
"github.com/ray-project/kuberay/ray-operator/controllers/ray/metrics"
3030
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils"
31+
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils/dashboardclient"
3132
"github.com/ray-project/kuberay/ray-operator/pkg/features"
3233
)
3334

@@ -43,7 +44,7 @@ type RayJobReconciler struct {
4344
Scheme *runtime.Scheme
4445
Recorder record.EventRecorder
4546

46-
dashboardClientFunc func(rayCluster *rayv1.RayCluster, url string) (utils.RayDashboardClientInterface, error)
47+
dashboardClientFunc func(rayCluster *rayv1.RayCluster, url string) (dashboardclient.RayDashboardClientInterface, error)
4748
options RayJobReconcilerOptions
4849
}
4950

ray-operator/controllers/ray/rayservice_controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
3333
"github.com/ray-project/kuberay/ray-operator/controllers/ray/common"
3434
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils"
35+
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils/dashboardclient"
3536
utiltypes "github.com/ray-project/kuberay/ray-operator/controllers/ray/utils/types"
3637
"github.com/ray-project/kuberay/ray-operator/pkg/features"
3738
)
@@ -53,7 +54,7 @@ type RayServiceReconciler struct {
5354
// Cache value is map of RayCluster name to Serve application config.
5455
ServeConfigs *lru.Cache
5556
RayClusterDeletionTimestamps cmap.ConcurrentMap[string, time.Time]
56-
dashboardClientFunc func(rayCluster *rayv1.RayCluster, url string) (utils.RayDashboardClientInterface, error)
57+
dashboardClientFunc func(rayCluster *rayv1.RayCluster, url string) (dashboardclient.RayDashboardClientInterface, error)
5758
httpProxyClientFunc func(hostIp, podNamespace, podName string, port int) utils.RayHttpProxyClientInterface
5859
}
5960

@@ -738,7 +739,7 @@ func checkIfNeedSubmitServeApplications(cachedServeConfigV2 string, serveConfigV
738739
return false, "Current V2 Serve config matches cached Serve config."
739740
}
740741

741-
func (r *RayServiceReconciler) updateServeDeployment(ctx context.Context, rayServiceInstance *rayv1.RayService, rayDashboardClient utils.RayDashboardClientInterface, clusterName string) error {
742+
func (r *RayServiceReconciler) updateServeDeployment(ctx context.Context, rayServiceInstance *rayv1.RayService, rayDashboardClient dashboardclient.RayDashboardClientInterface, clusterName string) error {
742743
logger := ctrl.LoggerFrom(ctx)
743744
logger.Info("updateServeDeployment", "V2 config", rayServiceInstance.Spec.ServeConfigV2)
744745

@@ -772,7 +773,7 @@ func (r *RayServiceReconciler) updateServeDeployment(ctx context.Context, raySer
772773
// (1) `isReady`: Whether the Serve applications are ready to serve incoming traffic or not.
773774
// (2) `newApplications`: The Serve applications' statuses.
774775
// (3) `err`: If `err` is not nil, it means that KubeRay failed to get Serve application statuses from the dashboard.
775-
func getAndCheckServeStatus(ctx context.Context, dashboardClient utils.RayDashboardClientInterface) (bool, map[string]rayv1.AppStatus, error) {
776+
func getAndCheckServeStatus(ctx context.Context, dashboardClient dashboardclient.RayDashboardClientInterface) (bool, map[string]rayv1.AppStatus, error) {
776777
logger := ctrl.LoggerFrom(ctx)
777778
var serveAppStatuses map[string]*utiltypes.ServeApplicationStatus
778779
var err error

ray-operator/controllers/ray/rayservice_controller_unit_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
2525
"github.com/ray-project/kuberay/ray-operator/controllers/ray/common"
2626
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils"
27+
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils/dashboardclient"
2728
utiltypes "github.com/ray-project/kuberay/ray-operator/controllers/ray/utils/types"
2829
"github.com/ray-project/kuberay/ray-operator/pkg/client/clientset/versioned/scheme"
2930
"github.com/ray-project/kuberay/ray-operator/test/support"
@@ -367,7 +368,7 @@ func TestGetAndCheckServeStatus(t *testing.T) {
367368

368369
for _, tc := range tests {
369370
t.Run(tc.name, func(t *testing.T) {
370-
var dashboardClient utils.RayDashboardClientInterface
371+
var dashboardClient dashboardclient.RayDashboardClientInterface
371372
if len(tc.rayServiceStatus) != 0 {
372373
dashboardClient = initFakeDashboardClient(serveAppName, tc.rayServiceStatus[DeploymentStatus], tc.rayServiceStatus[ApplicationStatus])
373374
} else {
@@ -586,7 +587,7 @@ func TestReconcileRayCluster_UpdatePendingCluster(t *testing.T) {
586587
assert.Len(t, pendingCluster.Spec.WorkerGroupSpecs, expectedWorkerGroupCount)
587588
}
588589

589-
func initFakeDashboardClient(appName string, deploymentStatus string, appStatus string) utils.RayDashboardClientInterface {
590+
func initFakeDashboardClient(appName string, deploymentStatus string, appStatus string) dashboardclient.RayDashboardClientInterface {
590591
fakeDashboardClient := utils.FakeRayDashboardClient{}
591592
status := generateServeStatus(deploymentStatus, appStatus)
592593
fakeDashboardClient.SetMultiApplicationStatuses(map[string]*utiltypes.ServeApplicationStatus{appName: &status})

ray-operator/controllers/ray/suite_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535

3636
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
3737
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils"
38+
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils/dashboardclient"
3839
)
3940

4041
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
@@ -51,8 +52,8 @@ var (
5152

5253
type TestClientProvider struct{}
5354

54-
func (testProvider TestClientProvider) GetDashboardClient(_ manager.Manager) func(rayCluster *rayv1.RayCluster, url string) (utils.RayDashboardClientInterface, error) {
55-
return func(_ *rayv1.RayCluster, _ string) (utils.RayDashboardClientInterface, error) {
55+
func (testProvider TestClientProvider) GetDashboardClient(_ manager.Manager) func(rayCluster *rayv1.RayCluster, url string) (dashboardclient.RayDashboardClientInterface, error) {
56+
return func(_ *rayv1.RayCluster, _ string) (dashboardclient.RayDashboardClientInterface, error) {
5657
return fakeRayDashboardClient, nil
5758
}
5859
}

ray-operator/controllers/ray/utils/dashboard_httpclient.go renamed to ray-operator/controllers/ray/utils/dashboardclient/dashboard_httpclient.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package dashboardclient
22

33
import (
44
"bytes"
@@ -24,6 +24,7 @@ var (
2424
)
2525

2626
type RayDashboardClientInterface interface {
27+
InitClient(client *http.Client, dashboardURL string)
2728
UpdateDeployments(ctx context.Context, configJson []byte) error
2829
// V2/multi-app Rest API
2930
GetServeDetails(ctx context.Context) (*utiltypes.ServeDetails, error)
@@ -42,6 +43,11 @@ type RayDashboardClient struct {
4243
dashboardURL string
4344
}
4445

46+
func (r *RayDashboardClient) InitClient(client *http.Client, dashboardURL string) {
47+
r.client = client
48+
r.dashboardURL = dashboardURL
49+
}
50+
4551
// UpdateDeployments update the deployments in the Ray cluster.
4652
func (r *RayDashboardClient) UpdateDeployments(ctx context.Context, configJson []byte) error {
4753
var req *http.Request

ray-operator/controllers/ray/utils/dashboard_httpclient_test.go renamed to ray-operator/controllers/ray/utils/dashboardclient/dashboard_httpclient_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package dashboardclient
22

33
import (
44
"context"

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package utils
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67
"sync/atomic"
78

89
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
10+
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils/dashboardclient"
911
utiltypes "github.com/ray-project/kuberay/ray-operator/controllers/ray/utils/types"
1012
)
1113

@@ -15,7 +17,10 @@ type FakeRayDashboardClient struct {
1517
serveDetails utiltypes.ServeDetails
1618
}
1719

18-
var _ RayDashboardClientInterface = (*FakeRayDashboardClient)(nil)
20+
var _ dashboardclient.RayDashboardClientInterface = (*FakeRayDashboardClient)(nil)
21+
22+
func (r *FakeRayDashboardClient) InitClient(_ *http.Client, _ string) {
23+
}
1924

2025
func (r *FakeRayDashboardClient) UpdateDeployments(_ context.Context, _ []byte) error {
2126
fmt.Print("UpdateDeployments fake succeeds.")

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"sigs.k8s.io/controller-runtime/pkg/manager"
2727

2828
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
29+
"github.com/ray-project/kuberay/ray-operator/controllers/ray/utils/dashboardclient"
2930
)
3031

3132
const (
@@ -640,7 +641,7 @@ func EnvVarByName(envName string, envVars []corev1.EnvVar) (corev1.EnvVar, bool)
640641
}
641642

642643
type ClientProvider interface {
643-
GetDashboardClient(mgr manager.Manager) func(rayCluster *rayv1.RayCluster, url string) (RayDashboardClientInterface, error)
644+
GetDashboardClient(mgr manager.Manager) func(rayCluster *rayv1.RayCluster, url string) (dashboardclient.RayDashboardClientInterface, error)
644645
GetHttpProxyClient(mgr manager.Manager) func(hostIp, podNamespace, podName string, port int) RayHttpProxyClientInterface
645646
}
646647

@@ -757,8 +758,9 @@ func FetchHeadServiceURL(ctx context.Context, cli client.Client, rayCluster *ray
757758
return headServiceURL, nil
758759
}
759760

760-
func GetRayDashboardClientFunc(mgr manager.Manager, useKubernetesProxy bool) func(rayCluster *rayv1.RayCluster, url string) (RayDashboardClientInterface, error) {
761-
return func(rayCluster *rayv1.RayCluster, url string) (RayDashboardClientInterface, error) {
761+
func GetRayDashboardClientFunc(mgr manager.Manager, useKubernetesProxy bool) func(rayCluster *rayv1.RayCluster, url string) (dashboardclient.RayDashboardClientInterface, error) {
762+
return func(rayCluster *rayv1.RayCluster, url string) (dashboardclient.RayDashboardClientInterface, error) {
763+
dashboardClient := &dashboardclient.RayDashboardClient{}
762764
if useKubernetesProxy {
763765
var err error
764766
headSvcName := rayCluster.Status.Head.ServiceName
@@ -769,20 +771,20 @@ func GetRayDashboardClientFunc(mgr manager.Manager, useKubernetesProxy bool) fun
769771
return nil, err
770772
}
771773
}
772-
return &RayDashboardClient{
774+
775+
dashboardClient.InitClient(
773776
// Use `mgr.GetHTTPClient()` instead of `http.Client{}` so that the client has proper authentication
774777
// configured to communicate with the Kubernetes API server.
775-
client: mgr.GetHTTPClient(),
776-
dashboardURL: fmt.Sprintf("%s/api/v1/namespaces/%s/services/%s:dashboard/proxy", mgr.GetConfig().Host, rayCluster.Namespace, headSvcName),
777-
}, nil
778+
mgr.GetHTTPClient(),
779+
fmt.Sprintf("%s/api/v1/namespaces/%s/services/%s:dashboard/proxy", mgr.GetConfig().Host, rayCluster.Namespace, headSvcName),
780+
)
781+
return dashboardClient, nil
778782
}
779783

780-
return &RayDashboardClient{
781-
client: &http.Client{
782-
Timeout: 2 * time.Second,
783-
},
784-
dashboardURL: "http://" + url,
785-
}, nil
784+
dashboardClient.InitClient(&http.Client{
785+
Timeout: 2 * time.Second,
786+
}, "http://"+url)
787+
return dashboardClient, nil
786788
}
787789
}
788790

0 commit comments

Comments
 (0)