Skip to content

Commit

Permalink
[gp-cli] provide workspace class info in top command
Browse files Browse the repository at this point in the history
  • Loading branch information
mustard-mh committed Aug 24, 2022
1 parent bd58d64 commit ef7ea8d
Show file tree
Hide file tree
Showing 8 changed files with 1,999 additions and 140 deletions.
88 changes: 72 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,75 @@ import (
"github.com/olekukonko/tablewriter"
)

var opts struct {
Json bool
}

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()

var (
wg sync.WaitGroup
workspaceResources *supervisor.ResourcesStatusResponse
wsInfo *supervisor.WorkspaceInfoResponse
)

wg.Add(2)

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

go func() {
wsInfo, err = supervisor.NewInfoServiceClient(conn).WorkspaceInfo(ctx, &supervisor.WorkspaceInfoRequest{})
if err != nil {
log.Fatalf("cannot get workspace info: %s", err)
}
wg.Done()
}()

wg.Wait()

if opts.Json {
obj := struct {
Resources *supervisor.ResourcesStatusResponse `json:"resources"`
Info *supervisor.WorkspaceInfoResponse `json:"info"`
}{
Resources: workspaceResources,
Info: wsInfo,
}
content, _ := json.Marshal(obj)
fmt.Println(string(content))
return
}
outputWorkspaceClass(wsInfo)
outputTable(workspaceResources)
},
}

func outputWorkspaceClass(wsInfo *supervisor.WorkspaceInfoResponse) {
if wsInfo.WorkspaceClass.DisplayName == "" {
fmt.Printf("Workspace Class: %s\n\n", wsInfo.WorkspaceClass.Id)
return
}
fmt.Printf("%s: %s\n\n", wsInfo.WorkspaceClass.DisplayName, wsInfo.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 +128,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(&opts.Json, "json", "j", false, "print like json")
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
208 changes: 171 additions & 37 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.

Loading

0 comments on commit ef7ea8d

Please sign in to comment.