Skip to content

Commit

Permalink
Cli: make namespace optional to adapt to ListAll operation (ray-proje…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffwan authored Jul 8, 2022
1 parent a47a3e1 commit d1ee745
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
29 changes: 17 additions & 12 deletions cli/pkg/cmd/cluster/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/ray-project/kuberay/cli/pkg/cmdutil"
"github.com/ray-project/kuberay/proto/go_client"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
)

type ListOptions struct {
Expand All @@ -31,9 +30,6 @@ func newCmdList() *cobra.Command {

cmd.Flags().StringVarP(&opts.namespace, "namespace", "n", "",
"kubernetes namespace where the cluster is provisioned")
if err := cmd.MarkFlagRequired("namespace"); err != nil {
klog.Warning(err)
}

return cmd
}
Expand All @@ -51,15 +47,24 @@ func listCluster(opts ListOptions) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

r, err := client.ListCluster(ctx, &go_client.ListClustersRequest{
Namespace: opts.namespace,
})
if err != nil {
log.Fatalf("could not list cluster clusters %v", err)
var clusters []*go_client.Cluster
if len(opts.namespace) == 0 {
r, err := client.ListAllClusters(ctx, &go_client.ListAllClustersRequest{})
if err != nil {
log.Fatalf("could not list all clusters %v", err)
}
clusters = r.GetClusters()
} else {
r, err := client.ListCluster(ctx, &go_client.ListClustersRequest{
Namespace: opts.namespace,
})
if err != nil {
log.Fatalf("could not list clusters %v", err)
}
clusters = r.GetClusters()
}
clusters := r.GetClusters()
rows, maxNumberWorkerGroups := convertClustersToStrings(clusters)
header := []string{"Name", "User", "Namespace", "Created At", "Version", "Environment", "Head Image", "Head Compute Template", "Head Service Type"}
header := []string{"Name", "Namespace", "User", "Version", "Environment", "Created At", "Head Image", "Head Compute Template", "Head Service Type"}
for i := 0; i < maxNumberWorkerGroups; i++ {
header = append(header, "Worker Group Name", "Worker Image", "Worker ComputeTemplate")
}
Expand Down Expand Up @@ -92,7 +97,7 @@ func convertClustersToStrings(clusters []*go_client.Cluster) ([][]string, int) {
func convertClusterToString(r *go_client.Cluster) ([]string, int) {
headResource := r.GetClusterSpec().GetHeadGroupSpec()
workerGroups := r.GetClusterSpec().GetWorkerGroupSpec()
line := []string{r.GetName(), r.GetUser(), r.GetNamespace(), r.GetCreatedAt().AsTime().String(), r.GetVersion(), r.GetEnvironment().String(),
line := []string{r.GetName(), r.GetNamespace(), r.GetUser(), r.GetVersion(), r.GetEnvironment().String(), r.GetCreatedAt().AsTime().String(),
headResource.GetImage(), headResource.GetComputeTemplate(), headResource.GetServiceType()}
nWorkGroups := len(workerGroups)

Expand Down
30 changes: 17 additions & 13 deletions cli/pkg/cmd/template/compute/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ray-project/kuberay/cli/pkg/cmdutil"
"github.com/ray-project/kuberay/proto/go_client"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
)

type ListOptions struct {
Expand All @@ -32,9 +31,6 @@ func newCmdList() *cobra.Command {

cmd.Flags().StringVarP(&opts.namespace, "namespace", "n", "",
"kubernetes namespace where the compute template is stored")
if err := cmd.MarkFlagRequired("namespace"); err != nil {
klog.Warning(err)
}

return cmd
}
Expand All @@ -52,17 +48,26 @@ func listComputeTemplates(opts ListOptions) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

r, err := client.ListComputeTemplates(ctx, &go_client.ListComputeTemplatesRequest{
Namespace: opts.namespace,
})
if err != nil {
log.Fatalf("could not list compute template %v", err)
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()
}
computeTemplates := r.GetComputeTemplates()
rows := convertComputeTemplatesToStrings(computeTemplates)

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "CPU", "Memory", "GPU", "GPU-Accelerator"})
table.SetHeader([]string{"Name", "Namespace", "CPU", "Memory", "GPU", "GPU-Accelerator"})
table.AppendBulk(rows)
table.Render()

Expand All @@ -77,11 +82,10 @@ func convertComputeTemplatesToStrings(computeTemplates []*go_client.ComputeTempl
}

return data

}

func convertComputeTemplatToString(r *go_client.ComputeTemplate) []string {
line := []string{r.GetName(), strconv.Itoa(int(r.GetCpu())), strconv.Itoa(int(r.Memory)),
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
}

0 comments on commit d1ee745

Please sign in to comment.