mirror of https://github.com/docker/docs.git
Sort docker info by name
Signed-off-by: Victor Vieux <victorvieux@gmail.com>
This commit is contained in:
parent
c101ab7548
commit
820527be7e
|
@ -0,0 +1,20 @@
|
|||
package cluster
|
||||
|
||||
type FakeNode struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (fn *FakeNode) ID() string { return "" }
|
||||
func (fn *FakeNode) Name() string { return fn.name }
|
||||
func (fn *FakeNode) IP() string { return "" }
|
||||
func (fn *FakeNode) Addr() string { return "" }
|
||||
func (fn *FakeNode) Images() []*Image { return nil }
|
||||
func (fn *FakeNode) Image(_ string) *Image { return nil }
|
||||
func (fn *FakeNode) Containers() []*Container { return nil }
|
||||
func (fn *FakeNode) Container(_ string) *Container { return nil }
|
||||
func (fn *FakeNode) TotalCpus() int64 { return 0 }
|
||||
func (fn *FakeNode) UsedCpus() int64 { return 0 }
|
||||
func (fn *FakeNode) TotalMemory() int64 { return 0 }
|
||||
func (fn *FakeNode) UsedMemory() int64 { return 0 }
|
||||
func (fn *FakeNode) Labels() map[string]string { return nil }
|
||||
func (fn *FakeNode) IsHealthy() bool { return true }
|
|
@ -33,3 +33,23 @@ func SerializeNode(node Node) string {
|
|||
"Addr", node.Addr(),
|
||||
"Ip", node.IP())
|
||||
}
|
||||
|
||||
// NodeSorter implements the Sort interface to sort Cluster.Node.
|
||||
// It is not guaranteed to be a stable sort.
|
||||
type NodeSorter []Node
|
||||
|
||||
// Len returns the number of nodes to be sorted.
|
||||
func (s NodeSorter) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
|
||||
// Swap exchanges the node elements with indices i and j.
|
||||
func (s NodeSorter) Swap(i, j int) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
|
||||
// Less reports whether the node with index i should sort before the node with index j.
|
||||
// Nodes are sorted chronologically by name.
|
||||
func (s NodeSorter) Less(i, j int) bool {
|
||||
return s[i].Name() < s[j].Name()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package cluster
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNodeSorter(t *testing.T) {
|
||||
nodes := []Node{&FakeNode{"name1"}, &FakeNode{"name3"}, &FakeNode{"name2"}}
|
||||
|
||||
sort.Sort(NodeSorter(nodes))
|
||||
|
||||
assert.Equal(t, nodes[0].Name(), "name1")
|
||||
assert.Equal(t, nodes[1].Name(), "name2")
|
||||
assert.Equal(t, nodes[2].Name(), "name3")
|
||||
}
|
|
@ -2,6 +2,7 @@ package swarm
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
|
@ -274,7 +275,10 @@ func (c *Cluster) Info() [][2]string {
|
|||
{"\bNodes", fmt.Sprintf("%d", len(c.nodes))},
|
||||
}
|
||||
|
||||
for _, node := range c.nodes {
|
||||
nodes := c.listNodes()
|
||||
sort.Sort(cluster.NodeSorter(nodes))
|
||||
|
||||
for _, node := range nodes {
|
||||
info = append(info, [2]string{node.Name(), node.Addr()})
|
||||
info = append(info, [2]string{" └ Containers", fmt.Sprintf("%d", len(node.Containers()))})
|
||||
info = append(info, [2]string{" └ Reserved CPUs", fmt.Sprintf("%d / %d", node.UsedCpus(), node.TotalCpus())})
|
||||
|
|
Loading…
Reference in New Issue