Skip to content

Commit

Permalink
[SPPSP-2925] add timeout to ListPodNodes (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
DuodenumL authored Nov 23, 2021
1 parent 338e42f commit e39840b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cmd/node/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (o *getNodeOptions) run(ctx context.Context) error {
return err
}

describe.Nodes(node)
describe.NodesWithInfo(node)
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions cmd/pod/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ func Command() *cli.Command {
Name: "label",
Usage: "labels to filter, e.g, a=1, b=2",
},
&cli.IntFlag{
Name: "timeout",
Usage: "timeout in second, default value is 10",
Value: 10,
},
&cli.BoolFlag{
Name: "show-info",
Usage: "show node info",
},
},
},
{
Expand Down
53 changes: 34 additions & 19 deletions cmd/pod/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ const (
)

type listPodNodesOptions struct {
client corepb.CoreRPCClient
name string
filter string
labels map[string]string
client corepb.CoreRPCClient
name string
filter string
labels map[string]string
timeoutInSecond int32
showInfo bool
}

func (o *listPodNodesOptions) run(ctx context.Context) error {
Expand All @@ -34,18 +36,20 @@ func (o *listPodNodesOptions) run(ctx context.Context) error {

func (o *listPodNodesOptions) listDown(ctx context.Context) error {
resp1, err := o.client.ListPodNodes(ctx, &corepb.ListNodesOptions{
Podname: o.name,
All: true,
Labels: o.labels,
Podname: o.name,
All: true,
Labels: o.labels,
TimeoutInSecond: o.timeoutInSecond,
})
if err != nil {
return err
}

resp2, err := o.client.ListPodNodes(ctx, &corepb.ListNodesOptions{
Podname: o.name,
All: false,
Labels: o.labels,
Podname: o.name,
All: false,
Labels: o.labels,
TimeoutInSecond: o.timeoutInSecond,
})
if err != nil {
return err
Expand All @@ -64,26 +68,35 @@ func (o *listPodNodesOptions) listDown(ctx context.Context) error {
nodes = append(nodes, node)
}

describe.Nodes(nodes...)
o.describeNodes(nodes)
return nil
}

func (o *listPodNodesOptions) listUpOrAll(ctx context.Context) error {
// filter == all, list all nodes
// filter == up, list available nodes only
resp, err := o.client.ListPodNodes(ctx, &corepb.ListNodesOptions{
Podname: o.name,
All: o.filter == all,
Labels: o.labels,
Podname: o.name,
All: o.filter == all,
Labels: o.labels,
TimeoutInSecond: o.timeoutInSecond,
})
if err != nil {
return err
}

describe.Nodes(resp.GetNodes()...)
o.describeNodes(resp.GetNodes())
return nil
}

func (o *listPodNodesOptions) describeNodes(nodes []*corepb.Node) {
if o.showInfo {
describe.NodesWithInfo(nodes...)
} else {
describe.Nodes(nodes...)
}
}

func cmdPodListNodes(c *cli.Context) error {
client, err := utils.NewCoreRPCClient(c)
if err != nil {
Expand All @@ -96,10 +109,12 @@ func cmdPodListNodes(c *cli.Context) error {
}

o := &listPodNodesOptions{
client: client,
name: c.Args().First(),
filter: filter,
labels: utils.SplitEquality(c.StringSlice("label")),
client: client,
name: c.Args().First(),
filter: filter,
labels: utils.SplitEquality(c.StringSlice("label")),
timeoutInSecond: int32(c.Int("timeout")),
showInfo: c.Bool("show-info"),
}
return o.run(c.Context)
}
25 changes: 22 additions & 3 deletions describe/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,30 @@ func Nodes(nodes ...*corepb.Node) {
case isYAML():
describeAsYAML(nodes)
default:
describeNodes(nodes)
describeNodes(nodes, false)
}
}

func describeNodes(nodes []*corepb.Node) {
// NodesWithInfo describes a list of Node with their info
func NodesWithInfo(nodes ...*corepb.Node) {
switch {
case isJSON():
describeAsJSON(nodes)
case isYAML():
describeAsYAML(nodes)
default:
describeNodes(nodes, true)
}
}

func describeNodes(nodes []*corepb.Node, showInfo bool) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Name", "Endpoint", "Status", "CPU", "Memory", "Volume", "Storage"})
if showInfo {
t.AppendHeader(table.Row{"Name", "Endpoint", "Status", "CPU", "Memory", "Volume", "Storage", "Info"})
} else {
t.AppendHeader(table.Row{"Name", "Endpoint", "Status", "CPU", "Memory", "Volume", "Storage"})
}

for _, node := range nodes {
totalVolumeCap := int64(0)
Expand All @@ -51,6 +67,9 @@ func describeNodes(nodes []*corepb.Node) {
{fmt.Sprintf("%d / %d bytes", node.VolumeUsed, totalVolumeCap)},
{fmt.Sprintf("%d / %d bytes", node.StorageUsed, node.InitStorage)},
}
if showInfo {
rows = append(rows, []string{node.Info})
}
t.AppendRows(toTableRows(rows))
t.AppendSeparator()
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/juju/testing v0.0.0-20201216035041-2be42bba85f3 // indirect
github.com/kless/term v0.0.0-20161130133337-e551c64f56c0 // indirect
github.com/pkg/term v1.1.0
github.com/projecteru2/core v0.0.0-20210923162924-912e9f1d49aa
github.com/projecteru2/core v0.0.0-20211123023700-4c093d1c5b6a
github.com/sethgrid/curse v0.0.0-20181231162520-d4ee583ebf0f
github.com/sethvargo/go-signalcontext v0.1.0
github.com/sirupsen/logrus v1.7.0
Expand Down
10 changes: 5 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ github.com/opencontainers/go-digest v0.0.0-20180430190053-c9281466c8b2/go.mod h1
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.0.1 h1:JMemWkRwHx4Zj+fVxWoMCFm/8sYGGrUVojFA6h/TRcI=
github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM=
github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
github.com/opencontainers/runc v0.0.0-20190115041553-12f6a991201f/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U=
github.com/opencontainers/runc v1.0.0-rc95 h1:RMuWVfY3E1ILlVsC3RhIq38n4sJtlOFwU9gfFZSqrd0=
github.com/opencontainers/runc v1.0.0-rc95/go.mod h1:z+bZxa/+Tz/FmYVWkhUajJdzFeOqjc5vrqskhVyHGUM=
Expand All @@ -444,9 +444,9 @@ github.com/pkg/term v1.1.0/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/projecteru2/core v0.0.0-20210923162924-912e9f1d49aa h1:/wxjr8H+dpYBNwqPPUiwbIWZpMYYPCtQ6iU5WvgTEXk=
github.com/projecteru2/core v0.0.0-20210923162924-912e9f1d49aa/go.mod h1:/7fZaf8yXd353d1tDX9KwvXMkptauKGe4zhIpqNetj8=
github.com/projecteru2/libyavirt v0.0.0-20210920022816-abc7aa0cf6ae/go.mod h1:FOc+hWBMLsMrmx5p3/moizKeSomedZPNwB6LhS+kEnE=
github.com/projecteru2/core v0.0.0-20211123023700-4c093d1c5b6a h1:04lfeJG46ldlC3gV1FEOyg1TvmKnIQ6c4tlCWX+lDYw=
github.com/projecteru2/core v0.0.0-20211123023700-4c093d1c5b6a/go.mod h1:pvAHWsHBU+qMK3KX2kL8J0y8lx729bZOltIAGRL/UHU=
github.com/projecteru2/libyavirt v0.0.0-20211112023512-fbd788e688d5/go.mod h1:FOc+hWBMLsMrmx5p3/moizKeSomedZPNwB6LhS+kEnE=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
Expand Down

0 comments on commit e39840b

Please sign in to comment.