Skip to content

Commit

Permalink
Feat: Export workspace gateway config for configuration in dashboard (#…
Browse files Browse the repository at this point in the history
…793)

```
❯ beta9 config export-config
{
    "gateway_http_url": "localhost",
    "gateway_http_port": 1994,
    "gateway_grpc_url": "localhost",
    "gateway_grpc_port": 1993,
    "workspace_id": "d78b6cfa-1357-45e8-b61f-07edef33f71e",
    "token": "********************"
}
```

This is the simplified way to configure a self-hosted beam cluster into
the management dashboard
  • Loading branch information
jsun-m authored Dec 19, 2024
1 parent 4303376 commit bb94191
Show file tree
Hide file tree
Showing 7 changed files with 622 additions and 303 deletions.
24 changes: 24 additions & 0 deletions pkg/api/v1/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewWorkspaceGroup(g *echo.Group, backendRepo repository.BackendRepository,

g.POST("", group.CreateWorkspace)
g.GET("/current", auth.WithAuth(group.CurrentWorkspace))
g.GET("/:workspaceId/export", auth.WithWorkspaceAuth(group.ExportWorkspaceConfig))

return group
}
Expand Down Expand Up @@ -56,3 +57,26 @@ func (g *WorkspaceGroup) CurrentWorkspace(ctx echo.Context) error {

return ctx.JSON(http.StatusOK, authContext.AuthInfo.Workspace)
}

type WorkspaceConfigExport struct {
GatewayHTTPURL string `json:"gateway_http_url"`
GatewayHTTPPort int `json:"gateway_http_port"`
GatewayGRPCURL string `json:"gateway_grpc_url"`
GatewayGRPCPort int `json:"gateway_grpc_port"`
WorkspaceID string `json:"workspace_id"`
Token string `json:"token"`
}

func (g *WorkspaceGroup) ExportWorkspaceConfig(ctx echo.Context) error {
workspaceId := ctx.Param("workspaceId")

config := WorkspaceConfigExport{
GatewayHTTPURL: g.config.GatewayService.HTTP.ExternalHost,
GatewayHTTPPort: g.config.GatewayService.HTTP.Port,
GatewayGRPCURL: g.config.GatewayService.GRPC.ExternalHost,
GatewayGRPCPort: g.config.GatewayService.GRPC.Port,
WorkspaceID: workspaceId,
}

return ctx.JSON(http.StatusOK, config)
}
14 changes: 14 additions & 0 deletions pkg/gateway/gateway.proto
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ service GatewayService {
rpc CordonWorker(CordonWorkerRequest) returns (CordonWorkerResponse);
rpc UncordonWorker(UncordonWorkerRequest) returns (UncordonWorkerResponse);
rpc DrainWorker(DrainWorkerRequest) returns (DrainWorkerResponse);

// Workspace
rpc ExportWorkspaceConfig(ExportWorkspaceConfigRequest)
returns (ExportWorkspaceConfigResponse);
}

message AuthorizeRequest {}
Expand Down Expand Up @@ -504,3 +508,13 @@ message DrainWorkerResponse {
bool ok = 1;
string err_msg = 2;
}

message ExportWorkspaceConfigRequest {}

message ExportWorkspaceConfigResponse {
string gateway_http_url = 1;
int32 gateway_http_port = 2;
string gateway_grpc_url = 3;
int32 gateway_grpc_port = 4;
string workspace_id = 5;
}
20 changes: 20 additions & 0 deletions pkg/gateway/services/workspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gatewayservices

import (
"context"

"github.com/beam-cloud/beta9/pkg/auth"
pb "github.com/beam-cloud/beta9/proto"
)

func (gws *GatewayService) ExportWorkspaceConfig(ctx context.Context, in *pb.ExportWorkspaceConfigRequest) (*pb.ExportWorkspaceConfigResponse, error) {
authInfo, _ := auth.AuthInfoFromContext(ctx)

return &pb.ExportWorkspaceConfigResponse{
GatewayHttpUrl: gws.appConfig.GatewayService.HTTP.ExternalHost,
GatewayHttpPort: int32(gws.appConfig.GatewayService.HTTP.ExternalPort),
GatewayGrpcUrl: gws.appConfig.GatewayService.GRPC.ExternalHost,
GatewayGrpcPort: int32(gws.appConfig.GatewayService.GRPC.ExternalPort),
WorkspaceId: authInfo.Workspace.ExternalId,
}, nil
}
715 changes: 442 additions & 273 deletions proto/gateway.pb.go

Large diffs are not rendered by default.

99 changes: 69 additions & 30 deletions proto/gateway_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions sdk/src/beta9/cli/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from pathlib import Path
from typing import Any

Expand All @@ -6,6 +7,9 @@
from rich.table import Column, Table, box

from .. import terminal
from ..channel import ServiceClient
from ..cli import extraclick
from ..clients.gateway import ExportWorkspaceConfigRequest
from ..config import (
DEFAULT_CONTEXT_NAME,
ConfigContext,
Expand Down Expand Up @@ -175,3 +179,29 @@ def select_context(name: str, config_path: Path):
save_config(contexts=contexts, path=config_path)

terminal.success(f"Default context updated with '{name}'.")


@management.command(
name="export",
help="Export the current workspace configuration",
)
@click.argument(
"name",
type=click.STRING,
default=DEFAULT_CONTEXT_NAME,
required=False,
)
@extraclick.pass_service_client
def export_config(
service: ServiceClient,
name: str,
):
contexts = load_config()
context = contexts.get(name)

if not context:
terminal.error(f"Context '{name}' does not exist.")

config = service.gateway.export_workspace_config(ExportWorkspaceConfigRequest()).to_dict()
config["token"] = context.token
terminal.print(json.dumps(config, indent=2))
23 changes: 23 additions & 0 deletions sdk/src/beta9/clients/gateway/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bb94191

Please sign in to comment.