Skip to content
Open
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
116 changes: 74 additions & 42 deletions cmd/pod/getPod.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pod

import (
"encoding/json"
"fmt"
"io"
"os"
"strings"

Expand All @@ -13,6 +15,7 @@ import (
)

var AllFields bool
var outputJSON bool

var GetPodCmd = &cobra.Command{
Use: "pod [podId]",
Expand All @@ -23,55 +26,84 @@ var GetPodCmd = &cobra.Command{
pods, err := api.GetPods()
cobra.CheckErr(err)

data := make([][]string, len(pods))
for i, p := range pods {
if len(args) == 1 && p.Id != strings.ToLower(args[0]) {
continue
}
row := []string{p.Id, p.Name, fmt.Sprintf("%d %s", p.GpuCount, p.Machine.GpuDisplayName), p.ImageName, p.DesiredStatus}
if AllFields {
pods = filter(pods, args)

var portEntries int = 0
if p.Runtime != nil && p.Runtime.Ports != nil {
portEntries = len(p.Runtime.Ports)
}
ports := make([]string, portEntries)
for j := range ports {
var privpub string = "prv"
if p.Runtime.Ports[j].IsIpPublic {
privpub = "pub"
}
ports[j] = fmt.Sprintf("%s:%d->%d\u00A0(%s,%s)", p.Runtime.Ports[j].Ip, p.Runtime.Ports[j].PublicPort, p.Runtime.Ports[j].PrivatePort, privpub, p.Runtime.Ports[j].PortType)
}
if outputJSON {
cobra.CheckErr(toJSON(os.Stdout, pods))
} else {
toTable(pods)
}
},
}

row = append(
row,
p.PodType,
fmt.Sprintf("%d", p.VcpuCount),
fmt.Sprintf("%d", p.MemoryInGb),
fmt.Sprintf("%d", p.ContainerDiskInGb),
fmt.Sprintf("%d", p.VolumeInGb),
fmt.Sprintf("%s", p.Machine.Location),
fmt.Sprintf("%.3f", p.CostPerHr),
fmt.Sprintf("%s", strings.Join(ports[:], ",")),
)
}
data[i] = row
func init() {
GetPodCmd.Flags().BoolVarP(&AllFields, "allfields", "a", false, "include all fields in output")
GetPodCmd.Flags().BoolVarP(&outputJSON, "json", "j", false, "use json as output format")
}

func filter(pods []*api.Pod, args []string) []*api.Pod {
filtered := make([]*api.Pod, 0) // ensures [] instead of "null" if no pods when mashalling
for _, p := range pods {
if len(args) == 1 && p.Id != strings.ToLower(args[0]) {
continue
}
filtered = append(filtered, p)
}
return filtered
}

header := []string{"ID", "Name", "GPU", "Image Name", "Status"}
func toTable(pods []*api.Pod) {
data := make([][]string, len(pods))
for i, p := range pods {
row := []string{p.Id, p.Name, fmt.Sprintf("%d %s", p.GpuCount, p.Machine.GpuDisplayName), p.ImageName, p.DesiredStatus}
if AllFields {
header = append(header, "Pod Type", "vCPU", "Mem", "Container Disk", "Volume Disk", "Location", "$/hr", "Ports")

var portEntries int = 0
if p.Runtime != nil && p.Runtime.Ports != nil {
portEntries = len(p.Runtime.Ports)
}
ports := make([]string, portEntries)
for j := range ports {
var privpub string = "prv"
if p.Runtime.Ports[j].IsIpPublic {
privpub = "pub"
}
ports[j] = fmt.Sprintf("%s:%d->%d\u00A0(%s,%s)", p.Runtime.Ports[j].Ip, p.Runtime.Ports[j].PublicPort, p.Runtime.Ports[j].PrivatePort, privpub, p.Runtime.Ports[j].PortType)
}

row = append(
row,
p.PodType,
fmt.Sprintf("%d", p.VcpuCount),
fmt.Sprintf("%d", p.MemoryInGb),
fmt.Sprintf("%d", p.ContainerDiskInGb),
fmt.Sprintf("%d", p.VolumeInGb),
fmt.Sprintf("%s", p.Machine.Location),
fmt.Sprintf("%.3f", p.CostPerHr),
fmt.Sprintf("%s", strings.Join(ports[:], ",")),
)
}
data[i] = row
}

tb := tablewriter.NewWriter(os.Stdout)
tb.SetHeader(header)
tb.AppendBulk(data)
format.TableDefaults(tb)
tb.Render()
},
header := []string{"ID", "Name", "GPU", "Image Name", "Status"}
if AllFields {
header = append(header, "Pod Type", "vCPU", "Mem", "Container Disk", "Volume Disk", "Location", "$/hr", "Ports")
}

tb := tablewriter.NewWriter(os.Stdout)
tb.SetHeader(header)
tb.AppendBulk(data)
format.TableDefaults(tb)
tb.Render()
}

func init() {
GetPodCmd.Flags().BoolVarP(&AllFields, "allfields", "a", false, "include all fields in output")
func toJSON(w io.Writer, pods []*api.Pod) error {
b, err := json.MarshalIndent(pods, "", " ")
if err != nil {
return err
}

_, err = w.Write(b)
return err
}
76 changes: 76 additions & 0 deletions cmd/pod/getPod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package pod

import (
"bytes"
"encoding/json"
"reflect"
"testing"

"github.com/runpod/runpodctl/api"
)

func TestGetPod(t *testing.T) {
pods := []*api.Pod{
{Id: "p1"},
{Id: "p2"},
{Id: "p3"},
}

t.Run("filter pods", func(t *testing.T) {

t.Run("without argument", func(t *testing.T) {
got := filter(pods, nil)
assertEqualIDs(t, getIDs(pods), got)
})

t.Run("with argument", func(t *testing.T) {
got := filter(pods, []string{"p2"})
assertEqualIDs(t, []string{"p2"}, got)
})
})

t.Run("output json", func(t *testing.T) {
t.Run("with results", func(t *testing.T) {
var buf bytes.Buffer

check(t, toJSON(&buf, pods))

var got []*api.Pod
check(t, json.NewDecoder(bytes.NewReader(buf.Bytes())).Decode(&got))

assertEqualIDs(t, []string{"p1", "p2", "p3"}, got)
})

t.Run("empty output", func(t *testing.T) {
var buf bytes.Buffer

_ = toJSON(&buf, []*api.Pod{})
got := string(buf.Bytes())

if got != "[]" {
t.Errorf("got %v, want %v", got, "[]")
}
})
})

}

func assertEqualIDs(t *testing.T, want []string, got []*api.Pod) {
if !reflect.DeepEqual(want, getIDs(got)) {
t.Errorf("got %v, want %v", getIDs(got), want)
}
}

func check(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}

func getIDs(pods []*api.Pod) []string {
out := make([]string, len(pods))
for i, p := range pods {
out[i] = p.Id
}
return out
}