Skip to content
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

[gp-cli] provide workspace class info in top command #12338

Merged
merged 1 commit into from
Aug 30, 2022
Merged
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
82 changes: 66 additions & 16 deletions components/gitpod-cli/cmd/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"sync"
"time"

supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
Expand All @@ -22,6 +24,69 @@ import (
"github.com/olekukonko/tablewriter"
)

var topCmdOpts struct {
Json bool
}

type topData struct {
Resources *supervisor.ResourcesStatusResponse `json:"resources"`
WorkspaceClass *supervisor.WorkspaceInfoResponse_WorkspaceClass `json:"workspace_class"`
}

var topCmd = &cobra.Command{
Use: "top",
Short: "Display usage of workspace resources (CPU and memory)",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

conn, err := supervisor_helper.Dial(ctx)
if err != nil {
log.Fatal(err)
}

defer conn.Close()

data := &topData{}

var wg sync.WaitGroup
wg.Add(2)

go func() {
workspaceResources, err := supervisor_helper.GetWorkspaceResources(ctx, conn)
if err != nil {
log.Fatalf("cannot get workspace resources: %s", err)
}
data.Resources = workspaceResources
wg.Done()
}()

go func() {
if wsInfo, err := supervisor.NewInfoServiceClient(conn).WorkspaceInfo(ctx, &supervisor.WorkspaceInfoRequest{}); err == nil {
data.WorkspaceClass = wsInfo.WorkspaceClass
}
wg.Done()
}()

wg.Wait()

if topCmdOpts.Json {
content, _ := json.Marshal(data)
fmt.Println(string(content))
return
}
outputWorkspaceClass(data.WorkspaceClass)
outputTable(data.Resources)
},
}

func outputWorkspaceClass(workspaceClass *supervisor.WorkspaceInfoResponse_WorkspaceClass) {
if workspaceClass == nil || workspaceClass.DisplayName == "" {
return
}
fmt.Printf("%s: %s\n\n", workspaceClass.DisplayName, workspaceClass.Description)
}

func outputTable(workspaceResources *supervisor.ResourcesStatusResponse) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"CPU (millicores)", "Memory (bytes)"})
Expand Down Expand Up @@ -57,23 +122,8 @@ func getColor(severity api.ResourceStatusSeverity) int {
}
}

var topCmd = &cobra.Command{
Use: "top",
Short: "Display usage of workspace resources (CPU and memory)",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

workspaceResources, err := supervisor_helper.GetWorkspaceResources(ctx)
if err != nil {
log.Fatalf("cannot get workspace resources: %s", err)
}

outputTable(workspaceResources)
},
}

func init() {
topCmd.Flags().BoolVarP(&noColor, "no-color", "", false, "Disable output colorization")
topCmd.Flags().BoolVarP(&topCmdOpts.Json, "json", "j", false, "Output in JSON format")
rootCmd.AddCommand(topCmd)
}
7 changes: 2 additions & 5 deletions components/gitpod-cli/pkg/supervisor-helper/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import (
"context"

supervisor "github.com/gitpod-io/gitpod/supervisor/api"
"google.golang.org/grpc"
)

func GetWorkspaceResources(ctx context.Context) (*supervisor.ResourcesStatusResponse, error) {
conn, err := Dial(ctx)
if err != nil {
return nil, err
}
func GetWorkspaceResources(ctx context.Context, conn *grpc.ClientConn) (*supervisor.ResourcesStatusResponse, error) {
client := supervisor.NewStatusServiceClient(conn)
workspaceResources, workspaceResourcesError := client.ResourcesStatus(ctx, &supervisor.ResourcesStatuRequest{})

Expand Down
9 changes: 9 additions & 0 deletions components/server/src/workspace/workspace-starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,14 @@ export class WorkspaceStarter {
contextEnv.setValue(JSON.stringify(workspace.context));
envvars.push(contextEnv);

const info = this.config.workspaceClasses.find((cls) => cls.id === instance.workspaceClass);
if (!!info) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need the !!?

Copy link
Member

@filiptronicek filiptronicek Aug 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd agree here, if is already checking truthiness / falsiness of the expression [1], so we are basically doing the boolean type coercion twice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mustard-mh did you see these comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mustard-mh did you see these comments?

@andreafalzetti Thanks for your reminder 🙏 . I have saw this, but want left it to webApp team to answer it 😂, I use !! because I see it exists in some typescript code

image

const workspaceClassInfoEnv = new EnvironmentVariable();
workspaceClassInfoEnv.setName("GITPOD_WORKSPACE_CLASS_INFO");
workspaceClassInfoEnv.setValue(JSON.stringify(info));
envvars.push(workspaceClassInfoEnv);
}

log.debug("Workspace config", workspace.config);
const tasks = this.ideService.resolveGitpodTasks(workspace);
if (tasks.length) {
Expand Down Expand Up @@ -1503,6 +1511,7 @@ export class WorkspaceStarter {
spec.setWorkspaceLocation(workspace.config.workspaceLocation || checkoutLocation);
spec.setFeatureFlagsList(this.toWorkspaceFeatureFlags(featureFlags));
spec.setClass(instance.workspaceClass!);

if (workspace.type === "regular") {
spec.setTimeout(this.userService.workspaceTimeoutToDuration(await userTimeoutPromise));
}
Expand Down
173 changes: 137 additions & 36 deletions components/supervisor-api/go/info.pb.go

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

13 changes: 13 additions & 0 deletions components/supervisor-api/info.proto
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,17 @@ message WorkspaceInfoResponse {

// ide_port is the port on which the IDE is to be run
uint32 ide_port = 14;

message WorkspaceClass {
mustard-mh marked this conversation as resolved.
Show resolved Hide resolved
// id is the id of the workspace class
string id = 1;

// display_name is the display_name of the workspace class
string display_name = 2;

// description is the description of the workspace class
string description = 3;
}
// workspace_class denotes the class of the workspace
WorkspaceClass workspace_class = 15;
}
Loading