/
list.go
93 lines (77 loc) · 2.28 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package compute
import (
"context"
"log"
"os"
"strconv"
"time"
"github.com/olekukonko/tablewriter"
"github.com/ray-project/kuberay/cli/pkg/cmdutil"
"github.com/ray-project/kuberay/proto/go_client"
"github.com/spf13/cobra"
)
type ListOptions struct {
namespace string
}
func newCmdList() *cobra.Command {
opts := ListOptions{}
cmd := &cobra.Command{
Use: "list",
Short: "List all compute templates",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return listComputeTemplates(opts)
},
}
cmd.Flags().StringVarP(&opts.namespace, "namespace", "n", "",
"kubernetes namespace where the compute template is stored")
return cmd
}
func listComputeTemplates(opts ListOptions) error {
// Get gRPC connection
conn, err := cmdutil.GetGrpcConn()
if err != nil {
return err
}
defer conn.Close()
// build gRPC client
client := go_client.NewComputeTemplateServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var computeTemplates []*go_client.ComputeTemplate
if len(opts.namespace) == 0 {
r, err := client.ListAllComputeTemplates(ctx, &go_client.ListAllComputeTemplatesRequest{})
if err != nil {
log.Fatalf("could not list all compute templates %v", err)
}
computeTemplates = r.GetComputeTemplates()
} else {
r, err := client.ListComputeTemplates(ctx, &go_client.ListComputeTemplatesRequest{
Namespace: opts.namespace,
})
if err != nil {
log.Fatalf("could not list compute templates %v", err)
}
computeTemplates = r.GetComputeTemplates()
}
rows := convertComputeTemplatesToStrings(computeTemplates)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Namespace", "CPU", "Memory", "GPU", "GPU-Accelerator"})
table.AppendBulk(rows)
table.Render()
return nil
}
func convertComputeTemplatesToStrings(computeTemplates []*go_client.ComputeTemplate) [][]string {
var data [][]string
for _, r := range computeTemplates {
data = append(data, convertComputeTemplatToString(r))
}
return data
}
func convertComputeTemplatToString(r *go_client.ComputeTemplate) []string {
line := []string{
r.GetName(), r.Namespace, strconv.Itoa(int(r.GetCpu())), strconv.Itoa(int(r.Memory)),
strconv.Itoa(int(r.GetGpu())), r.GetGpuAccelerator(),
}
return line
}